@midwayjs/passport 2.13.3 → 2.14.4
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 +50 -0
- package/LICENSE +21 -0
- package/README.md +2 -4
- package/dist/proxy/framework/koa.d.ts +39 -0
- package/dist/proxy/framework/koa.js +190 -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/service/passport.js +8 -1
- package/dist/util.js +3 -1
- package/package.json +12 -11
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## [2.14.4](https://github.com/midwayjs/midway/compare/v2.14.3...v2.14.4) (2022-01-17)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* Interrupt executions of middleware chaining after authenticatio… ([#1571](https://github.com/midwayjs/midway/issues/1571)) ([60a662c](https://github.com/midwayjs/midway/commit/60a662c83f7d6e1ece6c243a749014de2b7e046c))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## [2.14.2](https://github.com/midwayjs/midway/compare/v2.14.1...v2.14.2) (2021-12-28)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* generate middleware typings ([#1442](https://github.com/midwayjs/midway/issues/1442)) ([1b2f99b](https://github.com/midwayjs/midway/commit/1b2f99b0fcfa6d87fa216ac9277d9f1240ef26a2))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## [2.14.1](https://github.com/midwayjs/midway/compare/v2.14.0...v2.14.1) (2021-12-06)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Bug Fixes
|
|
32
|
+
|
|
33
|
+
* passport missing proxy file ([#1405](https://github.com/midwayjs/midway/issues/1405)) ([ded726a](https://github.com/midwayjs/midway/commit/ded726aeda756aea4dcb2c616d3bc85984e46a60))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# [2.14.0](https://github.com/midwayjs/midway/compare/v2.13.5...v2.14.0) (2021-12-06)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
### Bug Fixes
|
|
43
|
+
|
|
44
|
+
* express routing middleware takes effect at the controller level ([#1364](https://github.com/midwayjs/midway/issues/1364)) ([5d5f299](https://github.com/midwayjs/midway/commit/5d5f2992be116ca71b21f01fd782e3a2ac072496))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
### Features
|
|
48
|
+
|
|
49
|
+
* passport add presetProperty ([#1358](https://github.com/midwayjs/midway/issues/1358)) ([0e5e586](https://github.com/midwayjs/midway/commit/0e5e586c8a43971ff804f73000d44434ac0a9eb4))
|
|
50
|
+
* support passport and jwt ([#1343](https://github.com/midwayjs/midway/issues/1343)) ([f1a7668](https://github.com/midwayjs/midway/commit/f1a7668dfce5a82ddc37efa7cd6321e088d0b1cc))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 - Now midwayjs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -7,12 +7,10 @@ Passport是通过称为策略的可扩展插件进行身份验证请求。Passpo
|
|
|
7
7
|
## 准备
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
1. 安装 `npm i @midwayjs/passport`
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
1. 安装 `npm i @midwayjs/passport` 和相关依赖
|
|
13
11
|
|
|
14
12
|
```bash
|
|
15
|
-
$ npm i passport --save
|
|
13
|
+
$ npm i @midwayjs/passport passport --save
|
|
16
14
|
$ npm i @types/passport --save-dev
|
|
17
15
|
```
|
|
18
16
|
|
|
@@ -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,190 @@
|
|
|
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
|
+
// fix session manager missing
|
|
43
|
+
if (!req._sessionManager) {
|
|
44
|
+
req._sessionManager = passport._sm;
|
|
45
|
+
}
|
|
46
|
+
login.call(req, user, options, err => {
|
|
47
|
+
if (err)
|
|
48
|
+
reject(err);
|
|
49
|
+
else
|
|
50
|
+
resolve();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
// add aliases for passport's request extensions to Koa's context
|
|
55
|
+
ctx.logout = ctx.logOut = req.logout.bind(req);
|
|
56
|
+
ctx.isAuthenticated = req.isAuthenticated.bind(req);
|
|
57
|
+
ctx.isUnauthenticated = req.isUnauthenticated.bind(req);
|
|
58
|
+
return middleware(req, ctx).then(() => {
|
|
59
|
+
return next();
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Passport's authenticate middleware for Koa.
|
|
65
|
+
*
|
|
66
|
+
* @param {String|Array} name
|
|
67
|
+
* @param {Object} options
|
|
68
|
+
* @param {GeneratorFunction} callback
|
|
69
|
+
* @return {GeneratorFunction}
|
|
70
|
+
* @api private
|
|
71
|
+
*/
|
|
72
|
+
function authenticate(passport, name, options, callback) {
|
|
73
|
+
// normalize arguments
|
|
74
|
+
if (typeof options === 'function') {
|
|
75
|
+
callback = options;
|
|
76
|
+
options = {};
|
|
77
|
+
}
|
|
78
|
+
options = options || {};
|
|
79
|
+
if (callback) {
|
|
80
|
+
// When the callback is set, neither `next`, `res.redirect` or `res.end`
|
|
81
|
+
// are called. That is, a workaround to catch the `callback` is required.
|
|
82
|
+
// The `passportAuthenticate()` method below will therefore set
|
|
83
|
+
// `callback.resolve` and `callback.reject`. Then, once the authentication
|
|
84
|
+
// finishes, the modified callback calls the original one and afterwards
|
|
85
|
+
// triggers either `callback.resolve` or `callback.reject` to inform
|
|
86
|
+
// `passportAuthenticate()` that we are ready.
|
|
87
|
+
const _callback = callback;
|
|
88
|
+
callback = function (err, user, info, status) {
|
|
89
|
+
try {
|
|
90
|
+
Promise.resolve(_callback(err, user, info, status))
|
|
91
|
+
.then(() => callback.resolve(false))
|
|
92
|
+
.catch(err => callback.reject(err));
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
callback.reject(err);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const middleware = promisify(_authenticate(passport, name, options, callback));
|
|
100
|
+
return function passportAuthenticate(ctx, next) {
|
|
101
|
+
// this functions wraps the connect middleware
|
|
102
|
+
// to catch `next`, `res.redirect` and `res.end` calls
|
|
103
|
+
const p = new Promise((resolve, reject) => {
|
|
104
|
+
// mock the `req` object
|
|
105
|
+
const req = createReqMock(ctx, options.assignProperty || passport._userProperty || 'user');
|
|
106
|
+
function setBodyAndResolve(content) {
|
|
107
|
+
if (content)
|
|
108
|
+
ctx.body = content;
|
|
109
|
+
resolve(false);
|
|
110
|
+
}
|
|
111
|
+
// mock the `res` object
|
|
112
|
+
const res = {
|
|
113
|
+
redirect: function (url) {
|
|
114
|
+
ctx.redirect(url);
|
|
115
|
+
resolve(false);
|
|
116
|
+
},
|
|
117
|
+
set: ctx.set.bind(ctx),
|
|
118
|
+
setHeader: ctx.set.bind(ctx),
|
|
119
|
+
end: setBodyAndResolve,
|
|
120
|
+
send: setBodyAndResolve,
|
|
121
|
+
set statusCode(status) {
|
|
122
|
+
ctx.status = status;
|
|
123
|
+
},
|
|
124
|
+
get statusCode() {
|
|
125
|
+
return ctx.status;
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
req.res = res;
|
|
129
|
+
// update the custom callback above
|
|
130
|
+
if (callback) {
|
|
131
|
+
callback.resolve = resolve;
|
|
132
|
+
callback.reject = reject;
|
|
133
|
+
}
|
|
134
|
+
// call the connect middleware
|
|
135
|
+
middleware(req, res).then(resolve, reject);
|
|
136
|
+
});
|
|
137
|
+
return p.then(cont => {
|
|
138
|
+
// cont equals `false` when `res.redirect` or `res.end` got called
|
|
139
|
+
// in this case, call next to continue through Koa's middleware stack
|
|
140
|
+
if (cont !== false) {
|
|
141
|
+
return next();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Passport's authorize middleware for Koa.
|
|
148
|
+
*
|
|
149
|
+
* @param {String|Array} name
|
|
150
|
+
* @param {Object} options
|
|
151
|
+
* @param {GeneratorFunction} callback
|
|
152
|
+
* @return {GeneratorFunction}
|
|
153
|
+
* @api private
|
|
154
|
+
*/
|
|
155
|
+
function authorize(passport, name, options, callback) {
|
|
156
|
+
options = options || {};
|
|
157
|
+
options.assignProperty = 'account';
|
|
158
|
+
return authenticate(passport, name, options, callback);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Framework support for Koa.
|
|
162
|
+
*
|
|
163
|
+
* This module provides support for using Passport with Koa. It exposes
|
|
164
|
+
* middleware that conform to the `fn*(next)` signature and extends
|
|
165
|
+
* Node's built-in HTTP request object with useful authentication-related
|
|
166
|
+
* functions.
|
|
167
|
+
*
|
|
168
|
+
* @return {Object}
|
|
169
|
+
* @api protected
|
|
170
|
+
*/
|
|
171
|
+
module.exports = function () {
|
|
172
|
+
return {
|
|
173
|
+
initialize: initialize,
|
|
174
|
+
authenticate: authenticate,
|
|
175
|
+
authorize: authorize,
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
function promisify(expressMiddleware) {
|
|
179
|
+
return function (req, res) {
|
|
180
|
+
return new Promise((resolve, reject) => {
|
|
181
|
+
expressMiddleware(req, res, (err, result) => {
|
|
182
|
+
if (err)
|
|
183
|
+
reject(err);
|
|
184
|
+
else
|
|
185
|
+
resolve(result);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
//# 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/service/passport.js
CHANGED
|
@@ -160,6 +160,10 @@ let PassportService = class PassportService {
|
|
|
160
160
|
}
|
|
161
161
|
else {
|
|
162
162
|
res.status(401);
|
|
163
|
+
const unAuthorizeError = new Error('UnAuthorized!');
|
|
164
|
+
unAuthorizeError.name = 'UnauthorizedError';
|
|
165
|
+
unAuthorizeError.status = 401;
|
|
166
|
+
throw unAuthorizeError;
|
|
163
167
|
}
|
|
164
168
|
}
|
|
165
169
|
next();
|
|
@@ -223,7 +227,10 @@ let PassportService = class PassportService {
|
|
|
223
227
|
return;
|
|
224
228
|
}
|
|
225
229
|
else {
|
|
226
|
-
|
|
230
|
+
const unAuthorizeError = new Error('UnAuthorized!');
|
|
231
|
+
unAuthorizeError.name = 'UnauthorizedError';
|
|
232
|
+
unAuthorizeError.status = 401;
|
|
233
|
+
throw unAuthorizeError;
|
|
227
234
|
}
|
|
228
235
|
}
|
|
229
236
|
await next();
|
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.
|
|
4
|
+
"version": "2.14.4",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -21,20 +21,21 @@
|
|
|
21
21
|
"author": "Nawbc",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@midwayjs/core": "^2.
|
|
25
|
-
"@midwayjs/decorator": "^2.
|
|
26
|
-
"@midwayjs/express": "^2.
|
|
27
|
-
"@midwayjs/
|
|
28
|
-
"@midwayjs/
|
|
29
|
-
"@midwayjs/
|
|
30
|
-
"@midwayjs/
|
|
24
|
+
"@midwayjs/core": "^2.14.0",
|
|
25
|
+
"@midwayjs/decorator": "^2.14.0",
|
|
26
|
+
"@midwayjs/express": "^2.14.2",
|
|
27
|
+
"@midwayjs/jwt": "^2.14.2",
|
|
28
|
+
"@midwayjs/koa": "^2.14.2",
|
|
29
|
+
"@midwayjs/mock": "^2.14.0",
|
|
30
|
+
"@midwayjs/web": "^2.14.2",
|
|
31
31
|
"@types/passport": "^1.0.7",
|
|
32
32
|
"@types/passport-local": "^1.0.34",
|
|
33
|
+
"express-session": "^1.17.2",
|
|
33
34
|
"passport": "^0.5.0",
|
|
34
|
-
"passport-local": "^1.0.0"
|
|
35
|
-
"express-session": "^1.17.2"
|
|
35
|
+
"passport-local": "^1.0.0"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"passport": "^0.5.0"
|
|
39
|
-
}
|
|
39
|
+
},
|
|
40
|
+
"gitHead": "7d7f01072b2aabb1ee66561fc0406a327de90d8b"
|
|
40
41
|
}
|