vona-module-a-user 5.0.9 → 5.0.11

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.
@@ -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,2 @@
1
+ export declare const __ThisModule__ = "a-user";
2
+ export { ScopeModuleAUser as ScopeModule } from './index.ts';
@@ -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,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,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,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,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,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,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,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,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,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,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,414 @@
1
+ import { BeanInfo, BeanBase, beanFullNameFromOnionName, BeanScopeBase } from 'vona';
2
+ import { Bean, Scope } from 'vona-module-a-bean';
3
+ import { catchError } from '@cabloy/utils';
4
+ import { Event, BeanEventBase } from 'vona-module-a-event';
5
+ import { Guard, Aspect } from 'vona-module-a-aspect';
6
+ import { Meta } from 'vona-module-a-meta';
7
+
8
+ var _dec$9, _dec2$9, _class$9;
9
+ let BeanAuthInner = (_dec$9 = Bean(), _dec2$9 = BeanInfo({
10
+ module: "a-user"
11
+ }), _dec$9(_class$9 = _dec2$9(_class$9 = class BeanAuthInner extends BeanBase {
12
+ constructor(...args) {
13
+ super(...args);
14
+ this._authInnerAdapter = void 0;
15
+ }
16
+ get authInnerAdapter() {
17
+ if (!this._authInnerAdapter) {
18
+ const beanFullName = beanFullNameFromOnionName(this.scope.config.adapter.authInner, 'service');
19
+ this._authInnerAdapter = this.bean._getBean(beanFullName);
20
+ }
21
+ return this._authInnerAdapter;
22
+ }
23
+ async get(auth) {
24
+ if (String(auth.id).charAt(0) === '-') return auth;
25
+ return await this.authInnerAdapter.get(auth);
26
+ }
27
+ }) || _class$9) || _class$9);
28
+
29
+ let __authAdapter;
30
+ function setAuthAdapter(authAdapter) {
31
+ __authAdapter = authAdapter;
32
+ }
33
+ function $getAuthId(user) {
34
+ return __authAdapter.getAuthId(user);
35
+ }
36
+ function $getAuthIdSystem(_authName, authId) {
37
+ return authId;
38
+ }
39
+
40
+ let __userAdapter;
41
+ function setUserAdapter(userAdapter) {
42
+ __userAdapter = userAdapter;
43
+ }
44
+ function $getUserId(user) {
45
+ return __userAdapter.getUserId(user);
46
+ }
47
+ function $getUserName(user) {
48
+ return __userAdapter.getUserName(user);
49
+ }
50
+ function $getUserAvatar(user) {
51
+ return __userAdapter.getUserAvatar(user);
52
+ }
53
+ function $getUserLocale(user) {
54
+ return __userAdapter.getUserLocale(user);
55
+ }
56
+ function $getUserAnonymous(user) {
57
+ return __userAdapter.getUserAnonymous(user);
58
+ }
59
+ function $getUserIdSystem(_userName, userId) {
60
+ return userId;
61
+ }
62
+
63
+ var _dec$8, _dec2$8, _class$8;
64
+ let BeanPassport = (_dec$8 = Bean(), _dec2$8 = BeanInfo({
65
+ module: "a-user"
66
+ }), _dec$8(_class$8 = _dec2$8(_class$8 = class BeanPassport extends BeanBase {
67
+ constructor(...args) {
68
+ super(...args);
69
+ this._authTokenAdapter = void 0;
70
+ this._passportAdapter = void 0;
71
+ this._mockCounter = 0;
72
+ }
73
+ get authTokenAdapter() {
74
+ if (!this._authTokenAdapter) {
75
+ const beanFullName = beanFullNameFromOnionName(this.scope.config.adapter.authToken, 'service');
76
+ this._authTokenAdapter = this.bean._getBean(beanFullName);
77
+ }
78
+ return this._authTokenAdapter;
79
+ }
80
+ get passportAdapter() {
81
+ if (!this._passportAdapter) {
82
+ const beanFullName = beanFullNameFromOnionName(this.scope.config.adapter.passport, 'service');
83
+ this._passportAdapter = this.bean._getBean(beanFullName);
84
+ }
85
+ return this._passportAdapter;
86
+ }
87
+ get isAuthenticated() {
88
+ const user = this.getCurrentUser();
89
+ return !!user && !$getUserAnonymous(user);
90
+ }
91
+ async isAdmin() {
92
+ const user = this.getCurrentUser();
93
+ return !!user && this.isAuthenticated && (await this.passportAdapter.isAdmin(user));
94
+ }
95
+ async setCurrent(passport) {
96
+ this.ctx.state.passport = await this.passportAdapter.setCurrent(passport);
97
+ }
98
+ getCurrent() {
99
+ return this.ctx.state.passport;
100
+ }
101
+ getCurrentUser() {
102
+ return this.ctx.state.passport?.user;
103
+ }
104
+ getCurrentAuth() {
105
+ return this.ctx.state.passport?.auth;
106
+ }
107
+ async signin(passport, options) {
108
+ // current
109
+ await this.setCurrent(passport);
110
+ // event
111
+ await this.scope.event.signin.emit(passport);
112
+ // serialize: payloadData for client certificate
113
+ const payloadData = await this._passportSerialize(passport, options);
114
+ // jwt token
115
+ return await this.bean.jwt.create(payloadData, {
116
+ dev: passport.auth?.id.toString() === '-1'
117
+ });
118
+ }
119
+ async signout() {
120
+ // current
121
+ const passport = this.getCurrent();
122
+ if (!passport) return;
123
+ // removeAuthToken
124
+ const payloadData = await this.passportAdapter.serialize(passport);
125
+ await this.authTokenAdapter.remove(payloadData);
126
+ // event
127
+ await this.scope.event.signout.emit(passport);
128
+ // ok
129
+ await this.setCurrent(undefined);
130
+ }
131
+ async signinSystem(authName, authId, name, options) {
132
+ const user = await this.bean.userInner.getByName(name ?? 'admin');
133
+ if (!user) return this.app.throw(401);
134
+ const auth = {
135
+ id: $getAuthIdSystem(authName, authId)
136
+ };
137
+ const passport = {
138
+ user,
139
+ auth
140
+ };
141
+ return await this.signin(passport, options);
142
+ }
143
+ async signinMock(name, options) {
144
+ return await this.signinSystem('mock', -1e4 - ++this._mockCounter, name, options);
145
+ }
146
+ async signinWithAnonymous() {
147
+ const userAnonymous = await this.createUserAnonymous();
148
+ const passport = {
149
+ user: userAnonymous,
150
+ auth: undefined
151
+ };
152
+ await this.setCurrent(passport);
153
+ }
154
+ async createUserAnonymous() {
155
+ const userAnonymous = await this.bean.userInner.createAnonymous();
156
+ // event
157
+ await this.scope.event.createUserAnonymous.emit(userAnonymous);
158
+ // ok
159
+ return userAnonymous;
160
+ }
161
+ async kickOut(user) {
162
+ await this.authTokenAdapter.removeAll(user);
163
+ }
164
+ async checkAuthToken(accessToken, clientName) {
165
+ clientName = clientName ?? 'access';
166
+ const [payloadData, err] = await catchError(() => {
167
+ return this.bean.jwt.get(clientName).verify(accessToken);
168
+ });
169
+ if (err) {
170
+ if (['access', 'refresh'].includes(clientName)) {
171
+ err.code = 401;
172
+ }
173
+ throw err;
174
+ }
175
+ if (!payloadData) return; // no jwt token
176
+ const verified = await this.authTokenAdapter.verify(payloadData);
177
+ if (!verified) return this.app.throw(401);
178
+ const passport = await this.passportAdapter.deserialize(payloadData);
179
+ if (!passport) return this.app.throw(401);
180
+ await this.setCurrent(passport);
181
+ return payloadData;
182
+ }
183
+ async refreshAuthToken(refreshToken) {
184
+ // checkAuthToken by code
185
+ let payloadData = await this.checkAuthToken(refreshToken, 'refresh');
186
+ if (!payloadData) return this.app.throw(401);
187
+ // refreshAuthToken
188
+ const configRefreshAuthToken = this.scope.config.passport.refreshAuthToken;
189
+ payloadData = await this._handlePayloadData(payloadData, {
190
+ authToken: configRefreshAuthToken
191
+ });
192
+ // jwt token
193
+ return await this.bean.jwt.create(payloadData);
194
+ }
195
+
196
+ // only created by accessToken
197
+ async createTempAuthToken(options) {
198
+ // current
199
+ const passport = this.getCurrent();
200
+ if (!passport) return this.app.throw(401);
201
+ // payloadData
202
+ const payloadData = await this._passportSerialize(passport, {
203
+ authToken: 'nochange'
204
+ });
205
+ // jwt token
206
+ return await this.bean.jwt.createTemp(payloadData, options);
207
+ }
208
+ async createOauthAuthToken(options) {
209
+ // current
210
+ const passport = this.getCurrent();
211
+ if (!passport) return this.app.throw(401);
212
+ // payloadData
213
+ const payloadData = await this._passportSerialize(passport, {
214
+ authToken: 'nochange'
215
+ });
216
+ // jwt token
217
+ return await this.bean.jwt.createOauth(payloadData, options);
218
+ }
219
+ async createOauthCode(accessToken, options) {
220
+ // payloadData
221
+ const payloadData = await this.bean.jwt.get('access').verify(accessToken);
222
+ if (!payloadData) return this.app.throw(401);
223
+ // create
224
+ return await this.bean.jwt.createOauthCode(payloadData, options);
225
+ }
226
+ async createAuthTokenFromOauthCode(code) {
227
+ // checkAuthToken by code
228
+ const payloadData = await this.checkAuthToken(code, 'code');
229
+ if (!payloadData) return this.app.throw(401);
230
+ // jwt token
231
+ return await this.bean.jwt.create(payloadData);
232
+ }
233
+ async _passportSerialize(passport, options) {
234
+ // serialize
235
+ const payloadData = await this.passportAdapter.serialize(passport);
236
+ return await this._handlePayloadData(payloadData, options);
237
+ }
238
+ async _handlePayloadData(payloadData, options) {
239
+ // auth token
240
+ const authToken = options?.authToken ?? 'refresh';
241
+ if (authToken === 'recreate') {
242
+ return await this.authTokenAdapter.create(payloadData);
243
+ } else {
244
+ const payloadData2 = await this.authTokenAdapter.retrieve(payloadData);
245
+ if (!payloadData2) {
246
+ return await this.authTokenAdapter.create(payloadData);
247
+ }
248
+ if (authToken === 'refresh') {
249
+ await this.authTokenAdapter.refresh(payloadData2);
250
+ }
251
+ return payloadData2;
252
+ }
253
+ }
254
+ }) || _class$8) || _class$8);
255
+
256
+ var _dec$7, _dec2$7, _class$7;
257
+ let BeanUserInner = (_dec$7 = Bean(), _dec2$7 = BeanInfo({
258
+ module: "a-user"
259
+ }), _dec$7(_class$7 = _dec2$7(_class$7 = class BeanUserInner extends BeanBase {
260
+ constructor(...args) {
261
+ super(...args);
262
+ this._userInnerAdapter = void 0;
263
+ }
264
+ get userInnerAdapter() {
265
+ if (!this._userInnerAdapter) {
266
+ const beanFullName = beanFullNameFromOnionName(this.scope.config.adapter.userInner, 'service');
267
+ this._userInnerAdapter = this.bean._getBean(beanFullName);
268
+ }
269
+ return this._userInnerAdapter;
270
+ }
271
+ createByProfile(profile) {
272
+ return this.userInnerAdapter.createByProfile(profile);
273
+ }
274
+ createAnonymous() {
275
+ return this.userInnerAdapter.createAnonymous();
276
+ }
277
+ getByName(name) {
278
+ return this.userInnerAdapter.getByName(name);
279
+ }
280
+ get(user) {
281
+ return this.userInnerAdapter.get(user);
282
+ }
283
+ update(user) {
284
+ return this.userInnerAdapter.update(user);
285
+ }
286
+ delete(user) {
287
+ return this.userInnerAdapter.delete(user);
288
+ }
289
+ }) || _class$7) || _class$7);
290
+
291
+ var _dec$6, _dec2$6, _class$6;
292
+ let EventCreateUserAnonymous = (_dec$6 = Event(), _dec2$6 = BeanInfo({
293
+ module: "a-user"
294
+ }), _dec$6(_class$6 = _dec2$6(_class$6 = class EventCreateUserAnonymous extends BeanEventBase {}) || _class$6) || _class$6);
295
+
296
+ var _dec$5, _dec2$5, _class$5;
297
+ let EventSignin = (_dec$5 = Event(), _dec2$5 = BeanInfo({
298
+ module: "a-user"
299
+ }), _dec$5(_class$5 = _dec2$5(_class$5 = class EventSignin extends BeanEventBase {}) || _class$5) || _class$5);
300
+
301
+ var _dec$4, _dec2$4, _class$4;
302
+ let EventSignout = (_dec$4 = Event(), _dec2$4 = BeanInfo({
303
+ module: "a-user"
304
+ }), _dec$4(_class$4 = _dec2$4(_class$4 = class EventSignout extends BeanEventBase {}) || _class$4) || _class$4);
305
+
306
+ var _dec$3, _dec2$3, _class$3;
307
+ let GuardAdmin = (_dec$3 = Guard({
308
+ admin: true,
309
+ passWhenAdmin: true
310
+ }), _dec2$3 = BeanInfo({
311
+ module: "a-user"
312
+ }), _dec$3(_class$3 = _dec2$3(_class$3 = class GuardAdmin extends BeanBase {
313
+ async execute(options, next) {
314
+ if (options.admin) {
315
+ const isAdmin = await this.bean.passport.isAdmin();
316
+ if (!isAdmin) return this.app.throw(403);
317
+ if (options.passWhenAdmin) return true;
318
+ }
319
+ // next
320
+ return next();
321
+ }
322
+ }) || _class$3) || _class$3);
323
+
324
+ var _dec$2, _dec2$2, _class$2;
325
+ let GuardPassport = (_dec$2 = Guard({
326
+ global: true,
327
+ public: false,
328
+ checkAuthToken: true
329
+ }), _dec2$2 = BeanInfo({
330
+ module: "a-user"
331
+ }), _dec$2(_class$2 = _dec2$2(_class$2 = class GuardPassport extends BeanBase {
332
+ async execute(options, next) {
333
+ // auth token
334
+ if (!this.bean.passport.getCurrent()) {
335
+ if (options.checkAuthToken) {
336
+ // will return undefined if no accessToken, so not check options.public
337
+ const [_, err] = await catchError(() => {
338
+ return this.bean.passport.checkAuthToken();
339
+ });
340
+ if (err && !options.public) throw err;
341
+ }
342
+ }
343
+ // check current
344
+ if (!this.bean.passport.getCurrent()) {
345
+ await this.bean.passport.signinWithAnonymous();
346
+ }
347
+ if (!options.public && !this.bean.passport.isAuthenticated) {
348
+ // return false;
349
+ // 401 for this guard,403 for the next guards
350
+ return this.app.throw(401);
351
+ }
352
+ // check innerAccess
353
+ if (this.ctx.innerAccess) return true;
354
+ // next
355
+ return next();
356
+ }
357
+ }) || _class$2) || _class$2);
358
+
359
+ var _dec$1, _dec2$1, _class$1;
360
+ let MetaPrintTip = (_dec$1 = Meta(), _dec2$1 = BeanInfo({
361
+ module: "a-user"
362
+ }), _dec$1(_class$1 = _dec2$1(_class$1 = class MetaPrintTip extends BeanBase {
363
+ async execute() {
364
+ if (!this.app.meta.isLocal) return;
365
+ // signin
366
+ const jwt = await this.app.bean.executor.newCtx(async () => {
367
+ return await this.bean.passport.signinSystem('dev', '-1');
368
+ }, {
369
+ instanceName: ''
370
+ });
371
+ const accessToken = jwt.accessToken;
372
+ return {
373
+ title: 'access token [admin] [dev]',
374
+ path: `Bearer ${accessToken}`
375
+ };
376
+ }
377
+ }) || _class$1) || _class$1);
378
+
379
+ function config(_app) {
380
+ return {
381
+ passport: {
382
+ refreshAuthToken: 'recreate'
383
+ },
384
+ adapter: {
385
+ authToken: 'home-user:authTokenAdapter',
386
+ passport: 'home-user:passportAdapter',
387
+ userInner: 'home-user:userInnerAdapter',
388
+ authInner: 'home-user:authInnerAdapter'
389
+ }
390
+ };
391
+ }
392
+
393
+ var _dec, _dec2, _class;
394
+ let ScopeModuleAUser = (_dec = Scope(), _dec2 = BeanInfo({
395
+ module: "a-user"
396
+ }), _dec(_class = _dec2(_class = class ScopeModuleAUser extends BeanScopeBase {}) || _class) || _class);
397
+
398
+ /** scope: end */
399
+
400
+ function Public(options) {
401
+ const _public = options?.public === undefined ? true : options.public;
402
+ return Aspect.guardGlobal('a-user:passport', {
403
+ public: _public
404
+ });
405
+ }
406
+ function Admin(options) {
407
+ return Aspect.guard('a-user:admin', options);
408
+ }
409
+ const Passport = {
410
+ admin: Admin,
411
+ public: Public
412
+ };
413
+
414
+ export { $getAuthId, $getAuthIdSystem, $getUserAnonymous, $getUserAvatar, $getUserId, $getUserIdSystem, $getUserLocale, $getUserName, BeanAuthInner, BeanPassport, BeanUserInner, EventCreateUserAnonymous, EventSignin, EventSignout, GuardAdmin, GuardPassport, MetaPrintTip, Passport, ScopeModuleAUser, config, setAuthAdapter, setUserAdapter };
@@ -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,3 @@
1
+ export * from './auth.ts';
2
+ export * from './passport.ts';
3
+ export * from './user.ts';
@@ -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,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,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,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,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,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,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,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
+ }
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.9",
4
+ "version": "5.0.11",
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 && vona :bin:buildModule && tsc -p tsconfig.build.json"
31
35
  }
32
36
  }