vona-module-a-user 5.0.8 → 5.0.10

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 (47) hide show
  1. package/dist/.metadata/index.d.ts +129 -0
  2. package/dist/.metadata/index.js +40 -0
  3. package/dist/.metadata/this.d.ts +2 -0
  4. package/dist/.metadata/this.js +2 -0
  5. package/dist/bean/bean.authInner.d.ts +7 -0
  6. package/dist/bean/bean.authInner.js +27 -0
  7. package/dist/bean/bean.passport.d.ts +33 -0
  8. package/dist/bean/bean.passport.js +198 -0
  9. package/dist/bean/bean.userInner.d.ts +13 -0
  10. package/dist/bean/bean.userInner.js +40 -0
  11. package/dist/bean/event.createUserAnonymous.d.ts +6 -0
  12. package/dist/bean/event.createUserAnonymous.js +13 -0
  13. package/dist/bean/event.signin.d.ts +6 -0
  14. package/dist/bean/event.signin.js +13 -0
  15. package/dist/bean/event.signout.d.ts +6 -0
  16. package/dist/bean/event.signout.js +13 -0
  17. package/dist/bean/guard.admin.d.ts +10 -0
  18. package/dist/bean/guard.admin.js +25 -0
  19. package/dist/bean/guard.passport.d.ts +10 -0
  20. package/dist/bean/guard.passport.js +42 -0
  21. package/dist/bean/meta.printTip.d.ts +5 -0
  22. package/dist/bean/meta.printTip.js +27 -0
  23. package/dist/config/config.d.ts +14 -0
  24. package/dist/config/config.js +13 -0
  25. package/dist/index.d.ts +3 -0
  26. package/dist/index.js +3 -0
  27. package/dist/lib/auth.d.ts +5 -0
  28. package/dist/lib/auth.js +10 -0
  29. package/dist/lib/index.d.ts +3 -0
  30. package/dist/lib/index.js +3 -0
  31. package/dist/lib/passport.d.ts +10 -0
  32. package/dist/lib/passport.js +12 -0
  33. package/dist/lib/user.d.ts +10 -0
  34. package/dist/lib/user.js +22 -0
  35. package/dist/types/auth.d.ts +19 -0
  36. package/dist/types/auth.js +1 -0
  37. package/dist/types/authProfile.d.ts +19 -0
  38. package/dist/types/authProfile.js +1 -0
  39. package/dist/types/authToken.d.ts +10 -0
  40. package/dist/types/authToken.js +1 -0
  41. package/dist/types/index.d.ts +5 -0
  42. package/dist/types/index.js +5 -0
  43. package/dist/types/passport.d.ts +18 -0
  44. package/dist/types/passport.js +1 -0
  45. package/dist/types/user.d.ts +24 -0
  46. package/dist/types/user.js +1 -0
  47. package/package.json +7 -3
@@ -0,0 +1,129 @@
1
+ import type { BeanScopeUtil, TypeModuleConfig } from 'vona';
2
+ import type { EventOn } from 'vona-module-a-event';
3
+ /** bean: end */
4
+ /** bean: begin */
5
+ import type { BeanAuthInner } from '../bean/bean.authInner.ts';
6
+ import type { BeanPassport } from '../bean/bean.passport.ts';
7
+ import type { BeanUserInner } from '../bean/bean.userInner.ts';
8
+ /** event: end */
9
+ /** event: begin */
10
+ import type { EventCreateUserAnonymous } from '../bean/event.createUserAnonymous.ts';
11
+ /** event: end */
12
+ /** event: begin */
13
+ import type { TypeEventCreateUserAnonymousData, TypeEventCreateUserAnonymousResult } from '../bean/event.createUserAnonymous.ts';
14
+ import type { EventSignin } from '../bean/event.signin.ts';
15
+ import type { TypeEventSigninData, TypeEventSigninResult } from '../bean/event.signin.ts';
16
+ import type { EventSignout } from '../bean/event.signout.ts';
17
+ import type { TypeEventSignoutData, TypeEventSignoutResult } from '../bean/event.signout.ts';
18
+ /** guard: end */
19
+ /** bean: begin */
20
+ import type { IGuardOptionsAdmin } from '../bean/guard.admin.ts';
21
+ import type { IGuardOptionsPassport } from '../bean/guard.passport.ts';
22
+ import type { config } from '../config/config.ts';
23
+ /** config: end */
24
+ /** scope: begin */
25
+ import { BeanScopeBase } from 'vona';
26
+ import 'vona';
27
+ import 'vona';
28
+ import 'vona';
29
+ import 'vona';
30
+ import 'vona';
31
+ import 'vona';
32
+ export * from '../bean/bean.authInner.ts';
33
+ export * from '../bean/bean.passport.ts';
34
+ declare module 'vona-module-a-aspect' {
35
+ interface IGuardRecordGlobal {
36
+ 'a-user:passport': IGuardOptionsPassport;
37
+ }
38
+ interface IGuardRecordLocal {
39
+ 'a-user:admin': IGuardOptionsAdmin;
40
+ }
41
+ }
42
+ declare module 'vona-module-a-user' {
43
+ interface GuardAdmin {
44
+ }
45
+ interface GuardPassport {
46
+ }
47
+ }
48
+ export * from '../bean/bean.userInner.ts';
49
+ /** bean: end */
50
+ /** event: begin */
51
+ export * from '../bean/event.createUserAnonymous.ts';
52
+ export * from '../bean/event.signin.ts';
53
+ declare module 'vona' {
54
+ }
55
+ declare module 'vona-module-a-user' {
56
+ interface BeanAuthInner {
57
+ }
58
+ interface BeanPassport {
59
+ }
60
+ interface BeanUserInner {
61
+ }
62
+ }
63
+ declare module 'vona' {
64
+ interface IBeanRecordGlobal {
65
+ authInner: BeanAuthInner;
66
+ passport: BeanPassport;
67
+ userInner: BeanUserInner;
68
+ }
69
+ }
70
+ export * from '../bean/event.signout.ts';
71
+ /** guard: begin */
72
+ export * from '../bean/guard.admin.ts';
73
+ export * from '../bean/guard.passport.ts';
74
+ declare module 'vona' {
75
+ }
76
+ declare module 'vona-module-a-user' {
77
+ interface EventCreateUserAnonymous {
78
+ }
79
+ interface EventSignin {
80
+ }
81
+ interface EventSignout {
82
+ }
83
+ }
84
+ export interface IModuleEvent {
85
+ createUserAnonymous: EventCreateUserAnonymous;
86
+ signin: EventSignin;
87
+ signout: EventSignout;
88
+ }
89
+ declare module 'vona-module-a-event' {
90
+ interface IEventRecord {
91
+ 'a-user:createUserAnonymous': EventOn<TypeEventCreateUserAnonymousData, TypeEventCreateUserAnonymousResult>;
92
+ 'a-user:signin': EventOn<TypeEventSigninData, TypeEventSigninResult>;
93
+ 'a-user:signout': EventOn<TypeEventSignoutData, TypeEventSignoutResult>;
94
+ }
95
+ }
96
+ /** event: end */
97
+ /** meta: begin */
98
+ export * from '../bean/meta.printTip.ts';
99
+ declare module 'vona' {
100
+ interface IMetaRecord {
101
+ 'a-user:printTip': never;
102
+ }
103
+ }
104
+ declare module 'vona-module-a-user' {
105
+ interface MetaPrintTip {
106
+ }
107
+ }
108
+ /** meta: end */
109
+ /** config: begin */
110
+ export * from '../config/config.ts';
111
+ export declare class ScopeModuleAUser extends BeanScopeBase {
112
+ }
113
+ export interface ScopeModuleAUser {
114
+ util: BeanScopeUtil;
115
+ config: TypeModuleConfig<typeof config>;
116
+ event: IModuleEvent;
117
+ }
118
+ declare module 'vona' {
119
+ interface IBeanScopeRecord {
120
+ 'a-user': ScopeModuleAUser;
121
+ }
122
+ interface IBeanScopeContainer {
123
+ user: ScopeModuleAUser;
124
+ }
125
+ interface IBeanScopeConfig {
126
+ 'a-user': ReturnType<typeof config>;
127
+ }
128
+ }
129
+ /** scope: end */
@@ -0,0 +1,40 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ /** config: end */
8
+ /** scope: begin */
9
+ import { BeanScopeBase } from 'vona';
10
+ import { Scope } from 'vona-module-a-bean';
11
+ import 'vona';
12
+ import 'vona';
13
+ import 'vona';
14
+ import 'vona';
15
+ import 'vona';
16
+ import 'vona';
17
+ export * from "../bean/bean.authInner.js";
18
+ export * from "../bean/bean.passport.js";
19
+ export * from "../bean/bean.userInner.js";
20
+ /** bean: end */
21
+ /** event: begin */
22
+ export * from "../bean/event.createUserAnonymous.js";
23
+ export * from "../bean/event.signin.js";
24
+ export * from "../bean/event.signout.js";
25
+ /** guard: begin */
26
+ export * from "../bean/guard.admin.js";
27
+ export * from "../bean/guard.passport.js";
28
+ /** event: end */
29
+ /** meta: begin */
30
+ export * from "../bean/meta.printTip.js";
31
+ /** meta: end */
32
+ /** config: begin */
33
+ export * from "../config/config.js";
34
+ let ScopeModuleAUser = class ScopeModuleAUser extends BeanScopeBase {
35
+ };
36
+ ScopeModuleAUser = __decorate([
37
+ Scope()
38
+ ], ScopeModuleAUser);
39
+ export { ScopeModuleAUser };
40
+ /** scope: end */
@@ -0,0 +1,2 @@
1
+ export declare const __ThisModule__ = "a-user";
2
+ export { ScopeModuleAUser as ScopeModule } from './index.ts';
@@ -0,0 +1,2 @@
1
+ export const __ThisModule__ = 'a-user';
2
+ export { ScopeModuleAUser as ScopeModule } from "./index.js";
@@ -0,0 +1,7 @@
1
+ import type { IAuthBase } from '../types/auth.ts';
2
+ import { BeanBase } from 'vona';
3
+ export declare class BeanAuthInner extends BeanBase {
4
+ private _authInnerAdapter;
5
+ private get authInnerAdapter();
6
+ get(auth: Partial<IAuthBase>): Promise<IAuthBase | undefined>;
7
+ }
@@ -0,0 +1,27 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { BeanBase, beanFullNameFromOnionName } from 'vona';
8
+ import { Bean } from 'vona-module-a-bean';
9
+ let BeanAuthInner = class BeanAuthInner extends BeanBase {
10
+ _authInnerAdapter;
11
+ get authInnerAdapter() {
12
+ if (!this._authInnerAdapter) {
13
+ const beanFullName = beanFullNameFromOnionName(this.scope.config.adapter.authInner, 'service');
14
+ this._authInnerAdapter = this.bean._getBean(beanFullName);
15
+ }
16
+ return this._authInnerAdapter;
17
+ }
18
+ async get(auth) {
19
+ if (String(auth.id).charAt(0) === '-')
20
+ return auth;
21
+ return await this.authInnerAdapter.get(auth);
22
+ }
23
+ };
24
+ BeanAuthInner = __decorate([
25
+ Bean()
26
+ ], BeanAuthInner);
27
+ export { BeanAuthInner };
@@ -0,0 +1,33 @@
1
+ import type { IJwtClientRecord, IJwtSignOptions, IJwtToken, IPayloadDataBase } from 'vona-module-a-jwt';
2
+ import type { IAuthBase, IAuthIdRecord, ISigninOptions } from '../types/auth.ts';
3
+ import type { IPassportBase } from '../types/passport.ts';
4
+ import type { IUserBase } from '../types/user.ts';
5
+ import { BeanBase } from 'vona';
6
+ export declare class BeanPassport extends BeanBase {
7
+ private _authTokenAdapter;
8
+ private _passportAdapter;
9
+ private _mockCounter;
10
+ private get authTokenAdapter();
11
+ private get passportAdapter();
12
+ get isAuthenticated(): boolean;
13
+ isAdmin(): Promise<boolean>;
14
+ setCurrent(passport: IPassportBase | undefined): Promise<void>;
15
+ getCurrent(): IPassportBase | undefined;
16
+ getCurrentUser(): IUserBase | undefined;
17
+ getCurrentAuth(): IAuthBase | undefined;
18
+ signin(passport: IPassportBase, options?: ISigninOptions): Promise<IJwtToken>;
19
+ signout(): Promise<void>;
20
+ signinSystem<K extends keyof IAuthIdRecord>(authName: IAuthIdRecord[K], authId: K, name?: string, options?: ISigninOptions): Promise<IJwtToken>;
21
+ signinMock(name?: string, options?: ISigninOptions): Promise<IJwtToken>;
22
+ signinWithAnonymous(): Promise<void>;
23
+ createUserAnonymous(): Promise<IUserBase>;
24
+ kickOut(user: IUserBase): Promise<void>;
25
+ checkAuthToken(accessToken?: string, clientName?: keyof IJwtClientRecord): Promise<IPayloadDataBase | undefined>;
26
+ refreshAuthToken(refreshToken: string): Promise<IJwtToken>;
27
+ createTempAuthToken(options?: IJwtSignOptions): Promise<string>;
28
+ createOauthAuthToken(options?: IJwtSignOptions): Promise<string>;
29
+ createOauthCode(accessToken: string, options?: IJwtSignOptions): Promise<string>;
30
+ createAuthTokenFromOauthCode(code: string): Promise<IJwtToken>;
31
+ private _passportSerialize;
32
+ private _handlePayloadData;
33
+ }
@@ -0,0 +1,198 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { catchError } from '@cabloy/utils';
8
+ import { BeanBase, beanFullNameFromOnionName } from 'vona';
9
+ import { Bean } from 'vona-module-a-bean';
10
+ import { $getAuthIdSystem } from "../lib/auth.js";
11
+ import { $getUserAnonymous } from "../lib/user.js";
12
+ let BeanPassport = class BeanPassport extends BeanBase {
13
+ _authTokenAdapter;
14
+ _passportAdapter;
15
+ _mockCounter = 0;
16
+ get authTokenAdapter() {
17
+ if (!this._authTokenAdapter) {
18
+ const beanFullName = beanFullNameFromOnionName(this.scope.config.adapter.authToken, 'service');
19
+ this._authTokenAdapter = this.bean._getBean(beanFullName);
20
+ }
21
+ return this._authTokenAdapter;
22
+ }
23
+ get passportAdapter() {
24
+ if (!this._passportAdapter) {
25
+ const beanFullName = beanFullNameFromOnionName(this.scope.config.adapter.passport, 'service');
26
+ this._passportAdapter = this.bean._getBean(beanFullName);
27
+ }
28
+ return this._passportAdapter;
29
+ }
30
+ get isAuthenticated() {
31
+ const user = this.getCurrentUser();
32
+ return !!user && !$getUserAnonymous(user);
33
+ }
34
+ async isAdmin() {
35
+ const user = this.getCurrentUser();
36
+ return !!user && this.isAuthenticated && await this.passportAdapter.isAdmin(user);
37
+ }
38
+ async setCurrent(passport) {
39
+ this.ctx.state.passport = await this.passportAdapter.setCurrent(passport);
40
+ }
41
+ getCurrent() {
42
+ return this.ctx.state.passport;
43
+ }
44
+ getCurrentUser() {
45
+ return this.ctx.state.passport?.user;
46
+ }
47
+ getCurrentAuth() {
48
+ return this.ctx.state.passport?.auth;
49
+ }
50
+ async signin(passport, options) {
51
+ // current
52
+ await this.setCurrent(passport);
53
+ // event
54
+ await this.scope.event.signin.emit(passport);
55
+ // serialize: payloadData for client certificate
56
+ const payloadData = await this._passportSerialize(passport, options);
57
+ // jwt token
58
+ return await this.bean.jwt.create(payloadData, { dev: passport.auth?.id.toString() === '-1' });
59
+ }
60
+ async signout() {
61
+ // current
62
+ const passport = this.getCurrent();
63
+ if (!passport)
64
+ return;
65
+ // removeAuthToken
66
+ const payloadData = await this.passportAdapter.serialize(passport);
67
+ await this.authTokenAdapter.remove(payloadData);
68
+ // event
69
+ await this.scope.event.signout.emit(passport);
70
+ // ok
71
+ await this.setCurrent(undefined);
72
+ }
73
+ async signinSystem(authName, authId, name, options) {
74
+ const user = await this.bean.userInner.getByName(name ?? 'admin');
75
+ if (!user)
76
+ return this.app.throw(401);
77
+ const auth = { id: $getAuthIdSystem(authName, authId) };
78
+ const passport = { user, auth };
79
+ return await this.signin(passport, options);
80
+ }
81
+ async signinMock(name, options) {
82
+ return await this.signinSystem('mock', (-10000 - ++this._mockCounter), name, options);
83
+ }
84
+ async signinWithAnonymous() {
85
+ const userAnonymous = await this.createUserAnonymous();
86
+ const passport = { user: userAnonymous, auth: undefined };
87
+ await this.setCurrent(passport);
88
+ }
89
+ async createUserAnonymous() {
90
+ const userAnonymous = await this.bean.userInner.createAnonymous();
91
+ // event
92
+ await this.scope.event.createUserAnonymous.emit(userAnonymous);
93
+ // ok
94
+ return userAnonymous;
95
+ }
96
+ async kickOut(user) {
97
+ await this.authTokenAdapter.removeAll(user);
98
+ }
99
+ async checkAuthToken(accessToken, clientName) {
100
+ clientName = clientName ?? 'access';
101
+ const [payloadData, err] = await catchError(() => {
102
+ return this.bean.jwt.get(clientName).verify(accessToken);
103
+ });
104
+ if (err) {
105
+ if (['access', 'refresh'].includes(clientName)) {
106
+ err.code = 401;
107
+ }
108
+ throw err;
109
+ }
110
+ if (!payloadData)
111
+ return; // no jwt token
112
+ const verified = await this.authTokenAdapter.verify(payloadData);
113
+ if (!verified)
114
+ return this.app.throw(401);
115
+ const passport = await this.passportAdapter.deserialize(payloadData);
116
+ if (!passport)
117
+ return this.app.throw(401);
118
+ await this.setCurrent(passport);
119
+ return payloadData;
120
+ }
121
+ async refreshAuthToken(refreshToken) {
122
+ // checkAuthToken by code
123
+ let payloadData = await this.checkAuthToken(refreshToken, 'refresh');
124
+ if (!payloadData)
125
+ return this.app.throw(401);
126
+ // refreshAuthToken
127
+ const configRefreshAuthToken = this.scope.config.passport.refreshAuthToken;
128
+ payloadData = await this._handlePayloadData(payloadData, { authToken: configRefreshAuthToken });
129
+ // jwt token
130
+ return await this.bean.jwt.create(payloadData);
131
+ }
132
+ // only created by accessToken
133
+ async createTempAuthToken(options) {
134
+ // current
135
+ const passport = this.getCurrent();
136
+ if (!passport)
137
+ return this.app.throw(401);
138
+ // payloadData
139
+ const payloadData = await this._passportSerialize(passport, { authToken: 'nochange' });
140
+ // jwt token
141
+ return await this.bean.jwt.createTemp(payloadData, options);
142
+ }
143
+ async createOauthAuthToken(options) {
144
+ // current
145
+ const passport = this.getCurrent();
146
+ if (!passport)
147
+ return this.app.throw(401);
148
+ // payloadData
149
+ const payloadData = await this._passportSerialize(passport, { authToken: 'nochange' });
150
+ // jwt token
151
+ return await this.bean.jwt.createOauth(payloadData, options);
152
+ }
153
+ async createOauthCode(accessToken, options) {
154
+ // payloadData
155
+ const payloadData = await this.bean.jwt.get('access').verify(accessToken);
156
+ if (!payloadData)
157
+ return this.app.throw(401);
158
+ // create
159
+ return await this.bean.jwt.createOauthCode(payloadData, options);
160
+ }
161
+ async createAuthTokenFromOauthCode(code) {
162
+ // checkAuthToken by code
163
+ const payloadData = await this.checkAuthToken(code, 'code');
164
+ if (!payloadData)
165
+ return this.app.throw(401);
166
+ // jwt token
167
+ return await this.bean.jwt.create(payloadData);
168
+ }
169
+ async _passportSerialize(passport, options) {
170
+ // serialize
171
+ const payloadData = await this.passportAdapter.serialize(passport);
172
+ return await this._handlePayloadData(payloadData, options);
173
+ }
174
+ async _handlePayloadData(payloadData, options) {
175
+ // auth token
176
+ const authToken = options?.authToken ?? 'refresh';
177
+ if (authToken === 'recreate') {
178
+ return await this.authTokenAdapter.create(payloadData);
179
+ }
180
+ else {
181
+ const payloadData2 = await this.authTokenAdapter.retrieve(payloadData);
182
+ if (!payloadData2) {
183
+ return await this.authTokenAdapter.create(payloadData);
184
+ }
185
+ if (authToken === 'refresh') {
186
+ await this.authTokenAdapter.refresh(payloadData2);
187
+ }
188
+ else if (authToken === 'nochange') {
189
+ // do nothing
190
+ }
191
+ return payloadData2;
192
+ }
193
+ }
194
+ };
195
+ BeanPassport = __decorate([
196
+ Bean()
197
+ ], BeanPassport);
198
+ export { BeanPassport };
@@ -0,0 +1,13 @@
1
+ import type { IAuthUserProfile } from '../types/authProfile.ts';
2
+ import type { IUserBase } from '../types/user.ts';
3
+ import { BeanBase } from 'vona';
4
+ export declare class BeanUserInner extends BeanBase {
5
+ private _userInnerAdapter;
6
+ private get userInnerAdapter();
7
+ createByProfile(profile: IAuthUserProfile): Promise<IUserBase>;
8
+ createAnonymous(): Promise<IUserBase>;
9
+ getByName(name: string): Promise<IUserBase | undefined>;
10
+ get(user: Partial<IUserBase>): Promise<IUserBase | undefined>;
11
+ update(user: Partial<IUserBase>): Promise<void>;
12
+ delete(user: Partial<IUserBase>): Promise<void>;
13
+ }
@@ -0,0 +1,40 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { BeanBase, beanFullNameFromOnionName } from 'vona';
8
+ import { Bean } from 'vona-module-a-bean';
9
+ let BeanUserInner = class BeanUserInner extends BeanBase {
10
+ _userInnerAdapter;
11
+ get userInnerAdapter() {
12
+ if (!this._userInnerAdapter) {
13
+ const beanFullName = beanFullNameFromOnionName(this.scope.config.adapter.userInner, 'service');
14
+ this._userInnerAdapter = this.bean._getBean(beanFullName);
15
+ }
16
+ return this._userInnerAdapter;
17
+ }
18
+ createByProfile(profile) {
19
+ return this.userInnerAdapter.createByProfile(profile);
20
+ }
21
+ createAnonymous() {
22
+ return this.userInnerAdapter.createAnonymous();
23
+ }
24
+ getByName(name) {
25
+ return this.userInnerAdapter.getByName(name);
26
+ }
27
+ get(user) {
28
+ return this.userInnerAdapter.get(user);
29
+ }
30
+ update(user) {
31
+ return this.userInnerAdapter.update(user);
32
+ }
33
+ delete(user) {
34
+ return this.userInnerAdapter.delete(user);
35
+ }
36
+ };
37
+ BeanUserInner = __decorate([
38
+ Bean()
39
+ ], BeanUserInner);
40
+ export { BeanUserInner };
@@ -0,0 +1,6 @@
1
+ import type { IUserBase } from '../types/user.ts';
2
+ import { BeanEventBase } from 'vona-module-a-event';
3
+ export type TypeEventCreateUserAnonymousData = IUserBase;
4
+ export type TypeEventCreateUserAnonymousResult = void;
5
+ export declare class EventCreateUserAnonymous extends BeanEventBase<TypeEventCreateUserAnonymousData, TypeEventCreateUserAnonymousResult> {
6
+ }
@@ -0,0 +1,13 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { BeanEventBase, Event } from 'vona-module-a-event';
8
+ let EventCreateUserAnonymous = class EventCreateUserAnonymous extends BeanEventBase {
9
+ };
10
+ EventCreateUserAnonymous = __decorate([
11
+ Event()
12
+ ], EventCreateUserAnonymous);
13
+ export { EventCreateUserAnonymous };
@@ -0,0 +1,6 @@
1
+ import type { IPassportBase } from '../types/passport.ts';
2
+ import { BeanEventBase } from 'vona-module-a-event';
3
+ export type TypeEventSigninData = IPassportBase;
4
+ export type TypeEventSigninResult = void;
5
+ export declare class EventSignin extends BeanEventBase<TypeEventSigninData, TypeEventSigninResult> {
6
+ }
@@ -0,0 +1,13 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { BeanEventBase, Event } from 'vona-module-a-event';
8
+ let EventSignin = class EventSignin extends BeanEventBase {
9
+ };
10
+ EventSignin = __decorate([
11
+ Event()
12
+ ], EventSignin);
13
+ export { EventSignin };
@@ -0,0 +1,6 @@
1
+ import type { IPassportBase } from '../types/passport.ts';
2
+ import { BeanEventBase } from 'vona-module-a-event';
3
+ export type TypeEventSignoutData = IPassportBase;
4
+ export type TypeEventSignoutResult = void;
5
+ export declare class EventSignout extends BeanEventBase<TypeEventSignoutData, TypeEventSignoutResult> {
6
+ }
@@ -0,0 +1,13 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { BeanEventBase, Event } from 'vona-module-a-event';
8
+ let EventSignout = class EventSignout extends BeanEventBase {
9
+ };
10
+ EventSignout = __decorate([
11
+ Event()
12
+ ], EventSignout);
13
+ export { EventSignout };
@@ -0,0 +1,10 @@
1
+ import type { Next } from 'vona';
2
+ import type { IDecoratorGuardOptions, IGuardExecute } from 'vona-module-a-aspect';
3
+ import { BeanBase } from 'vona';
4
+ export interface IGuardOptionsAdmin extends IDecoratorGuardOptions {
5
+ admin: boolean;
6
+ passWhenAdmin: boolean;
7
+ }
8
+ export declare class GuardAdmin extends BeanBase implements IGuardExecute {
9
+ execute(options: IGuardOptionsAdmin, next: Next): Promise<boolean>;
10
+ }
@@ -0,0 +1,25 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { BeanBase } from 'vona';
8
+ import { Guard } from 'vona-module-a-aspect';
9
+ let GuardAdmin = class GuardAdmin extends BeanBase {
10
+ async execute(options, next) {
11
+ if (options.admin) {
12
+ const isAdmin = await this.bean.passport.isAdmin();
13
+ if (!isAdmin)
14
+ return this.app.throw(403);
15
+ if (options.passWhenAdmin)
16
+ return true;
17
+ }
18
+ // next
19
+ return next();
20
+ }
21
+ };
22
+ GuardAdmin = __decorate([
23
+ Guard({ admin: true, passWhenAdmin: true })
24
+ ], GuardAdmin);
25
+ export { GuardAdmin };
@@ -0,0 +1,10 @@
1
+ import type { Next } from 'vona';
2
+ import type { IDecoratorGuardOptionsGlobal, IGuardExecute } from 'vona-module-a-aspect';
3
+ import { BeanBase } from 'vona';
4
+ export interface IGuardOptionsPassport extends IDecoratorGuardOptionsGlobal {
5
+ public: boolean;
6
+ checkAuthToken: boolean;
7
+ }
8
+ export declare class GuardPassport extends BeanBase implements IGuardExecute {
9
+ execute(options: IGuardOptionsPassport, next: Next): Promise<boolean>;
10
+ }
@@ -0,0 +1,42 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { catchError } from '@cabloy/utils';
8
+ import { BeanBase } from 'vona';
9
+ import { Guard } from 'vona-module-a-aspect';
10
+ let GuardPassport = class GuardPassport extends BeanBase {
11
+ async execute(options, next) {
12
+ // auth token
13
+ if (!this.bean.passport.getCurrent()) {
14
+ if (options.checkAuthToken) {
15
+ // will return undefined if no accessToken, so not check options.public
16
+ const [_, err] = await catchError(() => {
17
+ return this.bean.passport.checkAuthToken();
18
+ });
19
+ if (err && !options.public)
20
+ throw err;
21
+ }
22
+ }
23
+ // check current
24
+ if (!this.bean.passport.getCurrent()) {
25
+ await this.bean.passport.signinWithAnonymous();
26
+ }
27
+ if (!options.public && !this.bean.passport.isAuthenticated) {
28
+ // return false;
29
+ // 401 for this guard,403 for the next guards
30
+ return this.app.throw(401);
31
+ }
32
+ // check innerAccess
33
+ if (this.ctx.innerAccess)
34
+ return true;
35
+ // next
36
+ return next();
37
+ }
38
+ };
39
+ GuardPassport = __decorate([
40
+ Guard({ global: true, public: false, checkAuthToken: true })
41
+ ], GuardPassport);
42
+ export { GuardPassport };
@@ -0,0 +1,5 @@
1
+ import type { IMetaPrintTipExecute, TypeMetaPrintTipResult } from 'vona-module-a-printtip';
2
+ import { BeanBase } from 'vona';
3
+ export declare class MetaPrintTip extends BeanBase implements IMetaPrintTipExecute {
4
+ execute(): Promise<TypeMetaPrintTipResult>;
5
+ }
@@ -0,0 +1,27 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { BeanBase } from 'vona';
8
+ import { Meta } from 'vona-module-a-meta';
9
+ let MetaPrintTip = class MetaPrintTip extends BeanBase {
10
+ async execute() {
11
+ if (!this.app.meta.isLocal)
12
+ return;
13
+ // signin
14
+ const jwt = await this.app.bean.executor.newCtx(async () => {
15
+ return await this.bean.passport.signinSystem('dev', '-1');
16
+ }, { instanceName: '' });
17
+ const accessToken = jwt.accessToken;
18
+ return {
19
+ title: 'access token [admin] [dev]',
20
+ path: `Bearer ${accessToken}`,
21
+ };
22
+ }
23
+ };
24
+ MetaPrintTip = __decorate([
25
+ Meta()
26
+ ], MetaPrintTip);
27
+ export { MetaPrintTip };
@@ -0,0 +1,14 @@
1
+ import type { VonaApplication } from 'vona';
2
+ import type { IServiceRecord } from 'vona-module-a-web';
3
+ import type { TypeAuthToken } from '../types/auth.ts';
4
+ export declare function config(_app: VonaApplication): {
5
+ passport: {
6
+ refreshAuthToken: TypeAuthToken;
7
+ };
8
+ adapter: {
9
+ authToken: keyof IServiceRecord;
10
+ passport: keyof IServiceRecord;
11
+ userInner: keyof IServiceRecord;
12
+ authInner: keyof IServiceRecord;
13
+ };
14
+ };
@@ -0,0 +1,13 @@
1
+ export function config(_app) {
2
+ return {
3
+ passport: {
4
+ refreshAuthToken: 'recreate',
5
+ },
6
+ adapter: {
7
+ authToken: 'home-user:authTokenAdapter',
8
+ passport: 'home-user:passportAdapter',
9
+ userInner: 'home-user:userInnerAdapter',
10
+ authInner: 'home-user:authInnerAdapter',
11
+ },
12
+ };
13
+ }
@@ -0,0 +1,3 @@
1
+ export * from './.metadata/index.ts';
2
+ export * from './lib/index.ts';
3
+ export * from './types/index.ts';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./.metadata/index.js";
2
+ export * from "./lib/index.js";
3
+ export * from "./types/index.js";
@@ -0,0 +1,5 @@
1
+ import type { TableIdentity } from 'vona-module-a-database';
2
+ import type { IAuthAdapter, IAuthBase, IAuthIdRecord } from '../types/auth.ts';
3
+ export declare function setAuthAdapter(authAdapter: IAuthAdapter): void;
4
+ export declare function $getAuthId(user: IAuthBase): TableIdentity;
5
+ export declare function $getAuthIdSystem<K extends keyof IAuthIdRecord>(_authName: IAuthIdRecord[K], authId: K): TableIdentity;
@@ -0,0 +1,10 @@
1
+ let __authAdapter;
2
+ export function setAuthAdapter(authAdapter) {
3
+ __authAdapter = authAdapter;
4
+ }
5
+ export function $getAuthId(user) {
6
+ return __authAdapter.getAuthId(user);
7
+ }
8
+ export function $getAuthIdSystem(_authName, authId) {
9
+ return authId;
10
+ }
@@ -0,0 +1,3 @@
1
+ export * from './auth.ts';
2
+ export * from './passport.ts';
3
+ export * from './user.ts';
@@ -0,0 +1,3 @@
1
+ export * from "./auth.js";
2
+ export * from "./passport.js";
3
+ export * from "./user.js";
@@ -0,0 +1,10 @@
1
+ import type { TypeUseOnionOmitOptionsGlobal } from 'vona-module-a-onion';
2
+ import type { IGuardOptionsAdmin } from '../bean/guard.admin.ts';
3
+ import type { IGuardOptionsPassport } from '../bean/guard.passport.ts';
4
+ declare function Public(options?: Partial<TypeUseOnionOmitOptionsGlobal<IGuardOptionsPassport>>): ClassDecorator & MethodDecorator;
5
+ declare function Admin(options?: Partial<IGuardOptionsAdmin>): ClassDecorator & MethodDecorator;
6
+ export declare const Passport: {
7
+ admin: typeof Admin;
8
+ public: typeof Public;
9
+ };
10
+ export {};
@@ -0,0 +1,12 @@
1
+ import { Aspect } from 'vona-module-a-aspect';
2
+ function Public(options) {
3
+ const _public = options?.public === undefined ? true : options.public;
4
+ return Aspect.guardGlobal('a-user:passport', { public: _public });
5
+ }
6
+ function Admin(options) {
7
+ return Aspect.guard('a-user:admin', options);
8
+ }
9
+ export const Passport = {
10
+ admin: Admin,
11
+ public: Public,
12
+ };
@@ -0,0 +1,10 @@
1
+ import type { ILocaleInfos } from 'vona';
2
+ import type { TableIdentity } from 'vona-module-a-database';
3
+ import type { IUserAdapter, IUserBase, IUserIdRecord } from '../types/user.ts';
4
+ export declare function setUserAdapter(userAdapter: IUserAdapter): void;
5
+ export declare function $getUserId(user: IUserBase): TableIdentity;
6
+ export declare function $getUserName(user: IUserBase): string;
7
+ export declare function $getUserAvatar(user: IUserBase): string | undefined;
8
+ export declare function $getUserLocale(user: IUserBase): keyof ILocaleInfos | undefined;
9
+ export declare function $getUserAnonymous(user: IUserBase): boolean;
10
+ export declare function $getUserIdSystem<K extends keyof IUserIdRecord>(_userName: IUserIdRecord[K], userId: K): TableIdentity;
@@ -0,0 +1,22 @@
1
+ let __userAdapter;
2
+ export function setUserAdapter(userAdapter) {
3
+ __userAdapter = userAdapter;
4
+ }
5
+ export function $getUserId(user) {
6
+ return __userAdapter.getUserId(user);
7
+ }
8
+ export function $getUserName(user) {
9
+ return __userAdapter.getUserName(user);
10
+ }
11
+ export function $getUserAvatar(user) {
12
+ return __userAdapter.getUserAvatar(user);
13
+ }
14
+ export function $getUserLocale(user) {
15
+ return __userAdapter.getUserLocale(user);
16
+ }
17
+ export function $getUserAnonymous(user) {
18
+ return __userAdapter.getUserAnonymous(user);
19
+ }
20
+ export function $getUserIdSystem(_userName, userId) {
21
+ return userId;
22
+ }
@@ -0,0 +1,19 @@
1
+ import type { TableIdentity } from 'vona-module-a-database';
2
+ export interface IAuthIdRecord {
3
+ '-1': 'dev';
4
+ '-10000': 'mock';
5
+ }
6
+ export interface IAuthBase {
7
+ id: TableIdentity;
8
+ }
9
+ export interface IAuthAdapter {
10
+ getAuthId(user: IAuthBase): TableIdentity;
11
+ }
12
+ export type TypeAuthToken = 'recreate' | 'refresh' | 'nochange';
13
+ export interface ISigninOptions {
14
+ /** default: refresh */
15
+ authToken?: TypeAuthToken;
16
+ }
17
+ export interface IAuthInnerAdapter {
18
+ get(auth: Partial<IAuthBase>): Promise<IAuthBase | undefined>;
19
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ export interface IAuthUserProfilePropSlice {
2
+ value: string;
3
+ }
4
+ export interface IAuthUserProfileName {
5
+ familyName?: string;
6
+ givenName?: string;
7
+ middleName?: string;
8
+ }
9
+ export interface IAuthUserProfile {
10
+ id: string;
11
+ username?: string;
12
+ displayName?: string;
13
+ name?: IAuthUserProfileName;
14
+ gender?: string;
15
+ profileUrl?: string;
16
+ emails?: IAuthUserProfilePropSlice[];
17
+ photos?: IAuthUserProfilePropSlice[];
18
+ confirmed?: boolean;
19
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { IPayloadDataBase } from 'vona-module-a-jwt';
2
+ import type { IUserBase } from './user.ts';
3
+ export interface IAuthTokenAdapter {
4
+ create(payloadData: IPayloadDataBase): Promise<IPayloadDataBase>;
5
+ retrieve(payloadData: IPayloadDataBase): Promise<IPayloadDataBase | undefined>;
6
+ verify(payloadData: IPayloadDataBase): Promise<boolean>;
7
+ refresh(payloadData: IPayloadDataBase): Promise<void>;
8
+ remove(payloadData: IPayloadDataBase): Promise<void>;
9
+ removeAll(user: IUserBase): Promise<void>;
10
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ export * from './auth.ts';
2
+ export * from './authProfile.ts';
3
+ export * from './authToken.ts';
4
+ export * from './passport.ts';
5
+ export * from './user.ts';
@@ -0,0 +1,5 @@
1
+ export * from "./auth.js";
2
+ export * from "./authProfile.js";
3
+ export * from "./authToken.js";
4
+ export * from "./passport.js";
5
+ export * from "./user.js";
@@ -0,0 +1,18 @@
1
+ import type { IPayloadDataBase } from 'vona-module-a-jwt';
2
+ import type { IAuthBase } from './auth.ts';
3
+ import type { IUserBase } from './user.ts';
4
+ export interface IPassportBase {
5
+ user?: IUserBase;
6
+ auth?: IAuthBase;
7
+ }
8
+ export interface IPassportAdapter {
9
+ isAdmin(user: IUserBase): Promise<boolean>;
10
+ setCurrent(passport: IPassportBase | undefined): Promise<IPassportBase | undefined>;
11
+ serialize(passport: IPassportBase): Promise<IPayloadDataBase>;
12
+ deserialize(payloadData: IPayloadDataBase): Promise<IPassportBase | undefined>;
13
+ }
14
+ declare module 'vona' {
15
+ interface ContextState {
16
+ passport?: IPassportBase;
17
+ }
18
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
1
+ import type { ILocaleInfos } from 'vona';
2
+ import type { TableIdentity } from 'vona-module-a-database';
3
+ import type { IAuthUserProfile } from './authProfile.ts';
4
+ export interface IUserIdRecord {
5
+ '-1': 'anonymous';
6
+ }
7
+ export interface IUserBase {
8
+ id: TableIdentity;
9
+ }
10
+ export interface IUserAdapter {
11
+ getUserId(user: IUserBase): TableIdentity;
12
+ getUserName(user: IUserBase): string;
13
+ getUserAvatar(user: IUserBase): string | undefined;
14
+ getUserLocale(user: IUserBase): keyof ILocaleInfos | undefined;
15
+ getUserAnonymous(user: IUserBase): boolean;
16
+ }
17
+ export interface IUserInnerAdapter {
18
+ createByProfile(profile: IAuthUserProfile): Promise<IUserBase>;
19
+ createAnonymous(): Promise<IUserBase>;
20
+ getByName(name: string): Promise<IUserBase | undefined>;
21
+ get(user: Partial<IUserBase>): Promise<IUserBase | undefined>;
22
+ update(user: Partial<IUserBase>): Promise<void>;
23
+ delete(user: Partial<IUserBase>): Promise<void>;
24
+ }
@@ -0,0 +1 @@
1
+ export {};
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.8",
4
+ "version": "5.0.10",
5
5
  "title": "a-user",
6
6
  "vonaModule": {
7
7
  "dependencies": {}
@@ -25,8 +25,12 @@
25
25
  "dist",
26
26
  "static"
27
27
  ],
28
+ "devDependencies": {
29
+ "clean-package": "^2.2.0",
30
+ "rimraf": "^6.0.1"
31
+ },
28
32
  "scripts": {
29
- "clean": "rimraf dist tsconfig.tsbuildinfo",
30
- "tsc:publish": "npm run clean && tsc"
33
+ "clean": "rimraf dist tsconfig.build.tsbuildinfo",
34
+ "tsc:publish": "npm run clean && tsc -p tsconfig.build.json"
31
35
  }
32
36
  }