@midwayjs/passport 3.4.0-beta.9 → 3.4.3
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/config/config.default.d.ts +2 -0
- package/dist/config/config.default.js +2 -0
- package/dist/configuration.js +2 -11
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/interface.d.ts +62 -8
- package/dist/interface.js +1 -4
- package/dist/passport/authenticator.d.ts +128 -0
- package/dist/passport/authenticator.js +439 -0
- package/dist/passport/passport.service.d.ts +6 -0
- package/dist/passport/passport.service.js +417 -0
- package/dist/passport/pause.d.ts +19 -0
- package/dist/passport/pause.js +50 -0
- package/dist/passport/request.d.ts +2 -0
- package/dist/{proxy/framework → passport}/request.js +17 -25
- package/dist/passport/session.stratey.d.ts +34 -0
- package/dist/passport/session.stratey.js +68 -0
- package/dist/passport/strategy.d.ts +14 -0
- package/dist/passport/strategy.js +10 -0
- package/index.d.ts +2 -2
- package/package.json +11 -15
- package/dist/proxy/framework/koa.d.ts +0 -39
- package/dist/proxy/framework/koa.js +0 -190
- package/dist/proxy/framework/request.d.ts +0 -11
- package/dist/proxy/index.d.ts +0 -7
- package/dist/proxy/index.js +0 -21
- package/dist/service/passport.service.d.ts +0 -6
- package/dist/service/passport.service.js +0 -226
- package/dist/util.d.ts +0 -3
- package/dist/util.js +0 -16
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PassportMiddleware = exports.PassportStrategy = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const interface_1 = require("../interface");
|
|
15
|
+
const core_1 = require("@midwayjs/core");
|
|
16
|
+
const authenticator_1 = require("./authenticator");
|
|
17
|
+
const strategy_1 = require("./strategy");
|
|
18
|
+
const request_1 = require("./request");
|
|
19
|
+
function PassportStrategy(Strategy, name) {
|
|
20
|
+
class InnerStrategyAbstractClass extends strategy_1.AbstractStrategyWrapper {
|
|
21
|
+
async init() {
|
|
22
|
+
const cb = async (...params) => {
|
|
23
|
+
const done = params[params.length - 1];
|
|
24
|
+
try {
|
|
25
|
+
const result = await this.validate(...params);
|
|
26
|
+
if (Array.isArray(result)) {
|
|
27
|
+
done(null, ...result);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
done(null, result);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
done(err, null);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
this.strategy = new Strategy(this.getStrategyOptions(), cb);
|
|
38
|
+
if (name) {
|
|
39
|
+
this.passport.use(name, this.strategy);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.passport.use(this.strategy);
|
|
43
|
+
}
|
|
44
|
+
if (this['serializeUser']) {
|
|
45
|
+
this.passport.addSerializer(this['serializeUser']);
|
|
46
|
+
}
|
|
47
|
+
if (this['deserializeUser']) {
|
|
48
|
+
this.passport.addDeserializer(this['deserializeUser']);
|
|
49
|
+
}
|
|
50
|
+
if (this['transformAuthInfo']) {
|
|
51
|
+
this.passport.addInfoTransformer(this['transformAuthInfo']);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
getStrategy() {
|
|
55
|
+
return this.strategy;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
__decorate([
|
|
59
|
+
(0, decorator_1.Inject)(),
|
|
60
|
+
__metadata("design:type", authenticator_1.PassportAuthenticator)
|
|
61
|
+
], InnerStrategyAbstractClass.prototype, "passport", void 0);
|
|
62
|
+
__decorate([
|
|
63
|
+
(0, decorator_1.Init)(),
|
|
64
|
+
__metadata("design:type", Function),
|
|
65
|
+
__metadata("design:paramtypes", []),
|
|
66
|
+
__metadata("design:returntype", Promise)
|
|
67
|
+
], InnerStrategyAbstractClass.prototype, "init", null);
|
|
68
|
+
return InnerStrategyAbstractClass;
|
|
69
|
+
}
|
|
70
|
+
exports.PassportStrategy = PassportStrategy;
|
|
71
|
+
function PassportMiddleware(strategy) {
|
|
72
|
+
class InnerPassportMiddleware extends interface_1.AbstractPassportMiddleware {
|
|
73
|
+
resolve() {
|
|
74
|
+
return this.authenticate(this.getAuthenticateOptions());
|
|
75
|
+
}
|
|
76
|
+
getAuthenticateOptions() {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
authenticate(options) {
|
|
80
|
+
if (!Array.isArray(strategy)) {
|
|
81
|
+
strategy = [strategy];
|
|
82
|
+
}
|
|
83
|
+
if (this.passport.isExpressMode()) {
|
|
84
|
+
return async function passportAuthenticate(req, res, next) {
|
|
85
|
+
// init req method
|
|
86
|
+
this.attachRequestMethod(req);
|
|
87
|
+
// merge options with default options
|
|
88
|
+
const authOptions = {
|
|
89
|
+
...this.passportConfig,
|
|
90
|
+
...options,
|
|
91
|
+
};
|
|
92
|
+
const strategyList = [];
|
|
93
|
+
for (const strategySingle of strategy) {
|
|
94
|
+
// got strategy
|
|
95
|
+
const strategyInstance = await this.app
|
|
96
|
+
.getApplicationContext()
|
|
97
|
+
.getAsync(strategySingle);
|
|
98
|
+
strategyList.push(strategyInstance.getStrategy());
|
|
99
|
+
}
|
|
100
|
+
// authenticate
|
|
101
|
+
const authenticate = this.passport.authenticate(strategyList, authOptions);
|
|
102
|
+
let authenticateResult;
|
|
103
|
+
try {
|
|
104
|
+
authenticateResult = await authenticate(req);
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
if (err instanceof core_1.MidwayHttpError) {
|
|
108
|
+
throw err;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
// 如果验证流程里有错误,抛出一个 500 错误
|
|
112
|
+
throw new core_1.httpError.InternalServerErrorError(err);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// success
|
|
116
|
+
if (authenticateResult.successResult) {
|
|
117
|
+
const breakNext = await this.onceSucceed(options, authenticateResult.successResult.user, authenticateResult.successResult.info, req, res);
|
|
118
|
+
if (breakNext) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else if (authenticateResult.redirectResult) {
|
|
123
|
+
// redirect
|
|
124
|
+
res.statusCode = authenticateResult.redirectResult.status || 302;
|
|
125
|
+
res.setHeader('Location', authenticateResult.redirectResult.url);
|
|
126
|
+
res.setHeader('Content-Length', '0');
|
|
127
|
+
res.end();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
try {
|
|
132
|
+
this.allFailed(options, authenticateResult.failResult, req, res);
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
next(err);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
next();
|
|
140
|
+
}.bind(this);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
return async function passportAuthenticate(ctx, next) {
|
|
144
|
+
// koa <-> connect compatibility:
|
|
145
|
+
const userProperty = this.passport.getUserProperty();
|
|
146
|
+
// create mock object for express' req object
|
|
147
|
+
const req = (0, request_1.create)(ctx, userProperty);
|
|
148
|
+
// init req method
|
|
149
|
+
this.attachRequestMethod(req);
|
|
150
|
+
// add Promise-based login method
|
|
151
|
+
const login = req.login;
|
|
152
|
+
ctx.login = ctx.logIn = function (user, options) {
|
|
153
|
+
return new Promise((resolve, reject) => {
|
|
154
|
+
login.call(req, user, options, err => {
|
|
155
|
+
if (err)
|
|
156
|
+
reject(err);
|
|
157
|
+
else
|
|
158
|
+
resolve();
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
// add aliases for passport's request extensions to Koa's context
|
|
163
|
+
ctx.logout = ctx.logOut = req.logout.bind(req);
|
|
164
|
+
ctx.isAuthenticated = req.isAuthenticated.bind(req);
|
|
165
|
+
ctx.isUnauthenticated = req.isUnauthenticated.bind(req);
|
|
166
|
+
// merge options with default options
|
|
167
|
+
const authOptions = {
|
|
168
|
+
...this.passportConfig,
|
|
169
|
+
...options,
|
|
170
|
+
};
|
|
171
|
+
const strategyList = [];
|
|
172
|
+
for (const strategySingle of strategy) {
|
|
173
|
+
// got strategy
|
|
174
|
+
const strategyInstance = await this.app
|
|
175
|
+
.getApplicationContext()
|
|
176
|
+
.getAsync(strategySingle);
|
|
177
|
+
strategyList.push(strategyInstance.getStrategy());
|
|
178
|
+
}
|
|
179
|
+
// authenticate
|
|
180
|
+
const authenticate = this.passport.authenticate(strategyList, authOptions);
|
|
181
|
+
let authenticateResult;
|
|
182
|
+
try {
|
|
183
|
+
authenticateResult = await authenticate(req);
|
|
184
|
+
}
|
|
185
|
+
catch (err) {
|
|
186
|
+
if (err instanceof core_1.MidwayHttpError) {
|
|
187
|
+
throw err;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
// 如果验证流程里有错误,抛出一个 500 错误
|
|
191
|
+
throw new core_1.httpError.InternalServerErrorError(err);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// success
|
|
195
|
+
if (authenticateResult.successResult) {
|
|
196
|
+
const breakNext = await this.onceSucceed(options, authenticateResult.successResult.user, authenticateResult.successResult.info, req, ctx);
|
|
197
|
+
if (breakNext) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
else if (authenticateResult.redirectResult) {
|
|
202
|
+
// redirect
|
|
203
|
+
ctx.status = authenticateResult.redirectResult.status || 302;
|
|
204
|
+
ctx.set('Location', authenticateResult.redirectResult.url);
|
|
205
|
+
ctx.set('Content-Length', '0');
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
this.allFailed(options, authenticateResult.failResult, req, {
|
|
210
|
+
end(data) {
|
|
211
|
+
ctx.body = data;
|
|
212
|
+
},
|
|
213
|
+
redirect(url) {
|
|
214
|
+
ctx.redirect(url);
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
await next();
|
|
220
|
+
}.bind(this);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
static getName() {
|
|
224
|
+
return 'passport';
|
|
225
|
+
}
|
|
226
|
+
async onceSucceed(options, user, info, req, res) {
|
|
227
|
+
let msg;
|
|
228
|
+
// if (options.successFlash) {
|
|
229
|
+
// let flash: any = options.successFlash;
|
|
230
|
+
// if (typeof flash === 'string') {
|
|
231
|
+
// flash = { type: 'success', message: flash };
|
|
232
|
+
// }
|
|
233
|
+
// flash.type = flash.type || 'success';
|
|
234
|
+
//
|
|
235
|
+
// const type = flash.type || info.type || 'success';
|
|
236
|
+
// msg = flash.message || info.message || info;
|
|
237
|
+
// if (typeof msg === 'string') {
|
|
238
|
+
// req.flash && req.flash(type, msg);
|
|
239
|
+
// }
|
|
240
|
+
// }
|
|
241
|
+
if (options.successMessage) {
|
|
242
|
+
msg = options.successMessage;
|
|
243
|
+
if (typeof msg === 'boolean') {
|
|
244
|
+
msg = info.message || info;
|
|
245
|
+
}
|
|
246
|
+
if (typeof msg === 'string') {
|
|
247
|
+
req.session.messages = req.session.messages || [];
|
|
248
|
+
req.session.messages.push(msg);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (options.assignProperty) {
|
|
252
|
+
req[options.assignProperty] = user;
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
await req.logIn(user, options);
|
|
256
|
+
if (options.authInfo !== false) {
|
|
257
|
+
await new Promise((resolve, reject) => {
|
|
258
|
+
this.passport.transformAuthInfo(info, req, (err, tinfo) => {
|
|
259
|
+
if (err) {
|
|
260
|
+
reject(err);
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
req.authInfo = tinfo;
|
|
264
|
+
resolve();
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
if (options.successReturnToOrRedirect) {
|
|
270
|
+
let url = options.successReturnToOrRedirect;
|
|
271
|
+
if (req.session && req.session.returnTo) {
|
|
272
|
+
url = req.session.returnTo;
|
|
273
|
+
delete req.session.returnTo;
|
|
274
|
+
}
|
|
275
|
+
res.redirect(url);
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
if (options.successRedirect) {
|
|
279
|
+
res.redirect(options.successRedirect);
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
allFailed(options, failResult, req, res) {
|
|
284
|
+
// Strategies are ordered by priority. For the purpose of flashing a
|
|
285
|
+
// message, the first failure will be displayed.
|
|
286
|
+
let failure = failResult.failures[0] || {}, challenge = (failure === null || failure === void 0 ? void 0 : failure.challenge) || {}, msg;
|
|
287
|
+
// if (options.failureFlash) {
|
|
288
|
+
// let flash: any = options.failureFlash;
|
|
289
|
+
// if (typeof flash === 'string') {
|
|
290
|
+
// flash = { type: 'error', message: flash };
|
|
291
|
+
// }
|
|
292
|
+
// flash.type = flash.type || 'error';
|
|
293
|
+
//
|
|
294
|
+
// const type = flash.type || challenge.type || 'error';
|
|
295
|
+
// msg = flash.message || challenge.message || challenge;
|
|
296
|
+
// if (typeof msg === 'string') {
|
|
297
|
+
// req.flash && req.flash(type, msg);
|
|
298
|
+
// }
|
|
299
|
+
// }
|
|
300
|
+
if (options.failureMessage) {
|
|
301
|
+
msg = options.failureMessage;
|
|
302
|
+
if (typeof msg === 'boolean') {
|
|
303
|
+
msg = challenge.message || challenge;
|
|
304
|
+
}
|
|
305
|
+
if (typeof msg === 'string') {
|
|
306
|
+
req.session.messages = req.session.messages || [];
|
|
307
|
+
req.session.messages.push(msg);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (options.failureRedirect) {
|
|
311
|
+
return res.redirect(options.failureRedirect);
|
|
312
|
+
}
|
|
313
|
+
// When failure handling is not delegated to the application, the default
|
|
314
|
+
// is to respond with 401 Unauthorized. Note that the WWW-Authenticate
|
|
315
|
+
// header will be set according to the strategies in use (see
|
|
316
|
+
// actions#fail). If multiple strategies failed, each of their challenges
|
|
317
|
+
// will be included in the response.
|
|
318
|
+
const rchallenge = [];
|
|
319
|
+
let rstatus, status;
|
|
320
|
+
for (let j = 0, len = failResult.failures.length; j < len; j++) {
|
|
321
|
+
failure = failResult.failures[j];
|
|
322
|
+
challenge = failure.challenge;
|
|
323
|
+
status = failure.status;
|
|
324
|
+
rstatus = rstatus || status;
|
|
325
|
+
if (typeof challenge === 'string') {
|
|
326
|
+
rchallenge.push(challenge);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
res.statusCode = rstatus || 401;
|
|
330
|
+
// eslint-disable-next-line eqeqeq
|
|
331
|
+
if (res.statusCode === 401 && rchallenge.length) {
|
|
332
|
+
res.setHeader('WWW-Authenticate', rchallenge);
|
|
333
|
+
}
|
|
334
|
+
// if (options.failWithError) {
|
|
335
|
+
// throw new httpError.UnauthorizedError();
|
|
336
|
+
// }
|
|
337
|
+
throw new core_1.httpError.UnauthorizedError();
|
|
338
|
+
}
|
|
339
|
+
attachRequestMethod(req) {
|
|
340
|
+
// init req method
|
|
341
|
+
req.login = req.logIn = (user, options, done) => {
|
|
342
|
+
if (typeof options === 'function') {
|
|
343
|
+
done = options;
|
|
344
|
+
options = {};
|
|
345
|
+
}
|
|
346
|
+
options = options || {};
|
|
347
|
+
const property = this.passport.getUserProperty();
|
|
348
|
+
req[property] = user;
|
|
349
|
+
if (this.passport.isEnableSession()) {
|
|
350
|
+
return this.passport.logInToSession(req, user).catch(err => {
|
|
351
|
+
req[property] = null;
|
|
352
|
+
if (done) {
|
|
353
|
+
done(err);
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
throw err;
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
return Promise.resolve().then(() => {
|
|
362
|
+
if (done) {
|
|
363
|
+
done();
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
req.logout = req.logOut = (options, done) => {
|
|
369
|
+
if (typeof options === 'function') {
|
|
370
|
+
done = options;
|
|
371
|
+
options = {};
|
|
372
|
+
}
|
|
373
|
+
options = options || {};
|
|
374
|
+
req[this.passport.getUserProperty()] = null;
|
|
375
|
+
if (this.passport.isEnableSession()) {
|
|
376
|
+
return this.passport.logOutFromSession(req, options).catch(err => {
|
|
377
|
+
if (done) {
|
|
378
|
+
done(err);
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
throw err;
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
return Promise.resolve().then(() => {
|
|
387
|
+
if (done) {
|
|
388
|
+
done();
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
req.isAuthenticated = () => {
|
|
394
|
+
const property = this.passport.getUserProperty();
|
|
395
|
+
return !!this[property];
|
|
396
|
+
};
|
|
397
|
+
req.isUnauthenticated = () => {
|
|
398
|
+
return !req.isAuthenticated();
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
__decorate([
|
|
403
|
+
(0, decorator_1.Config)('passport'),
|
|
404
|
+
__metadata("design:type", Object)
|
|
405
|
+
], InnerPassportMiddleware.prototype, "passportConfig", void 0);
|
|
406
|
+
__decorate([
|
|
407
|
+
(0, decorator_1.App)(),
|
|
408
|
+
__metadata("design:type", Object)
|
|
409
|
+
], InnerPassportMiddleware.prototype, "app", void 0);
|
|
410
|
+
__decorate([
|
|
411
|
+
(0, decorator_1.Inject)(),
|
|
412
|
+
__metadata("design:type", authenticator_1.PassportAuthenticator)
|
|
413
|
+
], InnerPassportMiddleware.prototype, "passport", void 0);
|
|
414
|
+
return InnerPassportMiddleware;
|
|
415
|
+
}
|
|
416
|
+
exports.PassportMiddleware = PassportMiddleware;
|
|
417
|
+
//# sourceMappingURL=passport.service.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* pause
|
|
3
|
+
* Copyright(c) 2012 TJ Holowaychuk
|
|
4
|
+
* Copyright(c) 2015 Douglas Christopher Wilson
|
|
5
|
+
* MIT Licensed
|
|
6
|
+
*/
|
|
7
|
+
/// <reference types="node" />
|
|
8
|
+
import { Stream } from 'stream';
|
|
9
|
+
/**
|
|
10
|
+
* Pause the data events on a stream.
|
|
11
|
+
*
|
|
12
|
+
* @param {object} stream
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export declare function pause(stream: Stream): {
|
|
16
|
+
end: () => void;
|
|
17
|
+
resume: () => void;
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=pause.d.ts.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*!
|
|
3
|
+
* pause
|
|
4
|
+
* Copyright(c) 2012 TJ Holowaychuk
|
|
5
|
+
* Copyright(c) 2015 Douglas Christopher Wilson
|
|
6
|
+
* MIT Licensed
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.pause = void 0;
|
|
10
|
+
/**
|
|
11
|
+
* Pause the data events on a stream.
|
|
12
|
+
*
|
|
13
|
+
* @param {object} stream
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
function pause(stream) {
|
|
17
|
+
const events = [];
|
|
18
|
+
const onData = createEventListener('data', events);
|
|
19
|
+
const onEnd = createEventListener('end', events);
|
|
20
|
+
// buffer data
|
|
21
|
+
stream.on('data', onData);
|
|
22
|
+
// buffer end
|
|
23
|
+
stream.on('end', onEnd);
|
|
24
|
+
return {
|
|
25
|
+
end: function end() {
|
|
26
|
+
stream.removeListener('data', onData);
|
|
27
|
+
stream.removeListener('end', onEnd);
|
|
28
|
+
},
|
|
29
|
+
resume: function resume() {
|
|
30
|
+
this.end();
|
|
31
|
+
for (let i = 0; i < events.length; i++) {
|
|
32
|
+
// eslint-disable-next-line prefer-spread
|
|
33
|
+
stream.emit.apply(stream, events[i]);
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
exports.pause = pause;
|
|
39
|
+
function createEventListener(name, events) {
|
|
40
|
+
return function onEvent() {
|
|
41
|
+
const args = new Array(arguments.length + 1);
|
|
42
|
+
args[0] = name;
|
|
43
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
44
|
+
// eslint-disable-next-line prefer-rest-params
|
|
45
|
+
args[i + 1] = arguments[i];
|
|
46
|
+
}
|
|
47
|
+
events.push(args);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=pause.js.map
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/* eslint-disable */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.create = void 0;
|
|
2
5
|
// Koa and Express are fundamental different in how they deal with extensions
|
|
3
6
|
// to the incoming request.
|
|
4
7
|
// Express pollutes Node's IncomingRequest directly, while Koa keeps Node's
|
|
@@ -116,31 +119,20 @@ function getObject(ctx, key) {
|
|
|
116
119
|
}
|
|
117
120
|
return undefined;
|
|
118
121
|
}
|
|
119
|
-
|
|
120
|
-
exports.create = function (ctx, userProperty) {
|
|
122
|
+
function create(ctx, userProperty) {
|
|
121
123
|
const req = Object.create(ctx.request, properties);
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
get: function () {
|
|
134
|
-
return ctx;
|
|
135
|
-
},
|
|
136
|
-
});
|
|
137
|
-
// add passport http.IncomingMessage extensions
|
|
138
|
-
req.login = IncomingMessageExt.logIn;
|
|
139
|
-
req.logIn = IncomingMessageExt.logIn;
|
|
140
|
-
req.logout = IncomingMessageExt.logOut;
|
|
141
|
-
req.logOut = IncomingMessageExt.logOut;
|
|
142
|
-
req.isAuthenticated = IncomingMessageExt.isAuthenticated;
|
|
143
|
-
req.isUnauthenticated = IncomingMessageExt.isUnauthenticated;
|
|
124
|
+
if (!ctx.state.hasOwnProperty(userProperty)) {
|
|
125
|
+
Object.defineProperty(ctx.state, userProperty, {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
get: function () {
|
|
128
|
+
return req[userProperty];
|
|
129
|
+
},
|
|
130
|
+
set: function (val) {
|
|
131
|
+
req[userProperty] = val;
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
}
|
|
144
135
|
return req;
|
|
145
|
-
}
|
|
136
|
+
}
|
|
137
|
+
exports.create = create;
|
|
146
138
|
//# sourceMappingURL=request.js.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Strategy } from './strategy';
|
|
2
|
+
/**
|
|
3
|
+
* `SessionStrategy` constructor.
|
|
4
|
+
*
|
|
5
|
+
* @api public
|
|
6
|
+
*/
|
|
7
|
+
export declare class SessionStrategy extends Strategy {
|
|
8
|
+
readonly options: {
|
|
9
|
+
userProperty: string;
|
|
10
|
+
sessionUserProperty: string;
|
|
11
|
+
pauseStream?: boolean;
|
|
12
|
+
};
|
|
13
|
+
_deserializeUser: any;
|
|
14
|
+
constructor(options: {
|
|
15
|
+
userProperty: string;
|
|
16
|
+
sessionUserProperty: string;
|
|
17
|
+
pauseStream?: boolean;
|
|
18
|
+
}, deserializeUser: any);
|
|
19
|
+
/**
|
|
20
|
+
* Authenticate request based on the current session state.
|
|
21
|
+
*
|
|
22
|
+
* The session authentication strategy uses the session to restore any login
|
|
23
|
+
* state across requests. If a login session has been established, `req.user`
|
|
24
|
+
* will be populated with the current user.
|
|
25
|
+
*
|
|
26
|
+
* This strategy is registered automatically by Passport.
|
|
27
|
+
*
|
|
28
|
+
* @param {Object} req
|
|
29
|
+
* @param {Object} options
|
|
30
|
+
* @api protected
|
|
31
|
+
*/
|
|
32
|
+
authenticate(req: any, options: any): void;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=session.stratey.d.ts.map
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SessionStrategy = void 0;
|
|
4
|
+
const strategy_1 = require("./strategy");
|
|
5
|
+
const core_1 = require("@midwayjs/core");
|
|
6
|
+
const pause_1 = require("./pause");
|
|
7
|
+
/**
|
|
8
|
+
* `SessionStrategy` constructor.
|
|
9
|
+
*
|
|
10
|
+
* @api public
|
|
11
|
+
*/
|
|
12
|
+
class SessionStrategy extends strategy_1.Strategy {
|
|
13
|
+
constructor(options, deserializeUser) {
|
|
14
|
+
super();
|
|
15
|
+
this.options = options;
|
|
16
|
+
this.name = 'session';
|
|
17
|
+
this._deserializeUser = deserializeUser;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Authenticate request based on the current session state.
|
|
21
|
+
*
|
|
22
|
+
* The session authentication strategy uses the session to restore any login
|
|
23
|
+
* state across requests. If a login session has been established, `req.user`
|
|
24
|
+
* will be populated with the current user.
|
|
25
|
+
*
|
|
26
|
+
* This strategy is registered automatically by Passport.
|
|
27
|
+
*
|
|
28
|
+
* @param {Object} req
|
|
29
|
+
* @param {Object} options
|
|
30
|
+
* @api protected
|
|
31
|
+
*/
|
|
32
|
+
authenticate(req, options) {
|
|
33
|
+
if (!req.session) {
|
|
34
|
+
throw new core_1.httpError.UnauthorizedError('Login sessions require session support,please enable it.');
|
|
35
|
+
}
|
|
36
|
+
options = options || {};
|
|
37
|
+
let su;
|
|
38
|
+
if (req.session[this.options.sessionUserProperty]) {
|
|
39
|
+
su = req.session[this.options.sessionUserProperty].user;
|
|
40
|
+
}
|
|
41
|
+
if (su || su === 0) {
|
|
42
|
+
// NOTE: Stream pausing is desirable in the case where later middleware is
|
|
43
|
+
// listening for events emitted from request. For discussion on the
|
|
44
|
+
// matter, refer to: https://github.com/jaredhanson/passport/pull/106
|
|
45
|
+
const paused = options.pauseStream ? (0, pause_1.pause)(req) : null;
|
|
46
|
+
this._deserializeUser(su, req, (err, user) => {
|
|
47
|
+
if (err) {
|
|
48
|
+
throw new core_1.httpError.UnauthorizedError(err.message);
|
|
49
|
+
}
|
|
50
|
+
if (!user) {
|
|
51
|
+
delete req.session[this.options.sessionUserProperty].user;
|
|
52
|
+
this.pass();
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
this.success(user);
|
|
56
|
+
}
|
|
57
|
+
if (paused) {
|
|
58
|
+
paused.resume();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
this.pass();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.SessionStrategy = SessionStrategy;
|
|
68
|
+
//# sourceMappingURL=session.stratey.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IncomingMessage } from 'http';
|
|
3
|
+
import { IPassportStrategy, StrategyCreatedStatic } from '../interface';
|
|
4
|
+
export declare abstract class Strategy {
|
|
5
|
+
name?: string | undefined;
|
|
6
|
+
abstract authenticate(req: IncomingMessage, options?: any): any;
|
|
7
|
+
}
|
|
8
|
+
export interface Strategy extends StrategyCreatedStatic {
|
|
9
|
+
}
|
|
10
|
+
export declare abstract class AbstractStrategyWrapper implements IPassportStrategy {
|
|
11
|
+
abstract validate(...args: any[]): any;
|
|
12
|
+
abstract getStrategyOptions(): any;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=strategy.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractStrategyWrapper = exports.Strategy = void 0;
|
|
4
|
+
class Strategy {
|
|
5
|
+
}
|
|
6
|
+
exports.Strategy = Strategy;
|
|
7
|
+
class AbstractStrategyWrapper {
|
|
8
|
+
}
|
|
9
|
+
exports.AbstractStrategyWrapper = AbstractStrategyWrapper;
|
|
10
|
+
//# sourceMappingURL=strategy.js.map
|