vona-module-a-user 5.0.10 → 5.0.12

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.
@@ -19,6 +19,7 @@ import type { TypeEventSignoutData, TypeEventSignoutResult } from '../bean/event
19
19
  /** bean: begin */
20
20
  import type { IGuardOptionsAdmin } from '../bean/guard.admin.ts';
21
21
  import type { IGuardOptionsPassport } from '../bean/guard.passport.ts';
22
+ import type { IGuardOptionsUserName } from '../bean/guard.userName.ts';
22
23
  import type { config } from '../config/config.ts';
23
24
  /** config: end */
24
25
  /** scope: begin */
@@ -31,12 +32,14 @@ import 'vona';
31
32
  import 'vona';
32
33
  export * from '../bean/bean.authInner.ts';
33
34
  export * from '../bean/bean.passport.ts';
35
+ export * from '../bean/bean.userInner.ts';
34
36
  declare module 'vona-module-a-aspect' {
35
37
  interface IGuardRecordGlobal {
36
38
  'a-user:passport': IGuardOptionsPassport;
37
39
  }
38
40
  interface IGuardRecordLocal {
39
41
  'a-user:admin': IGuardOptionsAdmin;
42
+ 'a-user:userName': IGuardOptionsUserName;
40
43
  }
41
44
  }
42
45
  declare module 'vona-module-a-user' {
@@ -44,12 +47,14 @@ declare module 'vona-module-a-user' {
44
47
  }
45
48
  interface GuardPassport {
46
49
  }
50
+ interface GuardUserName {
51
+ }
47
52
  }
48
- export * from '../bean/bean.userInner.ts';
49
53
  /** bean: end */
50
54
  /** event: begin */
51
55
  export * from '../bean/event.createUserAnonymous.ts';
52
56
  export * from '../bean/event.signin.ts';
57
+ export * from '../bean/event.signout.ts';
53
58
  declare module 'vona' {
54
59
  }
55
60
  declare module 'vona-module-a-user' {
@@ -67,10 +72,10 @@ declare module 'vona' {
67
72
  userInner: BeanUserInner;
68
73
  }
69
74
  }
70
- export * from '../bean/event.signout.ts';
71
75
  /** guard: begin */
72
76
  export * from '../bean/guard.admin.ts';
73
77
  export * from '../bean/guard.passport.ts';
78
+ export * from '../bean/guard.userName.ts';
74
79
  declare module 'vona' {
75
80
  }
76
81
  declare module 'vona-module-a-user' {
@@ -3,7 +3,7 @@ import type { IDecoratorGuardOptions, IGuardExecute } from 'vona-module-a-aspect
3
3
  import { BeanBase } from 'vona';
4
4
  export interface IGuardOptionsAdmin extends IDecoratorGuardOptions {
5
5
  admin: boolean;
6
- passWhenAdmin: boolean;
6
+ passWhenMatched: boolean;
7
7
  }
8
8
  export declare class GuardAdmin extends BeanBase implements IGuardExecute {
9
9
  execute(options: IGuardOptionsAdmin, next: Next): Promise<boolean>;
@@ -0,0 +1,11 @@
1
+ import type { Next } from 'vona';
2
+ import type { IDecoratorGuardOptions, IGuardExecute } from 'vona-module-a-aspect';
3
+ import type { IUserNameRecord } from '../types/user.ts';
4
+ import { BeanBase } from 'vona';
5
+ export interface IGuardOptionsUserName extends IDecoratorGuardOptions {
6
+ name?: keyof IUserNameRecord | (keyof IUserNameRecord)[];
7
+ passWhenMatched: boolean;
8
+ }
9
+ export declare class GuardUserName extends BeanBase implements IGuardExecute {
10
+ execute(options: IGuardOptionsUserName, next: Next): Promise<boolean>;
11
+ }
package/dist/index.js CHANGED
@@ -1,3 +1,437 @@
1
- export * from "./.metadata/index.js";
2
- export * from "./lib/index.js";
3
- export * from "./types/index.js";
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$a, _dec2$a, _class$a;
9
+ let BeanAuthInner = (_dec$a = Bean(), _dec2$a = BeanInfo({
10
+ module: "a-user"
11
+ }), _dec$a(_class$a = _dec2$a(_class$a = 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$a) || _class$a);
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$9, _dec2$9, _class$9;
64
+ let BeanPassport = (_dec$9 = Bean(), _dec2$9 = BeanInfo({
65
+ module: "a-user"
66
+ }), _dec$9(_class$9 = _dec2$9(_class$9 = 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$9) || _class$9);
255
+
256
+ var _dec$8, _dec2$8, _class$8;
257
+ let BeanUserInner = (_dec$8 = Bean(), _dec2$8 = BeanInfo({
258
+ module: "a-user"
259
+ }), _dec$8(_class$8 = _dec2$8(_class$8 = 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$8) || _class$8);
290
+
291
+ var _dec$7, _dec2$7, _class$7;
292
+ let EventCreateUserAnonymous = (_dec$7 = Event(), _dec2$7 = BeanInfo({
293
+ module: "a-user"
294
+ }), _dec$7(_class$7 = _dec2$7(_class$7 = class EventCreateUserAnonymous extends BeanEventBase {}) || _class$7) || _class$7);
295
+
296
+ var _dec$6, _dec2$6, _class$6;
297
+ let EventSignin = (_dec$6 = Event(), _dec2$6 = BeanInfo({
298
+ module: "a-user"
299
+ }), _dec$6(_class$6 = _dec2$6(_class$6 = class EventSignin extends BeanEventBase {}) || _class$6) || _class$6);
300
+
301
+ var _dec$5, _dec2$5, _class$5;
302
+ let EventSignout = (_dec$5 = Event(), _dec2$5 = BeanInfo({
303
+ module: "a-user"
304
+ }), _dec$5(_class$5 = _dec2$5(_class$5 = class EventSignout extends BeanEventBase {}) || _class$5) || _class$5);
305
+
306
+ var _dec$4, _dec2$4, _class$4;
307
+ let GuardAdmin = (_dec$4 = Guard({
308
+ admin: true,
309
+ passWhenMatched: true
310
+ }), _dec2$4 = BeanInfo({
311
+ module: "a-user"
312
+ }), _dec$4(_class$4 = _dec2$4(_class$4 = 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.passWhenMatched) return true;
318
+ }
319
+ // next
320
+ return next();
321
+ }
322
+ }) || _class$4) || _class$4);
323
+
324
+ var _dec$3, _dec2$3, _class$3;
325
+ let GuardPassport = (_dec$3 = Guard({
326
+ global: true,
327
+ public: false,
328
+ checkAuthToken: true
329
+ }), _dec2$3 = BeanInfo({
330
+ module: "a-user"
331
+ }), _dec$3(_class$3 = _dec2$3(_class$3 = 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$3) || _class$3);
358
+
359
+ var _dec$2, _dec2$2, _class$2;
360
+ let GuardUserName = (_dec$2 = Guard({
361
+ passWhenMatched: true
362
+ }), _dec2$2 = BeanInfo({
363
+ module: "a-user"
364
+ }), _dec$2(_class$2 = _dec2$2(_class$2 = class GuardUserName extends BeanBase {
365
+ async execute(options, next) {
366
+ if (!options.name) return this.app.throw(403);
367
+ const user = this.bean.passport.getCurrentUser();
368
+ if (!user || $getUserAnonymous(user)) return this.app.throw(403);
369
+ const userName = $getUserName(user);
370
+ const optionsName = Array.isArray(options.name) ? options.name : [options.name];
371
+ if (!optionsName.includes(userName)) return this.app.throw(403);
372
+ if (options.passWhenMatched) return true;
373
+ // next
374
+ return next();
375
+ }
376
+ }) || _class$2) || _class$2);
377
+
378
+ var _dec$1, _dec2$1, _class$1;
379
+ let MetaPrintTip = (_dec$1 = Meta(), _dec2$1 = BeanInfo({
380
+ module: "a-user"
381
+ }), _dec$1(_class$1 = _dec2$1(_class$1 = class MetaPrintTip extends BeanBase {
382
+ async execute() {
383
+ if (!this.app.meta.isLocal) return;
384
+ // signin
385
+ const jwt = await this.app.bean.executor.newCtx(async () => {
386
+ return await this.bean.passport.signinSystem('dev', '-1');
387
+ }, {
388
+ instanceName: ''
389
+ });
390
+ const accessToken = jwt.accessToken;
391
+ return {
392
+ title: 'access token [admin] [dev]',
393
+ path: `Bearer ${accessToken}`
394
+ };
395
+ }
396
+ }) || _class$1) || _class$1);
397
+
398
+ function config(_app) {
399
+ return {
400
+ passport: {
401
+ refreshAuthToken: 'recreate'
402
+ },
403
+ adapter: {
404
+ authToken: 'home-user:authTokenAdapter',
405
+ passport: 'home-user:passportAdapter',
406
+ userInner: 'home-user:userInnerAdapter',
407
+ authInner: 'home-user:authInnerAdapter'
408
+ }
409
+ };
410
+ }
411
+
412
+ var _dec, _dec2, _class;
413
+ let ScopeModuleAUser = (_dec = Scope(), _dec2 = BeanInfo({
414
+ module: "a-user"
415
+ }), _dec(_class = _dec2(_class = class ScopeModuleAUser extends BeanScopeBase {}) || _class) || _class);
416
+
417
+ /** scope: end */
418
+
419
+ function Public(options) {
420
+ const _public = options?.public === undefined ? true : options.public;
421
+ return Aspect.guardGlobal('a-user:passport', {
422
+ public: _public
423
+ });
424
+ }
425
+ function Admin(options) {
426
+ return Aspect.guard('a-user:admin', options);
427
+ }
428
+ function UserName(options) {
429
+ return Aspect.guard('a-user:userName', options);
430
+ }
431
+ const Passport = {
432
+ admin: Admin,
433
+ public: Public,
434
+ userName: UserName
435
+ };
436
+
437
+ export { $getAuthId, $getAuthIdSystem, $getUserAnonymous, $getUserAvatar, $getUserId, $getUserIdSystem, $getUserLocale, $getUserName, BeanAuthInner, BeanPassport, BeanUserInner, EventCreateUserAnonymous, EventSignin, EventSignout, GuardAdmin, GuardPassport, GuardUserName, MetaPrintTip, Passport, ScopeModuleAUser, config, setAuthAdapter, setUserAdapter };
@@ -1,10 +1,14 @@
1
1
  import type { TypeUseOnionOmitOptionsGlobal } from 'vona-module-a-onion';
2
2
  import type { IGuardOptionsAdmin } from '../bean/guard.admin.ts';
3
3
  import type { IGuardOptionsPassport } from '../bean/guard.passport.ts';
4
+ import type { IGuardOptionsUserName } from '../bean/guard.userName.ts';
4
5
  declare function Public(options?: Partial<TypeUseOnionOmitOptionsGlobal<IGuardOptionsPassport>>): ClassDecorator & MethodDecorator;
5
6
  declare function Admin(options?: Partial<IGuardOptionsAdmin>): ClassDecorator & MethodDecorator;
6
- export declare const Passport: {
7
+ declare function UserName(options?: Partial<IGuardOptionsUserName>): ClassDecorator & MethodDecorator;
8
+ export interface IDecoratorGroupPassport {
7
9
  admin: typeof Admin;
8
10
  public: typeof Public;
9
- };
11
+ userName: typeof UserName;
12
+ }
13
+ export declare const Passport: IDecoratorGroupPassport;
10
14
  export {};
@@ -1,6 +1,9 @@
1
1
  import type { ILocaleInfos } from 'vona';
2
2
  import type { TableIdentity } from 'vona-module-a-database';
3
3
  import type { IAuthUserProfile } from './authProfile.ts';
4
+ export interface IUserNameRecord {
5
+ admin: never;
6
+ }
4
7
  export interface IUserIdRecord {
5
8
  '-1': 'anonymous';
6
9
  }
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.10",
4
+ "version": "5.0.12",
5
5
  "title": "a-user",
6
6
  "vonaModule": {
7
7
  "dependencies": {}
@@ -31,6 +31,6 @@
31
31
  },
32
32
  "scripts": {
33
33
  "clean": "rimraf dist tsconfig.build.tsbuildinfo",
34
- "tsc:publish": "npm run clean && tsc -p tsconfig.build.json"
34
+ "tsc:publish": "npm run clean && vona :bin:buildModule && tsc -p tsconfig.build.json"
35
35
  }
36
36
  }
@@ -1,40 +0,0 @@
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 */
@@ -1,2 +0,0 @@
1
- export const __ThisModule__ = 'a-user';
2
- export { ScopeModuleAUser as ScopeModule } from "./index.js";
@@ -1,27 +0,0 @@
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 };
@@ -1,198 +0,0 @@
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 };
@@ -1,40 +0,0 @@
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 };
@@ -1,13 +0,0 @@
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 };
@@ -1,13 +0,0 @@
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 };
@@ -1,13 +0,0 @@
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 };
@@ -1,25 +0,0 @@
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 };
@@ -1,42 +0,0 @@
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 };
@@ -1,27 +0,0 @@
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 };
@@ -1,13 +0,0 @@
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
- }
package/dist/lib/auth.js DELETED
@@ -1,10 +0,0 @@
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
- }
package/dist/lib/index.js DELETED
@@ -1,3 +0,0 @@
1
- export * from "./auth.js";
2
- export * from "./passport.js";
3
- export * from "./user.js";
@@ -1,12 +0,0 @@
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
- };
package/dist/lib/user.js DELETED
@@ -1,22 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,5 +0,0 @@
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";
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};