@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.
Files changed (43) hide show
  1. package/lib/core/index.js +87 -42
  2. package/lib/core/index.js.map +1 -1
  3. package/lib/rest-express/endpoints/authorize.js +2 -2
  4. package/lib/rest-express/endpoints/authorize.js.map +1 -1
  5. package/lib/rest-express/endpoints/impersonate.js +25 -23
  6. package/lib/rest-express/endpoints/impersonate.js.map +1 -1
  7. package/lib/rest-express/endpoints/login.js +95 -93
  8. package/lib/rest-express/endpoints/login.js.map +1 -1
  9. package/lib/rest-express/endpoints/logout.js +80 -73
  10. package/lib/rest-express/endpoints/logout.js.map +1 -1
  11. package/lib/rest-express/endpoints/oauth/provider-callback.js +35 -33
  12. package/lib/rest-express/endpoints/oauth/provider-callback.js.map +1 -1
  13. package/lib/rest-express/endpoints/password/change-password.js +95 -90
  14. package/lib/rest-express/endpoints/password/change-password.js.map +1 -1
  15. package/lib/rest-express/endpoints/refresh-access-token.js +25 -23
  16. package/lib/rest-express/endpoints/refresh-access-token.js.map +1 -1
  17. package/lib/rest-express/endpoints/service-authenticate.js +76 -74
  18. package/lib/rest-express/endpoints/service-authenticate.js.map +1 -1
  19. package/lib/rest-express/endpoints/steedos/get-tenant.js +62 -39
  20. package/lib/rest-express/endpoints/steedos/get-tenant.js.map +1 -1
  21. package/lib/rest-express/endpoints/steedos/settings.js +119 -88
  22. package/lib/rest-express/endpoints/steedos/settings.js.map +1 -1
  23. package/lib/rest-express/endpoints/update-session.js +44 -42
  24. package/lib/rest-express/endpoints/update-session.js.map +1 -1
  25. package/lib/rest-express/user-loader.js +82 -67
  26. package/lib/rest-express/user-loader.js.map +1 -1
  27. package/lib/rest-express/utils/getClientIp.js +16 -0
  28. package/lib/rest-express/utils/getClientIp.js.map +1 -0
  29. package/package.json +5 -6
  30. package/src/core/index.ts +197 -145
  31. package/src/rest-express/endpoints/authorize.ts +2 -2
  32. package/src/rest-express/endpoints/impersonate.ts +30 -31
  33. package/src/rest-express/endpoints/login.ts +66 -61
  34. package/src/rest-express/endpoints/logout.ts +74 -72
  35. package/src/rest-express/endpoints/oauth/provider-callback.ts +45 -38
  36. package/src/rest-express/endpoints/password/change-password.ts +94 -83
  37. package/src/rest-express/endpoints/refresh-access-token.ts +23 -24
  38. package/src/rest-express/endpoints/service-authenticate.ts +87 -68
  39. package/src/rest-express/endpoints/steedos/get-tenant.ts +56 -38
  40. package/src/rest-express/endpoints/steedos/settings.ts +117 -88
  41. package/src/rest-express/endpoints/update-session.ts +50 -42
  42. package/src/rest-express/user-loader.ts +68 -58
  43. package/src/rest-express/utils/getClientIp.ts +25 -0
@@ -3,80 +3,82 @@
3
3
  * @Date: 2022-03-28 09:35:34
4
4
  * @LastEditors: baozhoutao@steedos.com
5
5
  * @LastEditTime: 2024-01-23 14:24:35
6
- * @Description:
6
+ * @Description:
7
7
  */
8
- import * as express from 'express';
9
- import { get, isEmpty, map } from 'lodash';
10
- import { AccountsServer } from '../../server';
11
- import { sendError } from '../utils/send-error';
12
- import { clearAuthCookies } from '../utils/steedos-auth';
13
- import { getObject } from '@steedos/objectql';
14
- import * as requestIp from 'request-ip';
15
- import { getUserAgent } from '../utils/get-user-agent';
16
- import isMobile from 'ismobilejs';
17
- import { getSteedosSchema } from '@steedos/objectql'
18
- export const logout = (accountsServer: AccountsServer) => async (
19
- req: express.Request,
20
- res: express.Response
21
- ) => {
22
-
23
- let authToken =
24
- get(req.cookies, 'X-Auth-Token') ||
25
- get(req.headers, 'Authorization') ||
26
- get(req.headers, 'authorization');
8
+ import * as express from "express";
9
+ import { get, isEmpty, map } from "lodash";
10
+ import { AccountsServer } from "../../server";
11
+ import { sendError } from "../utils/send-error";
12
+ import { clearAuthCookies } from "../utils/steedos-auth";
13
+ import { getObject } from "@steedos/objectql";
14
+ import { getClientIp } from "../utils/getClientIp";
15
+ import { getUserAgent } from "../utils/get-user-agent";
16
+ import isMobile from "ismobilejs";
17
+ import { getSteedosSchema } from "@steedos/objectql";
18
+ export const logout =
19
+ (accountsServer: AccountsServer) =>
20
+ async (req: express.Request, res: express.Response) => {
21
+ let authToken =
22
+ get(req.cookies, "X-Auth-Token") ||
23
+ get(req.headers, "Authorization") ||
24
+ get(req.headers, "authorization");
27
25
 
28
- authToken = authToken && authToken.replace('Bearer ', 'token');
29
- authToken = authToken && authToken.split(',').length >1?authToken.split(',')[0]:authToken;
26
+ authToken = authToken && authToken.replace("Bearer ", "token");
27
+ authToken =
28
+ authToken && authToken.split(",").length > 1
29
+ ? authToken.split(",")[0]
30
+ : authToken;
30
31
 
31
- clearAuthCookies(req, res);
32
- let session = null;
33
- try {
34
- session = await accountsServer.logout(authToken);
35
- } catch (err) {
36
- //sendError(res, err);
37
- }finally{
38
- let userAgent = getUserAgent(req) || '';
39
- const ip = requestIp.getClientIp(req);
40
- let status = 'success';
41
- let message = '';
42
- let is_phone = false;
43
- let is_tablet = false;
44
- if (userAgent) {
45
- try {
46
- const { phone, tablet } = isMobile(userAgent);
47
- is_phone = phone;
48
- is_tablet = tablet;
49
- } catch (Exception) {
50
- console.log(`Exception`, Exception);
32
+ clearAuthCookies(req, res);
33
+ let session = null;
34
+ try {
35
+ session = await accountsServer.logout(authToken);
36
+ } catch (err) {
37
+ //sendError(res, err);
38
+ } finally {
39
+ let userAgent = getUserAgent(req) || "";
40
+ const ip = getClientIp(req);
41
+ let status = "success";
42
+ let message = "";
43
+ let is_phone = false;
44
+ let is_tablet = false;
45
+ if (userAgent) {
46
+ try {
47
+ const { phone, tablet } = isMobile(userAgent);
48
+ is_phone = phone;
49
+ is_tablet = tablet;
50
+ } catch (Exception) {
51
+ console.log(`Exception`, Exception);
52
+ }
51
53
  }
52
- }
53
- await getObject('operation_logs').insert({
54
- name: '注销',
55
- type: 'logout',
56
- remote_user: session?.userId,
57
- remote_addr: ip,
58
- http_user_agent: userAgent,
59
- is_mobile: is_phone,
60
- is_tablet,
61
- object: 'users',
62
- status: status,
63
- create: new Date(),
64
- space: session?.space,
65
- message: message,
66
- data: JSON.stringify({
67
- authToken: authToken,
68
- session: session
69
- }),
70
- related_to: {
71
- o: "users",
72
- ids: [session?.userId]
54
+ await getObject("operation_logs").insert({
55
+ name: "注销",
56
+ type: "logout",
57
+ remote_user: session?.userId,
58
+ remote_addr: ip,
59
+ http_user_agent: userAgent,
60
+ is_mobile: is_phone,
61
+ is_tablet,
62
+ object: "users",
63
+ status: status,
64
+ create: new Date(),
65
+ space: session?.space,
66
+ message: message,
67
+ data: JSON.stringify({
68
+ authToken: authToken,
69
+ session: session,
70
+ }),
71
+ related_to: {
72
+ o: "users",
73
+ ids: [session?.userId],
74
+ },
75
+ });
76
+ if (authToken) {
77
+ const broker = getSteedosSchema().broker;
78
+ broker.broadcast("$user.logout", {
79
+ authToken: authToken,
80
+ });
73
81
  }
74
- });
75
-
76
- const broker = getSteedosSchema().broker;
77
- broker.broadcast("$user.logout", {
78
- authToken: authToken
79
- });
80
- }
81
- res.json(null);
82
- };
82
+ }
83
+ res.json(null);
84
+ };
@@ -1,46 +1,53 @@
1
- import * as express from 'express';
2
- import * as requestIp from 'request-ip';
3
- import { AccountsServer } from '../../../server';
4
- import { getUserAgent } from '../../utils/get-user-agent';
5
- import { sendError } from '../../utils/send-error';
6
- import { AccountsExpressOptions } from '../../types';
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
- options?: AccountsExpressOptions
15
- ) => async (req: express.Request, res: express.Response) => {
16
- try {
17
- const userAgent = getUserAgent(req);
18
- const ip = requestIp.getClientIp(req);
19
- const loggedInUser = await accountsServer.loginWithService(
20
- 'oauth',
21
- {
22
- ...(req.params || {}),
23
- ...(req.query || {}),
24
- ...(req.body || {}),
25
- ...((req as RequestWithSession).session || {}),
26
- },
27
- { ip, userAgent }
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
- if (options && options.onOAuthSuccess) {
31
- options.onOAuthSuccess(req, res, loggedInUser);
32
- }
37
+ if (options && options.onOAuthSuccess) {
38
+ options.onOAuthSuccess(req, res, loggedInUser);
39
+ }
33
40
 
34
- if (options && options.transformOAuthResponse) {
35
- res.json(options.transformOAuthResponse(loggedInUser));
36
- } else {
37
- res.json(loggedInUser);
38
- }
39
- } catch (err) {
40
- if (options && options.onOAuthError) {
41
- options.onOAuthError(req, res, err);
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
- sendError(res, err);
45
- }
46
- };
51
+ sendError(res, err);
52
+ }
53
+ };
@@ -1,103 +1,114 @@
1
1
  /*
2
2
  * @Author: baozhoutao@steedos.com
3
3
  * @Date: 2022-05-19 11:38:30
4
- * @LastEditors: baozhoutao@steedos.com
5
- * @LastEditTime: 2023-09-18 17:58:22
6
- * @Description:
4
+ * @LastEditors: 孙浩林 sunhaolin@steedos.com
5
+ * @LastEditTime: 2025-09-12 14:29:58
6
+ * @Description:
7
7
  */
8
- import * as express from 'express';
9
- import { AccountsServer } from '../../../server';
10
- import { sendError } from '../../utils/send-error';
11
- import { getSteedosConfig, getObject } from '@steedos/objectql'
12
- import { hashPassword } from '../../../password/utils';
8
+ import * as express from "express";
9
+ import { AccountsServer } from "../../../server";
10
+ import { sendError } from "../../utils/send-error";
11
+ import { getSteedosConfig, getObject } from "@steedos/objectql";
12
+ import { hashPassword } from "../../../password/utils";
13
13
 
14
- import * as requestIp from 'request-ip';
15
- import { getUserAgent } from '../../utils/get-user-agent';
14
+ import { getClientIp } from "../../utils/getClientIp";
15
+ import { getUserAgent } from "../../utils/get-user-agent";
16
16
  import isMobile from "ismobilejs";
17
- import { db } from '../../../db';
17
+ import { db } from "../../../db";
18
18
 
19
19
  const config = getSteedosConfig();
20
20
  declare var Creator;
21
21
 
22
- export const changePassword = (accountsServer: AccountsServer) => async (
23
- req: express.Request,
24
- res: express.Response
25
- ) => {
26
- try {
27
- if (!(req as any).userId) {
28
- res.status(401);
29
- res.json({ message: 'Unauthorized' });
30
- return;
31
- }
32
- // oldPassword newPassword 已经是 sha256之后的
33
- const { oldPassword, newPassword } = req.body;
22
+ export const changePassword =
23
+ (accountsServer: AccountsServer) =>
24
+ async (req: express.Request, res: express.Response) => {
25
+ try {
26
+ if (!(req as any).userId) {
27
+ res.status(401);
28
+ res.json({ message: "Unauthorized" });
29
+ return;
30
+ }
31
+ // oldPassword 、newPassword 已经是 sha256之后的
32
+ const { oldPassword, newPassword } = req.body;
34
33
 
35
- // let passworPolicy = ((config as any).password || {}).policy
34
+ // let passworPolicy = ((config as any).password || {}).policy
36
35
 
37
- // if(passworPolicy){
38
- // if(!(new RegExp(passworPolicy)).test(newPassword || '')){
39
- // sendError(res, new Error((config as any).password.policyError));
40
- // return;
41
- // }
42
- // }
43
-
44
- const password: any = accountsServer.getServices().password;
36
+ // if(passworPolicy){
37
+ // if(!(new RegExp(passworPolicy)).test(newPassword || '')){
38
+ // sendError(res, new Error((config as any).password.policyError));
39
+ // return;
40
+ // }
41
+ // }
45
42
 
46
- await password.changePassword((req as any).userId, oldPassword, newPassword);
47
- password.db.collection.updateOne({_id: (req as any).userId}, {$set: {password_expired: false}})
48
- try {
49
- Creator.getCollection('space_users').update({user: (req as any).userId}, {$set: {password_expired: false}}, {
50
- multi: true
51
- })
43
+ const password: any = accountsServer.getServices().password;
44
+
45
+ await password.changePassword(
46
+ (req as any).userId,
47
+ oldPassword,
48
+ newPassword,
49
+ );
50
+ password.db.collection.updateOne(
51
+ { _id: (req as any).userId },
52
+ { $set: { password_expired: false } },
53
+ );
54
+ try {
55
+ await db.updateMany(
56
+ "space_users",
57
+ [["user", "=", (req as any).userId]],
58
+ { password_expired: false },
59
+ );
52
60
 
53
- const userAgent = getUserAgent(req);
54
- const ip = requestIp.getClientIp(req);
55
- let is_phone = false;
56
- let is_tablet = false;
57
- if (userAgent) {
58
- try {
59
- const { phone, tablet } = isMobile(userAgent);
60
- is_phone = phone;
61
- is_tablet = tablet;
62
- } catch (Exception) {
63
- console.log(`Exception`, Exception);
61
+ const userAgent = getUserAgent(req);
62
+ const ip = getClientIp(req);
63
+ let is_phone = false;
64
+ let is_tablet = false;
65
+ if (userAgent) {
66
+ try {
67
+ const { phone, tablet } = isMobile(userAgent);
68
+ is_phone = phone;
69
+ is_tablet = tablet;
70
+ } catch (Exception) {
71
+ console.log(`Exception`, Exception);
72
+ }
64
73
  }
65
- }
66
74
 
67
- const userSpaces = await db.find("space_users", {
68
- filters: [["user", "=", (req as any).userId],["user_accepted", "=", true]],
69
- fields: ["space"],
70
- });
75
+ const userSpaces = await db.find("space_users", {
76
+ filters: [
77
+ ["user", "=", (req as any).userId],
78
+ ["user_accepted", "=", true],
79
+ ],
80
+ fields: ["space"],
81
+ });
71
82
 
72
- if(userSpaces && userSpaces.length > 0){
73
- for (let userSpace of userSpaces) {
74
- const userId = (req as any).userId
75
- await getObject('operation_logs').insert({
76
- name: '修改密码',
77
- type: 'change_password',
78
- remote_user: userId,
79
- remote_addr: ip,
80
- http_user_agent: userAgent,
81
- is_mobile: is_phone,
82
- is_tablet,
83
- object: 'users',
84
- status: 'success',
85
- create: new Date(),
86
- create_by: userId,
87
- modified_by: userId,
88
- space: userSpace.space,
89
- related_to: {
90
- o: "users",
91
- ids: [userId]
92
- }
93
- })
83
+ if (userSpaces && userSpaces.length > 0) {
84
+ for (let userSpace of userSpaces) {
85
+ const userId = (req as any).userId;
86
+ await getObject("operation_logs").insert({
87
+ name: "修改密码",
88
+ type: "change_password",
89
+ remote_user: userId,
90
+ remote_addr: ip,
91
+ http_user_agent: userAgent,
92
+ is_mobile: is_phone,
93
+ is_tablet,
94
+ object: "users",
95
+ status: "success",
96
+ create: new Date(),
97
+ create_by: userId,
98
+ modified_by: userId,
99
+ space: userSpace.space,
100
+ related_to: {
101
+ o: "users",
102
+ ids: [userId],
103
+ },
104
+ });
105
+ }
94
106
  }
107
+ } catch (error) {
108
+ console.log("error", error);
95
109
  }
96
- } catch (error) {
97
- console.log('error', error);
110
+ res.json({ userId: (req as any).userId, password_expired: false });
111
+ } catch (err) {
112
+ sendError(res, err);
98
113
  }
99
- res.json({userId: (req as any).userId, password_expired: false});
100
- } catch (err) {
101
- sendError(res, err);
102
- }
103
- };
114
+ };
@@ -1,25 +1,24 @@
1
- import * as express from 'express';
2
- import * as requestIp from 'request-ip';
3
- import { AccountsServer } from '../../server';
4
- import { getUserAgent } from '../utils/get-user-agent';
5
- import { sendError } from '../utils/send-error';
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 = (accountsServer: AccountsServer) => async (
8
- req: express.Request,
9
- res: express.Response
10
- ) => {
11
- try {
12
- const { accessToken, refreshToken } = req.body;
13
- const userAgent = getUserAgent(req);
14
- const ip = requestIp.getClientIp(req);
15
- const refreshedSession = await accountsServer.refreshTokens(
16
- accessToken,
17
- refreshToken,
18
- ip,
19
- userAgent
20
- );
21
- res.json(refreshedSession);
22
- } catch (err) {
23
- sendError(res, err);
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
+ };