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.
- package/LICENSE +0 -0
- package/dist/.metadata/index.d.ts +17 -1
- package/dist/.metadata/index.d.ts.map +1 -0
- package/dist/.metadata/locales.d.ts +11 -0
- package/dist/.metadata/locales.d.ts.map +1 -0
- package/dist/.metadata/this.d.ts +1 -0
- package/dist/.metadata/this.d.ts.map +1 -0
- package/dist/bean/bean.passport.d.ts +5 -2
- package/dist/bean/bean.passport.d.ts.map +1 -0
- package/dist/bean/bean.role.d.ts +3 -1
- package/dist/bean/bean.role.d.ts.map +1 -0
- package/dist/bean/bean.user.d.ts +4 -3
- package/dist/bean/bean.user.d.ts.map +1 -0
- package/dist/bean/cacheRedis.authToken.d.ts +1 -0
- package/dist/bean/cacheRedis.authToken.d.ts.map +1 -0
- package/dist/bean/event.activate.d.ts +2 -1
- package/dist/bean/event.activate.d.ts.map +1 -0
- package/dist/bean/event.createAnonymous.d.ts +2 -1
- package/dist/bean/event.createAnonymous.d.ts.map +1 -0
- package/dist/bean/event.register.d.ts +2 -1
- package/dist/bean/event.register.d.ts.map +1 -0
- package/dist/bean/event.signin.d.ts +2 -1
- package/dist/bean/event.signin.d.ts.map +1 -0
- package/dist/bean/event.signout.d.ts +2 -1
- package/dist/bean/event.signout.d.ts.map +1 -0
- package/dist/bean/guard.passport.d.ts +1 -0
- package/dist/bean/guard.passport.d.ts.map +1 -0
- package/dist/bean/guard.roleName.d.ts +2 -1
- package/dist/bean/guard.roleName.d.ts.map +1 -0
- package/dist/bean/guard.userName.d.ts +2 -1
- package/dist/bean/guard.userName.d.ts.map +1 -0
- package/dist/bean/meta.printTip.d.ts +1 -0
- package/dist/bean/meta.printTip.d.ts.map +1 -0
- package/dist/bean/meta.runtime.d.ts +1 -0
- package/dist/bean/meta.runtime.d.ts.map +1 -0
- package/dist/config/config.d.ts +1 -0
- package/dist/config/config.d.ts.map +1 -0
- package/dist/config/errors.d.ts +4 -0
- package/dist/config/errors.d.ts.map +1 -0
- package/dist/config/locale/en-us.d.ts +5 -0
- package/dist/config/locale/en-us.d.ts.map +1 -0
- package/dist/config/locale/zh-cn.d.ts +5 -0
- package/dist/config/locale/zh-cn.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +65 -20
- package/dist/index.js.map +1 -0
- package/dist/lib/auth.d.ts +1 -0
- package/dist/lib/auth.d.ts.map +1 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/passport.d.ts +1 -0
- package/dist/lib/passport.d.ts.map +1 -0
- package/dist/lib/user.d.ts +1 -0
- package/dist/lib/user.d.ts.map +1 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/service/authTokenAdapter.d.ts +2 -1
- package/dist/service/authTokenAdapter.d.ts.map +1 -0
- package/dist/service/redisToken.d.ts +1 -0
- package/dist/service/redisToken.d.ts.map +1 -0
- package/dist/types/auth.d.ts +1 -0
- package/dist/types/auth.d.ts.map +1 -0
- package/dist/types/authProfile.d.ts +1 -0
- package/dist/types/authProfile.d.ts.map +1 -0
- package/dist/types/authToken.d.ts +1 -0
- package/dist/types/authToken.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/passport.d.ts +1 -0
- package/dist/types/passport.d.ts.map +1 -0
- package/dist/types/role.d.ts +2 -0
- package/dist/types/role.d.ts.map +1 -0
- package/dist/types/user.d.ts +1 -0
- package/dist/types/user.d.ts.map +1 -0
- package/package.json +21 -16
- package/src/.metadata/index.ts +382 -0
- package/src/.metadata/locales.ts +13 -0
- package/src/.metadata/this.ts +2 -0
- package/src/bean/bean.passport.ts +249 -0
- package/src/bean/bean.role.ts +39 -0
- package/src/bean/bean.user.ts +88 -0
- package/src/bean/cacheRedis.authToken.ts +10 -0
- package/src/bean/event.activate.ts +10 -0
- package/src/bean/event.createAnonymous.ts +10 -0
- package/src/bean/event.register.ts +14 -0
- package/src/bean/event.signin.ts +10 -0
- package/src/bean/event.signout.ts +10 -0
- package/src/bean/guard.passport.ts +53 -0
- package/src/bean/guard.roleName.ts +35 -0
- package/src/bean/guard.userName.ts +35 -0
- package/src/bean/meta.printTip.ts +18 -0
- package/src/bean/meta.runtime.ts +19 -0
- package/src/config/config.ts +28 -0
- package/src/config/errors.ts +3 -0
- package/src/config/locale/en-us.ts +3 -0
- package/src/config/locale/zh-cn.ts +3 -0
- package/src/index.ts +4 -0
- package/src/lib/auth.ts +7 -0
- package/src/lib/index.ts +3 -0
- package/src/lib/passport.ts +41 -0
- package/src/lib/user.ts +7 -0
- package/src/main.ts +25 -0
- package/src/service/authTokenAdapter.ts +42 -0
- package/src/service/redisToken.ts +68 -0
- package/src/types/auth.ts +26 -0
- package/src/types/authProfile.ts +23 -0
- package/src/types/authToken.ts +12 -0
- package/src/types/index.ts +6 -0
- package/src/types/passport.ts +29 -0
- package/src/types/role.ts +19 -0
- 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;
|
package/src/lib/user.ts
ADDED
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,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
|
+
}
|