@midwayjs/passport 3.0.0-beta.9 → 3.0.0

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/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('local') // 此处可以使用一个简短的identifier
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: ['local'] })
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: ['jwtPassportMiddleware'] })
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: ['githubPassportMiddleware'] })
262
+ @Get('/github', { middleware: [GithubPassportMiddleware] })
263
263
  async githubOAuth() {}
264
264
 
265
- @Get('/github/cb', { middleware: ['githubPassportMiddleware'] })
265
+ @Get('/github/cb', { middleware: [GithubPassportMiddleware] })
266
266
  async githubOAuthCallback() {
267
267
  return this.ctx.req.user;
268
268
  }
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.passport = void 0;
4
4
  exports.passport = {
5
- session: false,
5
+ session: true,
6
6
  userProperty: 'user',
7
7
  };
8
8
  //# sourceMappingURL=config.default.js.map
@@ -1,6 +1,7 @@
1
+ import { IMidwayContainer, MidwayApplicationManager, MidwayConfigService } from '@midwayjs/core';
1
2
  export declare class PassportConfiguration {
2
- app: any;
3
- passportConfig: any;
4
- onReady(): Promise<void>;
3
+ applicationManager: MidwayApplicationManager;
4
+ configService: MidwayConfigService;
5
+ onReady(container: IMidwayContainer): Promise<void>;
5
6
  }
6
7
  //# sourceMappingURL=configuration.d.ts.map
@@ -13,23 +13,29 @@ exports.PassportConfiguration = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
14
  const DefaultConfig = require("./config/config.default");
15
15
  const util_1 = require("./util");
16
+ const core_1 = require("@midwayjs/core");
16
17
  let PassportConfiguration = class PassportConfiguration {
17
- async onReady() {
18
+ async onReady(container) {
19
+ const passportConfig = this.configService.getConfiguration('passport');
18
20
  const passport = (0, util_1.getPassport)();
19
- this.app.useMiddleware(passport.initialize());
20
- if (this.passportConfig.session) {
21
- this.app.useMiddleware(passport.session());
22
- }
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
+ });
23
29
  }
24
30
  };
25
31
  __decorate([
26
- (0, decorator_1.App)(),
27
- __metadata("design:type", Object)
28
- ], PassportConfiguration.prototype, "app", void 0);
32
+ (0, decorator_1.Inject)(),
33
+ __metadata("design:type", core_1.MidwayApplicationManager)
34
+ ], PassportConfiguration.prototype, "applicationManager", void 0);
29
35
  __decorate([
30
- (0, decorator_1.Config)('passport'),
31
- __metadata("design:type", Object)
32
- ], PassportConfiguration.prototype, "passportConfig", void 0);
36
+ (0, decorator_1.Inject)(),
37
+ __metadata("design:type", core_1.MidwayConfigService)
38
+ ], PassportConfiguration.prototype, "configService", void 0);
33
39
  PassportConfiguration = __decorate([
34
40
  (0, decorator_1.Configuration)({
35
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);
@@ -13,6 +13,7 @@ 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
+ const core_1 = require("@midwayjs/core");
16
17
  function PassportStrategy(Strategy, name) {
17
18
  class InnerStrategyAbstractClass extends interface_1.AbstractStrategy {
18
19
  async init() {
@@ -132,7 +133,7 @@ function PassportMiddleware(strategy) {
132
133
  return;
133
134
  }
134
135
  else {
135
- res.status(401);
136
+ throw new core_1.httpError.UnauthorizedError();
136
137
  }
137
138
  }
138
139
  next();
@@ -195,7 +196,7 @@ function PassportMiddleware(strategy) {
195
196
  return;
196
197
  }
197
198
  else {
198
- ctx.status = 401;
199
+ throw new core_1.httpError.UnauthorizedError();
199
200
  }
200
201
  }
201
202
  await next();
@@ -207,6 +208,9 @@ function PassportMiddleware(strategy) {
207
208
  }.bind(this);
208
209
  }
209
210
  }
211
+ static getName() {
212
+ return 'passport';
213
+ }
210
214
  }
211
215
  __decorate([
212
216
  (0, decorator_1.Config)('passport'),
package/index.d.ts CHANGED
@@ -7,3 +7,51 @@ declare module '@midwayjs/core/dist/interface' {
7
7
  passport?: passport.AuthenticateOptions;
8
8
  }
9
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,7 +1,7 @@
1
1
  {
2
2
  "name": "@midwayjs/passport",
3
3
  "description": "midway passport component",
4
- "version": "3.0.0-beta.9",
4
+ "version": "3.0.0",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
7
7
  "files": [
@@ -12,7 +12,7 @@
12
12
  "scripts": {
13
13
  "build": "tsc",
14
14
  "test": "node --require=ts-node/register ../../node_modules/jest/bin/jest.js",
15
- "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit"
15
+ "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit"
16
16
  },
17
17
  "keywords": [
18
18
  "midway",
@@ -22,22 +22,22 @@
22
22
  "author": "Nawbc",
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
- "@midwayjs/core": "^3.0.0-beta.9",
26
- "@midwayjs/decorator": "^3.0.0-beta.9",
27
- "@midwayjs/express": "^3.0.0-beta.9",
28
- "@midwayjs/jwt": "^3.0.0-beta.9",
29
- "@midwayjs/koa": "^3.0.0-beta.9",
30
- "@midwayjs/mock": "^3.0.0-beta.9",
31
- "@midwayjs/web": "^3.0.0-beta.9",
32
- "@types/passport": "^1.0.7",
33
- "@types/passport-local": "^1.0.34",
34
- "express-session": "^1.17.2",
35
- "passport": "^0.5.0",
36
- "passport-jwt": "^4.0.0",
37
- "passport-local": "^1.0.0"
25
+ "@midwayjs/core": "^3.0.0",
26
+ "@midwayjs/decorator": "^3.0.0",
27
+ "@midwayjs/express": "^3.0.0",
28
+ "@midwayjs/jwt": "^3.0.0",
29
+ "@midwayjs/koa": "^3.0.0",
30
+ "@midwayjs/mock": "^3.0.0",
31
+ "@midwayjs/web": "^3.0.0",
32
+ "@types/passport": "1.0.7",
33
+ "@types/passport-local": "1.0.34",
34
+ "express-session": "1.17.2",
35
+ "passport": "0.5.2",
36
+ "passport-jwt": "4.0.0",
37
+ "passport-local": "1.0.0"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "passport": "^0.5.0"
41
41
  },
42
- "gitHead": "d23e4a4fee58097b98461625c428a37d55535cec"
42
+ "gitHead": "55c26029bccf7bbb739fa1597e8f418dafa2caa0"
43
43
  }
package/CHANGELOG.md DELETED
@@ -1,26 +0,0 @@
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
- # [3.0.0-beta.9](https://github.com/midwayjs/midway/compare/v3.0.0-beta.8...v3.0.0-beta.9) (2021-12-09)
7
-
8
- **Note:** Version bump only for package @midwayjs/passport
9
-
10
-
11
-
12
-
13
-
14
- # [3.0.0-beta.8](https://github.com/midwayjs/midway/compare/v3.0.0-beta.7...v3.0.0-beta.8) (2021-12-08)
15
-
16
-
17
- ### Bug Fixes
18
-
19
- * 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))
20
- * passport missing proxy file ([#1405](https://github.com/midwayjs/midway/issues/1405)) ([5c9bdae](https://github.com/midwayjs/midway/commit/5c9bdae8323b41ead72c3d3f867aa11150bb3e78))
21
-
22
-
23
- ### Features
24
-
25
- * passport add presetProperty ([#1358](https://github.com/midwayjs/midway/issues/1358)) ([4db8eda](https://github.com/midwayjs/midway/commit/4db8eda592c0486898edabcacbc9a69eb2d87004))
26
- * support passport and jwt ([#1343](https://github.com/midwayjs/midway/issues/1343)) ([f26af4f](https://github.com/midwayjs/midway/commit/f26af4f3e16507d6f3ffe0467f8f5be69e6306d7))