@midwayjs/passport 2.14.1 → 3.0.0-beta.12
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 +36 -7
- package/README.md +5 -5
- package/dist/config/config.default.d.ts +1 -1
- package/dist/config/config.default.js +2 -2
- package/dist/configuration.d.ts +5 -3
- package/dist/configuration.js +24 -3
- package/dist/proxy/framework/koa.js +4 -0
- package/dist/service/passport.d.ts +1 -8
- package/dist/service/passport.js +151 -178
- package/index.d.ts +57 -0
- package/package.json +13 -11
package/CHANGELOG.md
CHANGED
|
@@ -3,26 +3,55 @@
|
|
|
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
|
-
|
|
6
|
+
# [3.0.0-beta.12](https://github.com/midwayjs/midway/compare/v3.0.0-beta.11...v3.0.0-beta.12) (2021-12-28)
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
###
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* support throw err status ([#1440](https://github.com/midwayjs/midway/issues/1440)) ([7b98110](https://github.com/midwayjs/midway/commit/7b98110d65c5287a8fcb3eb5356dea2d7a32cee9))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# [3.0.0-beta.11](https://github.com/midwayjs/midway/compare/v3.0.0-beta.10...v3.0.0-beta.11) (2021-12-21)
|
|
18
|
+
|
|
19
|
+
**Note:** Version bump only for package @midwayjs/passport
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# [3.0.0-beta.10](https://github.com/midwayjs/midway/compare/v3.0.0-beta.9...v3.0.0-beta.10) (2021-12-20)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Features
|
|
29
|
+
|
|
30
|
+
* default add session & bodyparser support for koa/express/faas ([#1420](https://github.com/midwayjs/midway/issues/1420)) ([cdaff31](https://github.com/midwayjs/midway/commit/cdaff317c3e862a95494a167995a28280af639bf))
|
|
31
|
+
* implement i18n for validate ([#1426](https://github.com/midwayjs/midway/issues/1426)) ([4c7ed2f](https://github.com/midwayjs/midway/commit/4c7ed2ff2e7ccf10f88f62abad230f92f5e76b97))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# [3.0.0-beta.9](https://github.com/midwayjs/midway/compare/v3.0.0-beta.8...v3.0.0-beta.9) (2021-12-09)
|
|
10
38
|
|
|
11
|
-
|
|
39
|
+
**Note:** Version bump only for package @midwayjs/passport
|
|
12
40
|
|
|
13
41
|
|
|
14
42
|
|
|
15
43
|
|
|
16
44
|
|
|
17
|
-
# [
|
|
45
|
+
# [3.0.0-beta.8](https://github.com/midwayjs/midway/compare/v3.0.0-beta.7...v3.0.0-beta.8) (2021-12-08)
|
|
18
46
|
|
|
19
47
|
|
|
20
48
|
### Bug Fixes
|
|
21
49
|
|
|
22
|
-
* express routing middleware takes effect at the controller level ([#1364](https://github.com/midwayjs/midway/issues/1364)) ([
|
|
50
|
+
* express routing middleware takes effect at the controller level ([#1364](https://github.com/midwayjs/midway/issues/1364)) ([b9272e0](https://github.com/midwayjs/midway/commit/b9272e0971003443304b0c53815be31a0061b4bd))
|
|
51
|
+
* passport missing proxy file ([#1405](https://github.com/midwayjs/midway/issues/1405)) ([5c9bdae](https://github.com/midwayjs/midway/commit/5c9bdae8323b41ead72c3d3f867aa11150bb3e78))
|
|
23
52
|
|
|
24
53
|
|
|
25
54
|
### Features
|
|
26
55
|
|
|
27
|
-
* passport add presetProperty ([#1358](https://github.com/midwayjs/midway/issues/1358)) ([
|
|
28
|
-
* support passport and jwt ([#1343](https://github.com/midwayjs/midway/issues/1343)) ([
|
|
56
|
+
* passport add presetProperty ([#1358](https://github.com/midwayjs/midway/issues/1358)) ([4db8eda](https://github.com/midwayjs/midway/commit/4db8eda592c0486898edabcacbc9a69eb2d87004))
|
|
57
|
+
* support passport and jwt ([#1343](https://github.com/midwayjs/midway/issues/1343)) ([f26af4f](https://github.com/midwayjs/midway/commit/f26af4f3e16507d6f3ffe0467f8f5be69e6306d7))
|
package/README.md
CHANGED
|
@@ -87,7 +87,7 @@ import { Inject, Provide } from '@midwayjs/decorator';
|
|
|
87
87
|
import { PassportMiddleware } from '@midwayjs/passport';
|
|
88
88
|
import { Context } from '@midwayjs/express';
|
|
89
89
|
|
|
90
|
-
@Provide(
|
|
90
|
+
@Provide()
|
|
91
91
|
export class LocalPassportMiddleware extends PassportMiddleware(LocalStrategy) {
|
|
92
92
|
// 设置 AuthenticateOptions
|
|
93
93
|
getAuthenticateOptions(): Promise<passport.AuthenticateOptions> | passport.AuthenticateOptions {
|
|
@@ -107,7 +107,7 @@ import { Provide, Post, Inject, Controller } from '@midwayjs/decorator';
|
|
|
107
107
|
@Controller('/')
|
|
108
108
|
export class LocalController {
|
|
109
109
|
|
|
110
|
-
@Post('/passport/local', { middleware: [
|
|
110
|
+
@Post('/passport/local', { middleware: [LocalPassportMiddleware] })
|
|
111
111
|
async localPassport() {
|
|
112
112
|
console.log('local user: ', this.ctx.req.user);
|
|
113
113
|
return this.ctx.req.user;
|
|
@@ -186,7 +186,7 @@ export class JwtController {
|
|
|
186
186
|
@Inject()
|
|
187
187
|
ctx: any;
|
|
188
188
|
|
|
189
|
-
@Post('/passport/jwt', { middleware: [
|
|
189
|
+
@Post('/passport/jwt', { middleware: [JwtPassportMiddleware] })
|
|
190
190
|
async jwtPassport() {
|
|
191
191
|
console.log('jwt user: ', this.ctx.req.user);
|
|
192
192
|
return this.ctx.req.user;
|
|
@@ -259,10 +259,10 @@ export class AuthController {
|
|
|
259
259
|
@Inject()
|
|
260
260
|
ctx: any;
|
|
261
261
|
|
|
262
|
-
@Get('/github', { middleware: [
|
|
262
|
+
@Get('/github', { middleware: [GithubPassportMiddleware] })
|
|
263
263
|
async githubOAuth() {}
|
|
264
264
|
|
|
265
|
-
@Get('/github/cb', { middleware: [
|
|
265
|
+
@Get('/github/cb', { middleware: [GithubPassportMiddleware] })
|
|
266
266
|
async githubOAuthCallback() {
|
|
267
267
|
return this.ctx.req.user;
|
|
268
268
|
}
|
package/dist/configuration.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class PassportConfiguration
|
|
3
|
-
|
|
1
|
+
import { IMidwayContainer, MidwayApplicationManager, MidwayConfigService } from '@midwayjs/core';
|
|
2
|
+
export declare class PassportConfiguration {
|
|
3
|
+
applicationManager: MidwayApplicationManager;
|
|
4
|
+
configService: MidwayConfigService;
|
|
5
|
+
onReady(container: IMidwayContainer): Promise<void>;
|
|
4
6
|
}
|
|
5
7
|
//# sourceMappingURL=configuration.d.ts.map
|
package/dist/configuration.js
CHANGED
|
@@ -5,16 +5,37 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
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
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
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
|
+
};
|
|
8
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
12
|
exports.PassportConfiguration = void 0;
|
|
10
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
11
14
|
const DefaultConfig = require("./config/config.default");
|
|
12
|
-
const
|
|
15
|
+
const util_1 = require("./util");
|
|
16
|
+
const core_1 = require("@midwayjs/core");
|
|
13
17
|
let PassportConfiguration = class PassportConfiguration {
|
|
14
|
-
async onReady(container
|
|
15
|
-
|
|
18
|
+
async onReady(container) {
|
|
19
|
+
const passportConfig = this.configService.getConfiguration('passport');
|
|
20
|
+
const passport = (0, util_1.getPassport)();
|
|
21
|
+
this.applicationManager
|
|
22
|
+
.getApplications(['express', 'koa', 'egg', 'faas'])
|
|
23
|
+
.forEach(app => {
|
|
24
|
+
app.useMiddleware(passport.initialize());
|
|
25
|
+
if (passportConfig.session) {
|
|
26
|
+
app.useMiddleware(passport.session());
|
|
27
|
+
}
|
|
28
|
+
});
|
|
16
29
|
}
|
|
17
30
|
};
|
|
31
|
+
__decorate([
|
|
32
|
+
(0, decorator_1.Inject)(),
|
|
33
|
+
__metadata("design:type", core_1.MidwayApplicationManager)
|
|
34
|
+
], PassportConfiguration.prototype, "applicationManager", void 0);
|
|
35
|
+
__decorate([
|
|
36
|
+
(0, decorator_1.Inject)(),
|
|
37
|
+
__metadata("design:type", core_1.MidwayConfigService)
|
|
38
|
+
], PassportConfiguration.prototype, "configService", void 0);
|
|
18
39
|
PassportConfiguration = __decorate([
|
|
19
40
|
(0, decorator_1.Configuration)({
|
|
20
41
|
namespace: 'passport',
|
|
@@ -39,6 +39,10 @@ function initialize(passport) {
|
|
|
39
39
|
const login = req.login;
|
|
40
40
|
ctx.login = ctx.logIn = function (user, options) {
|
|
41
41
|
return new Promise((resolve, reject) => {
|
|
42
|
+
// fix session manager missing
|
|
43
|
+
if (!req._sessionManager) {
|
|
44
|
+
req._sessionManager = passport._sm;
|
|
45
|
+
}
|
|
42
46
|
login.call(req, user, options, err => {
|
|
43
47
|
if (err)
|
|
44
48
|
reject(err);
|
|
@@ -2,12 +2,5 @@ import * as passport from 'passport';
|
|
|
2
2
|
import { AbstractPassportMiddleware, AbstractStrategy } from '../interface';
|
|
3
3
|
export declare function PassportStrategy(Strategy: new (...args: any[]) => passport.Strategy, name?: string): new (...args: any[]) => AbstractStrategy;
|
|
4
4
|
export declare type StrategyClass = new (...args: any[]) => AbstractStrategy;
|
|
5
|
-
export declare function PassportMiddleware(
|
|
6
|
-
export declare class PassportService {
|
|
7
|
-
passportConfig: any;
|
|
8
|
-
app: any;
|
|
9
|
-
inited: boolean;
|
|
10
|
-
initMiddleware(): void;
|
|
11
|
-
authenticate(strategy: StrategyClass | StrategyClass[], options: passport.AuthenticateOptions): any;
|
|
12
|
-
}
|
|
5
|
+
export declare function PassportMiddleware(strategy: StrategyClass | StrategyClass[]): new (...args: any[]) => AbstractPassportMiddleware;
|
|
13
6
|
//# sourceMappingURL=passport.d.ts.map
|
package/dist/service/passport.js
CHANGED
|
@@ -9,192 +9,105 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.PassportMiddleware = exports.PassportStrategy = void 0;
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
14
|
const util_1 = require("../util");
|
|
15
15
|
const interface_1 = require("../interface");
|
|
16
16
|
function PassportStrategy(Strategy, name) {
|
|
17
17
|
class InnerStrategyAbstractClass extends interface_1.AbstractStrategy {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const cb = async (...params) => {
|
|
26
|
-
const done = params[params.length - 1];
|
|
27
|
-
try {
|
|
28
|
-
const result = await this.validate(...params);
|
|
29
|
-
if (Array.isArray(result)) {
|
|
30
|
-
done(null, ...result);
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
done(null, result);
|
|
34
|
-
}
|
|
18
|
+
async init() {
|
|
19
|
+
const cb = async (...params) => {
|
|
20
|
+
const done = params[params.length - 1];
|
|
21
|
+
try {
|
|
22
|
+
const result = await this.validate(...params);
|
|
23
|
+
if (Array.isArray(result)) {
|
|
24
|
+
done(null, ...result);
|
|
35
25
|
}
|
|
36
|
-
|
|
37
|
-
done(
|
|
26
|
+
else {
|
|
27
|
+
done(null, result);
|
|
38
28
|
}
|
|
39
|
-
};
|
|
40
|
-
this.strategy = new Strategy(this.getStrategyOptions(), cb);
|
|
41
|
-
const passport = (0, util_1.getPassport)();
|
|
42
|
-
if (name) {
|
|
43
|
-
passport.use(name, this.strategy);
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
passport.use(this.strategy);
|
|
47
|
-
}
|
|
48
|
-
if (this['serializeUser']) {
|
|
49
|
-
passport.serializeUser(this['serializeUser']);
|
|
50
29
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
if (this['transformAuthInfo']) {
|
|
55
|
-
passport.transformAuthInfo(this['transformAuthInfo']);
|
|
30
|
+
catch (err) {
|
|
31
|
+
done(err, null);
|
|
56
32
|
}
|
|
33
|
+
};
|
|
34
|
+
this.strategy = new Strategy(this.getStrategyOptions(), cb);
|
|
35
|
+
const passport = (0, util_1.getPassport)();
|
|
36
|
+
if (name) {
|
|
37
|
+
passport.use(name, this.strategy);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
passport.use(this.strategy);
|
|
41
|
+
}
|
|
42
|
+
if (this['serializeUser']) {
|
|
43
|
+
passport.serializeUser(this['serializeUser']);
|
|
44
|
+
}
|
|
45
|
+
if (this['deserializeUser']) {
|
|
46
|
+
passport.deserializeUser(this['deserializeUser']);
|
|
47
|
+
}
|
|
48
|
+
if (this['transformAuthInfo']) {
|
|
49
|
+
passport.transformAuthInfo(this['transformAuthInfo']);
|
|
57
50
|
}
|
|
58
51
|
}
|
|
59
52
|
getStrategy() {
|
|
60
53
|
return this.strategy;
|
|
61
54
|
}
|
|
62
55
|
}
|
|
56
|
+
__decorate([
|
|
57
|
+
(0, decorator_1.Init)(),
|
|
58
|
+
__metadata("design:type", Function),
|
|
59
|
+
__metadata("design:paramtypes", []),
|
|
60
|
+
__metadata("design:returntype", Promise)
|
|
61
|
+
], InnerStrategyAbstractClass.prototype, "init", null);
|
|
63
62
|
return InnerStrategyAbstractClass;
|
|
64
63
|
}
|
|
65
64
|
exports.PassportStrategy = PassportStrategy;
|
|
66
|
-
function PassportMiddleware(
|
|
65
|
+
function PassportMiddleware(strategy) {
|
|
67
66
|
class InnerPassportMiddleware extends interface_1.AbstractPassportMiddleware {
|
|
68
|
-
authenticate(options) {
|
|
69
|
-
return this.passportService.authenticate(type, options);
|
|
70
|
-
}
|
|
71
67
|
resolve() {
|
|
72
|
-
this.passportService.initMiddleware();
|
|
73
68
|
if ((0, util_1.isExpressMode)()) {
|
|
74
|
-
return async (req, res, next)
|
|
69
|
+
return async function passportMiddleware(req, res, next) {
|
|
75
70
|
return this.authenticate(await this.getAuthenticateOptions())(req, res, next);
|
|
76
|
-
};
|
|
71
|
+
}.bind(this);
|
|
77
72
|
}
|
|
78
73
|
else {
|
|
79
|
-
return async (ctx, next)
|
|
74
|
+
return async function passportMiddleware(ctx, next) {
|
|
80
75
|
return this.authenticate(await this.getAuthenticateOptions())(ctx, next);
|
|
81
|
-
};
|
|
76
|
+
}.bind(this);
|
|
82
77
|
}
|
|
83
78
|
}
|
|
84
79
|
getAuthenticateOptions() {
|
|
85
80
|
return undefined;
|
|
86
81
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
__metadata("design:type", PassportService)
|
|
91
|
-
], InnerPassportMiddleware.prototype, "passportService", void 0);
|
|
92
|
-
return InnerPassportMiddleware;
|
|
93
|
-
}
|
|
94
|
-
exports.PassportMiddleware = PassportMiddleware;
|
|
95
|
-
let PassportService = class PassportService {
|
|
96
|
-
constructor() {
|
|
97
|
-
this.inited = false;
|
|
98
|
-
}
|
|
99
|
-
initMiddleware() {
|
|
100
|
-
if (!this.inited) {
|
|
101
|
-
this.inited = true;
|
|
102
|
-
const passport = (0, util_1.getPassport)();
|
|
103
|
-
this.app.use(passport.initialize());
|
|
104
|
-
if (this.passportConfig.session) {
|
|
105
|
-
this.app.use(passport.session());
|
|
82
|
+
authenticate(options) {
|
|
83
|
+
if (!Array.isArray(strategy)) {
|
|
84
|
+
strategy = [strategy];
|
|
106
85
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
...this.passportConfig,
|
|
118
|
-
...options,
|
|
119
|
-
};
|
|
120
|
-
if (authOptions.session && req.session[authOptions.property]) {
|
|
121
|
-
req[authOptions.property] = req.session[authOptions.property];
|
|
122
|
-
}
|
|
123
|
-
// ignore user has exists
|
|
124
|
-
if (req[authOptions.property]) {
|
|
125
|
-
next();
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
const passport = (0, util_1.getPassport)();
|
|
129
|
-
const strategyList = [];
|
|
130
|
-
for (const strategySingle of strategy) {
|
|
131
|
-
// got strategy
|
|
132
|
-
const strategyInstance = await this.app
|
|
133
|
-
.getApplicationContext()
|
|
134
|
-
.getAsync(strategySingle);
|
|
135
|
-
await strategyInstance.initStrategy();
|
|
136
|
-
strategyList.push(strategyInstance.getStrategy());
|
|
86
|
+
if ((0, util_1.isExpressMode)()) {
|
|
87
|
+
return async (req, res, next) => {
|
|
88
|
+
// merge options with default options
|
|
89
|
+
const authOptions = {
|
|
90
|
+
...this.passportConfig,
|
|
91
|
+
...options,
|
|
92
|
+
};
|
|
93
|
+
if (authOptions.session && req.session[authOptions.userProperty]) {
|
|
94
|
+
req[authOptions.userProperty] =
|
|
95
|
+
req.session[authOptions.userProperty];
|
|
137
96
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (err) {
|
|
142
|
-
reject(err);
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
resolve(user);
|
|
146
|
-
}
|
|
147
|
-
})(req, res, err => (err ? reject(err) : resolve(0)));
|
|
148
|
-
});
|
|
149
|
-
if (user) {
|
|
150
|
-
req[authOptions.property] = user;
|
|
151
|
-
if (authOptions.session) {
|
|
152
|
-
req.logIn(user, options, next);
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
97
|
+
// ignore user has exists
|
|
98
|
+
if (req[authOptions.userProperty]) {
|
|
99
|
+
next();
|
|
155
100
|
}
|
|
156
101
|
else {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
102
|
+
const passport = (0, util_1.getPassport)();
|
|
103
|
+
const strategyList = [];
|
|
104
|
+
for (const strategySingle of strategy) {
|
|
105
|
+
// got strategy
|
|
106
|
+
const strategyInstance = await this.app
|
|
107
|
+
.getApplicationContext()
|
|
108
|
+
.getAsync(strategySingle);
|
|
109
|
+
strategyList.push(strategyInstance.getStrategy());
|
|
160
110
|
}
|
|
161
|
-
else {
|
|
162
|
-
res.status(401);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
next();
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
return async (ctx, next) => {
|
|
171
|
-
// merge options with default options
|
|
172
|
-
const authOptions = {
|
|
173
|
-
...this.passportConfig,
|
|
174
|
-
...options,
|
|
175
|
-
};
|
|
176
|
-
if (authOptions.session &&
|
|
177
|
-
ctx.session.passport &&
|
|
178
|
-
ctx.session.passport[authOptions.property]) {
|
|
179
|
-
ctx.state[authOptions.property] =
|
|
180
|
-
ctx.session.passport[authOptions.property];
|
|
181
|
-
}
|
|
182
|
-
// ignore user has exists
|
|
183
|
-
if (ctx.state[authOptions.property]) {
|
|
184
|
-
await next();
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
const passport = (0, util_1.getPassport)();
|
|
188
|
-
const strategyList = [];
|
|
189
|
-
for (const strategySingle of strategy) {
|
|
190
|
-
// got strategy
|
|
191
|
-
const strategyInstance = await this.app
|
|
192
|
-
.getApplicationContext()
|
|
193
|
-
.getAsync(strategySingle);
|
|
194
|
-
await strategyInstance.initStrategy();
|
|
195
|
-
strategyList.push(strategyInstance.getStrategy());
|
|
196
|
-
}
|
|
197
|
-
try {
|
|
198
111
|
const user = await new Promise((resolve, reject) => {
|
|
199
112
|
// authenticate
|
|
200
113
|
passport.authenticate(strategyList, authOptions, (err, user, info, status) => {
|
|
@@ -204,49 +117,109 @@ let PassportService = class PassportService {
|
|
|
204
117
|
else {
|
|
205
118
|
resolve(user);
|
|
206
119
|
}
|
|
207
|
-
})(
|
|
120
|
+
})(req, res, err => (err ? reject(err) : resolve(0)));
|
|
208
121
|
});
|
|
209
122
|
if (user) {
|
|
210
|
-
|
|
123
|
+
req[authOptions.userProperty] = user;
|
|
211
124
|
if (authOptions.session) {
|
|
212
|
-
|
|
213
|
-
await ctx.login(user, options);
|
|
214
|
-
}
|
|
215
|
-
if (options.successRedirect) {
|
|
216
|
-
ctx.redirect(options.successRedirect);
|
|
125
|
+
req.logIn(user, options, next);
|
|
217
126
|
return;
|
|
218
127
|
}
|
|
219
128
|
}
|
|
220
129
|
else {
|
|
221
130
|
if (options.failureRedirect) {
|
|
222
|
-
|
|
131
|
+
res.redirect(options.failureRedirect);
|
|
223
132
|
return;
|
|
224
133
|
}
|
|
225
134
|
else {
|
|
226
|
-
|
|
135
|
+
res.status(401);
|
|
227
136
|
}
|
|
228
137
|
}
|
|
138
|
+
next();
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
return async function bbb(ctx, next) {
|
|
144
|
+
// merge options with default options
|
|
145
|
+
const authOptions = {
|
|
146
|
+
...this.passportConfig,
|
|
147
|
+
...options,
|
|
148
|
+
};
|
|
149
|
+
if (authOptions.session &&
|
|
150
|
+
ctx.session.passport &&
|
|
151
|
+
ctx.session.passport[authOptions.userProperty]) {
|
|
152
|
+
ctx.state[authOptions.userProperty] =
|
|
153
|
+
ctx.session.passport[authOptions.userProperty];
|
|
154
|
+
}
|
|
155
|
+
// ignore user has exists
|
|
156
|
+
if (ctx.state[authOptions.userProperty]) {
|
|
229
157
|
await next();
|
|
230
158
|
}
|
|
231
|
-
|
|
232
|
-
|
|
159
|
+
else {
|
|
160
|
+
const passport = (0, util_1.getPassport)();
|
|
161
|
+
const strategyList = [];
|
|
162
|
+
for (const strategySingle of strategy) {
|
|
163
|
+
// got strategy
|
|
164
|
+
const strategyInstance = await this.app
|
|
165
|
+
.getApplicationContext()
|
|
166
|
+
.getAsync(strategySingle);
|
|
167
|
+
strategyList.push(strategyInstance.getStrategy());
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
const user = await new Promise((resolve, reject) => {
|
|
171
|
+
// authenticate
|
|
172
|
+
passport.authenticate(strategyList, authOptions, (err, user, info, status) => {
|
|
173
|
+
if (err) {
|
|
174
|
+
reject(err);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
resolve(user);
|
|
178
|
+
}
|
|
179
|
+
})(ctx, err => (err ? reject(err) : resolve(0)));
|
|
180
|
+
});
|
|
181
|
+
if (user) {
|
|
182
|
+
ctx.state[authOptions.userProperty] = user;
|
|
183
|
+
if (authOptions.session) {
|
|
184
|
+
// save to ctx.session.passport
|
|
185
|
+
await ctx.login(user, options);
|
|
186
|
+
}
|
|
187
|
+
if (options.successRedirect) {
|
|
188
|
+
ctx.redirect(options.successRedirect);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
if (options.failureRedirect) {
|
|
194
|
+
ctx.redirect(options.failureRedirect);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
ctx.status = 401;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
await next();
|
|
202
|
+
}
|
|
203
|
+
catch (err) {
|
|
204
|
+
ctx.throw(err);
|
|
205
|
+
}
|
|
233
206
|
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
207
|
+
}.bind(this);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
static getName() {
|
|
211
|
+
return 'passport';
|
|
236
212
|
}
|
|
237
213
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
|
|
250
|
-
], PassportService);
|
|
251
|
-
exports.PassportService = PassportService;
|
|
214
|
+
__decorate([
|
|
215
|
+
(0, decorator_1.Config)('passport'),
|
|
216
|
+
__metadata("design:type", Object)
|
|
217
|
+
], InnerPassportMiddleware.prototype, "passportConfig", void 0);
|
|
218
|
+
__decorate([
|
|
219
|
+
(0, decorator_1.App)(),
|
|
220
|
+
__metadata("design:type", Object)
|
|
221
|
+
], InnerPassportMiddleware.prototype, "app", void 0);
|
|
222
|
+
return InnerPassportMiddleware;
|
|
223
|
+
}
|
|
224
|
+
exports.PassportMiddleware = PassportMiddleware;
|
|
252
225
|
//# sourceMappingURL=passport.js.map
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as passport from 'passport';
|
|
2
|
+
|
|
3
|
+
export * from './dist/index';
|
|
4
|
+
|
|
5
|
+
declare module '@midwayjs/core/dist/interface' {
|
|
6
|
+
interface MidwayConfig {
|
|
7
|
+
passport?: passport.AuthenticateOptions;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare module '@midwayjs/koa/dist/interface' {
|
|
12
|
+
interface Context {
|
|
13
|
+
state: {
|
|
14
|
+
user?: any;
|
|
15
|
+
};
|
|
16
|
+
isAuthenticated(): boolean;
|
|
17
|
+
isUnauthenticated(): boolean;
|
|
18
|
+
login(): Promise<void>;
|
|
19
|
+
logout(): void;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare module '@midwayjs/web/dist/interface' {
|
|
24
|
+
interface Context {
|
|
25
|
+
state: {
|
|
26
|
+
user?: any;
|
|
27
|
+
};
|
|
28
|
+
isAuthenticated(): boolean;
|
|
29
|
+
isUnauthenticated(): boolean;
|
|
30
|
+
login(): Promise<void>;
|
|
31
|
+
logout(): void;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare module '@midwayjs/faas/dist/interface' {
|
|
36
|
+
interface Context {
|
|
37
|
+
state: {
|
|
38
|
+
user?: any;
|
|
39
|
+
};
|
|
40
|
+
isAuthenticated(): boolean;
|
|
41
|
+
isUnauthenticated(): boolean;
|
|
42
|
+
login(): Promise<void>;
|
|
43
|
+
logout(): void;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare module '@midwayjs/express/dist/interface' {
|
|
48
|
+
interface Context {
|
|
49
|
+
user?: any;
|
|
50
|
+
// These declarations are merged into express's Request type
|
|
51
|
+
login(user: any, done: (err: any) => void): void;
|
|
52
|
+
login(user: any, options: any, done: (err: any) => void): void;
|
|
53
|
+
logout(): void;
|
|
54
|
+
isAuthenticated(): boolean;
|
|
55
|
+
isUnauthenticated(): boolean;
|
|
56
|
+
}
|
|
57
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/passport",
|
|
3
3
|
"description": "midway passport component",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.0-beta.12",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
-
"typings": "
|
|
6
|
+
"typings": "index.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist/**/*.js",
|
|
9
|
-
"dist/**/*.d.ts"
|
|
9
|
+
"dist/**/*.d.ts",
|
|
10
|
+
"index.d.ts"
|
|
10
11
|
],
|
|
11
12
|
"scripts": {
|
|
12
13
|
"build": "tsc",
|
|
@@ -21,21 +22,22 @@
|
|
|
21
22
|
"author": "Nawbc",
|
|
22
23
|
"license": "MIT",
|
|
23
24
|
"devDependencies": {
|
|
24
|
-
"@midwayjs/core": "^
|
|
25
|
-
"@midwayjs/decorator": "^
|
|
26
|
-
"@midwayjs/express": "^
|
|
27
|
-
"@midwayjs/jwt": "^
|
|
28
|
-
"@midwayjs/koa": "^
|
|
29
|
-
"@midwayjs/mock": "^
|
|
30
|
-
"@midwayjs/web": "^
|
|
25
|
+
"@midwayjs/core": "^3.0.0-beta.12",
|
|
26
|
+
"@midwayjs/decorator": "^3.0.0-beta.12",
|
|
27
|
+
"@midwayjs/express": "^3.0.0-beta.12",
|
|
28
|
+
"@midwayjs/jwt": "^3.0.0-beta.12",
|
|
29
|
+
"@midwayjs/koa": "^3.0.0-beta.12",
|
|
30
|
+
"@midwayjs/mock": "^3.0.0-beta.12",
|
|
31
|
+
"@midwayjs/web": "^3.0.0-beta.12",
|
|
31
32
|
"@types/passport": "^1.0.7",
|
|
32
33
|
"@types/passport-local": "^1.0.34",
|
|
33
34
|
"express-session": "^1.17.2",
|
|
34
35
|
"passport": "^0.5.0",
|
|
36
|
+
"passport-jwt": "^4.0.0",
|
|
35
37
|
"passport-local": "^1.0.0"
|
|
36
38
|
},
|
|
37
39
|
"peerDependencies": {
|
|
38
40
|
"passport": "^0.5.0"
|
|
39
41
|
},
|
|
40
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "1c46e53eb934248007eeb7fe3920f5ac24e272c6"
|
|
41
43
|
}
|