@midwayjs/passport 3.4.0-beta.1 → 3.4.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.
@@ -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