@midwayjs/passport 3.4.1 → 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.
@@ -61,7 +61,7 @@ export declare class PassportAuthenticator {
61
61
  }>;
62
62
  };
63
63
  }>;
64
- serializeUser(fn: any, req?: any, done?: any): number;
64
+ serializeUser(user: any, req: any, done: any): void;
65
65
  /**
66
66
  * Registers a function used to deserialize user objects out of the session.
67
67
  *
@@ -75,7 +75,7 @@ export declare class PassportAuthenticator {
75
75
  *
76
76
  * @api public
77
77
  */
78
- deserializeUser(fn: any, req?: any, done?: any): number;
78
+ deserializeUser(obj: any, req: any, done: any): void;
79
79
  /**
80
80
  * Registers a function used to transform auth info.
81
81
  *
@@ -114,12 +114,15 @@ export declare class PassportAuthenticator {
114
114
  *
115
115
  * @api public
116
116
  */
117
- transformAuthInfo(fn: any, req?: any, done?: any): number;
117
+ transformAuthInfo(info: any, req: any, done: any): void;
118
118
  logInToSession(req: IncomingMessage & {
119
119
  session: any;
120
120
  }, user: any): Promise<void>;
121
121
  logOutFromSession(req: any, options?: {
122
122
  keepSessionInfo?: boolean;
123
123
  }): Promise<void>;
124
+ addSerializer(fn: any): void;
125
+ addDeserializer(fn: any): void;
126
+ addInfoTransformer(fn: any): void;
124
127
  }
125
128
  //# sourceMappingURL=authenticator.d.ts.map
@@ -198,18 +198,7 @@ let PassportAuthenticator = class PassportAuthenticator {
198
198
  };
199
199
  };
200
200
  }
201
- serializeUser(fn, req, done) {
202
- if (typeof fn === 'function') {
203
- return this._serializers.push(fn);
204
- }
205
- // private implementation that traverses the chain of serializers, attempting
206
- // to serialize a user
207
- const user = fn;
208
- // For backwards compatibility
209
- if (typeof req === 'function') {
210
- done = req;
211
- req = undefined;
212
- }
201
+ serializeUser(user, req, done) {
213
202
  const stack = this._serializers;
214
203
  (function pass(i, err, obj) {
215
204
  // serializers use 'pass' as an error to skip processing
@@ -254,18 +243,7 @@ let PassportAuthenticator = class PassportAuthenticator {
254
243
  *
255
244
  * @api public
256
245
  */
257
- deserializeUser(fn, req, done) {
258
- if (typeof fn === 'function') {
259
- return this._deserializers.push(fn);
260
- }
261
- // private implementation that traverses the chain of deserializers,
262
- // attempting to deserialize a user
263
- const obj = fn;
264
- // For backwards compatibility
265
- if (typeof req === 'function') {
266
- done = req;
267
- req = undefined;
268
- }
246
+ deserializeUser(obj, req, done) {
269
247
  const stack = this._deserializers;
270
248
  (function pass(i, err, user) {
271
249
  // deserializers use 'pass' as an error to skip processing
@@ -340,18 +318,7 @@ let PassportAuthenticator = class PassportAuthenticator {
340
318
  *
341
319
  * @api public
342
320
  */
343
- transformAuthInfo(fn, req, done) {
344
- if (typeof fn === 'function') {
345
- return this._infoTransformers.push(fn);
346
- }
347
- // private implementation that traverses the chain of transformers,
348
- // attempting to transform auth info
349
- const info = fn;
350
- // For backwards compatibility
351
- if (typeof req === 'function') {
352
- done = req;
353
- req = undefined;
354
- }
321
+ transformAuthInfo(info, req, done) {
355
322
  const stack = this._infoTransformers;
356
323
  (function pass(i, err, tinfo) {
357
324
  // transformers use 'pass' as an error to skip processing
@@ -440,6 +407,15 @@ let PassportAuthenticator = class PassportAuthenticator {
440
407
  });
441
408
  });
442
409
  }
410
+ addSerializer(fn) {
411
+ this._serializers.push(fn);
412
+ }
413
+ addDeserializer(fn) {
414
+ this._deserializers.push(fn);
415
+ }
416
+ addInfoTransformer(fn) {
417
+ this._infoTransformers.push(fn);
418
+ }
443
419
  };
444
420
  __decorate([
445
421
  (0, decorator_1.ApplicationContext)(),
@@ -42,13 +42,13 @@ function PassportStrategy(Strategy, name) {
42
42
  this.passport.use(this.strategy);
43
43
  }
44
44
  if (this['serializeUser']) {
45
- this.passport.serializeUser(this['serializeUser']);
45
+ this.passport.addSerializer(this['serializeUser']);
46
46
  }
47
47
  if (this['deserializeUser']) {
48
- this.passport.deserializeUser(this['deserializeUser']);
48
+ this.passport.addDeserializer(this['deserializeUser']);
49
49
  }
50
50
  if (this['transformAuthInfo']) {
51
- this.passport.transformAuthInfo(this['transformAuthInfo']);
51
+ this.passport.addInfoTransformer(this['transformAuthInfo']);
52
52
  }
53
53
  }
54
54
  getStrategy() {
@@ -99,7 +99,19 @@ function PassportMiddleware(strategy) {
99
99
  }
100
100
  // authenticate
101
101
  const authenticate = this.passport.authenticate(strategyList, authOptions);
102
- const authenticateResult = await authenticate(req);
102
+ let authenticateResult;
103
+ try {
104
+ authenticateResult = await authenticate(req);
105
+ }
106
+ catch (err) {
107
+ if (err instanceof core_1.MidwayHttpError) {
108
+ throw err;
109
+ }
110
+ else {
111
+ // 如果验证流程里有错误,抛出一个 500 错误
112
+ throw new core_1.httpError.InternalServerErrorError(err);
113
+ }
114
+ }
103
115
  // success
104
116
  if (authenticateResult.successResult) {
105
117
  const breakNext = await this.onceSucceed(options, authenticateResult.successResult.user, authenticateResult.successResult.info, req, res);
@@ -166,7 +178,19 @@ function PassportMiddleware(strategy) {
166
178
  }
167
179
  // authenticate
168
180
  const authenticate = this.passport.authenticate(strategyList, authOptions);
169
- const authenticateResult = await authenticate(req);
181
+ let authenticateResult;
182
+ try {
183
+ authenticateResult = await authenticate(req);
184
+ }
185
+ catch (err) {
186
+ if (err instanceof core_1.MidwayHttpError) {
187
+ throw err;
188
+ }
189
+ else {
190
+ // 如果验证流程里有错误,抛出一个 500 错误
191
+ throw new core_1.httpError.InternalServerErrorError(err);
192
+ }
193
+ }
170
194
  // success
171
195
  if (authenticateResult.successResult) {
172
196
  const breakNext = await this.onceSucceed(options, authenticateResult.successResult.user, authenticateResult.successResult.info, req, ctx);
@@ -29,6 +29,6 @@ export declare class SessionStrategy extends Strategy {
29
29
  * @param {Object} options
30
30
  * @api protected
31
31
  */
32
- authenticate(req: any, options: any): import("@midwayjs/core/dist/error/http").UnauthorizedError;
32
+ authenticate(req: any, options: any): void;
33
33
  }
34
34
  //# sourceMappingURL=session.stratey.d.ts.map
@@ -31,7 +31,7 @@ class SessionStrategy extends strategy_1.Strategy {
31
31
  */
32
32
  authenticate(req, options) {
33
33
  if (!req.session) {
34
- return new core_1.httpError.UnauthorizedError('Login sessions require session support,please enable it.');
34
+ throw new core_1.httpError.UnauthorizedError('Login sessions require session support,please enable it.');
35
35
  }
36
36
  options = options || {};
37
37
  let su;
@@ -45,7 +45,7 @@ class SessionStrategy extends strategy_1.Strategy {
45
45
  const paused = options.pauseStream ? (0, pause_1.pause)(req) : null;
46
46
  this._deserializeUser(su, req, (err, user) => {
47
47
  if (err) {
48
- return new core_1.httpError.UnauthorizedError(err.message);
48
+ throw new core_1.httpError.UnauthorizedError(err.message);
49
49
  }
50
50
  if (!user) {
51
51
  delete req.session[this.options.sessionUserProperty].user;
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.1",
4
+ "version": "3.4.3",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -22,18 +22,18 @@
22
22
  "author": "Nawbc",
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
- "@midwayjs/core": "^3.4.1",
25
+ "@midwayjs/core": "^3.4.3",
26
26
  "@midwayjs/decorator": "^3.4.1",
27
- "@midwayjs/express": "^3.4.1",
28
- "@midwayjs/jwt": "^3.4.1",
29
- "@midwayjs/koa": "^3.4.1",
30
- "@midwayjs/mock": "^3.4.1",
31
- "@midwayjs/web": "^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",
32
32
  "@types/passport-local": "1.0.34",
33
33
  "express-session": "1.17.3",
34
34
  "passport-jwt": "4.0.0",
35
35
  "passport-local": "1.0.0",
36
36
  "passport-openidconnect": "0.1.1"
37
37
  },
38
- "gitHead": "c7b47267c80f93407537677d8366f68bc6dfc462"
38
+ "gitHead": "0d79293f56d01a3ef3589e6a6ef53c582ac0c46f"
39
39
  }