vona-module-a-user 5.0.24 → 5.0.25
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.
|
@@ -107,6 +107,29 @@ declare module 'vona' {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
/** service: end */
|
|
110
|
+
/** cacheRedis: begin */
|
|
111
|
+
export * from '../bean/cacheRedis.authToken.ts';
|
|
112
|
+
import { type IDecoratorCacheRedisOptions } from 'vona-module-a-cache';
|
|
113
|
+
declare module 'vona-module-a-cache' {
|
|
114
|
+
interface ICacheRedisRecord {
|
|
115
|
+
'a-user:authToken': IDecoratorCacheRedisOptions;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
declare module 'vona-module-a-user' {
|
|
119
|
+
interface CacheRedisAuthToken {
|
|
120
|
+
}
|
|
121
|
+
interface CacheRedisAuthToken {
|
|
122
|
+
get $beanFullName(): 'a-user.cacheRedis.authToken';
|
|
123
|
+
get $onionName(): 'a-user:authToken';
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/** cacheRedis: end */
|
|
127
|
+
/** cacheRedis: begin */
|
|
128
|
+
import type { CacheRedisAuthToken } from '../bean/cacheRedis.authToken.ts';
|
|
129
|
+
export interface IModuleCacheRedis {
|
|
130
|
+
'authToken': CacheRedisAuthToken;
|
|
131
|
+
}
|
|
132
|
+
/** cacheRedis: end */
|
|
110
133
|
/** event: begin */
|
|
111
134
|
export * from '../bean/event.activate.ts';
|
|
112
135
|
export * from '../bean/event.createUserAnonymous.ts';
|
|
@@ -209,6 +232,7 @@ export interface ScopeModuleAUser {
|
|
|
209
232
|
util: BeanScopeUtil;
|
|
210
233
|
config: TypeModuleConfig<typeof config>;
|
|
211
234
|
service: IModuleService;
|
|
235
|
+
cacheRedis: IModuleCacheRedis;
|
|
212
236
|
event: IModuleEvent;
|
|
213
237
|
}
|
|
214
238
|
import 'vona';
|
|
@@ -24,7 +24,7 @@ export declare class BeanPassport extends BeanBase {
|
|
|
24
24
|
signinMock(name?: keyof IUserNameRecord, options?: ISigninOptions): Promise<IJwtToken>;
|
|
25
25
|
signinWithAnonymous(): Promise<void>;
|
|
26
26
|
createUserAnonymous(): Promise<IUserBase>;
|
|
27
|
-
kickOut(user
|
|
27
|
+
kickOut(user?: IUserBase): Promise<void>;
|
|
28
28
|
checkAuthToken(accessToken?: string, clientName?: keyof IJwtClientRecord, options?: IJwtVerifyOptions): Promise<IPayloadDataBase | undefined>;
|
|
29
29
|
refreshAuthToken(refreshToken: string): Promise<IJwtToken>;
|
|
30
30
|
createTempAuthToken(options?: IJwtSignOptions): Promise<string>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { BeanCacheRedisBase } from 'vona-module-a-cache';
|
|
2
|
+
export type TCacheRedisAuthTokenKey = string;
|
|
3
|
+
export type TCacheRedisAuthTokenData = string;
|
|
4
|
+
export declare class CacheRedisAuthToken extends BeanCacheRedisBase<TCacheRedisAuthTokenKey, TCacheRedisAuthTokenData> {
|
|
5
|
+
}
|
package/dist/config/config.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -2,18 +2,19 @@ import { BeanInfo, BeanBase, beanFullNameFromOnionName, createHash, uuidv4, Bean
|
|
|
2
2
|
import { catchError } from '@cabloy/utils';
|
|
3
3
|
import { Guard, Aspect } from 'vona-module-a-aspect';
|
|
4
4
|
import { Bean, Service, Scope } from 'vona-module-a-bean';
|
|
5
|
+
import { CacheRedis, BeanCacheRedisBase } from 'vona-module-a-cache';
|
|
5
6
|
import { Event, BeanEventBase } from 'vona-module-a-event';
|
|
6
7
|
import { Meta } from 'vona-module-a-meta';
|
|
7
8
|
|
|
8
|
-
var _dec$
|
|
9
|
-
let GuardPassport = (_dec$
|
|
9
|
+
var _dec$f, _dec2$f, _class$f;
|
|
10
|
+
let GuardPassport = (_dec$f = Guard({
|
|
10
11
|
global: true,
|
|
11
12
|
public: false,
|
|
12
13
|
activated: true,
|
|
13
14
|
checkAuthToken: true
|
|
14
|
-
}), _dec2$
|
|
15
|
+
}), _dec2$f = BeanInfo({
|
|
15
16
|
module: "a-user"
|
|
16
|
-
}), _dec$
|
|
17
|
+
}), _dec$f(_class$f = _dec2$f(_class$f = class GuardPassport extends BeanBase {
|
|
17
18
|
async execute(options, next) {
|
|
18
19
|
// auth token
|
|
19
20
|
if (!this.bean.passport.getCurrent()) {
|
|
@@ -47,14 +48,14 @@ let GuardPassport = (_dec$e = Guard({
|
|
|
47
48
|
// next
|
|
48
49
|
return next();
|
|
49
50
|
}
|
|
50
|
-
}) || _class$
|
|
51
|
+
}) || _class$f) || _class$f);
|
|
51
52
|
|
|
52
|
-
var _dec$
|
|
53
|
-
let GuardRoleName = (_dec$
|
|
53
|
+
var _dec$e, _dec2$e, _class$e;
|
|
54
|
+
let GuardRoleName = (_dec$e = Guard({
|
|
54
55
|
passWhenMatched: true
|
|
55
|
-
}), _dec2$
|
|
56
|
+
}), _dec2$e = BeanInfo({
|
|
56
57
|
module: "a-user"
|
|
57
|
-
}), _dec$
|
|
58
|
+
}), _dec$e(_class$e = _dec2$e(_class$e = class GuardRoleName extends BeanBase {
|
|
58
59
|
async execute(options, next) {
|
|
59
60
|
if (!options.name) return this.app.throw(403);
|
|
60
61
|
const user = this.bean.passport.getCurrentUser();
|
|
@@ -68,14 +69,14 @@ let GuardRoleName = (_dec$d = Guard({
|
|
|
68
69
|
// next
|
|
69
70
|
return next();
|
|
70
71
|
}
|
|
71
|
-
}) || _class$
|
|
72
|
+
}) || _class$e) || _class$e);
|
|
72
73
|
|
|
73
|
-
var _dec$
|
|
74
|
-
let GuardUserName = (_dec$
|
|
74
|
+
var _dec$d, _dec2$d, _class$d;
|
|
75
|
+
let GuardUserName = (_dec$d = Guard({
|
|
75
76
|
passWhenMatched: true
|
|
76
|
-
}), _dec2$
|
|
77
|
+
}), _dec2$d = BeanInfo({
|
|
77
78
|
module: "a-user"
|
|
78
|
-
}), _dec$
|
|
79
|
+
}), _dec$d(_class$d = _dec2$d(_class$d = class GuardUserName extends BeanBase {
|
|
79
80
|
async execute(options, next) {
|
|
80
81
|
if (!options.name) return this.app.throw(403);
|
|
81
82
|
const user = this.bean.passport.getCurrentUser();
|
|
@@ -87,16 +88,16 @@ let GuardUserName = (_dec$c = Guard({
|
|
|
87
88
|
// next
|
|
88
89
|
return next();
|
|
89
90
|
}
|
|
90
|
-
}) || _class$
|
|
91
|
+
}) || _class$d) || _class$d);
|
|
91
92
|
|
|
92
93
|
function $getAuthIdSystem(_authName, authId) {
|
|
93
94
|
return authId;
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
var _dec$
|
|
97
|
-
let BeanPassport = (_dec$
|
|
97
|
+
var _dec$c, _dec2$c, _class$c;
|
|
98
|
+
let BeanPassport = (_dec$c = Bean(), _dec2$c = BeanInfo({
|
|
98
99
|
module: "a-user"
|
|
99
|
-
}), _dec$
|
|
100
|
+
}), _dec$c(_class$c = _dec2$c(_class$c = class BeanPassport extends BeanBase {
|
|
100
101
|
constructor(...args) {
|
|
101
102
|
super(...args);
|
|
102
103
|
this._authTokenAdapter = void 0;
|
|
@@ -199,6 +200,7 @@ let BeanPassport = (_dec$b = Bean(), _dec2$b = BeanInfo({
|
|
|
199
200
|
return userAnonymous;
|
|
200
201
|
}
|
|
201
202
|
async kickOut(user) {
|
|
203
|
+
if (!user) return;
|
|
202
204
|
await this.authTokenAdapter.removeAll(user);
|
|
203
205
|
}
|
|
204
206
|
async checkAuthToken(accessToken, clientName, options) {
|
|
@@ -302,12 +304,12 @@ let BeanPassport = (_dec$b = Bean(), _dec2$b = BeanInfo({
|
|
|
302
304
|
return payloadData2;
|
|
303
305
|
}
|
|
304
306
|
}
|
|
305
|
-
}) || _class$
|
|
307
|
+
}) || _class$c) || _class$c);
|
|
306
308
|
|
|
307
|
-
var _dec$
|
|
308
|
-
let BeanRole = (_dec$
|
|
309
|
+
var _dec$b, _dec2$b, _class$b;
|
|
310
|
+
let BeanRole = (_dec$b = Bean(), _dec2$b = BeanInfo({
|
|
309
311
|
module: "a-user"
|
|
310
|
-
}), _dec$
|
|
312
|
+
}), _dec$b(_class$b = _dec2$b(_class$b = class BeanRole extends BeanBase {
|
|
311
313
|
constructor(...args) {
|
|
312
314
|
super(...args);
|
|
313
315
|
this._roleAdapter = void 0;
|
|
@@ -328,12 +330,12 @@ let BeanRole = (_dec$a = Bean(), _dec2$a = BeanInfo({
|
|
|
328
330
|
findAllByUserId(userId) {
|
|
329
331
|
return this.roleAdapter.findAllByUserId(userId);
|
|
330
332
|
}
|
|
331
|
-
}) || _class$
|
|
333
|
+
}) || _class$b) || _class$b);
|
|
332
334
|
|
|
333
|
-
var _dec$
|
|
334
|
-
let BeanUser = (_dec$
|
|
335
|
+
var _dec$a, _dec2$a, _class$a;
|
|
336
|
+
let BeanUser = (_dec$a = Bean(), _dec2$a = BeanInfo({
|
|
335
337
|
module: "a-user"
|
|
336
|
-
}), _dec$
|
|
338
|
+
}), _dec$a(_class$a = _dec2$a(_class$a = class BeanUser extends BeanBase {
|
|
337
339
|
constructor(...args) {
|
|
338
340
|
super(...args);
|
|
339
341
|
this._userAdapter = void 0;
|
|
@@ -386,12 +388,12 @@ let BeanUser = (_dec$9 = Bean(), _dec2$9 = BeanInfo({
|
|
|
386
388
|
remove(user) {
|
|
387
389
|
return this.userAdapter.remove(user);
|
|
388
390
|
}
|
|
389
|
-
}) || _class$
|
|
391
|
+
}) || _class$a) || _class$a);
|
|
390
392
|
|
|
391
|
-
var _dec$
|
|
392
|
-
let ServiceAuthTokenAdapter = (_dec$
|
|
393
|
+
var _dec$9, _dec2$9, _class$9;
|
|
394
|
+
let ServiceAuthTokenAdapter = (_dec$9 = Service(), _dec2$9 = BeanInfo({
|
|
393
395
|
module: "a-user"
|
|
394
|
-
}), _dec$
|
|
396
|
+
}), _dec$9(_class$9 = _dec2$9(_class$9 = class ServiceAuthTokenAdapter extends BeanBase {
|
|
395
397
|
async create(payloadData) {
|
|
396
398
|
const authIdStr = this._getAuthId(payloadData)?.toString();
|
|
397
399
|
const token = authIdStr === '-1' ? createHash(authIdStr) : uuidv4();
|
|
@@ -419,15 +421,12 @@ let ServiceAuthTokenAdapter = (_dec$8 = Service(), _dec2$8 = BeanInfo({
|
|
|
419
421
|
_getAuthId(payloadData) {
|
|
420
422
|
return payloadData[this.scope.config.payloadData.fields.authId];
|
|
421
423
|
}
|
|
422
|
-
}) || _class$
|
|
424
|
+
}) || _class$9) || _class$9);
|
|
423
425
|
|
|
424
|
-
var _dec$
|
|
425
|
-
let ServiceRedisToken = (_dec$
|
|
426
|
+
var _dec$8, _dec2$8, _class$8;
|
|
427
|
+
let ServiceRedisToken = (_dec$8 = Service(), _dec2$8 = BeanInfo({
|
|
426
428
|
module: "a-user"
|
|
427
|
-
}), _dec$
|
|
428
|
-
get redisAuth() {
|
|
429
|
-
return this.bean.redis.get('auth');
|
|
430
|
-
}
|
|
429
|
+
}), _dec$8(_class$8 = _dec2$8(_class$8 = class ServiceRedisToken extends BeanBase {
|
|
431
430
|
async verify(payloadData) {
|
|
432
431
|
const payloadData2 = await this.retrieve(payloadData);
|
|
433
432
|
if (!payloadData2) return false;
|
|
@@ -437,7 +436,7 @@ let ServiceRedisToken = (_dec$7 = Service(), _dec2$7 = BeanInfo({
|
|
|
437
436
|
async retrieve(payloadData) {
|
|
438
437
|
const key = this._getAuthRedisKey(payloadData);
|
|
439
438
|
if (!key) return;
|
|
440
|
-
const token = await this.
|
|
439
|
+
const token = await this.scope.cacheRedis.authToken.get(key);
|
|
441
440
|
if (!token) return;
|
|
442
441
|
return {
|
|
443
442
|
...payloadData,
|
|
@@ -447,33 +446,29 @@ let ServiceRedisToken = (_dec$7 = Service(), _dec2$7 = BeanInfo({
|
|
|
447
446
|
async create(payloadData) {
|
|
448
447
|
const key = this._getAuthRedisKey(payloadData);
|
|
449
448
|
if (!key || !this._getToken(payloadData)) return this.app.throw(401);
|
|
450
|
-
await this.
|
|
449
|
+
await this.scope.cacheRedis.authToken.set(this._getToken(payloadData), key);
|
|
451
450
|
}
|
|
452
451
|
async refresh(payloadData) {
|
|
453
452
|
const key = this._getAuthRedisKey(payloadData);
|
|
454
453
|
if (!key) return this.app.throw(401);
|
|
455
|
-
await this.
|
|
454
|
+
await this.scope.cacheRedis.authToken.expire(key);
|
|
456
455
|
}
|
|
457
456
|
async remove(payloadData) {
|
|
458
457
|
const key = this._getAuthRedisKey(payloadData);
|
|
459
458
|
if (!key) return;
|
|
460
|
-
await this.
|
|
459
|
+
await this.scope.cacheRedis.authToken.del(key);
|
|
461
460
|
}
|
|
462
461
|
async removeAll(user) {
|
|
463
|
-
const keyPrefix = this.
|
|
464
|
-
const
|
|
465
|
-
|
|
466
|
-
for (const fullKey of keys) {
|
|
467
|
-
const key = keyPrefix ? fullKey.substring(keyPrefix.length) : fullKey;
|
|
468
|
-
await this.redisAuth.del(key);
|
|
469
|
-
}
|
|
462
|
+
const keyPrefix = this._getAuthRedisKeyPrefix(user);
|
|
463
|
+
const keys = await this.scope.cacheRedis.authToken.lookupKeys(keyPrefix, true);
|
|
464
|
+
await this.scope.cacheRedis.authToken.mdel(keys);
|
|
470
465
|
}
|
|
471
466
|
_getAuthRedisKey(payloadData) {
|
|
472
467
|
if (!this.ctx.instance) return;
|
|
473
|
-
return
|
|
468
|
+
return `${this._getUserId(payloadData)}:${this._getAuthId(payloadData)}`;
|
|
474
469
|
}
|
|
475
|
-
|
|
476
|
-
return `${
|
|
470
|
+
_getAuthRedisKeyPrefix(user) {
|
|
471
|
+
return `${user.id}`;
|
|
477
472
|
}
|
|
478
473
|
_getToken(payloadData) {
|
|
479
474
|
return payloadData[this.scope.config.payloadData.fields.token];
|
|
@@ -484,7 +479,15 @@ let ServiceRedisToken = (_dec$7 = Service(), _dec2$7 = BeanInfo({
|
|
|
484
479
|
_getUserId(payloadData) {
|
|
485
480
|
return payloadData[this.scope.config.payloadData.fields.userId];
|
|
486
481
|
}
|
|
487
|
-
}) || _class$
|
|
482
|
+
}) || _class$8) || _class$8);
|
|
483
|
+
|
|
484
|
+
var _dec$7, _dec2$7, _class$7;
|
|
485
|
+
let CacheRedisAuthToken = (_dec$7 = CacheRedis({
|
|
486
|
+
ttl: 30 * 24 * 60 * 60 * 1000,
|
|
487
|
+
disableTransactionCompensate: true
|
|
488
|
+
}), _dec2$7 = BeanInfo({
|
|
489
|
+
module: "a-user"
|
|
490
|
+
}), _dec$7(_class$7 = _dec2$7(_class$7 = class CacheRedisAuthToken extends BeanCacheRedisBase {}) || _class$7) || _class$7);
|
|
488
491
|
|
|
489
492
|
var _dec$6, _dec2$6, _class$6;
|
|
490
493
|
let EventActivate = (_dec$6 = Event(), _dec2$6 = BeanInfo({
|
|
@@ -551,9 +554,6 @@ function config(_app) {
|
|
|
551
554
|
userId: 'userId',
|
|
552
555
|
token: 'token'
|
|
553
556
|
}
|
|
554
|
-
},
|
|
555
|
-
redisToken: {
|
|
556
|
-
maxAge: 30 * 24 * 60 * 60
|
|
557
557
|
}
|
|
558
558
|
};
|
|
559
559
|
}
|
|
@@ -600,4 +600,4 @@ function $getUserIdSystem(_userName, userId) {
|
|
|
600
600
|
return userId;
|
|
601
601
|
}
|
|
602
602
|
|
|
603
|
-
export { $getAuthIdSystem, $getUserIdSystem, BeanPassport, BeanRole, BeanUser, EventActivate, EventCreateUserAnonymous, EventRegister, EventSignin, EventSignout, GuardPassport, GuardRoleName, GuardUserName, MetaPrintTip, Passport, ScopeModuleAUser, ServiceAuthTokenAdapter, ServiceRedisToken, config };
|
|
603
|
+
export { $getAuthIdSystem, $getUserIdSystem, BeanPassport, BeanRole, BeanUser, CacheRedisAuthToken, EventActivate, EventCreateUserAnonymous, EventRegister, EventSignin, EventSignout, GuardPassport, GuardRoleName, GuardUserName, MetaPrintTip, Passport, ScopeModuleAUser, ServiceAuthTokenAdapter, ServiceRedisToken, config };
|
|
@@ -2,7 +2,6 @@ import type { IPayloadDataBase } from 'vona-module-a-jwt';
|
|
|
2
2
|
import type { IUserBase } from 'vona-module-a-user';
|
|
3
3
|
import { BeanBase } from 'vona';
|
|
4
4
|
export declare class ServiceRedisToken extends BeanBase {
|
|
5
|
-
private get redisAuth();
|
|
6
5
|
verify(payloadData: IPayloadDataBase): Promise<boolean>;
|
|
7
6
|
retrieve(payloadData: IPayloadDataBase): Promise<IPayloadDataBase | undefined>;
|
|
8
7
|
create(payloadData: IPayloadDataBase): Promise<undefined>;
|
|
@@ -10,7 +9,7 @@ export declare class ServiceRedisToken extends BeanBase {
|
|
|
10
9
|
remove(payloadData: IPayloadDataBase): Promise<void>;
|
|
11
10
|
removeAll(user: IUserBase): Promise<void>;
|
|
12
11
|
private _getAuthRedisKey;
|
|
13
|
-
private
|
|
12
|
+
private _getAuthRedisKeyPrefix;
|
|
14
13
|
private _getToken;
|
|
15
14
|
private _getAuthId;
|
|
16
15
|
private _getUserId;
|