@midwayjs/passport 3.4.0-beta.10 → 3.4.0-beta.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/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 +65 -4
- package/dist/passport/authenticator.d.ts +125 -0
- package/dist/passport/authenticator.js +463 -0
- package/dist/{service → passport}/passport.service.d.ts +2 -2
- package/dist/passport/passport.service.js +383 -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 +10 -0
- package/dist/passport/strategy.js +7 -0
- package/index.d.ts +2 -2
- package/package.json +13 -14
- 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.js +0 -226
- package/dist/util.d.ts +0 -3
- package/dist/util.js +0 -16
|
@@ -0,0 +1,383 @@
|
|
|
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 http = require("http");
|
|
18
|
+
const request_1 = require("./request");
|
|
19
|
+
function PassportStrategy(Strategy, name) {
|
|
20
|
+
class InnerStrategyAbstractClass extends interface_1.AbstractStrategy {
|
|
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.serializeUser(this['serializeUser']);
|
|
46
|
+
}
|
|
47
|
+
if (this['deserializeUser']) {
|
|
48
|
+
this.passport.deserializeUser(this['deserializeUser']);
|
|
49
|
+
}
|
|
50
|
+
if (this['transformAuthInfo']) {
|
|
51
|
+
this.passport.transformAuthInfo(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
|
+
const authenticateResult = await authenticate(req);
|
|
103
|
+
// success
|
|
104
|
+
if (authenticateResult.successResult) {
|
|
105
|
+
const breakNext = await this.onceSucceed(options, authenticateResult.successResult.user, authenticateResult.successResult.info, req, res);
|
|
106
|
+
if (breakNext) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else if (authenticateResult.redirectResult) {
|
|
111
|
+
// redirect
|
|
112
|
+
res.statusCode = authenticateResult.redirectResult.status || 302;
|
|
113
|
+
res.setHeader('Location', authenticateResult.redirectResult.url);
|
|
114
|
+
res.setHeader('Content-Length', '0');
|
|
115
|
+
res.end();
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
this.allFailed(options, authenticateResult.failResult, req, res);
|
|
120
|
+
}
|
|
121
|
+
next();
|
|
122
|
+
}.bind(this);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
return async function passportAuthenticate(ctx, next) {
|
|
126
|
+
// koa <-> connect compatibility:
|
|
127
|
+
const userProperty = this.passport.getUserProperty();
|
|
128
|
+
// create mock object for express' req object
|
|
129
|
+
const req = (0, request_1.create)(ctx, userProperty);
|
|
130
|
+
// init req method
|
|
131
|
+
this.attachRequestMethod(req);
|
|
132
|
+
// add Promise-based login method
|
|
133
|
+
const login = req.login;
|
|
134
|
+
ctx.login = ctx.logIn = function (user, options) {
|
|
135
|
+
return new Promise((resolve, reject) => {
|
|
136
|
+
login.call(req, user, options, err => {
|
|
137
|
+
if (err)
|
|
138
|
+
reject(err);
|
|
139
|
+
else
|
|
140
|
+
resolve();
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
// add aliases for passport's request extensions to Koa's context
|
|
145
|
+
ctx.logout = ctx.logOut = req.logout.bind(req);
|
|
146
|
+
ctx.isAuthenticated = req.isAuthenticated.bind(req);
|
|
147
|
+
ctx.isUnauthenticated = req.isUnauthenticated.bind(req);
|
|
148
|
+
// merge options with default options
|
|
149
|
+
const authOptions = {
|
|
150
|
+
...this.passportConfig,
|
|
151
|
+
...options,
|
|
152
|
+
};
|
|
153
|
+
const strategyList = [];
|
|
154
|
+
for (const strategySingle of strategy) {
|
|
155
|
+
// got strategy
|
|
156
|
+
const strategyInstance = await this.app
|
|
157
|
+
.getApplicationContext()
|
|
158
|
+
.getAsync(strategySingle);
|
|
159
|
+
strategyList.push(strategyInstance.getStrategy());
|
|
160
|
+
}
|
|
161
|
+
// authenticate
|
|
162
|
+
const authenticate = this.passport.authenticate(strategyList, authOptions);
|
|
163
|
+
const authenticateResult = await authenticate(req);
|
|
164
|
+
// success
|
|
165
|
+
if (authenticateResult.successResult) {
|
|
166
|
+
const breakNext = await this.onceSucceed(options, authenticateResult.successResult.user, authenticateResult.successResult.info, req, ctx);
|
|
167
|
+
if (breakNext) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else if (authenticateResult.redirectResult) {
|
|
172
|
+
// redirect
|
|
173
|
+
ctx.status = authenticateResult.redirectResult.status || 302;
|
|
174
|
+
ctx.set('Location', authenticateResult.redirectResult.url);
|
|
175
|
+
ctx.set('Content-Length', '0');
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
this.allFailed(options, authenticateResult.failResult, req, ctx);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
await next();
|
|
183
|
+
}.bind(this);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
static getName() {
|
|
187
|
+
return 'passport';
|
|
188
|
+
}
|
|
189
|
+
async onceSucceed(options, user, info, req, res) {
|
|
190
|
+
let msg;
|
|
191
|
+
if (options.successFlash) {
|
|
192
|
+
let flash = options.successFlash;
|
|
193
|
+
if (typeof flash === 'string') {
|
|
194
|
+
flash = { type: 'success', message: flash };
|
|
195
|
+
}
|
|
196
|
+
flash.type = flash.type || 'success';
|
|
197
|
+
const type = flash.type || info.type || 'success';
|
|
198
|
+
msg = flash.message || info.message || info;
|
|
199
|
+
if (typeof msg === 'string') {
|
|
200
|
+
req.flash(type, msg);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (options.successMessage) {
|
|
204
|
+
msg = options.successMessage;
|
|
205
|
+
if (typeof msg === 'boolean') {
|
|
206
|
+
msg = info.message || info;
|
|
207
|
+
}
|
|
208
|
+
if (typeof msg === 'string') {
|
|
209
|
+
req.session.messages = req.session.messages || [];
|
|
210
|
+
req.session.messages.push(msg);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (options.assignProperty) {
|
|
214
|
+
req[options.assignProperty] = user;
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
await req.logIn(user, options);
|
|
218
|
+
if (options.authInfo !== false) {
|
|
219
|
+
await new Promise((resolve, reject) => {
|
|
220
|
+
this.passport.transformAuthInfo(info, req, (err, tinfo) => {
|
|
221
|
+
if (err) {
|
|
222
|
+
reject(err);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
req.authInfo = tinfo;
|
|
226
|
+
resolve();
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
if (options.successReturnToOrRedirect) {
|
|
232
|
+
let url = options.successReturnToOrRedirect;
|
|
233
|
+
if (req.session && req.session.returnTo) {
|
|
234
|
+
url = req.session.returnTo;
|
|
235
|
+
delete req.session.returnTo;
|
|
236
|
+
}
|
|
237
|
+
res.redirect(url);
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
if (options.successRedirect) {
|
|
241
|
+
res.redirect(options.successRedirect);
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
allFailed(options, failResult, req, res) {
|
|
246
|
+
// Strategies are ordered by priority. For the purpose of flashing a
|
|
247
|
+
// message, the first failure will be displayed.
|
|
248
|
+
let failure = failResult.failures[0] || {}, challenge = (failure === null || failure === void 0 ? void 0 : failure.challenge) || {}, msg;
|
|
249
|
+
if (options.failureFlash) {
|
|
250
|
+
let flash = options.failureFlash;
|
|
251
|
+
if (typeof flash === 'string') {
|
|
252
|
+
flash = { type: 'error', message: flash };
|
|
253
|
+
}
|
|
254
|
+
flash.type = flash.type || 'error';
|
|
255
|
+
const type = flash.type || challenge.type || 'error';
|
|
256
|
+
msg = flash.message || challenge.message || challenge;
|
|
257
|
+
if (typeof msg === 'string') {
|
|
258
|
+
// TODO
|
|
259
|
+
req.flash(type, msg);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (options.failureMessage) {
|
|
263
|
+
msg = options.failureMessage;
|
|
264
|
+
if (typeof msg === 'boolean') {
|
|
265
|
+
msg = challenge.message || challenge;
|
|
266
|
+
}
|
|
267
|
+
if (typeof msg === 'string') {
|
|
268
|
+
// TODO
|
|
269
|
+
req.session.messages = req.session.messages || [];
|
|
270
|
+
req.session.messages.push(msg);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (options.failureRedirect) {
|
|
274
|
+
// TODO
|
|
275
|
+
return res.redirect(options.failureRedirect);
|
|
276
|
+
}
|
|
277
|
+
// When failure handling is not delegated to the application, the default
|
|
278
|
+
// is to respond with 401 Unauthorized. Note that the WWW-Authenticate
|
|
279
|
+
// header will be set according to the strategies in use (see
|
|
280
|
+
// actions#fail). If multiple strategies failed, each of their challenges
|
|
281
|
+
// will be included in the response.
|
|
282
|
+
const rchallenge = [];
|
|
283
|
+
let rstatus, status;
|
|
284
|
+
for (let j = 0, len = failResult.failures.length; j < len; j++) {
|
|
285
|
+
failure = failResult.failures[j];
|
|
286
|
+
challenge = failure.challenge;
|
|
287
|
+
status = failure.status;
|
|
288
|
+
rstatus = rstatus || status;
|
|
289
|
+
if (typeof challenge === 'string') {
|
|
290
|
+
rchallenge.push(challenge);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
res.statusCode = rstatus || 401;
|
|
294
|
+
// eslint-disable-next-line eqeqeq
|
|
295
|
+
if (res.statusCode === 401 && rchallenge.length) {
|
|
296
|
+
// TODO
|
|
297
|
+
res.setHeader('WWW-Authenticate', rchallenge);
|
|
298
|
+
}
|
|
299
|
+
if (options.failWithError) {
|
|
300
|
+
throw new core_1.httpError.UnauthorizedError();
|
|
301
|
+
}
|
|
302
|
+
// TODO
|
|
303
|
+
res.end(http.STATUS_CODES[res.statusCode]);
|
|
304
|
+
}
|
|
305
|
+
attachRequestMethod(req) {
|
|
306
|
+
// init req method
|
|
307
|
+
req.login = req.logIn = (user, options, done) => {
|
|
308
|
+
if (typeof options === 'function') {
|
|
309
|
+
done = options;
|
|
310
|
+
options = {};
|
|
311
|
+
}
|
|
312
|
+
options = options || {};
|
|
313
|
+
const property = this.passport.getUserProperty();
|
|
314
|
+
req[property] = user;
|
|
315
|
+
if (this.passport.isEnableSession()) {
|
|
316
|
+
return this.passport.logInToSession(req, user).catch(err => {
|
|
317
|
+
req[property] = null;
|
|
318
|
+
if (done) {
|
|
319
|
+
done(err);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
throw err;
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
return Promise.resolve().then(() => {
|
|
328
|
+
if (done) {
|
|
329
|
+
done();
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
req.logout = req.logOut = (options, done) => {
|
|
335
|
+
if (typeof options === 'function') {
|
|
336
|
+
done = options;
|
|
337
|
+
options = {};
|
|
338
|
+
}
|
|
339
|
+
options = options || {};
|
|
340
|
+
req[this.passport.getUserProperty()] = null;
|
|
341
|
+
if (this.passport.isEnableSession()) {
|
|
342
|
+
return this.passport.logOutFromSession(req, options).catch(err => {
|
|
343
|
+
if (done) {
|
|
344
|
+
done(err);
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
throw err;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
return Promise.resolve().then(() => {
|
|
353
|
+
if (done) {
|
|
354
|
+
done();
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
req.isAuthenticated = () => {
|
|
360
|
+
const property = this.passport.getUserProperty();
|
|
361
|
+
return !!this[property];
|
|
362
|
+
};
|
|
363
|
+
req.isUnauthenticated = () => {
|
|
364
|
+
return !req.isAuthenticated();
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
__decorate([
|
|
369
|
+
(0, decorator_1.Config)('passport'),
|
|
370
|
+
__metadata("design:type", Object)
|
|
371
|
+
], InnerPassportMiddleware.prototype, "passportConfig", void 0);
|
|
372
|
+
__decorate([
|
|
373
|
+
(0, decorator_1.App)(),
|
|
374
|
+
__metadata("design:type", Object)
|
|
375
|
+
], InnerPassportMiddleware.prototype, "app", void 0);
|
|
376
|
+
__decorate([
|
|
377
|
+
(0, decorator_1.Inject)(),
|
|
378
|
+
__metadata("design:type", authenticator_1.PassportAuthenticator)
|
|
379
|
+
], InnerPassportMiddleware.prototype, "passport", void 0);
|
|
380
|
+
return InnerPassportMiddleware;
|
|
381
|
+
}
|
|
382
|
+
exports.PassportMiddleware = PassportMiddleware;
|
|
383
|
+
//# sourceMappingURL=passport.service.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): import("@midwayjs/core/dist/error/http").UnauthorizedError;
|
|
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 = 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
|
+
return 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 ? pause(req) : null;
|
|
46
|
+
this._deserializeUser(su, req, (err, user) => {
|
|
47
|
+
if (err) {
|
|
48
|
+
return 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,10 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IncomingMessage } from 'http';
|
|
3
|
+
import { 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
|
+
//# sourceMappingURL=strategy.d.ts.map
|
package/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { AuthenticateOptions } from './dist/index';
|
|
2
2
|
|
|
3
3
|
export * from './dist/index';
|
|
4
4
|
|
|
5
5
|
declare module '@midwayjs/core/dist/interface' {
|
|
6
6
|
interface MidwayConfig {
|
|
7
|
-
passport?:
|
|
7
|
+
passport?: AuthenticateOptions;
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/passport",
|
|
3
3
|
"description": "midway passport component",
|
|
4
|
-
"version": "3.4.0-beta.
|
|
4
|
+
"version": "3.4.0-beta.11",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -22,22 +22,21 @@
|
|
|
22
22
|
"author": "Nawbc",
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@midwayjs/core": "^3.4.0-beta.
|
|
26
|
-
"@midwayjs/decorator": "^3.4.0-beta.
|
|
27
|
-
"@midwayjs/express": "^3.4.0-beta.
|
|
28
|
-
"@midwayjs/jwt": "^3.4.0-beta.
|
|
29
|
-
"@midwayjs/koa": "^3.4.0-beta.
|
|
30
|
-
"@midwayjs/mock": "^3.4.0-beta.
|
|
31
|
-
"@midwayjs/web": "^3.4.0-beta.
|
|
32
|
-
"@types/passport": "1.0.9",
|
|
25
|
+
"@midwayjs/core": "^3.4.0-beta.11",
|
|
26
|
+
"@midwayjs/decorator": "^3.4.0-beta.11",
|
|
27
|
+
"@midwayjs/express": "^3.4.0-beta.11",
|
|
28
|
+
"@midwayjs/jwt": "^3.4.0-beta.11",
|
|
29
|
+
"@midwayjs/koa": "^3.4.0-beta.11",
|
|
30
|
+
"@midwayjs/mock": "^3.4.0-beta.11",
|
|
31
|
+
"@midwayjs/web": "^3.4.0-beta.11",
|
|
33
32
|
"@types/passport-local": "1.0.34",
|
|
34
33
|
"express-session": "1.17.3",
|
|
35
|
-
"passport": "0.5.2",
|
|
36
34
|
"passport-jwt": "4.0.0",
|
|
37
|
-
"passport-local": "1.0.0"
|
|
35
|
+
"passport-local": "1.0.0",
|
|
36
|
+
"passport-openidconnect": "0.1.1"
|
|
38
37
|
},
|
|
39
|
-
"
|
|
40
|
-
"
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"pause": "0.0.1"
|
|
41
40
|
},
|
|
42
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "b1c7a439b0df37d3e381cd182ea3b9e74323107b"
|
|
43
42
|
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Module dependencies.
|
|
3
|
-
*/
|
|
4
|
-
declare const passport: any;
|
|
5
|
-
/**
|
|
6
|
-
* Passport's default/connect middleware.
|
|
7
|
-
*/
|
|
8
|
-
declare const _initialize: any;
|
|
9
|
-
declare const _authenticate: any;
|
|
10
|
-
declare const createReqMock: any;
|
|
11
|
-
/**
|
|
12
|
-
* Passport's initialization middleware for Koa.
|
|
13
|
-
*
|
|
14
|
-
* @return {GeneratorFunction}
|
|
15
|
-
* @api private
|
|
16
|
-
*/
|
|
17
|
-
declare function initialize(passport: any): (ctx: any, next: any) => Promise<any>;
|
|
18
|
-
/**
|
|
19
|
-
* Passport's authenticate middleware for Koa.
|
|
20
|
-
*
|
|
21
|
-
* @param {String|Array} name
|
|
22
|
-
* @param {Object} options
|
|
23
|
-
* @param {GeneratorFunction} callback
|
|
24
|
-
* @return {GeneratorFunction}
|
|
25
|
-
* @api private
|
|
26
|
-
*/
|
|
27
|
-
declare function authenticate(passport: any, name: any, options: any, callback: any): (ctx: any, next: any) => Promise<any>;
|
|
28
|
-
/**
|
|
29
|
-
* Passport's authorize middleware for Koa.
|
|
30
|
-
*
|
|
31
|
-
* @param {String|Array} name
|
|
32
|
-
* @param {Object} options
|
|
33
|
-
* @param {GeneratorFunction} callback
|
|
34
|
-
* @return {GeneratorFunction}
|
|
35
|
-
* @api private
|
|
36
|
-
*/
|
|
37
|
-
declare function authorize(passport: any, name: any, options: any, callback: any): (ctx: any, next: any) => Promise<any>;
|
|
38
|
-
declare function promisify(expressMiddleware: any): (req: any, res: any) => Promise<unknown>;
|
|
39
|
-
//# sourceMappingURL=koa.d.ts.map
|