@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/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import * as passport from 'passport';
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?: passport.AuthenticateOptions;
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.9",
4
+ "version": "3.4.3",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -22,22 +22,18 @@
22
22
  "author": "Nawbc",
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
- "@midwayjs/core": "^3.4.0-beta.9",
26
- "@midwayjs/decorator": "^3.4.0-beta.9",
27
- "@midwayjs/express": "^3.4.0-beta.9",
28
- "@midwayjs/jwt": "^3.4.0-beta.9",
29
- "@midwayjs/koa": "^3.4.0-beta.9",
30
- "@midwayjs/mock": "^3.4.0-beta.9",
31
- "@midwayjs/web": "^3.4.0-beta.9",
32
- "@types/passport": "1.0.9",
25
+ "@midwayjs/core": "^3.4.3",
26
+ "@midwayjs/decorator": "^3.4.1",
27
+ "@midwayjs/express": "^3.4.3",
28
+ "@midwayjs/jwt": "^3.4.3",
29
+ "@midwayjs/koa": "^3.4.3",
30
+ "@midwayjs/mock": "^3.4.3",
31
+ "@midwayjs/web": "^3.4.3",
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
- "peerDependencies": {
40
- "passport": "^0.5.0"
41
- },
42
- "gitHead": "41e82a0fba386c6ec42c2eefd1dff4795a81b389"
38
+ "gitHead": "0d79293f56d01a3ef3589e6a6ef53c582ac0c46f"
43
39
  }
@@ -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
@@ -1,190 +0,0 @@
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
@@ -1,11 +0,0 @@
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
@@ -1,7 +0,0 @@
1
- declare const Passport: any;
2
- export declare class KoaPassport extends Passport {
3
- constructor();
4
- }
5
- export declare const passport: any;
6
- export {};
7
- //# sourceMappingURL=index.d.ts.map
@@ -1,21 +0,0 @@
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
@@ -1,6 +0,0 @@
1
- import * as passport from 'passport';
2
- import { AbstractPassportMiddleware, AbstractStrategy } from '../interface';
3
- export declare function PassportStrategy(Strategy: new (...args: any[]) => passport.Strategy, name?: string): new (...args: any[]) => AbstractStrategy;
4
- export declare type StrategyClass = new (...args: any[]) => AbstractStrategy;
5
- export declare function PassportMiddleware(strategy: StrategyClass | StrategyClass[]): new (...args: any[]) => AbstractPassportMiddleware;
6
- //# sourceMappingURL=passport.service.d.ts.map
@@ -1,226 +0,0 @@
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 util_1 = require("../util");
15
- const interface_1 = require("../interface");
16
- const core_1 = require("@midwayjs/core");
17
- function PassportStrategy(Strategy, name) {
18
- class InnerStrategyAbstractClass extends interface_1.AbstractStrategy {
19
- async init() {
20
- const cb = async (...params) => {
21
- const done = params[params.length - 1];
22
- try {
23
- const result = await this.validate(...params);
24
- if (Array.isArray(result)) {
25
- done(null, ...result);
26
- }
27
- else {
28
- done(null, result);
29
- }
30
- }
31
- catch (err) {
32
- done(err, null);
33
- }
34
- };
35
- this.strategy = new Strategy(this.getStrategyOptions(), cb);
36
- const passport = (0, util_1.getPassport)();
37
- if (name) {
38
- passport.use(name, this.strategy);
39
- }
40
- else {
41
- passport.use(this.strategy);
42
- }
43
- if (this['serializeUser']) {
44
- passport.serializeUser(this['serializeUser']);
45
- }
46
- if (this['deserializeUser']) {
47
- passport.deserializeUser(this['deserializeUser']);
48
- }
49
- if (this['transformAuthInfo']) {
50
- passport.transformAuthInfo(this['transformAuthInfo']);
51
- }
52
- }
53
- getStrategy() {
54
- return this.strategy;
55
- }
56
- }
57
- __decorate([
58
- (0, decorator_1.Init)(),
59
- __metadata("design:type", Function),
60
- __metadata("design:paramtypes", []),
61
- __metadata("design:returntype", Promise)
62
- ], InnerStrategyAbstractClass.prototype, "init", null);
63
- return InnerStrategyAbstractClass;
64
- }
65
- exports.PassportStrategy = PassportStrategy;
66
- function PassportMiddleware(strategy) {
67
- class InnerPassportMiddleware extends interface_1.AbstractPassportMiddleware {
68
- resolve() {
69
- if ((0, util_1.isExpressMode)()) {
70
- return async function passportMiddleware(req, res, next) {
71
- return this.authenticate(await this.getAuthenticateOptions())(req, res, next);
72
- }.bind(this);
73
- }
74
- else {
75
- return async function passportMiddleware(ctx, next) {
76
- return this.authenticate(await this.getAuthenticateOptions())(ctx, next);
77
- }.bind(this);
78
- }
79
- }
80
- getAuthenticateOptions() {
81
- return undefined;
82
- }
83
- authenticate(options) {
84
- if (!Array.isArray(strategy)) {
85
- strategy = [strategy];
86
- }
87
- if ((0, util_1.isExpressMode)()) {
88
- return async (req, res, next) => {
89
- // merge options with default options
90
- const authOptions = {
91
- ...this.passportConfig,
92
- ...options,
93
- };
94
- if (authOptions.session && req.session[authOptions.userProperty]) {
95
- req[authOptions.userProperty] =
96
- req.session[authOptions.userProperty];
97
- }
98
- // ignore user has exists
99
- if (req[authOptions.userProperty]) {
100
- next();
101
- }
102
- else {
103
- const passport = (0, util_1.getPassport)();
104
- const strategyList = [];
105
- for (const strategySingle of strategy) {
106
- // got strategy
107
- const strategyInstance = await this.app
108
- .getApplicationContext()
109
- .getAsync(strategySingle);
110
- strategyList.push(strategyInstance.getStrategy());
111
- }
112
- const user = await new Promise((resolve, reject) => {
113
- // authenticate
114
- passport.authenticate(strategyList, authOptions, (err, user, info, status) => {
115
- if (err) {
116
- reject(err);
117
- }
118
- else {
119
- resolve(user);
120
- }
121
- })(req, res, err => (err ? reject(err) : resolve(0)));
122
- });
123
- if (user) {
124
- req[authOptions.userProperty] = user;
125
- if (authOptions.session) {
126
- req.logIn(user, options, next);
127
- return;
128
- }
129
- }
130
- else {
131
- if (options.failureRedirect) {
132
- res.redirect(options.failureRedirect);
133
- return;
134
- }
135
- else {
136
- throw new core_1.httpError.UnauthorizedError();
137
- }
138
- }
139
- next();
140
- }
141
- };
142
- }
143
- else {
144
- return async function bbb(ctx, next) {
145
- // merge options with default options
146
- const authOptions = {
147
- ...this.passportConfig,
148
- ...options,
149
- };
150
- if (authOptions.session &&
151
- ctx.session.passport &&
152
- ctx.session.passport[authOptions.userProperty]) {
153
- ctx.state[authOptions.userProperty] =
154
- ctx.session.passport[authOptions.userProperty];
155
- }
156
- // ignore user has exists
157
- if (ctx.state[authOptions.userProperty]) {
158
- await next();
159
- }
160
- else {
161
- const passport = (0, util_1.getPassport)();
162
- const strategyList = [];
163
- for (const strategySingle of strategy) {
164
- // got strategy
165
- const strategyInstance = await this.app
166
- .getApplicationContext()
167
- .getAsync(strategySingle);
168
- strategyList.push(strategyInstance.getStrategy());
169
- }
170
- try {
171
- const user = await new Promise((resolve, reject) => {
172
- // authenticate
173
- passport.authenticate(strategyList, authOptions, (err, user, info, status) => {
174
- if (err) {
175
- reject(err);
176
- }
177
- else {
178
- resolve(user);
179
- }
180
- })(ctx, err => (err ? reject(err) : resolve(0)));
181
- });
182
- if (user) {
183
- ctx.state[authOptions.userProperty] = user;
184
- if (authOptions.session) {
185
- // save to ctx.session.passport
186
- await ctx.login(user, options);
187
- }
188
- if (options.successRedirect) {
189
- ctx.redirect(options.successRedirect);
190
- return;
191
- }
192
- }
193
- else {
194
- if (options.failureRedirect) {
195
- ctx.redirect(options.failureRedirect);
196
- return;
197
- }
198
- else {
199
- throw new core_1.httpError.UnauthorizedError();
200
- }
201
- }
202
- await next();
203
- }
204
- catch (err) {
205
- ctx.throw(err);
206
- }
207
- }
208
- }.bind(this);
209
- }
210
- }
211
- static getName() {
212
- return 'passport';
213
- }
214
- }
215
- __decorate([
216
- (0, decorator_1.Config)('passport'),
217
- __metadata("design:type", Object)
218
- ], InnerPassportMiddleware.prototype, "passportConfig", void 0);
219
- __decorate([
220
- (0, decorator_1.App)(),
221
- __metadata("design:type", Object)
222
- ], InnerPassportMiddleware.prototype, "app", void 0);
223
- return InnerPassportMiddleware;
224
- }
225
- exports.PassportMiddleware = PassportMiddleware;
226
- //# sourceMappingURL=passport.service.js.map
package/dist/util.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export declare function getPassport(): any;
2
- export declare function isExpressMode(): boolean;
3
- //# sourceMappingURL=util.d.ts.map
package/dist/util.js DELETED
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isExpressMode = exports.getPassport = void 0;
4
- const core_1 = require("@midwayjs/core");
5
- function getPassport() {
6
- return isExpressMode()
7
- ? require('passport')
8
- : require('./proxy/index').passport;
9
- }
10
- exports.getPassport = getPassport;
11
- function isExpressMode() {
12
- return !!(process.env['MIDWAY_PASSPORT_MODE'] === 'express' ||
13
- (0, core_1.safeRequire)('@midwayjs/express'));
14
- }
15
- exports.isExpressMode = isExpressMode;
16
- //# sourceMappingURL=util.js.map