@steedos/accounts 3.0.0-beta.99 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core/index.js +22 -17
- package/lib/core/index.js.map +1 -1
- package/lib/rest-express/endpoints/authorize.js +2 -2
- package/lib/rest-express/endpoints/authorize.js.map +1 -1
- package/lib/rest-express/endpoints/impersonate.js +25 -23
- package/lib/rest-express/endpoints/impersonate.js.map +1 -1
- package/lib/rest-express/endpoints/login.js +95 -93
- package/lib/rest-express/endpoints/login.js.map +1 -1
- package/lib/rest-express/endpoints/logout.js +2 -2
- package/lib/rest-express/endpoints/logout.js.map +1 -1
- package/lib/rest-express/endpoints/oauth/provider-callback.js +35 -33
- package/lib/rest-express/endpoints/oauth/provider-callback.js.map +1 -1
- package/lib/rest-express/endpoints/password/change-password.js +2 -2
- package/lib/rest-express/endpoints/password/change-password.js.map +1 -1
- package/lib/rest-express/endpoints/refresh-access-token.js +25 -23
- package/lib/rest-express/endpoints/refresh-access-token.js.map +1 -1
- package/lib/rest-express/endpoints/service-authenticate.js +76 -74
- package/lib/rest-express/endpoints/service-authenticate.js.map +1 -1
- package/lib/rest-express/endpoints/update-session.js +44 -42
- package/lib/rest-express/endpoints/update-session.js.map +1 -1
- package/lib/rest-express/utils/getClientIp.js +16 -0
- package/lib/rest-express/utils/getClientIp.js.map +1 -0
- package/package.json +7 -8
- package/src/core/index.ts +11 -19
- package/src/rest-express/endpoints/authorize.ts +2 -2
- package/src/rest-express/endpoints/impersonate.ts +30 -31
- package/src/rest-express/endpoints/login.ts +66 -61
- package/src/rest-express/endpoints/logout.ts +2 -2
- package/src/rest-express/endpoints/oauth/provider-callback.ts +45 -38
- package/src/rest-express/endpoints/password/change-password.ts +4 -4
- package/src/rest-express/endpoints/refresh-access-token.ts +23 -24
- package/src/rest-express/endpoints/service-authenticate.ts +87 -68
- package/src/rest-express/endpoints/update-session.ts +50 -42
- package/src/rest-express/utils/getClientIp.ts +25 -0
|
@@ -1,55 +1,58 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* @Author: baozhoutao@steedos.com
|
|
3
3
|
* @Date: 2022-03-28 09:35:34
|
|
4
|
-
* @LastEditors:
|
|
5
|
-
* @LastEditTime:
|
|
6
|
-
* @Description:
|
|
4
|
+
* @LastEditors: 孙浩林 sunhaolin@steedos.com
|
|
5
|
+
* @LastEditTime: 2025-09-12 14:08:55
|
|
6
|
+
* @Description:
|
|
7
7
|
*/
|
|
8
|
-
import * as express from
|
|
9
|
-
import
|
|
10
|
-
import { AccountsServer, generateRandomToken } from
|
|
11
|
-
import { getUserAgent } from
|
|
12
|
-
import { sendError } from
|
|
13
|
-
import { setAuthCookies } from
|
|
14
|
-
import isMobile from
|
|
15
|
-
import { getObject } from
|
|
16
|
-
import { db } from
|
|
17
|
-
import { getFirstSpace } from
|
|
8
|
+
import * as express from "express";
|
|
9
|
+
import { getClientIp } from "../utils/getClientIp";
|
|
10
|
+
import { AccountsServer, generateRandomToken } from "../../server";
|
|
11
|
+
import { getUserAgent } from "../utils/get-user-agent";
|
|
12
|
+
import { sendError } from "../utils/send-error";
|
|
13
|
+
import { setAuthCookies } from "../utils/steedos-auth";
|
|
14
|
+
import isMobile from "ismobilejs";
|
|
15
|
+
import { getObject } from "@steedos/objectql";
|
|
16
|
+
import { db } from "../../db";
|
|
17
|
+
import { getFirstSpace } from "./spaces";
|
|
18
18
|
|
|
19
|
-
export const login =
|
|
20
|
-
|
|
21
|
-
res: express.Response
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
let result: any = null;
|
|
28
|
-
try {
|
|
29
|
-
result = await accountsServer.loginWithService('password', req.body, {
|
|
30
|
-
ip,
|
|
31
|
-
userAgent
|
|
32
|
-
});
|
|
33
|
-
if(result._next){
|
|
34
|
-
return res.json(result);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
setAuthCookies(req, res, result.user._id, result.token, result.tokens.accessToken);
|
|
19
|
+
export const login =
|
|
20
|
+
(accountsServer: AccountsServer) =>
|
|
21
|
+
async (req: express.Request, res: express.Response) => {
|
|
22
|
+
let userAgent = getUserAgent(req) || "";
|
|
23
|
+
const ip = getClientIp(req);
|
|
24
|
+
let status = "success";
|
|
25
|
+
let message = "";
|
|
26
|
+
let result: any = null;
|
|
38
27
|
try {
|
|
39
|
-
|
|
40
|
-
|
|
28
|
+
result = await accountsServer.loginWithService("password", req.body, {
|
|
29
|
+
ip,
|
|
30
|
+
userAgent,
|
|
31
|
+
});
|
|
32
|
+
if (result._next) {
|
|
33
|
+
return res.json(result);
|
|
41
34
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
35
|
+
|
|
36
|
+
setAuthCookies(
|
|
37
|
+
req,
|
|
38
|
+
res,
|
|
39
|
+
result.user._id,
|
|
40
|
+
result.token,
|
|
41
|
+
result.tokens.accessToken,
|
|
42
|
+
);
|
|
43
|
+
try {
|
|
44
|
+
if (result && result.user) {
|
|
45
|
+
delete result.user["services"];
|
|
46
|
+
}
|
|
47
|
+
} catch (error) {}
|
|
48
|
+
res.json(result);
|
|
49
|
+
return;
|
|
50
|
+
} catch (err) {
|
|
51
|
+
console.log(err);
|
|
52
|
+
status = "fail";
|
|
53
|
+
message = err.message;
|
|
54
|
+
sendError(res, { message: err.message });
|
|
55
|
+
} finally {
|
|
53
56
|
let is_phone = false;
|
|
54
57
|
let is_tablet = false;
|
|
55
58
|
if (userAgent) {
|
|
@@ -64,25 +67,27 @@ export const login = (accountsServer: AccountsServer) => async (
|
|
|
64
67
|
const space = await getFirstSpace(accountsServer);
|
|
65
68
|
|
|
66
69
|
let remote_user = null;
|
|
67
|
-
if(!result){
|
|
68
|
-
let foundUser: any | null = await accountsServer
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
remote_user =
|
|
70
|
+
if (!result) {
|
|
71
|
+
let foundUser: any | null = await accountsServer
|
|
72
|
+
.getServices()
|
|
73
|
+
["password"].foundUser(req.body.user);
|
|
74
|
+
remote_user = foundUser ? foundUser._id : null;
|
|
75
|
+
} else {
|
|
76
|
+
remote_user = result?.user?._id;
|
|
72
77
|
}
|
|
73
78
|
|
|
74
|
-
if(space){
|
|
79
|
+
if (space) {
|
|
75
80
|
// 记录登录日志, 不记录密码.
|
|
76
|
-
const { password, ...data} = req.body;
|
|
77
|
-
await getObject(
|
|
78
|
-
name:
|
|
79
|
-
type:
|
|
81
|
+
const { password, ...data } = req.body;
|
|
82
|
+
await getObject("operation_logs").insert({
|
|
83
|
+
name: "登录",
|
|
84
|
+
type: "login",
|
|
80
85
|
remote_user: remote_user,
|
|
81
86
|
remote_addr: ip,
|
|
82
87
|
http_user_agent: userAgent,
|
|
83
88
|
is_mobile: is_phone,
|
|
84
89
|
is_tablet,
|
|
85
|
-
object:
|
|
90
|
+
object: "users",
|
|
86
91
|
status: status,
|
|
87
92
|
create: new Date(),
|
|
88
93
|
space: space._id,
|
|
@@ -90,9 +95,9 @@ export const login = (accountsServer: AccountsServer) => async (
|
|
|
90
95
|
data: JSON.stringify(data),
|
|
91
96
|
related_to: {
|
|
92
97
|
o: "users",
|
|
93
|
-
ids: [remote_user]
|
|
94
|
-
}
|
|
95
|
-
})
|
|
98
|
+
ids: [remote_user],
|
|
99
|
+
},
|
|
100
|
+
});
|
|
96
101
|
}
|
|
97
|
-
|
|
98
|
-
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
@@ -11,7 +11,7 @@ import { AccountsServer } from "../../server";
|
|
|
11
11
|
import { sendError } from "../utils/send-error";
|
|
12
12
|
import { clearAuthCookies } from "../utils/steedos-auth";
|
|
13
13
|
import { getObject } from "@steedos/objectql";
|
|
14
|
-
import
|
|
14
|
+
import { getClientIp } from "../utils/getClientIp";
|
|
15
15
|
import { getUserAgent } from "../utils/get-user-agent";
|
|
16
16
|
import isMobile from "ismobilejs";
|
|
17
17
|
import { getSteedosSchema } from "@steedos/objectql";
|
|
@@ -37,7 +37,7 @@ export const logout =
|
|
|
37
37
|
//sendError(res, err);
|
|
38
38
|
} finally {
|
|
39
39
|
let userAgent = getUserAgent(req) || "";
|
|
40
|
-
const ip =
|
|
40
|
+
const ip = getClientIp(req);
|
|
41
41
|
let status = "success";
|
|
42
42
|
let message = "";
|
|
43
43
|
let is_phone = false;
|
|
@@ -1,46 +1,53 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
/*
|
|
2
|
+
* @Author: 孙浩林 sunhaolin@steedos.com
|
|
3
|
+
* @Date: 2025-02-17 09:39:59
|
|
4
|
+
* @LastEditors: 孙浩林 sunhaolin@steedos.com
|
|
5
|
+
* @LastEditTime: 2025-09-12 14:29:52
|
|
6
|
+
* @FilePath: /steedos-platform-3.0/packages/accounts/src/rest-express/endpoints/oauth/provider-callback.ts
|
|
7
|
+
* @Description:
|
|
8
|
+
*/
|
|
9
|
+
import * as express from "express";
|
|
10
|
+
import { getClientIp } from "../../utils/getClientIp";
|
|
11
|
+
import { AccountsServer } from "../../../server";
|
|
12
|
+
import { getUserAgent } from "../../utils/get-user-agent";
|
|
13
|
+
import { sendError } from "../../utils/send-error";
|
|
14
|
+
import { AccountsExpressOptions } from "../../types";
|
|
7
15
|
|
|
8
16
|
interface RequestWithSession extends express.Request {
|
|
9
17
|
session: any;
|
|
10
18
|
}
|
|
11
19
|
|
|
12
|
-
export const providerCallback =
|
|
13
|
-
accountsServer: AccountsServer,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
);
|
|
20
|
+
export const providerCallback =
|
|
21
|
+
(accountsServer: AccountsServer, options?: AccountsExpressOptions) =>
|
|
22
|
+
async (req: express.Request, res: express.Response) => {
|
|
23
|
+
try {
|
|
24
|
+
const userAgent = getUserAgent(req);
|
|
25
|
+
const ip = getClientIp(req);
|
|
26
|
+
const loggedInUser = await accountsServer.loginWithService(
|
|
27
|
+
"oauth",
|
|
28
|
+
{
|
|
29
|
+
...(req.params || {}),
|
|
30
|
+
...(req.query || {}),
|
|
31
|
+
...(req.body || {}),
|
|
32
|
+
...((req as RequestWithSession).session || {}),
|
|
33
|
+
},
|
|
34
|
+
{ ip, userAgent },
|
|
35
|
+
);
|
|
29
36
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
37
|
+
if (options && options.onOAuthSuccess) {
|
|
38
|
+
options.onOAuthSuccess(req, res, loggedInUser);
|
|
39
|
+
}
|
|
33
40
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
if (options && options.transformOAuthResponse) {
|
|
42
|
+
res.json(options.transformOAuthResponse(loggedInUser));
|
|
43
|
+
} else {
|
|
44
|
+
res.json(loggedInUser);
|
|
45
|
+
}
|
|
46
|
+
} catch (err) {
|
|
47
|
+
if (options && options.onOAuthError) {
|
|
48
|
+
options.onOAuthError(req, res, err);
|
|
49
|
+
}
|
|
43
50
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
};
|
|
51
|
+
sendError(res, err);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* @Author: baozhoutao@steedos.com
|
|
3
3
|
* @Date: 2022-05-19 11:38:30
|
|
4
|
-
* @LastEditors:
|
|
5
|
-
* @LastEditTime:
|
|
4
|
+
* @LastEditors: 孙浩林 sunhaolin@steedos.com
|
|
5
|
+
* @LastEditTime: 2025-09-12 14:29:58
|
|
6
6
|
* @Description:
|
|
7
7
|
*/
|
|
8
8
|
import * as express from "express";
|
|
@@ -11,7 +11,7 @@ import { sendError } from "../../utils/send-error";
|
|
|
11
11
|
import { getSteedosConfig, getObject } from "@steedos/objectql";
|
|
12
12
|
import { hashPassword } from "../../../password/utils";
|
|
13
13
|
|
|
14
|
-
import
|
|
14
|
+
import { getClientIp } from "../../utils/getClientIp";
|
|
15
15
|
import { getUserAgent } from "../../utils/get-user-agent";
|
|
16
16
|
import isMobile from "ismobilejs";
|
|
17
17
|
import { db } from "../../../db";
|
|
@@ -59,7 +59,7 @@ export const changePassword =
|
|
|
59
59
|
);
|
|
60
60
|
|
|
61
61
|
const userAgent = getUserAgent(req);
|
|
62
|
-
const ip =
|
|
62
|
+
const ip = getClientIp(req);
|
|
63
63
|
let is_phone = false;
|
|
64
64
|
let is_tablet = false;
|
|
65
65
|
if (userAgent) {
|
|
@@ -1,25 +1,24 @@
|
|
|
1
|
-
import * as express from
|
|
2
|
-
import
|
|
3
|
-
import { AccountsServer } from
|
|
4
|
-
import { getUserAgent } from
|
|
5
|
-
import { sendError } from
|
|
1
|
+
import * as express from "express";
|
|
2
|
+
import { getClientIp } from "../utils/getClientIp";
|
|
3
|
+
import { AccountsServer } from "../../server";
|
|
4
|
+
import { getUserAgent } from "../utils/get-user-agent";
|
|
5
|
+
import { sendError } from "../utils/send-error";
|
|
6
6
|
|
|
7
|
-
export const refreshAccessToken =
|
|
8
|
-
|
|
9
|
-
res: express.Response
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
};
|
|
7
|
+
export const refreshAccessToken =
|
|
8
|
+
(accountsServer: AccountsServer) =>
|
|
9
|
+
async (req: express.Request, res: express.Response) => {
|
|
10
|
+
try {
|
|
11
|
+
const { accessToken, refreshToken } = req.body;
|
|
12
|
+
const userAgent = getUserAgent(req);
|
|
13
|
+
const ip = getClientIp(req);
|
|
14
|
+
const refreshedSession = await accountsServer.refreshTokens(
|
|
15
|
+
accessToken,
|
|
16
|
+
refreshToken,
|
|
17
|
+
ip,
|
|
18
|
+
userAgent,
|
|
19
|
+
);
|
|
20
|
+
res.json(refreshedSession);
|
|
21
|
+
} catch (err) {
|
|
22
|
+
sendError(res, err);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
@@ -1,76 +1,95 @@
|
|
|
1
|
-
import * as express from
|
|
2
|
-
import
|
|
3
|
-
import { AccountsServer } from
|
|
4
|
-
import { getUserAgent } from
|
|
5
|
-
import { sendError } from
|
|
6
|
-
import { setAuthCookies, hashStampedToken } from
|
|
7
|
-
import { db } from
|
|
8
|
-
import * as _ from
|
|
9
|
-
import { getUserSpace } from
|
|
1
|
+
import * as express from "express";
|
|
2
|
+
import { getClientIp } from "../utils/getClientIp";
|
|
3
|
+
import { AccountsServer } from "../../server";
|
|
4
|
+
import { getUserAgent } from "../utils/get-user-agent";
|
|
5
|
+
import { sendError } from "../utils/send-error";
|
|
6
|
+
import { setAuthCookies, hashStampedToken } from "../utils/steedos-auth";
|
|
7
|
+
import { db } from "../../db";
|
|
8
|
+
import * as _ from "lodash";
|
|
9
|
+
import { getUserSpace } from "../utils/users";
|
|
10
10
|
|
|
11
|
-
export const serviceAuthenticate =
|
|
12
|
-
|
|
13
|
-
res: express.Response
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
let db = services[serviceName].db;
|
|
11
|
+
export const serviceAuthenticate =
|
|
12
|
+
(accountsServer: AccountsServer) =>
|
|
13
|
+
async (req: express.Request, res: express.Response) => {
|
|
14
|
+
try {
|
|
15
|
+
const serviceName = req.params.service;
|
|
16
|
+
let userAgent = getUserAgent(req) || "";
|
|
17
|
+
const ip = getClientIp(req);
|
|
18
|
+
const email = req.body.user.email;
|
|
19
|
+
const spaceId = req.body.spaceId;
|
|
20
|
+
let services: any = accountsServer.getServices();
|
|
21
|
+
let db = services[serviceName].db;
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
if (email && email.indexOf("@") < 0) {
|
|
24
|
+
req.body.user.username = email;
|
|
25
|
+
}
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
const loggedInUser: any = await accountsServer.loginWithService(
|
|
28
|
+
serviceName,
|
|
29
|
+
req.body,
|
|
30
|
+
{
|
|
31
|
+
ip,
|
|
32
|
+
userAgent,
|
|
33
|
+
},
|
|
34
|
+
);
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
//获取user session
|
|
37
|
+
let session: any = await accountsServer.findSessionByAccessToken(
|
|
38
|
+
loggedInUser.tokens.accessToken,
|
|
39
|
+
);
|
|
35
40
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
//获取用户有效的工作区Id,并且写入Sessions中
|
|
42
|
+
let validSpaceId = await getUserSpace(session.userId, spaceId);
|
|
43
|
+
if (validSpaceId) {
|
|
44
|
+
userAgent = `${userAgent} Space/${validSpaceId}`;
|
|
45
|
+
db.updateSession(loggedInUser.sessionId, {
|
|
46
|
+
ip,
|
|
47
|
+
userAgent,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
44
50
|
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
//确认用户密码是否过期
|
|
52
|
+
let user = await db.collection.findOne(
|
|
53
|
+
{ _id: session.userId },
|
|
54
|
+
{ password_expired: 1 },
|
|
55
|
+
);
|
|
47
56
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
57
|
+
//创建Meteor token
|
|
58
|
+
let authToken = null;
|
|
59
|
+
let stampedAuthToken = {
|
|
60
|
+
token: session.token,
|
|
61
|
+
when: new Date(),
|
|
62
|
+
};
|
|
63
|
+
authToken = stampedAuthToken.token;
|
|
64
|
+
let hashedToken = hashStampedToken(stampedAuthToken);
|
|
65
|
+
let _user = await db.collection.findOne(
|
|
66
|
+
{ _id: session.userId },
|
|
67
|
+
{ services: 1 },
|
|
68
|
+
);
|
|
69
|
+
if (!_user["services"]) {
|
|
70
|
+
_user["services"] = {};
|
|
71
|
+
}
|
|
72
|
+
if (!_user["services"]["resume"]) {
|
|
73
|
+
_user["services"]["resume"] = { loginTokens: [] };
|
|
74
|
+
}
|
|
75
|
+
if (!_user["services"]["resume"]["loginTokens"]) {
|
|
76
|
+
_user["services"]["resume"]["loginTokens"] = [];
|
|
77
|
+
}
|
|
78
|
+
_user["services"]["resume"]["loginTokens"].push(hashedToken);
|
|
79
|
+
let data = { services: _user["services"] };
|
|
80
|
+
await db.collection.updateOne({ _id: session.userId }, { $set: data });
|
|
81
|
+
// 设置cookies
|
|
82
|
+
setAuthCookies(
|
|
83
|
+
req,
|
|
84
|
+
res,
|
|
85
|
+
session.userId,
|
|
86
|
+
authToken,
|
|
87
|
+
loggedInUser.tokens.accessToken,
|
|
88
|
+
validSpaceId,
|
|
89
|
+
);
|
|
71
90
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
};
|
|
91
|
+
res.json(loggedInUser);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
sendError(res, err);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
@@ -1,46 +1,54 @@
|
|
|
1
|
-
import * as express from
|
|
2
|
-
import
|
|
3
|
-
import { AccountsServer } from
|
|
4
|
-
import { getUserAgent } from
|
|
5
|
-
import { sendError } from
|
|
6
|
-
import { setAuthCookies, getAuthTokenCookie } from
|
|
7
|
-
import { db } from
|
|
8
|
-
import { getUserSpace } from
|
|
9
|
-
import * as _ from
|
|
1
|
+
import * as express from "express";
|
|
2
|
+
import { getClientIp } from "../utils/getClientIp";
|
|
3
|
+
import { AccountsServer } from "../../server";
|
|
4
|
+
import { getUserAgent } from "../utils/get-user-agent";
|
|
5
|
+
import { sendError } from "../utils/send-error";
|
|
6
|
+
import { setAuthCookies, getAuthTokenCookie } from "../utils/steedos-auth";
|
|
7
|
+
import { db } from "../../db";
|
|
8
|
+
import { getUserSpace } from "../utils/users";
|
|
9
|
+
import * as _ from "lodash";
|
|
10
10
|
|
|
11
|
-
export const updateSession =
|
|
12
|
-
|
|
13
|
-
res: express.Response
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
export const updateSession =
|
|
12
|
+
(accountsServer: AccountsServer) =>
|
|
13
|
+
async (req: express.Request, res: express.Response) => {
|
|
14
|
+
try {
|
|
15
|
+
const userId = (req as any).user._id;
|
|
16
|
+
const serviceName = req.params.service;
|
|
17
|
+
let userAgent = getUserAgent(req) || "";
|
|
18
|
+
const ip = getClientIp(req);
|
|
19
|
+
let services: any = accountsServer.getServices();
|
|
20
|
+
let db = services[serviceName].db;
|
|
21
|
+
const spaceId = req.body.spaceId;
|
|
22
|
+
let accessToken = req.body.accessToken;
|
|
23
|
+
let session: any =
|
|
24
|
+
await accountsServer.findSessionByAccessToken(accessToken);
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
if (!session) {
|
|
27
|
+
throw new Error("Invalid accessToken");
|
|
28
|
+
}
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// 设置cookies
|
|
40
|
-
setAuthCookies(req, res, session.userId, getAuthTokenCookie(req, res), accessToken, validSpaceId);
|
|
30
|
+
//获取用户有效的工作区Id,并且写入Sessions中
|
|
31
|
+
let validSpaceId = await getUserSpace(userId, spaceId);
|
|
32
|
+
if (validSpaceId) {
|
|
33
|
+
userAgent = `${userAgent} Space/${validSpaceId}`;
|
|
34
|
+
db.updateSession(session.id, {
|
|
35
|
+
ip,
|
|
36
|
+
userAgent,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
41
39
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
// 设置cookies
|
|
41
|
+
setAuthCookies(
|
|
42
|
+
req,
|
|
43
|
+
res,
|
|
44
|
+
session.userId,
|
|
45
|
+
getAuthTokenCookie(req, res),
|
|
46
|
+
accessToken,
|
|
47
|
+
validSpaceId,
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
res.json({});
|
|
51
|
+
} catch (err) {
|
|
52
|
+
sendError(res, err);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: 孙浩林 sunhaolin@steedos.com
|
|
3
|
+
* @Date: 2025-09-12 14:05:36
|
|
4
|
+
* @LastEditors: 孙浩林 sunhaolin@steedos.com
|
|
5
|
+
* @LastEditTime: 2025-09-12 14:08:08
|
|
6
|
+
* @FilePath: /steedos-platform-3.0/packages/accounts/src/rest-express/utils/getClientIp.ts
|
|
7
|
+
* @Description:
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import * as requestIp from "request-ip";
|
|
11
|
+
|
|
12
|
+
export const getClientIp = (req) => {
|
|
13
|
+
let ip = requestIp.getClientIp(req);
|
|
14
|
+
// 去掉 IPv6 前缀 ::ffff:
|
|
15
|
+
if (ip && ip.startsWith("::ffff:")) {
|
|
16
|
+
ip = ip.substring(7);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// IPv6 localhost 映射
|
|
20
|
+
if (ip === "::1") {
|
|
21
|
+
ip = "127.0.0.1";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return ip;
|
|
25
|
+
};
|