@steedos/accounts 3.0.0-beta.15 → 3.0.0-beta.150
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
package/src/core/index.ts
CHANGED
|
@@ -1,226 +1,278 @@
|
|
|
1
|
-
import { getSteedosConfig } from
|
|
2
|
-
import { db } from
|
|
3
|
-
import * as _ from
|
|
4
|
-
import chalk from
|
|
5
|
-
const clone = require(
|
|
1
|
+
import { getSteedosConfig, getSteedosSchema } from "@steedos/objectql";
|
|
2
|
+
import { db } from "../db";
|
|
3
|
+
import * as _ from "lodash";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
const clone = require("clone");
|
|
6
6
|
|
|
7
|
-
declare var MailQueue;
|
|
7
|
+
// declare var MailQueue;
|
|
8
8
|
declare var SMSQueue;
|
|
9
9
|
|
|
10
10
|
const config = getSteedosConfig();
|
|
11
11
|
|
|
12
|
-
export const getSettings = async ()=>{
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
12
|
+
export const getSettings = async () => {
|
|
13
|
+
let tenant = {
|
|
14
|
+
name: "Steedos",
|
|
15
|
+
logo_url: undefined,
|
|
16
|
+
background_url: undefined,
|
|
17
|
+
enable_create_tenant: true,
|
|
18
|
+
enable_register: true,
|
|
19
|
+
enable_forget_password: true,
|
|
20
|
+
enable_password_login: true,
|
|
21
|
+
enable_mobile_code_login: false,
|
|
22
|
+
enable_email_code_login: false,
|
|
23
|
+
enable_bind_mobile: false,
|
|
24
|
+
enable_bind_email: false,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
if (config.tenant) {
|
|
28
|
+
_.assignIn(tenant, config.tenant);
|
|
29
|
+
}
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
if (config.tenant && config.tenant._id) {
|
|
32
|
+
let spaceDoc = await db.findOne("spaces", config.tenant._id, {
|
|
33
|
+
fields: [
|
|
34
|
+
"name",
|
|
35
|
+
"avatar",
|
|
36
|
+
"avatar_dark",
|
|
37
|
+
"background",
|
|
38
|
+
"enable_register",
|
|
39
|
+
],
|
|
40
|
+
});
|
|
41
|
+
let steedosService = getSteedosService();
|
|
42
|
+
if (steedosService && spaceDoc) {
|
|
43
|
+
_.assignIn(tenant, spaceDoc);
|
|
44
|
+
if (spaceDoc.avatar_dark) {
|
|
45
|
+
tenant.logo_url =
|
|
46
|
+
steedosService +
|
|
47
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
48
|
+
spaceDoc.avatar_dark;
|
|
49
|
+
} else if (spaceDoc.avatar) {
|
|
50
|
+
tenant.logo_url =
|
|
51
|
+
steedosService +
|
|
52
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
53
|
+
spaceDoc.avatar;
|
|
29
54
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
_.assignIn(tenant, spaceDoc);
|
|
36
|
-
if (spaceDoc.avatar_dark) {
|
|
37
|
-
tenant.logo_url = steedosService + "api/files/avatars/" + spaceDoc.avatar_dark
|
|
38
|
-
} else if (spaceDoc.avatar) {
|
|
39
|
-
tenant.logo_url = steedosService + "api/files/avatars/" + spaceDoc.avatar
|
|
40
|
-
}
|
|
41
|
-
if (spaceDoc.background) {
|
|
42
|
-
tenant.background_url = steedosService + "api/files/avatars/" + spaceDoc.background
|
|
43
|
-
}
|
|
44
|
-
}
|
|
55
|
+
if (spaceDoc.background) {
|
|
56
|
+
tenant.background_url =
|
|
57
|
+
steedosService +
|
|
58
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
59
|
+
spaceDoc.background;
|
|
45
60
|
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
46
63
|
|
|
47
|
-
|
|
64
|
+
const _tenant = clone(tenant);
|
|
48
65
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
66
|
+
delete _tenant["tokenSecret"];
|
|
67
|
+
delete _tenant["accessTokenExpiresIn"];
|
|
68
|
+
delete _tenant["refreshTokenExpiresIn"];
|
|
52
69
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
70
|
+
return {
|
|
71
|
+
tenant: _tenant,
|
|
72
|
+
password: config.password ? config.password : {},
|
|
73
|
+
root_url: process.env.ROOT_URL,
|
|
74
|
+
};
|
|
75
|
+
};
|
|
59
76
|
|
|
60
|
-
export const getTenant = async (spaceId)=>{
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const spaceDoc = await db.findOne("spaces", spaceId, {fields: ["name", "avatar", "avatar_dark", "background", "enable_register"]})
|
|
77
|
+
export const getTenant = async (spaceId) => {
|
|
78
|
+
if (!spaceId) {
|
|
79
|
+
return {};
|
|
80
|
+
}
|
|
66
81
|
|
|
67
|
-
|
|
68
|
-
|
|
82
|
+
const spaceDoc = await db.findOne("spaces", spaceId, {
|
|
83
|
+
fields: ["name", "avatar", "avatar_dark", "background", "enable_register"],
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (!spaceDoc) {
|
|
87
|
+
return {};
|
|
88
|
+
}
|
|
89
|
+
let steedosService = getSteedosService();
|
|
90
|
+
if (steedosService) {
|
|
91
|
+
if (spaceDoc.avatar_dark) {
|
|
92
|
+
spaceDoc.logo_url =
|
|
93
|
+
steedosService +
|
|
94
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
95
|
+
spaceDoc.avatar_dark;
|
|
96
|
+
} else if (spaceDoc.avatar) {
|
|
97
|
+
spaceDoc.logo_url =
|
|
98
|
+
steedosService +
|
|
99
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
100
|
+
spaceDoc.avatar;
|
|
69
101
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
spaceDoc.logo_url = steedosService + "api/files/avatars/" + spaceDoc.avatar
|
|
76
|
-
}
|
|
77
|
-
if (spaceDoc.background) {
|
|
78
|
-
spaceDoc.background_url = steedosService + "api/files/avatars/" + spaceDoc.background
|
|
79
|
-
}
|
|
102
|
+
if (spaceDoc.background) {
|
|
103
|
+
spaceDoc.background_url =
|
|
104
|
+
steedosService +
|
|
105
|
+
"api/v6/files/cfs.avatars.filerecord/" +
|
|
106
|
+
spaceDoc.background;
|
|
80
107
|
}
|
|
108
|
+
}
|
|
81
109
|
|
|
82
|
-
|
|
83
|
-
}
|
|
110
|
+
return spaceDoc;
|
|
111
|
+
};
|
|
84
112
|
|
|
85
|
-
export const spaceExists = async(spaceId)=>{
|
|
86
|
-
const spaceDoc = await db.findOne("spaces", spaceId, {
|
|
87
|
-
|
|
113
|
+
export const spaceExists = async (spaceId) => {
|
|
114
|
+
const spaceDoc = await db.findOne("spaces", spaceId, {
|
|
115
|
+
fields: ["name", "avatar", "avatar_dark", "background", "enable_register"],
|
|
116
|
+
});
|
|
117
|
+
if (spaceDoc) {
|
|
88
118
|
return true;
|
|
89
119
|
}
|
|
90
120
|
return false;
|
|
91
|
-
}
|
|
121
|
+
};
|
|
92
122
|
|
|
93
|
-
export const getMergedTenant = async (spaceId?)=>{
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
123
|
+
export const getMergedTenant = async (spaceId?) => {
|
|
124
|
+
const settings: any = await getSettings();
|
|
125
|
+
const tenant: any = await getTenant(spaceId);
|
|
126
|
+
return Object.assign({}, settings.tenant, tenant);
|
|
127
|
+
};
|
|
98
128
|
|
|
99
|
-
export const canRegister = async (spaceId, action)=>{
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
129
|
+
export const canRegister = async (spaceId, action) => {
|
|
130
|
+
const tenant: any = await getMergedTenant(spaceId);
|
|
131
|
+
if (action === "emailSignupAccount" && !tenant.enable_email_code_login) {
|
|
132
|
+
return false;
|
|
133
|
+
} else if (
|
|
134
|
+
action === "mobileSignupAccount" &&
|
|
135
|
+
!tenant.enable_mobile_code_login
|
|
136
|
+
) {
|
|
137
|
+
return false;
|
|
138
|
+
} else if (action === "withPassword") {
|
|
139
|
+
return (
|
|
140
|
+
tenant.enable_register &&
|
|
141
|
+
tenant.enable_password_login &&
|
|
142
|
+
tenant.disabled_account_register != true
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return tenant.enable_register && tenant.disabled_account_register != true;
|
|
146
|
+
};
|
|
110
147
|
|
|
111
|
-
export const loginWithCode = async (spaceId)=>{
|
|
148
|
+
export const loginWithCode = async (spaceId) => {
|
|
112
149
|
let loginWithCode = false;
|
|
113
150
|
const tenant: any = await getMergedTenant(spaceId);
|
|
114
|
-
if(tenant.enable_mobile_code_login || tenant.enable_email_code_login){
|
|
151
|
+
if (tenant.enable_mobile_code_login || tenant.enable_email_code_login) {
|
|
115
152
|
loginWithCode = true;
|
|
116
153
|
}
|
|
117
154
|
return loginWithCode;
|
|
118
|
-
}
|
|
155
|
+
};
|
|
119
156
|
|
|
120
|
-
export const canPasswordLogin = async ()=>{
|
|
157
|
+
export const canPasswordLogin = async () => {
|
|
121
158
|
const tenant: any = await getMergedTenant();
|
|
122
159
|
return tenant.enable_password_login;
|
|
123
|
-
}
|
|
160
|
+
};
|
|
124
161
|
|
|
125
|
-
function isEmpty(str){
|
|
126
|
-
if(!str){
|
|
162
|
+
function isEmpty(str) {
|
|
163
|
+
if (!str) {
|
|
127
164
|
return true;
|
|
128
165
|
}
|
|
129
166
|
|
|
130
|
-
if(str ===
|
|
167
|
+
if (str === "undefined") {
|
|
131
168
|
return true;
|
|
132
169
|
}
|
|
133
170
|
|
|
134
|
-
if(_.isString(str) && str.startsWith("${")){
|
|
171
|
+
if (_.isString(str) && str.startsWith("${")) {
|
|
135
172
|
return true;
|
|
136
173
|
}
|
|
137
174
|
|
|
138
175
|
return false;
|
|
139
176
|
}
|
|
140
177
|
|
|
141
|
-
export const canSendEmail = ()=>{
|
|
178
|
+
export const canSendEmail = () => {
|
|
142
179
|
const config = getSteedosConfig().email || {};
|
|
143
180
|
let canSend = true;
|
|
144
181
|
if (!config) {
|
|
145
182
|
canSend = false;
|
|
146
|
-
}else if (isEmpty(config.from)) {
|
|
183
|
+
} else if (isEmpty(config.from)) {
|
|
147
184
|
canSend = false;
|
|
148
|
-
}else if (
|
|
185
|
+
} else if (
|
|
186
|
+
isEmpty(config.url) &&
|
|
187
|
+
(isEmpty(config.host) ||
|
|
188
|
+
isEmpty(config.port) ||
|
|
189
|
+
isEmpty(config.username) ||
|
|
190
|
+
isEmpty(config.password))
|
|
191
|
+
) {
|
|
149
192
|
canSend = false;
|
|
150
193
|
}
|
|
151
194
|
return canSend;
|
|
152
|
-
}
|
|
195
|
+
};
|
|
153
196
|
|
|
154
197
|
//TODO twilio
|
|
155
|
-
export const canSendSMS = ()=>{
|
|
198
|
+
export const canSendSMS = () => {
|
|
156
199
|
const config = (getSteedosConfig().sms || {}).qcloud || {};
|
|
157
200
|
let canSend = true;
|
|
158
201
|
if (!config) {
|
|
159
202
|
canSend = false;
|
|
160
|
-
}else if (
|
|
203
|
+
} else if (
|
|
204
|
+
isEmpty(config.sdkappid) ||
|
|
205
|
+
isEmpty(config.appkey) ||
|
|
206
|
+
isEmpty(config.signname)
|
|
207
|
+
) {
|
|
161
208
|
canSend = false;
|
|
162
209
|
}
|
|
163
210
|
return canSend;
|
|
164
|
-
}
|
|
211
|
+
};
|
|
165
212
|
|
|
166
213
|
export const getRootUrlPathPrefix = (rootUrl) => {
|
|
167
214
|
if (rootUrl) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
215
|
+
var parsedUrl = require("url").parse(rootUrl);
|
|
216
|
+
if (
|
|
217
|
+
!parsedUrl.host ||
|
|
218
|
+
["http:", "https:"].indexOf(parsedUrl.protocol) === -1
|
|
219
|
+
) {
|
|
220
|
+
throw Error("$ROOT_URL, if specified, must be an URL");
|
|
221
|
+
}
|
|
222
|
+
var pathPrefix = parsedUrl.pathname;
|
|
223
|
+
if (pathPrefix.slice(-1) === "/") {
|
|
224
|
+
pathPrefix = pathPrefix.slice(0, -1);
|
|
225
|
+
}
|
|
226
|
+
return pathPrefix;
|
|
177
227
|
} else {
|
|
178
|
-
|
|
228
|
+
return "";
|
|
179
229
|
}
|
|
180
|
-
}
|
|
230
|
+
};
|
|
181
231
|
|
|
182
|
-
export const getSteedosService = ()=>{
|
|
232
|
+
export const getSteedosService = () => {
|
|
183
233
|
let steedosService = getRootUrlPathPrefix(process.env.ROOT_URL);
|
|
184
234
|
if (config.webservices && config.webservices.steedos) {
|
|
185
235
|
if (!config.webservices.steedos.endsWith("/"))
|
|
186
|
-
config.webservices.steedos += "/"
|
|
236
|
+
config.webservices.steedos += "/";
|
|
187
237
|
steedosService = config.webservices.steedos;
|
|
188
238
|
}
|
|
189
|
-
if (!steedosService.endsWith("/"))
|
|
190
|
-
steedosService += "/" ;
|
|
239
|
+
if (!steedosService.endsWith("/")) steedosService += "/";
|
|
191
240
|
return steedosService;
|
|
192
|
-
}
|
|
241
|
+
};
|
|
193
242
|
|
|
194
243
|
export const sendMail = async (mail: any): Promise<void> => {
|
|
195
|
-
const {to, subject, html} = mail;
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
from: config.from || "华炎魔方",
|
|
207
|
-
subject: subject,
|
|
208
|
-
html: html
|
|
209
|
-
});
|
|
244
|
+
const { to, subject, html } = mail;
|
|
245
|
+
console.log(chalk.green(`MAIL: ${to}, ${subject}`));
|
|
246
|
+
try {
|
|
247
|
+
const mailOptions = {
|
|
248
|
+
to, // 收件人
|
|
249
|
+
subject, // 主题
|
|
250
|
+
html, // 内容
|
|
251
|
+
};
|
|
252
|
+
await getSteedosSchema().broker.call("@builder6/email.send", mailOptions);
|
|
253
|
+
} catch (e) {
|
|
254
|
+
console.log(e);
|
|
210
255
|
}
|
|
211
256
|
};
|
|
212
257
|
|
|
213
258
|
export const sendSMS = async (sms: any): Promise<void> => {
|
|
214
|
-
const {mobile, message, spaceId} = sms;
|
|
259
|
+
const { mobile, message, spaceId } = sms;
|
|
215
260
|
let canSend = canSendSMS();
|
|
216
|
-
console.log(chalk.green(`SMS: ${mobile}, ${message}`))
|
|
217
|
-
if(!canSend){
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
261
|
+
console.log(chalk.green(`SMS: ${mobile}, ${message}`));
|
|
262
|
+
if (!canSend) {
|
|
263
|
+
console.log(
|
|
264
|
+
chalk.red(
|
|
265
|
+
"ERROR sending sms, Please set sms configs in steedos.config.js",
|
|
266
|
+
),
|
|
267
|
+
);
|
|
268
|
+
return;
|
|
269
|
+
} else {
|
|
270
|
+
SMSQueue.send(
|
|
271
|
+
{
|
|
272
|
+
RecNum: mobile,
|
|
273
|
+
msg: message,
|
|
274
|
+
},
|
|
275
|
+
spaceId,
|
|
276
|
+
);
|
|
225
277
|
}
|
|
226
|
-
}
|
|
278
|
+
};
|
|
@@ -5,7 +5,7 @@ import { AccountsServer } from "../../server";
|
|
|
5
5
|
import { sendError } from "../utils/send-error";
|
|
6
6
|
import { setAuthCookies, clearAuthCookies } from "../utils/steedos-auth";
|
|
7
7
|
import { getUserIdByToken } from "@steedos/auth";
|
|
8
|
-
import
|
|
8
|
+
import { getClientIp } from "../utils/getClientIp";
|
|
9
9
|
import { getUserAgent } from "../utils/get-user-agent";
|
|
10
10
|
|
|
11
11
|
const queryString = require("querystring");
|
|
@@ -20,7 +20,7 @@ export const authorize =
|
|
|
20
20
|
const connection = req.query.connection || "steedos";
|
|
21
21
|
const state = req.query.state || "";
|
|
22
22
|
const userAgent = getUserAgent(req);
|
|
23
|
-
const ip =
|
|
23
|
+
const ip = getClientIp(req);
|
|
24
24
|
let query = queryString.stringify(req.query);
|
|
25
25
|
let redirect_uri = req.query.redirect_uri
|
|
26
26
|
? (req.query.redirect_uri as string)
|
|
@@ -1,32 +1,31 @@
|
|
|
1
|
-
import * as express from
|
|
2
|
-
import
|
|
3
|
-
import { AccountsServer } from
|
|
4
|
-
import { LoginUserIdentity } from
|
|
5
|
-
import { getUserAgent } from
|
|
6
|
-
import { sendError } from
|
|
1
|
+
import * as express from "express";
|
|
2
|
+
import { getClientIp } from "../utils/getClientIp";
|
|
3
|
+
import { AccountsServer } from "../../server";
|
|
4
|
+
import { LoginUserIdentity } from "@accounts/types";
|
|
5
|
+
import { getUserAgent } from "../utils/get-user-agent";
|
|
6
|
+
import { sendError } from "../utils/send-error";
|
|
7
7
|
|
|
8
|
-
export const impersonate =
|
|
9
|
-
|
|
10
|
-
res: express.Response
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
};
|
|
8
|
+
export const impersonate =
|
|
9
|
+
(accountsServer: AccountsServer) =>
|
|
10
|
+
async (req: express.Request, res: express.Response) => {
|
|
11
|
+
try {
|
|
12
|
+
const {
|
|
13
|
+
impersonated,
|
|
14
|
+
accessToken,
|
|
15
|
+
}: {
|
|
16
|
+
accessToken: string;
|
|
17
|
+
impersonated: LoginUserIdentity;
|
|
18
|
+
} = req.body;
|
|
19
|
+
const userAgent = getUserAgent(req);
|
|
20
|
+
const ip = getClientIp(req);
|
|
21
|
+
const impersonateRes = await accountsServer.impersonate(
|
|
22
|
+
accessToken,
|
|
23
|
+
impersonated,
|
|
24
|
+
ip,
|
|
25
|
+
userAgent,
|
|
26
|
+
);
|
|
27
|
+
res.json(impersonateRes);
|
|
28
|
+
} catch (err) {
|
|
29
|
+
sendError(res, err);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -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
|
+
};
|