@midwayjs/passport 2.14.0 → 2.14.1
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/CHANGELOG.md +11 -0
- package/dist/proxy/framework/koa.d.ts +39 -0
- package/dist/proxy/framework/koa.js +186 -0
- package/dist/proxy/framework/request.d.ts +11 -0
- package/dist/proxy/framework/request.js +146 -0
- package/dist/proxy/index.d.ts +7 -0
- package/dist/proxy/index.js +21 -0
- package/dist/util.js +3 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [2.14.1](https://github.com/midwayjs/midway/compare/v2.14.0...v2.14.1) (2021-12-06)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* passport missing proxy file ([#1405](https://github.com/midwayjs/midway/issues/1405)) ([ded726a](https://github.com/midwayjs/midway/commit/ded726aeda756aea4dcb2c616d3bc85984e46a60))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [2.14.0](https://github.com/midwayjs/midway/compare/v2.13.5...v2.14.0) (2021-12-06)
|
|
7
18
|
|
|
8
19
|
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* Module dependencies.
|
|
4
|
+
*/
|
|
5
|
+
const passport = require('passport');
|
|
6
|
+
/**
|
|
7
|
+
* Passport's default/connect middleware.
|
|
8
|
+
*/
|
|
9
|
+
const _initialize = require('passport/lib/middleware/initialize');
|
|
10
|
+
const _authenticate = require('passport/lib/middleware/authenticate');
|
|
11
|
+
const createReqMock = require('./request').create;
|
|
12
|
+
/**
|
|
13
|
+
* Passport's initialization middleware for Koa.
|
|
14
|
+
*
|
|
15
|
+
* @return {GeneratorFunction}
|
|
16
|
+
* @api private
|
|
17
|
+
*/
|
|
18
|
+
function initialize(passport) {
|
|
19
|
+
const middleware = promisify(_initialize(passport));
|
|
20
|
+
return function passportInitialize(ctx, next) {
|
|
21
|
+
// koa <-> connect compatibility:
|
|
22
|
+
const userProperty = passport._userProperty || 'user';
|
|
23
|
+
// check ctx.req has the userProperty
|
|
24
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
25
|
+
if (!ctx.req.hasOwnProperty(userProperty)) {
|
|
26
|
+
Object.defineProperty(ctx.req, userProperty, {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () {
|
|
29
|
+
return ctx.state[userProperty];
|
|
30
|
+
},
|
|
31
|
+
set: function (val) {
|
|
32
|
+
ctx.state[userProperty] = val;
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
// create mock object for express' req object
|
|
37
|
+
const req = createReqMock(ctx, userProperty);
|
|
38
|
+
// add Promise-based login method
|
|
39
|
+
const login = req.login;
|
|
40
|
+
ctx.login = ctx.logIn = function (user, options) {
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
login.call(req, user, options, err => {
|
|
43
|
+
if (err)
|
|
44
|
+
reject(err);
|
|
45
|
+
else
|
|
46
|
+
resolve();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
// add aliases for passport's request extensions to Koa's context
|
|
51
|
+
ctx.logout = ctx.logOut = req.logout.bind(req);
|
|
52
|
+
ctx.isAuthenticated = req.isAuthenticated.bind(req);
|
|
53
|
+
ctx.isUnauthenticated = req.isUnauthenticated.bind(req);
|
|
54
|
+
return middleware(req, ctx).then(() => {
|
|
55
|
+
return next();
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Passport's authenticate middleware for Koa.
|
|
61
|
+
*
|
|
62
|
+
* @param {String|Array} name
|
|
63
|
+
* @param {Object} options
|
|
64
|
+
* @param {GeneratorFunction} callback
|
|
65
|
+
* @return {GeneratorFunction}
|
|
66
|
+
* @api private
|
|
67
|
+
*/
|
|
68
|
+
function authenticate(passport, name, options, callback) {
|
|
69
|
+
// normalize arguments
|
|
70
|
+
if (typeof options === 'function') {
|
|
71
|
+
callback = options;
|
|
72
|
+
options = {};
|
|
73
|
+
}
|
|
74
|
+
options = options || {};
|
|
75
|
+
if (callback) {
|
|
76
|
+
// When the callback is set, neither `next`, `res.redirect` or `res.end`
|
|
77
|
+
// are called. That is, a workaround to catch the `callback` is required.
|
|
78
|
+
// The `passportAuthenticate()` method below will therefore set
|
|
79
|
+
// `callback.resolve` and `callback.reject`. Then, once the authentication
|
|
80
|
+
// finishes, the modified callback calls the original one and afterwards
|
|
81
|
+
// triggers either `callback.resolve` or `callback.reject` to inform
|
|
82
|
+
// `passportAuthenticate()` that we are ready.
|
|
83
|
+
const _callback = callback;
|
|
84
|
+
callback = function (err, user, info, status) {
|
|
85
|
+
try {
|
|
86
|
+
Promise.resolve(_callback(err, user, info, status))
|
|
87
|
+
.then(() => callback.resolve(false))
|
|
88
|
+
.catch(err => callback.reject(err));
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
callback.reject(err);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const middleware = promisify(_authenticate(passport, name, options, callback));
|
|
96
|
+
return function passportAuthenticate(ctx, next) {
|
|
97
|
+
// this functions wraps the connect middleware
|
|
98
|
+
// to catch `next`, `res.redirect` and `res.end` calls
|
|
99
|
+
const p = new Promise((resolve, reject) => {
|
|
100
|
+
// mock the `req` object
|
|
101
|
+
const req = createReqMock(ctx, options.assignProperty || passport._userProperty || 'user');
|
|
102
|
+
function setBodyAndResolve(content) {
|
|
103
|
+
if (content)
|
|
104
|
+
ctx.body = content;
|
|
105
|
+
resolve(false);
|
|
106
|
+
}
|
|
107
|
+
// mock the `res` object
|
|
108
|
+
const res = {
|
|
109
|
+
redirect: function (url) {
|
|
110
|
+
ctx.redirect(url);
|
|
111
|
+
resolve(false);
|
|
112
|
+
},
|
|
113
|
+
set: ctx.set.bind(ctx),
|
|
114
|
+
setHeader: ctx.set.bind(ctx),
|
|
115
|
+
end: setBodyAndResolve,
|
|
116
|
+
send: setBodyAndResolve,
|
|
117
|
+
set statusCode(status) {
|
|
118
|
+
ctx.status = status;
|
|
119
|
+
},
|
|
120
|
+
get statusCode() {
|
|
121
|
+
return ctx.status;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
req.res = res;
|
|
125
|
+
// update the custom callback above
|
|
126
|
+
if (callback) {
|
|
127
|
+
callback.resolve = resolve;
|
|
128
|
+
callback.reject = reject;
|
|
129
|
+
}
|
|
130
|
+
// call the connect middleware
|
|
131
|
+
middleware(req, res).then(resolve, reject);
|
|
132
|
+
});
|
|
133
|
+
return p.then(cont => {
|
|
134
|
+
// cont equals `false` when `res.redirect` or `res.end` got called
|
|
135
|
+
// in this case, call next to continue through Koa's middleware stack
|
|
136
|
+
if (cont !== false) {
|
|
137
|
+
return next();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Passport's authorize middleware for Koa.
|
|
144
|
+
*
|
|
145
|
+
* @param {String|Array} name
|
|
146
|
+
* @param {Object} options
|
|
147
|
+
* @param {GeneratorFunction} callback
|
|
148
|
+
* @return {GeneratorFunction}
|
|
149
|
+
* @api private
|
|
150
|
+
*/
|
|
151
|
+
function authorize(passport, name, options, callback) {
|
|
152
|
+
options = options || {};
|
|
153
|
+
options.assignProperty = 'account';
|
|
154
|
+
return authenticate(passport, name, options, callback);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Framework support for Koa.
|
|
158
|
+
*
|
|
159
|
+
* This module provides support for using Passport with Koa. It exposes
|
|
160
|
+
* middleware that conform to the `fn*(next)` signature and extends
|
|
161
|
+
* Node's built-in HTTP request object with useful authentication-related
|
|
162
|
+
* functions.
|
|
163
|
+
*
|
|
164
|
+
* @return {Object}
|
|
165
|
+
* @api protected
|
|
166
|
+
*/
|
|
167
|
+
module.exports = function () {
|
|
168
|
+
return {
|
|
169
|
+
initialize: initialize,
|
|
170
|
+
authenticate: authenticate,
|
|
171
|
+
authorize: authorize,
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
function promisify(expressMiddleware) {
|
|
175
|
+
return function (req, res) {
|
|
176
|
+
return new Promise((resolve, reject) => {
|
|
177
|
+
expressMiddleware(req, res, (err, result) => {
|
|
178
|
+
if (err)
|
|
179
|
+
reject(err);
|
|
180
|
+
else
|
|
181
|
+
resolve(result);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
//# sourceMappingURL=koa.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare let keys: string[];
|
|
2
|
+
declare const properties: {
|
|
3
|
+
app: {
|
|
4
|
+
get: () => {
|
|
5
|
+
get: (key: any) => any;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
declare function getObject(ctx: any, key: any): any;
|
|
10
|
+
declare const IncomingMessageExt: any;
|
|
11
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
// Koa and Express are fundamental different in how they deal with extensions
|
|
3
|
+
// to the incoming request.
|
|
4
|
+
// Express pollutes Node's IncomingRequest directly, while Koa keeps Node's
|
|
5
|
+
// IncomingRequest untouched and adds is own high-level request object.
|
|
6
|
+
// These both approaches are not directly compatible with each other, since
|
|
7
|
+
// properties/methods found in Express' `req` object are now spread between
|
|
8
|
+
// Koa's context, Koa's request object and the original incoming request.
|
|
9
|
+
// This makes moking the Express `req` object an ugly task. With ES6 we could
|
|
10
|
+
// simply use a Proxy, e.g.:
|
|
11
|
+
//
|
|
12
|
+
// function createReqMock(ctx) {
|
|
13
|
+
// // Use a proxy that forwards `req` reads to either `ctx.passport`,
|
|
14
|
+
// // Node's request, Koa's request or Koa's context. Writes are persistet
|
|
15
|
+
// // into `ctx.passport`.
|
|
16
|
+
// return Proxy.create(handler(ctx.passport, {
|
|
17
|
+
// get: function(receiver, key) {
|
|
18
|
+
// return ctx.passport[key] || ctx.req[key] || ctx.request[key] || ctx[key]
|
|
19
|
+
// }
|
|
20
|
+
// }))
|
|
21
|
+
// }
|
|
22
|
+
//
|
|
23
|
+
// However, the current Proxy implementation does not allow debugging.
|
|
24
|
+
// See: https://github.com/rkusa/koa-passport/issues/17
|
|
25
|
+
//
|
|
26
|
+
// Until this is fixed, koa-passport tries to properly delegate every possible
|
|
27
|
+
// used property/method.
|
|
28
|
+
// Property/Method names to be delegated
|
|
29
|
+
let keys = [
|
|
30
|
+
// passport
|
|
31
|
+
'_passport',
|
|
32
|
+
'authInfo',
|
|
33
|
+
// http.IncomingMessage
|
|
34
|
+
'httpVersion',
|
|
35
|
+
'headers',
|
|
36
|
+
'trailers',
|
|
37
|
+
'setTimeout',
|
|
38
|
+
'method',
|
|
39
|
+
'url',
|
|
40
|
+
'statusCode',
|
|
41
|
+
'socket',
|
|
42
|
+
'connection',
|
|
43
|
+
'protocol',
|
|
44
|
+
// Koa's context
|
|
45
|
+
'cookies',
|
|
46
|
+
'throw',
|
|
47
|
+
'ip',
|
|
48
|
+
// Others. Are not supported directly - require proper plugins/middlewares.
|
|
49
|
+
'param',
|
|
50
|
+
'params',
|
|
51
|
+
'route',
|
|
52
|
+
'xhr',
|
|
53
|
+
'baseUrl',
|
|
54
|
+
'session',
|
|
55
|
+
'body',
|
|
56
|
+
'flash',
|
|
57
|
+
];
|
|
58
|
+
// remove duplicates
|
|
59
|
+
keys = keys.filter((key, i, self) => {
|
|
60
|
+
return self.indexOf(key) === i;
|
|
61
|
+
});
|
|
62
|
+
// create a delegate for each key
|
|
63
|
+
const properties = {
|
|
64
|
+
// mock express' .get('trust proxy')
|
|
65
|
+
app: {
|
|
66
|
+
// getter returning a mock for `req.app` containing
|
|
67
|
+
// the `.get()` method
|
|
68
|
+
get: function () {
|
|
69
|
+
const ctx = this.ctx;
|
|
70
|
+
return {
|
|
71
|
+
get: function (key) {
|
|
72
|
+
if (key === 'trust proxy') {
|
|
73
|
+
return ctx.app.proxy;
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
keys.forEach(key => {
|
|
82
|
+
properties[key] = {
|
|
83
|
+
get: function () {
|
|
84
|
+
const obj = getObject(this.ctx, key);
|
|
85
|
+
if (!obj)
|
|
86
|
+
return undefined;
|
|
87
|
+
// if its a function, call with the proper context
|
|
88
|
+
if (typeof obj[key] === 'function') {
|
|
89
|
+
return function () {
|
|
90
|
+
return obj[key].apply(obj, arguments);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
// otherwise, simply return it
|
|
94
|
+
return obj[key];
|
|
95
|
+
},
|
|
96
|
+
set: function (value) {
|
|
97
|
+
const obj = getObject(this.ctx, key) || this.ctx.state;
|
|
98
|
+
obj[key] = value;
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
// test where the key is available, either in `ctx.state`, Node's request,
|
|
103
|
+
// Koa's request or Koa's context
|
|
104
|
+
function getObject(ctx, key) {
|
|
105
|
+
if (ctx.state && key in ctx.state) {
|
|
106
|
+
return ctx.state;
|
|
107
|
+
}
|
|
108
|
+
if (key in ctx.request) {
|
|
109
|
+
return ctx.request;
|
|
110
|
+
}
|
|
111
|
+
if (key in ctx.req) {
|
|
112
|
+
return ctx.req;
|
|
113
|
+
}
|
|
114
|
+
if (key in ctx) {
|
|
115
|
+
return ctx;
|
|
116
|
+
}
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
const IncomingMessageExt = require('passport/lib/http/request');
|
|
120
|
+
exports.create = function (ctx, userProperty) {
|
|
121
|
+
const req = Object.create(ctx.request, properties);
|
|
122
|
+
Object.defineProperty(req, userProperty, {
|
|
123
|
+
enumerable: true,
|
|
124
|
+
get: function () {
|
|
125
|
+
return ctx.state[userProperty];
|
|
126
|
+
},
|
|
127
|
+
set: function (val) {
|
|
128
|
+
ctx.state[userProperty] = val;
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
Object.defineProperty(req, 'ctx', {
|
|
132
|
+
enumerable: true,
|
|
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;
|
|
144
|
+
return req;
|
|
145
|
+
};
|
|
146
|
+
//# sourceMappingURL=request.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.passport = exports.KoaPassport = void 0;
|
|
4
|
+
// prevent passport from monkey patching
|
|
5
|
+
const connect = require('passport/lib/framework/connect');
|
|
6
|
+
connect.__monkeypatchNode = function () { };
|
|
7
|
+
// load passport and add the koa framework
|
|
8
|
+
const originPassport = require('passport');
|
|
9
|
+
const Passport = require('passport').Passport;
|
|
10
|
+
const framework = require('./framework/koa')();
|
|
11
|
+
originPassport.framework(framework);
|
|
12
|
+
class KoaPassport extends Passport {
|
|
13
|
+
constructor() {
|
|
14
|
+
super();
|
|
15
|
+
this.framework(framework);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.KoaPassport = KoaPassport;
|
|
19
|
+
// Export default singleton.
|
|
20
|
+
exports.passport = originPassport;
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
package/dist/util.js
CHANGED
|
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.isExpressMode = exports.getPassport = void 0;
|
|
4
4
|
const core_1 = require("@midwayjs/core");
|
|
5
5
|
function getPassport() {
|
|
6
|
-
return isExpressMode()
|
|
6
|
+
return isExpressMode()
|
|
7
|
+
? require('passport')
|
|
8
|
+
: require('./proxy/index').passport;
|
|
7
9
|
}
|
|
8
10
|
exports.getPassport = getPassport;
|
|
9
11
|
function isExpressMode() {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/passport",
|
|
3
3
|
"description": "midway passport component",
|
|
4
|
-
"version": "2.14.
|
|
4
|
+
"version": "2.14.1",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"passport": "^0.5.0"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "b3dc5e27f4d611880e9525a3b798b99b349c3dd5"
|
|
41
41
|
}
|