vona-module-a-user 5.0.17 → 5.0.19

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.
@@ -58,6 +58,40 @@ declare module 'vona' {
58
58
  }
59
59
  }
60
60
  /** bean: end */
61
+ /** service: begin */
62
+ export * from '../service/authTokenAdapter.ts';
63
+ export * from '../service/redisToken.ts';
64
+ import 'vona';
65
+ declare module 'vona-module-a-bean' {
66
+ interface IServiceRecord {
67
+ 'a-user:authTokenAdapter': never;
68
+ 'a-user:redisToken': never;
69
+ }
70
+ }
71
+ declare module 'vona-module-a-user' {
72
+ interface ServiceAuthTokenAdapter {
73
+ }
74
+ interface ServiceRedisToken {
75
+ }
76
+ }
77
+ /** service: end */
78
+ /** service: begin */
79
+ import type { ServiceAuthTokenAdapter } from '../service/authTokenAdapter.ts';
80
+ import type { ServiceRedisToken } from '../service/redisToken.ts';
81
+ export interface IModuleService {
82
+ 'authTokenAdapter': ServiceAuthTokenAdapter;
83
+ 'redisToken': ServiceRedisToken;
84
+ }
85
+ /** service: end */
86
+ /** service: begin */
87
+ import 'vona';
88
+ declare module 'vona' {
89
+ interface IBeanRecordGeneral {
90
+ 'a-user.service.authTokenAdapter': ServiceAuthTokenAdapter;
91
+ 'a-user.service.redisToken': ServiceRedisToken;
92
+ }
93
+ }
94
+ /** service: end */
61
95
  /** event: begin */
62
96
  export * from '../bean/event.createUserAnonymous.ts';
63
97
  export * from '../bean/event.signin.ts';
@@ -121,6 +155,7 @@ export declare class ScopeModuleAUser extends BeanScopeBase {
121
155
  export interface ScopeModuleAUser {
122
156
  util: BeanScopeUtil;
123
157
  config: TypeModuleConfig<typeof config>;
158
+ service: IModuleService;
124
159
  event: IModuleEvent;
125
160
  }
126
161
  import 'vona';
@@ -12,4 +12,14 @@ export declare function config(_app: VonaApplication): {
12
12
  authInner: keyof IServiceRecord;
13
13
  roleInner: keyof IServiceRecord;
14
14
  };
15
+ payloadData: {
16
+ fields: {
17
+ authId: string;
18
+ userId: string;
19
+ token: string;
20
+ };
21
+ };
22
+ redisToken: {
23
+ maxAge: number;
24
+ };
15
25
  };
package/dist/index.js CHANGED
@@ -1,18 +1,18 @@
1
- import { BeanInfo, BeanBase, beanFullNameFromOnionName, BeanScopeBase } from 'vona';
1
+ import { BeanInfo, BeanBase, beanFullNameFromOnionName, createHash, uuidv4, BeanScopeBase } from 'vona';
2
2
  import { catchError } from '@cabloy/utils';
3
3
  import { Guard, Aspect } from 'vona-module-a-aspect';
4
- import { Bean, Scope } from 'vona-module-a-bean';
4
+ import { Bean, Service, Scope } from 'vona-module-a-bean';
5
5
  import { Event, BeanEventBase } from 'vona-module-a-event';
6
6
  import { Meta } from 'vona-module-a-meta';
7
7
 
8
- var _dec$b, _dec2$b, _class$b;
9
- let GuardPassport = (_dec$b = Guard({
8
+ var _dec$d, _dec2$d, _class$d;
9
+ let GuardPassport = (_dec$d = Guard({
10
10
  global: true,
11
11
  public: false,
12
12
  checkAuthToken: true
13
- }), _dec2$b = BeanInfo({
13
+ }), _dec2$d = BeanInfo({
14
14
  module: "a-user"
15
- }), _dec$b(_class$b = _dec2$b(_class$b = class GuardPassport extends BeanBase {
15
+ }), _dec$d(_class$d = _dec2$d(_class$d = class GuardPassport extends BeanBase {
16
16
  async execute(options, next) {
17
17
  // auth token
18
18
  if (!this.bean.passport.getCurrent()) {
@@ -38,7 +38,7 @@ let GuardPassport = (_dec$b = Guard({
38
38
  // next
39
39
  return next();
40
40
  }
41
- }) || _class$b) || _class$b);
41
+ }) || _class$d) || _class$d);
42
42
 
43
43
  let __roleAdapter;
44
44
  function setRoleAdapter(roleAdapter) {
@@ -74,12 +74,12 @@ function $getUserIdSystem(_userName, userId) {
74
74
  return userId;
75
75
  }
76
76
 
77
- var _dec$a, _dec2$a, _class$a;
78
- let GuardRoleName = (_dec$a = Guard({
77
+ var _dec$c, _dec2$c, _class$c;
78
+ let GuardRoleName = (_dec$c = Guard({
79
79
  passWhenMatched: true
80
- }), _dec2$a = BeanInfo({
80
+ }), _dec2$c = BeanInfo({
81
81
  module: "a-user"
82
- }), _dec$a(_class$a = _dec2$a(_class$a = class GuardRoleName extends BeanBase {
82
+ }), _dec$c(_class$c = _dec2$c(_class$c = class GuardRoleName extends BeanBase {
83
83
  async execute(options, next) {
84
84
  if (!options.name) return this.app.throw(403);
85
85
  const user = this.bean.passport.getCurrentUser();
@@ -93,14 +93,14 @@ let GuardRoleName = (_dec$a = Guard({
93
93
  // next
94
94
  return next();
95
95
  }
96
- }) || _class$a) || _class$a);
96
+ }) || _class$c) || _class$c);
97
97
 
98
- var _dec$9, _dec2$9, _class$9;
99
- let GuardUserName = (_dec$9 = Guard({
98
+ var _dec$b, _dec2$b, _class$b;
99
+ let GuardUserName = (_dec$b = Guard({
100
100
  passWhenMatched: true
101
- }), _dec2$9 = BeanInfo({
101
+ }), _dec2$b = BeanInfo({
102
102
  module: "a-user"
103
- }), _dec$9(_class$9 = _dec2$9(_class$9 = class GuardUserName extends BeanBase {
103
+ }), _dec$b(_class$b = _dec2$b(_class$b = class GuardUserName extends BeanBase {
104
104
  async execute(options, next) {
105
105
  if (!options.name) return this.app.throw(403);
106
106
  const user = this.bean.passport.getCurrentUser();
@@ -112,12 +112,12 @@ let GuardUserName = (_dec$9 = Guard({
112
112
  // next
113
113
  return next();
114
114
  }
115
- }) || _class$9) || _class$9);
115
+ }) || _class$b) || _class$b);
116
116
 
117
- var _dec$8, _dec2$8, _class$8;
118
- let BeanAuthInner = (_dec$8 = Bean(), _dec2$8 = BeanInfo({
117
+ var _dec$a, _dec2$a, _class$a;
118
+ let BeanAuthInner = (_dec$a = Bean(), _dec2$a = BeanInfo({
119
119
  module: "a-user"
120
- }), _dec$8(_class$8 = _dec2$8(_class$8 = class BeanAuthInner extends BeanBase {
120
+ }), _dec$a(_class$a = _dec2$a(_class$a = class BeanAuthInner extends BeanBase {
121
121
  constructor(...args) {
122
122
  super(...args);
123
123
  this._authInnerAdapter = void 0;
@@ -133,7 +133,7 @@ let BeanAuthInner = (_dec$8 = Bean(), _dec2$8 = BeanInfo({
133
133
  if (String(auth.id).charAt(0) === '-') return auth;
134
134
  return await this.authInnerAdapter.findOne(auth);
135
135
  }
136
- }) || _class$8) || _class$8);
136
+ }) || _class$a) || _class$a);
137
137
 
138
138
  let __authAdapter;
139
139
  function setAuthAdapter(authAdapter) {
@@ -146,10 +146,10 @@ function $getAuthIdSystem(_authName, authId) {
146
146
  return authId;
147
147
  }
148
148
 
149
- var _dec$7, _dec2$7, _class$7;
150
- let BeanPassport = (_dec$7 = Bean(), _dec2$7 = BeanInfo({
149
+ var _dec$9, _dec2$9, _class$9;
150
+ let BeanPassport = (_dec$9 = Bean(), _dec2$9 = BeanInfo({
151
151
  module: "a-user"
152
- }), _dec$7(_class$7 = _dec2$7(_class$7 = class BeanPassport extends BeanBase {
152
+ }), _dec$9(_class$9 = _dec2$9(_class$9 = class BeanPassport extends BeanBase {
153
153
  constructor(...args) {
154
154
  super(...args);
155
155
  this._authTokenAdapter = void 0;
@@ -340,12 +340,12 @@ let BeanPassport = (_dec$7 = Bean(), _dec2$7 = BeanInfo({
340
340
  return payloadData2;
341
341
  }
342
342
  }
343
- }) || _class$7) || _class$7);
343
+ }) || _class$9) || _class$9);
344
344
 
345
- var _dec$6, _dec2$6, _class$6;
346
- let BeanRoleInner = (_dec$6 = Bean(), _dec2$6 = BeanInfo({
345
+ var _dec$8, _dec2$8, _class$8;
346
+ let BeanRoleInner = (_dec$8 = Bean(), _dec2$8 = BeanInfo({
347
347
  module: "a-user"
348
- }), _dec$6(_class$6 = _dec2$6(_class$6 = class BeanRoleInner extends BeanBase {
348
+ }), _dec$8(_class$8 = _dec2$8(_class$8 = class BeanRoleInner extends BeanBase {
349
349
  constructor(...args) {
350
350
  super(...args);
351
351
  this._roleInnerAdapter = void 0;
@@ -366,12 +366,12 @@ let BeanRoleInner = (_dec$6 = Bean(), _dec2$6 = BeanInfo({
366
366
  findAllByUserId(userId) {
367
367
  return this.roleInnerAdapter.findAllByUserId(userId);
368
368
  }
369
- }) || _class$6) || _class$6);
369
+ }) || _class$8) || _class$8);
370
370
 
371
- var _dec$5, _dec2$5, _class$5;
372
- let BeanUserInner = (_dec$5 = Bean(), _dec2$5 = BeanInfo({
371
+ var _dec$7, _dec2$7, _class$7;
372
+ let BeanUserInner = (_dec$7 = Bean(), _dec2$7 = BeanInfo({
373
373
  module: "a-user"
374
- }), _dec$5(_class$5 = _dec2$5(_class$5 = class BeanUserInner extends BeanBase {
374
+ }), _dec$7(_class$7 = _dec2$7(_class$7 = class BeanUserInner extends BeanBase {
375
375
  constructor(...args) {
376
376
  super(...args);
377
377
  this._userInnerAdapter = void 0;
@@ -401,6 +401,104 @@ let BeanUserInner = (_dec$5 = Bean(), _dec2$5 = BeanInfo({
401
401
  remove(user) {
402
402
  return this.userInnerAdapter.remove(user);
403
403
  }
404
+ }) || _class$7) || _class$7);
405
+
406
+ var _dec$6, _dec2$6, _class$6;
407
+ let ServiceAuthTokenAdapter = (_dec$6 = Service(), _dec2$6 = BeanInfo({
408
+ module: "a-user"
409
+ }), _dec$6(_class$6 = _dec2$6(_class$6 = class ServiceAuthTokenAdapter extends BeanBase {
410
+ async create(payloadData) {
411
+ const authIdStr = this._getAuthId(payloadData)?.toString();
412
+ const token = authIdStr === '-1' ? createHash(authIdStr) : uuidv4();
413
+ const payloadDataNew = Object.assign({}, payloadData, {
414
+ [this.scope.config.payloadData.fields.token]: token
415
+ });
416
+ await this.scope.service.redisToken.create(payloadDataNew);
417
+ return payloadDataNew;
418
+ }
419
+ async retrieve(payloadData) {
420
+ return await this.scope.service.redisToken.retrieve(payloadData);
421
+ }
422
+ async verify(payloadData) {
423
+ return await this.scope.service.redisToken.verify(payloadData);
424
+ }
425
+ async refresh(payloadData) {
426
+ await this.scope.service.redisToken.refresh(payloadData);
427
+ }
428
+ async remove(payloadData) {
429
+ await this.scope.service.redisToken.remove(payloadData);
430
+ }
431
+ async removeAll(user) {
432
+ await this.scope.service.redisToken.removeAll(user);
433
+ }
434
+ _getAuthId(payloadData) {
435
+ return payloadData[this.scope.config.payloadData.fields.authId];
436
+ }
437
+ }) || _class$6) || _class$6);
438
+
439
+ var _dec$5, _dec2$5, _class$5;
440
+ let ServiceRedisToken = (_dec$5 = Service(), _dec2$5 = BeanInfo({
441
+ module: "a-user"
442
+ }), _dec$5(_class$5 = _dec2$5(_class$5 = class ServiceRedisToken extends BeanBase {
443
+ get redisAuth() {
444
+ return this.bean.redis.get('auth');
445
+ }
446
+ async verify(payloadData) {
447
+ const payloadData2 = await this.retrieve(payloadData);
448
+ if (!payloadData2) return false;
449
+ if (this._getToken(payloadData2) !== this._getToken(payloadData)) return false;
450
+ return true;
451
+ }
452
+ async retrieve(payloadData) {
453
+ const key = this._getAuthRedisKey(payloadData);
454
+ if (!key) return;
455
+ const token = await this.redisAuth.get(key);
456
+ if (!token) return;
457
+ return {
458
+ ...payloadData,
459
+ [this.scope.config.payloadData.fields.token]: token
460
+ };
461
+ }
462
+ async create(payloadData) {
463
+ const key = this._getAuthRedisKey(payloadData);
464
+ if (!key || !this._getToken(payloadData)) return this.app.throw(401);
465
+ await this.redisAuth.set(key, this._getToken(payloadData), 'EX', this.scope.config.redisToken.maxAge);
466
+ }
467
+ async refresh(payloadData) {
468
+ const key = this._getAuthRedisKey(payloadData);
469
+ if (!key) return this.app.throw(401);
470
+ await this.redisAuth.expire(key, this.scope.config.redisToken.maxAge);
471
+ }
472
+ async remove(payloadData) {
473
+ const key = this._getAuthRedisKey(payloadData);
474
+ if (!key) return;
475
+ await this.redisAuth.del(key);
476
+ }
477
+ async removeAll(user) {
478
+ const keyPrefix = this.redisAuth.options.keyPrefix;
479
+ const keyPattern = this._getAuthRedisKeyPattern(user, keyPrefix);
480
+ const keys = await this.redisAuth.keys(keyPattern);
481
+ for (const fullKey of keys) {
482
+ const key = keyPrefix ? fullKey.substring(keyPrefix.length) : fullKey;
483
+ await this.redisAuth.del(key);
484
+ }
485
+ }
486
+ _getAuthRedisKey(payloadData) {
487
+ if (!this.ctx.instance) return;
488
+ return `authToken:${this.ctx.instance.id}:${this._getUserId(payloadData)}:${this._getAuthId(payloadData)}`;
489
+ }
490
+ _getAuthRedisKeyPattern(user, keyPrefix) {
491
+ return `${keyPrefix ?? ''}authToken:${this.ctx.instance.id}:${user.id}:*`;
492
+ }
493
+ _getToken(payloadData) {
494
+ return payloadData[this.scope.config.payloadData.fields.token];
495
+ }
496
+ _getAuthId(payloadData) {
497
+ return payloadData[this.scope.config.payloadData.fields.authId];
498
+ }
499
+ _getUserId(payloadData) {
500
+ return payloadData[this.scope.config.payloadData.fields.userId];
501
+ }
404
502
  }) || _class$5) || _class$5);
405
503
 
406
504
  var _dec$4, _dec2$4, _class$4;
@@ -444,11 +542,21 @@ function config(_app) {
444
542
  refreshAuthToken: 'recreate'
445
543
  },
446
544
  adapter: {
447
- authToken: 'home-user:authTokenAdapter',
545
+ authToken: 'a-user:authTokenAdapter',
448
546
  passport: 'home-user:passportAdapter',
449
547
  userInner: 'home-user:userInnerAdapter',
450
548
  authInner: 'home-user:authInnerAdapter',
451
549
  roleInner: 'home-user:roleInnerAdapter'
550
+ },
551
+ payloadData: {
552
+ fields: {
553
+ authId: 'authId',
554
+ userId: 'userId',
555
+ token: 'token'
556
+ }
557
+ },
558
+ redisToken: {
559
+ maxAge: 30 * 24 * 60 * 60
452
560
  }
453
561
  };
454
562
  }
@@ -484,4 +592,4 @@ const Passport = {
484
592
  admin: Admin
485
593
  };
486
594
 
487
- export { $getAuthId, $getAuthIdSystem, $getRoleId, $getRoleName, $getUserAnonymous, $getUserAvatar, $getUserId, $getUserIdSystem, $getUserLocale, $getUserName, BeanAuthInner, BeanPassport, BeanRoleInner, BeanUserInner, EventCreateUserAnonymous, EventSignin, EventSignout, GuardPassport, GuardRoleName, GuardUserName, MetaPrintTip, Passport, ScopeModuleAUser, config, setAuthAdapter, setRoleAdapter, setUserAdapter };
595
+ export { $getAuthId, $getAuthIdSystem, $getRoleId, $getRoleName, $getUserAnonymous, $getUserAvatar, $getUserId, $getUserIdSystem, $getUserLocale, $getUserName, BeanAuthInner, BeanPassport, BeanRoleInner, BeanUserInner, EventCreateUserAnonymous, EventSignin, EventSignout, GuardPassport, GuardRoleName, GuardUserName, MetaPrintTip, Passport, ScopeModuleAUser, ServiceAuthTokenAdapter, ServiceRedisToken, config, setAuthAdapter, setRoleAdapter, setUserAdapter };
@@ -0,0 +1,13 @@
1
+ import type { IPayloadDataBase } from 'vona-module-a-jwt';
2
+ import type { IAuthTokenAdapter } from '../types/authToken.ts';
3
+ import type { IUserBase } from '../types/user.ts';
4
+ import { BeanBase } from 'vona';
5
+ export declare class ServiceAuthTokenAdapter extends BeanBase implements IAuthTokenAdapter {
6
+ create(payloadData: IPayloadDataBase): Promise<IPayloadDataBase>;
7
+ retrieve(payloadData: IPayloadDataBase): Promise<IPayloadDataBase | undefined>;
8
+ verify(payloadData: IPayloadDataBase): Promise<boolean>;
9
+ refresh(payloadData: IPayloadDataBase): Promise<void>;
10
+ remove(payloadData: IPayloadDataBase): Promise<void>;
11
+ removeAll(user: IUserBase): Promise<void>;
12
+ private _getAuthId;
13
+ }
@@ -0,0 +1,17 @@
1
+ import type { IPayloadDataBase } from 'vona-module-a-jwt';
2
+ import type { IUserBase } from 'vona-module-a-user';
3
+ import { BeanBase } from 'vona';
4
+ export declare class ServiceRedisToken extends BeanBase {
5
+ private get redisAuth();
6
+ verify(payloadData: IPayloadDataBase): Promise<boolean>;
7
+ retrieve(payloadData: IPayloadDataBase): Promise<IPayloadDataBase | undefined>;
8
+ create(payloadData: IPayloadDataBase): Promise<undefined>;
9
+ refresh(payloadData: IPayloadDataBase): Promise<undefined>;
10
+ remove(payloadData: IPayloadDataBase): Promise<void>;
11
+ removeAll(user: IUserBase): Promise<void>;
12
+ private _getAuthRedisKey;
13
+ private _getAuthRedisKeyPattern;
14
+ private _getToken;
15
+ private _getAuthId;
16
+ private _getUserId;
17
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-user",
3
3
  "type": "module",
4
- "version": "5.0.17",
4
+ "version": "5.0.19",
5
5
  "title": "a-user",
6
6
  "vonaModule": {
7
7
  "dependencies": {}