vona-module-a-user 5.0.10 → 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.
- package/dist/index.js +414 -3
- package/package.json +2 -2
- package/dist/.metadata/index.js +0 -40
- package/dist/.metadata/this.js +0 -2
- package/dist/bean/bean.authInner.js +0 -27
- package/dist/bean/bean.passport.js +0 -198
- package/dist/bean/bean.userInner.js +0 -40
- package/dist/bean/event.createUserAnonymous.js +0 -13
- package/dist/bean/event.signin.js +0 -13
- package/dist/bean/event.signout.js +0 -13
- package/dist/bean/guard.admin.js +0 -25
- package/dist/bean/guard.passport.js +0 -42
- package/dist/bean/meta.printTip.js +0 -27
- package/dist/config/config.js +0 -13
- package/dist/lib/auth.js +0 -10
- package/dist/lib/index.js +0 -3
- package/dist/lib/passport.js +0 -12
- package/dist/lib/user.js +0 -22
- package/dist/types/auth.js +0 -1
- package/dist/types/authProfile.js +0 -1
- package/dist/types/authToken.js +0 -1
- package/dist/types/index.js +0 -5
- package/dist/types/passport.js +0 -1
- package/dist/types/user.js +0 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,414 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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 };
|
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.
|
|
4
|
+
"version": "5.0.11",
|
|
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
|
}
|
package/dist/.metadata/index.js
DELETED
|
@@ -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 */
|
package/dist/.metadata/this.js
DELETED
|
@@ -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 };
|
package/dist/bean/guard.admin.js
DELETED
|
@@ -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 };
|
package/dist/config/config.js
DELETED
|
@@ -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
package/dist/lib/index.js
DELETED
package/dist/lib/passport.js
DELETED
|
@@ -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
|
-
}
|
package/dist/types/auth.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/types/authToken.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/types/index.js
DELETED
package/dist/types/passport.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/types/user.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|