vona-module-a-user 5.0.49 → 5.1.2

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 (112) hide show
  1. package/LICENSE +0 -0
  2. package/dist/.metadata/index.d.ts +17 -1
  3. package/dist/.metadata/index.d.ts.map +1 -0
  4. package/dist/.metadata/locales.d.ts +11 -0
  5. package/dist/.metadata/locales.d.ts.map +1 -0
  6. package/dist/.metadata/this.d.ts +1 -0
  7. package/dist/.metadata/this.d.ts.map +1 -0
  8. package/dist/bean/bean.passport.d.ts +5 -2
  9. package/dist/bean/bean.passport.d.ts.map +1 -0
  10. package/dist/bean/bean.role.d.ts +3 -1
  11. package/dist/bean/bean.role.d.ts.map +1 -0
  12. package/dist/bean/bean.user.d.ts +4 -3
  13. package/dist/bean/bean.user.d.ts.map +1 -0
  14. package/dist/bean/cacheRedis.authToken.d.ts +1 -0
  15. package/dist/bean/cacheRedis.authToken.d.ts.map +1 -0
  16. package/dist/bean/event.activate.d.ts +2 -1
  17. package/dist/bean/event.activate.d.ts.map +1 -0
  18. package/dist/bean/event.createAnonymous.d.ts +2 -1
  19. package/dist/bean/event.createAnonymous.d.ts.map +1 -0
  20. package/dist/bean/event.register.d.ts +2 -1
  21. package/dist/bean/event.register.d.ts.map +1 -0
  22. package/dist/bean/event.signin.d.ts +2 -1
  23. package/dist/bean/event.signin.d.ts.map +1 -0
  24. package/dist/bean/event.signout.d.ts +2 -1
  25. package/dist/bean/event.signout.d.ts.map +1 -0
  26. package/dist/bean/guard.passport.d.ts +1 -0
  27. package/dist/bean/guard.passport.d.ts.map +1 -0
  28. package/dist/bean/guard.roleName.d.ts +2 -1
  29. package/dist/bean/guard.roleName.d.ts.map +1 -0
  30. package/dist/bean/guard.userName.d.ts +2 -1
  31. package/dist/bean/guard.userName.d.ts.map +1 -0
  32. package/dist/bean/meta.printTip.d.ts +1 -0
  33. package/dist/bean/meta.printTip.d.ts.map +1 -0
  34. package/dist/bean/meta.runtime.d.ts +1 -0
  35. package/dist/bean/meta.runtime.d.ts.map +1 -0
  36. package/dist/config/config.d.ts +1 -0
  37. package/dist/config/config.d.ts.map +1 -0
  38. package/dist/config/errors.d.ts +4 -0
  39. package/dist/config/errors.d.ts.map +1 -0
  40. package/dist/config/locale/en-us.d.ts +5 -0
  41. package/dist/config/locale/en-us.d.ts.map +1 -0
  42. package/dist/config/locale/zh-cn.d.ts +5 -0
  43. package/dist/config/locale/zh-cn.d.ts.map +1 -0
  44. package/dist/index.d.ts +2 -0
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +65 -20
  47. package/dist/index.js.map +1 -0
  48. package/dist/lib/auth.d.ts +1 -0
  49. package/dist/lib/auth.d.ts.map +1 -0
  50. package/dist/lib/index.d.ts +1 -0
  51. package/dist/lib/index.d.ts.map +1 -0
  52. package/dist/lib/passport.d.ts +1 -0
  53. package/dist/lib/passport.d.ts.map +1 -0
  54. package/dist/lib/user.d.ts +1 -0
  55. package/dist/lib/user.d.ts.map +1 -0
  56. package/dist/main.d.ts +1 -0
  57. package/dist/main.d.ts.map +1 -0
  58. package/dist/service/authTokenAdapter.d.ts +2 -1
  59. package/dist/service/authTokenAdapter.d.ts.map +1 -0
  60. package/dist/service/redisToken.d.ts +1 -0
  61. package/dist/service/redisToken.d.ts.map +1 -0
  62. package/dist/types/auth.d.ts +1 -0
  63. package/dist/types/auth.d.ts.map +1 -0
  64. package/dist/types/authProfile.d.ts +1 -0
  65. package/dist/types/authProfile.d.ts.map +1 -0
  66. package/dist/types/authToken.d.ts +1 -0
  67. package/dist/types/authToken.d.ts.map +1 -0
  68. package/dist/types/index.d.ts +1 -0
  69. package/dist/types/index.d.ts.map +1 -0
  70. package/dist/types/passport.d.ts +1 -0
  71. package/dist/types/passport.d.ts.map +1 -0
  72. package/dist/types/role.d.ts +2 -0
  73. package/dist/types/role.d.ts.map +1 -0
  74. package/dist/types/user.d.ts +1 -0
  75. package/dist/types/user.d.ts.map +1 -0
  76. package/package.json +21 -16
  77. package/src/.metadata/index.ts +382 -0
  78. package/src/.metadata/locales.ts +13 -0
  79. package/src/.metadata/this.ts +2 -0
  80. package/src/bean/bean.passport.ts +249 -0
  81. package/src/bean/bean.role.ts +39 -0
  82. package/src/bean/bean.user.ts +88 -0
  83. package/src/bean/cacheRedis.authToken.ts +10 -0
  84. package/src/bean/event.activate.ts +10 -0
  85. package/src/bean/event.createAnonymous.ts +10 -0
  86. package/src/bean/event.register.ts +14 -0
  87. package/src/bean/event.signin.ts +10 -0
  88. package/src/bean/event.signout.ts +10 -0
  89. package/src/bean/guard.passport.ts +53 -0
  90. package/src/bean/guard.roleName.ts +35 -0
  91. package/src/bean/guard.userName.ts +35 -0
  92. package/src/bean/meta.printTip.ts +18 -0
  93. package/src/bean/meta.runtime.ts +19 -0
  94. package/src/config/config.ts +28 -0
  95. package/src/config/errors.ts +3 -0
  96. package/src/config/locale/en-us.ts +3 -0
  97. package/src/config/locale/zh-cn.ts +3 -0
  98. package/src/index.ts +4 -0
  99. package/src/lib/auth.ts +7 -0
  100. package/src/lib/index.ts +3 -0
  101. package/src/lib/passport.ts +41 -0
  102. package/src/lib/user.ts +7 -0
  103. package/src/main.ts +25 -0
  104. package/src/service/authTokenAdapter.ts +42 -0
  105. package/src/service/redisToken.ts +68 -0
  106. package/src/types/auth.ts +26 -0
  107. package/src/types/authProfile.ts +23 -0
  108. package/src/types/authToken.ts +12 -0
  109. package/src/types/index.ts +6 -0
  110. package/src/types/passport.ts +29 -0
  111. package/src/types/role.ts +19 -0
  112. package/src/types/user.ts +35 -0
@@ -0,0 +1,41 @@
1
+ import { Aspect } from 'vona-module-a-aspect';
2
+
3
+ import type { IGuardOptionsRoleName } from '../bean/guard.roleName.ts';
4
+ import type { IGuardOptionsUserName } from '../bean/guard.userName.ts';
5
+
6
+ function Public(_public: boolean = true): ClassDecorator & MethodDecorator {
7
+ return Aspect.guardGlobal('a-user:passport', { public: _public });
8
+ }
9
+
10
+ // true/false/undefined
11
+ function Activated(activated?: boolean): ClassDecorator & MethodDecorator {
12
+ return Aspect.guardGlobal('a-user:passport', { activated });
13
+ }
14
+
15
+ function UserName(options?: Partial<IGuardOptionsUserName>): ClassDecorator & MethodDecorator {
16
+ return Aspect.guard('a-user:userName', options);
17
+ }
18
+
19
+ function RoleName(options?: Partial<IGuardOptionsRoleName>): ClassDecorator & MethodDecorator {
20
+ return Aspect.guard('a-user:roleName', options);
21
+ }
22
+
23
+ function Admin(options?: Partial<Omit<IGuardOptionsRoleName, 'name'>>): ClassDecorator & MethodDecorator {
24
+ return Aspect.guard('a-user:roleName', Object.assign({}, options, { name: 'admin' as const }));
25
+ }
26
+
27
+ export interface IDecoratorGroupPassport {
28
+ public: typeof Public;
29
+ activated: typeof Activated;
30
+ userName: typeof UserName;
31
+ roleName: typeof RoleName;
32
+ admin: typeof Admin;
33
+ }
34
+
35
+ export const Passport: IDecoratorGroupPassport = {
36
+ public: Public,
37
+ activated: Activated,
38
+ userName: UserName,
39
+ roleName: RoleName,
40
+ admin: Admin,
41
+ } as unknown as IDecoratorGroupPassport;
@@ -0,0 +1,7 @@
1
+ import type { TableIdentity } from 'table-identity';
2
+
3
+ import type { IUserIdRecord } from '../types/user.ts';
4
+
5
+ export function $getUserIdSystem<K extends keyof IUserIdRecord>(_userName: IUserIdRecord[K], userId: K): TableIdentity {
6
+ return userId;
7
+ }
package/src/main.ts ADDED
@@ -0,0 +1,25 @@
1
+ import type { IModuleMain, VonaContext } from 'vona';
2
+
3
+ import { BeanSimple } from 'vona';
4
+
5
+ export class Main extends BeanSimple implements IModuleMain {
6
+ async moduleLoading() {}
7
+ async moduleLoaded() {
8
+ // user
9
+ Object.defineProperty(this.app.context, 'user', {
10
+ enumerable: false,
11
+ get(this: VonaContext) {
12
+ return this.app.bean.passport.currentUser;
13
+ },
14
+ });
15
+ // passport
16
+ Object.defineProperty(this.app.context, 'passport', {
17
+ enumerable: false,
18
+ get(this: VonaContext) {
19
+ return this.app.bean.passport.current;
20
+ },
21
+ });
22
+ }
23
+
24
+ async configLoaded(_config: any) {}
25
+ }
@@ -0,0 +1,42 @@
1
+ import type { IPayloadData } from 'vona-module-a-jwt';
2
+
3
+ import { BeanBase, createHash, uuidv4 } from 'vona';
4
+ import { Service } from 'vona-module-a-bean';
5
+
6
+ import type { IAuthTokenAdapter } from '../types/authToken.ts';
7
+ import type { IUser } from '../types/user.ts';
8
+
9
+ @Service()
10
+ export class ServiceAuthTokenAdapter extends BeanBase implements IAuthTokenAdapter {
11
+ async create(payloadData: IPayloadData): Promise<IPayloadData> {
12
+ const authIdStr = this._getAuthId(payloadData)?.toString();
13
+ const token = authIdStr === '-1' ? createHash(authIdStr) : uuidv4();
14
+ const payloadDataNew = Object.assign({}, payloadData, { [this.scope.config.payloadData.fields.token]: token });
15
+ await this.scope.service.redisToken.create(payloadDataNew);
16
+ return payloadDataNew;
17
+ }
18
+
19
+ async retrieve(payloadData: IPayloadData): Promise<IPayloadData | undefined> {
20
+ return await this.scope.service.redisToken.retrieve(payloadData);
21
+ }
22
+
23
+ async verify(payloadData: IPayloadData): Promise<boolean> {
24
+ return await this.scope.service.redisToken.verify(payloadData);
25
+ }
26
+
27
+ async refresh(payloadData: IPayloadData): Promise<void> {
28
+ await this.scope.service.redisToken.refresh(payloadData);
29
+ }
30
+
31
+ async remove(payloadData: IPayloadData): Promise<void> {
32
+ await this.scope.service.redisToken.remove(payloadData);
33
+ }
34
+
35
+ async removeAll(user: IUser): Promise<void> {
36
+ await this.scope.service.redisToken.removeAll(user);
37
+ }
38
+
39
+ private _getAuthId(payloadData: IPayloadData) {
40
+ return payloadData[this.scope.config.payloadData.fields.authId];
41
+ }
42
+ }
@@ -0,0 +1,68 @@
1
+ import type { IPayloadData } from 'vona-module-a-jwt';
2
+ import type { IUser } from 'vona-module-a-user';
3
+
4
+ import { BeanBase } from 'vona';
5
+ import { Service } from 'vona-module-a-bean';
6
+
7
+ @Service()
8
+ export class ServiceRedisToken extends BeanBase {
9
+ async verify(payloadData: IPayloadData) {
10
+ const payloadData2 = await this.retrieve(payloadData);
11
+ if (!payloadData2) return false;
12
+ if (this._getToken(payloadData2) !== this._getToken(payloadData)) return false;
13
+ return true;
14
+ }
15
+
16
+ async retrieve(payloadData: IPayloadData): Promise<IPayloadData | undefined> {
17
+ const key = this._getAuthRedisKey(payloadData);
18
+ if (!key) return;
19
+ const token = await this.scope.cacheRedis.authToken.get(key);
20
+ if (!token) return;
21
+ return { ...payloadData, [this.scope.config.payloadData.fields.token]: token };
22
+ }
23
+
24
+ async create(payloadData: IPayloadData) {
25
+ const key = this._getAuthRedisKey(payloadData);
26
+ if (!key || !this._getToken(payloadData)) return this.app.throw(401);
27
+ await this.scope.cacheRedis.authToken.set(this._getToken(payloadData), key);
28
+ }
29
+
30
+ async refresh(payloadData: IPayloadData) {
31
+ const key = this._getAuthRedisKey(payloadData);
32
+ if (!key) return this.app.throw(401);
33
+ await this.scope.cacheRedis.authToken.expire(key);
34
+ }
35
+
36
+ async remove(payloadData: IPayloadData) {
37
+ const key = this._getAuthRedisKey(payloadData);
38
+ if (!key) return;
39
+ await this.scope.cacheRedis.authToken.del(key);
40
+ }
41
+
42
+ async removeAll(user: IUser) {
43
+ const keyPrefix = this._getAuthRedisKeyPrefix(user);
44
+ const keys = await this.scope.cacheRedis.authToken.lookupKeys(keyPrefix, true);
45
+ await this.scope.cacheRedis.authToken.mdel(keys);
46
+ }
47
+
48
+ private _getAuthRedisKey(payloadData: IPayloadData) {
49
+ if (!this.ctx.instance) return;
50
+ return `${this._getUserId(payloadData)}:${this._getAuthId(payloadData)}`;
51
+ }
52
+
53
+ private _getAuthRedisKeyPrefix(user: IUser) {
54
+ return `${user.id}`;
55
+ }
56
+
57
+ private _getToken(payloadData: IPayloadData) {
58
+ return payloadData[this.scope.config.payloadData.fields.token];
59
+ }
60
+
61
+ private _getAuthId(payloadData: IPayloadData) {
62
+ return payloadData[this.scope.config.payloadData.fields.authId];
63
+ }
64
+
65
+ private _getUserId(payloadData: IPayloadData) {
66
+ return payloadData[this.scope.config.payloadData.fields.userId];
67
+ }
68
+ }
@@ -0,0 +1,26 @@
1
+ import type { TableIdentity } from 'table-identity';
2
+ import type { IAuthProviderRecord } from 'vona-module-a-auth';
3
+
4
+ export interface IAuthIdRecord {
5
+ '-1': 'dev';
6
+ '-10000': 'mock';
7
+ }
8
+
9
+ export interface IAuthProvider {
10
+ id: TableIdentity;
11
+ providerName: keyof IAuthProviderRecord;
12
+ clientName: string;
13
+ }
14
+
15
+ export interface IAuth {
16
+ id: TableIdentity;
17
+ profileId?: string;
18
+ authProvider?: IAuthProvider;
19
+ }
20
+
21
+ export type TypeAuthToken = 'recreate' | 'refresh' | 'nochange';
22
+
23
+ export interface ISigninOptions {
24
+ /** default: refresh */
25
+ authToken?: TypeAuthToken;
26
+ }
@@ -0,0 +1,23 @@
1
+ import type { ILocaleRecord } from 'vona';
2
+
3
+ export interface IAuthUserProfilePropSlice {
4
+ value: string;
5
+ }
6
+
7
+ export interface IAuthUserProfileName {
8
+ familyName?: string;
9
+ givenName?: string;
10
+ middleName?: string;
11
+ }
12
+ export interface IAuthUserProfile {
13
+ id: string;
14
+ username?: string;
15
+ displayName?: string;
16
+ name?: IAuthUserProfileName;
17
+ gender?: string; // male/female
18
+ profileUrl?: string;
19
+ emails?: IAuthUserProfilePropSlice[];
20
+ photos?: IAuthUserProfilePropSlice[];
21
+ locale?: keyof ILocaleRecord;
22
+ confirmed?: boolean;
23
+ }
@@ -0,0 +1,12 @@
1
+ import type { IPayloadData } from 'vona-module-a-jwt';
2
+
3
+ import type { IUser } from './user.ts';
4
+
5
+ export interface IAuthTokenAdapter {
6
+ create(payloadData: IPayloadData): Promise<IPayloadData>;
7
+ retrieve(payloadData: IPayloadData): Promise<IPayloadData | undefined>;
8
+ verify(payloadData: IPayloadData): Promise<boolean>;
9
+ refresh(payloadData: IPayloadData): Promise<void>;
10
+ remove(payloadData: IPayloadData): Promise<void>;
11
+ removeAll(user: IUser): Promise<void>;
12
+ }
@@ -0,0 +1,6 @@
1
+ export * from './auth.ts';
2
+ export * from './authProfile.ts';
3
+ export * from './authToken.ts';
4
+ export * from './passport.ts';
5
+ export * from './role.ts';
6
+ export * from './user.ts';
@@ -0,0 +1,29 @@
1
+ import type { IPayloadData } from 'vona-module-a-jwt';
2
+
3
+ import type { IAuth } from './auth.ts';
4
+ import type { IRole } from './role.ts';
5
+ import type { IUser } from './user.ts';
6
+
7
+ export interface IPassport {
8
+ user?: IUser;
9
+ auth?: IAuth;
10
+ roles?: IRole[];
11
+ }
12
+
13
+ export interface IPassportAdapter {
14
+ isAdmin(passport: IPassport | undefined): Promise<boolean>;
15
+ setCurrent(passport: IPassport | undefined): Promise<IPassport | undefined>;
16
+ serialize(passport: IPassport): Promise<IPayloadData>;
17
+ deserialize(payloadData: IPayloadData): Promise<IPassport | undefined>;
18
+ }
19
+
20
+ declare module 'vona' {
21
+ export interface ContextState {
22
+ passport?: IPassport;
23
+ }
24
+
25
+ export interface VonaContext {
26
+ get user(): IUser;
27
+ get passport(): IPassport;
28
+ }
29
+ }
@@ -0,0 +1,19 @@
1
+ import type { TableIdentity } from 'table-identity';
2
+
3
+ export interface IRoleNameRecord {
4
+ admin: never;
5
+ }
6
+
7
+ export interface IRoleIdRecord {}
8
+
9
+ export interface IRole {
10
+ id: TableIdentity;
11
+ name: string;
12
+ }
13
+
14
+ export interface IRoleAdapter {
15
+ findOneByName(name: string): Promise<IRole | undefined>;
16
+ findOne(role: Partial<IRole>): Promise<IRole | undefined>;
17
+ findAllByUserId(userId: TableIdentity): Promise<IRole[] | undefined>;
18
+ addUserId(id: TableIdentity, userId: TableIdentity): Promise<TableIdentity>;
19
+ }
@@ -0,0 +1,35 @@
1
+ import type { TableIdentity } from 'table-identity';
2
+ import type { ILocaleRecord } from 'vona';
3
+
4
+ import type { IAuthUserProfile } from './authProfile.ts';
5
+
6
+ export interface IUserNameRecord {
7
+ admin: never;
8
+ }
9
+
10
+ export interface IUserIdRecord {
11
+ '-1': 'anonymous';
12
+ }
13
+
14
+ export interface IUser {
15
+ id: TableIdentity;
16
+ name: string;
17
+ avatar?: string;
18
+ email?: string;
19
+ mobile?: string;
20
+ activated?: boolean;
21
+ locale?: keyof ILocaleRecord;
22
+ tz?: string;
23
+ anonymous?: boolean;
24
+ }
25
+
26
+ export interface IUserAdapter {
27
+ create(user: Partial<IUser>): Promise<IUser>;
28
+ userOfProfile(profile: IAuthUserProfile): Promise<Partial<IUser>>;
29
+ createAnonymous(): Promise<Partial<IUser>>;
30
+ findOneByName(name: string): Promise<IUser | undefined>;
31
+ findOne(user: Partial<IUser>): Promise<IUser | undefined>;
32
+ update(user: Partial<IUser>): Promise<void>;
33
+ remove(user: Partial<IUser>): Promise<void>;
34
+ setActivated(id: TableIdentity, activated: boolean): Promise<void>;
35
+ }