@steedos/accounts 3.0.0-beta.14 → 3.0.0-beta.140
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 +87 -42
- 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 +80 -73
- 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 +95 -90
- 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/steedos/get-tenant.js +62 -39
- package/lib/rest-express/endpoints/steedos/get-tenant.js.map +1 -1
- package/lib/rest-express/endpoints/steedos/settings.js +119 -88
- package/lib/rest-express/endpoints/steedos/settings.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/user-loader.js +82 -67
- package/lib/rest-express/user-loader.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 +5 -6
- package/src/core/index.ts +197 -145
- 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 +74 -72
- package/src/rest-express/endpoints/oauth/provider-callback.ts +45 -38
- package/src/rest-express/endpoints/password/change-password.ts +94 -83
- 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/steedos/get-tenant.ts +56 -38
- package/src/rest-express/endpoints/steedos/settings.ts +117 -88
- package/src/rest-express/endpoints/update-session.ts +50 -42
- package/src/rest-express/user-loader.ts +68 -58
- package/src/rest-express/utils/getClientIp.ts +25 -0
|
@@ -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,44 +1,62 @@
|
|
|
1
|
-
import * as express from
|
|
2
|
-
import { AccountsServer } from
|
|
3
|
-
import { sendError } from
|
|
4
|
-
import { getSteedosConfig } from
|
|
5
|
-
import { db } from
|
|
6
|
-
import {getSteedosService } from
|
|
1
|
+
import * as express from "express";
|
|
2
|
+
import { AccountsServer } from "../../../server";
|
|
3
|
+
import { sendError } from "../../utils/send-error";
|
|
4
|
+
import { getSteedosConfig } from "@steedos/objectql";
|
|
5
|
+
import { db } from "../../../db";
|
|
6
|
+
import { getSteedosService } from "../../../core";
|
|
7
7
|
|
|
8
|
-
export const getTenant =
|
|
9
|
-
|
|
10
|
-
res: express.Response
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
export const getTenant =
|
|
9
|
+
(accountsServer: AccountsServer) =>
|
|
10
|
+
async (req: express.Request, res: express.Response) => {
|
|
11
|
+
try {
|
|
12
|
+
const spaceId = req.params.id;
|
|
13
|
+
if (!spaceId) throw new Error("accounts.tenant_id_required");
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
const spaceDoc = await db.findOne("spaces", spaceId, {
|
|
16
|
+
fields: [
|
|
17
|
+
"name",
|
|
18
|
+
"avatar",
|
|
19
|
+
"avatar_dark",
|
|
20
|
+
"background",
|
|
21
|
+
"enable_register",
|
|
22
|
+
"account_logo",
|
|
23
|
+
],
|
|
23
24
|
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
let steedosService = getSteedosService();
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (
|
|
37
|
-
spaceDoc.
|
|
26
|
+
if (!spaceDoc) {
|
|
27
|
+
return res.send({
|
|
28
|
+
exists: false,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let steedosService = getSteedosService();
|
|
33
|
+
|
|
34
|
+
if (steedosService) {
|
|
35
|
+
if (spaceDoc.account_logo) {
|
|
36
|
+
spaceDoc.logo_url =
|
|
37
|
+
steedosService +
|
|
38
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
39
|
+
spaceDoc.account_logo;
|
|
40
|
+
} else if (spaceDoc.avatar_dark) {
|
|
41
|
+
spaceDoc.logo_url =
|
|
42
|
+
steedosService +
|
|
43
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
44
|
+
spaceDoc.avatar_dark;
|
|
45
|
+
} else if (spaceDoc.avatar) {
|
|
46
|
+
spaceDoc.logo_url =
|
|
47
|
+
steedosService +
|
|
48
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
49
|
+
spaceDoc.avatar;
|
|
50
|
+
}
|
|
51
|
+
if (spaceDoc.background) {
|
|
52
|
+
spaceDoc.background_url =
|
|
53
|
+
steedosService +
|
|
54
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
55
|
+
spaceDoc.background;
|
|
56
|
+
}
|
|
38
57
|
}
|
|
58
|
+
res.json(spaceDoc);
|
|
59
|
+
} catch (err) {
|
|
60
|
+
sendError(res, err);
|
|
39
61
|
}
|
|
40
|
-
|
|
41
|
-
} catch (err) {
|
|
42
|
-
sendError(res, err);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
62
|
+
};
|
|
@@ -3,110 +3,139 @@
|
|
|
3
3
|
* @Date: 2022-03-28 09:35:34
|
|
4
4
|
* @LastEditors: baozhoutao@steedos.com
|
|
5
5
|
* @LastEditTime: 2025-02-21 14:33:10
|
|
6
|
-
* @Description:
|
|
6
|
+
* @Description:
|
|
7
7
|
*/
|
|
8
|
-
import * as express from
|
|
9
|
-
import * as _ from
|
|
10
|
-
import { AccountsServer } from
|
|
11
|
-
import { getSteedosConfig, getSteedosSchema } from
|
|
12
|
-
import { db } from
|
|
13
|
-
import { canSendEmail, canSendSMS, getSteedosService } from
|
|
14
|
-
const validator = require(
|
|
15
|
-
const util = require(
|
|
16
|
-
const clone = require(
|
|
8
|
+
import * as express from "express";
|
|
9
|
+
import * as _ from "lodash";
|
|
10
|
+
import { AccountsServer } from "../../../server";
|
|
11
|
+
import { getSteedosConfig, getSteedosSchema } from "@steedos/objectql";
|
|
12
|
+
import { db } from "../../../db";
|
|
13
|
+
import { canSendEmail, canSendSMS, getSteedosService } from "../../../core";
|
|
14
|
+
const validator = require("validator");
|
|
15
|
+
const util = require("@steedos/utils");
|
|
16
|
+
const clone = require("clone");
|
|
17
17
|
|
|
18
18
|
const config = getSteedosConfig();
|
|
19
19
|
|
|
20
|
-
export const getSettings =
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
20
|
+
export const getSettings =
|
|
21
|
+
(accountsServer: AccountsServer) =>
|
|
22
|
+
async (req: express.Request, res: express.Response) => {
|
|
23
|
+
let tenant: any = {
|
|
24
|
+
name: "Steedos",
|
|
25
|
+
logo_url: undefined,
|
|
26
|
+
background_url: undefined,
|
|
27
|
+
enable_create_tenant: true,
|
|
28
|
+
enable_register: true,
|
|
29
|
+
enable_forget_password: true,
|
|
30
|
+
enable_password_login: true,
|
|
31
|
+
enable_mobile_code_login: false,
|
|
32
|
+
enable_email_code_login: false,
|
|
33
|
+
enable_bind_mobile: false,
|
|
34
|
+
enable_bind_email: false,
|
|
35
|
+
enable_saas: validator.toBoolean(
|
|
36
|
+
process.env.STEEDOS_TENANT_ENABLE_SAAS || "false",
|
|
37
|
+
true,
|
|
38
|
+
),
|
|
39
|
+
enable_open_geetest: validator.toBoolean(
|
|
40
|
+
process.env.STEEDOS_CAPTCHA_GEETEST_ENABLED || "false",
|
|
41
|
+
),
|
|
42
|
+
page_login: process.env.STEEDOS_TENANT_PAGE_LOGIN,
|
|
43
|
+
page_logout: process.env.STEEDOS_TENANT_PAGE_LOGOUT,
|
|
44
|
+
};
|
|
41
45
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
if (config.tenant) {
|
|
47
|
+
_.assignIn(tenant, config.tenant);
|
|
48
|
+
}
|
|
45
49
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
if (!tenant._id) {
|
|
51
|
+
tenant._id = process.env.STEEDOS_TENANT_ID;
|
|
52
|
+
}
|
|
49
53
|
|
|
50
|
-
|
|
51
|
-
|
|
54
|
+
const platform = (global as any).Steedos.settings.public?.platform || {};
|
|
52
55
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
if (tenant._id) {
|
|
57
|
+
let spaceDoc = await db.findOne("spaces", tenant._id, {
|
|
58
|
+
fields: [
|
|
59
|
+
"name",
|
|
60
|
+
"avatar",
|
|
61
|
+
"avatar_dark",
|
|
62
|
+
"background",
|
|
63
|
+
"enable_register",
|
|
64
|
+
"account_logo",
|
|
65
|
+
"favicon",
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
let steedosService = getSteedosService();
|
|
69
|
+
if (steedosService && spaceDoc) {
|
|
57
70
|
_.assignIn(tenant, spaceDoc);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
if (spaceDoc.account_logo) {
|
|
72
|
+
tenant.logo_url =
|
|
73
|
+
steedosService +
|
|
74
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
75
|
+
spaceDoc.account_logo;
|
|
76
|
+
} else if (spaceDoc.avatar_dark) {
|
|
77
|
+
tenant.logo_url =
|
|
78
|
+
steedosService +
|
|
79
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
80
|
+
spaceDoc.avatar_dark;
|
|
81
|
+
} else if (spaceDoc.avatar) {
|
|
82
|
+
tenant.logo_url =
|
|
83
|
+
steedosService +
|
|
84
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
85
|
+
spaceDoc.avatar;
|
|
86
|
+
}
|
|
87
|
+
if (spaceDoc.background) {
|
|
88
|
+
tenant.background_url =
|
|
89
|
+
steedosService +
|
|
90
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
91
|
+
spaceDoc.background;
|
|
92
|
+
}
|
|
93
|
+
if (platform?.is_oem && spaceDoc.favicon) {
|
|
94
|
+
tenant.favicon_url =
|
|
95
|
+
steedosService +
|
|
96
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
97
|
+
spaceDoc.favicon;
|
|
98
|
+
}
|
|
70
99
|
}
|
|
71
100
|
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
let already_mail_service = canSendEmail();
|
|
75
|
-
let already_sms_service = true || canSendSMS();
|
|
76
101
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const serverInitInfo = {
|
|
80
|
-
|
|
81
|
-
};
|
|
102
|
+
let already_mail_service = canSendEmail();
|
|
103
|
+
let already_sms_service = true || canSendSMS();
|
|
82
104
|
|
|
83
|
-
|
|
105
|
+
//allowInit
|
|
106
|
+
const broker = getSteedosSchema().broker;
|
|
107
|
+
const serverInitInfo = {};
|
|
84
108
|
|
|
85
|
-
|
|
86
|
-
delete _tenant['accessTokenExpiresIn']
|
|
87
|
-
delete _tenant['refreshTokenExpiresIn']
|
|
109
|
+
const _tenant = clone(tenant);
|
|
88
110
|
|
|
89
|
-
|
|
90
|
-
delete _tenant
|
|
91
|
-
|
|
111
|
+
delete _tenant["tokenSecret"];
|
|
112
|
+
delete _tenant["accessTokenExpiresIn"];
|
|
113
|
+
delete _tenant["refreshTokenExpiresIn"];
|
|
92
114
|
|
|
115
|
+
if (tenant.enable_saas) {
|
|
116
|
+
delete _tenant._id;
|
|
117
|
+
}
|
|
93
118
|
|
|
94
|
-
|
|
119
|
+
let settings = {};
|
|
95
120
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
121
|
+
if (tenant._id) {
|
|
122
|
+
settings = await util.getSettings(tenant._id, true);
|
|
123
|
+
}
|
|
99
124
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
125
|
+
res.json({
|
|
126
|
+
tenant: _tenant,
|
|
127
|
+
password: config.password
|
|
128
|
+
? config.password
|
|
129
|
+
: config.public?.password
|
|
130
|
+
? config.public?.password
|
|
131
|
+
: {},
|
|
132
|
+
root_url: process.env.ROOT_URL,
|
|
133
|
+
already_mail_service: already_mail_service,
|
|
134
|
+
already_sms_service: already_sms_service,
|
|
135
|
+
serverInitInfo: serverInitInfo,
|
|
136
|
+
redirect_url_whitelist: process.env.REDIRECT_URL_WHITELIST,
|
|
137
|
+
platform: platform,
|
|
138
|
+
public: (global as any).Steedos.settings.public || {},
|
|
139
|
+
settings: settings,
|
|
140
|
+
});
|
|
141
|
+
};
|
|
@@ -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
|
+
};
|