@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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013 - Now midwayjs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,5 +1,7 @@
1
1
  export declare const passport: {
2
2
  session: boolean;
3
+ assignProperty: boolean;
4
+ sessionUserProperty: string;
3
5
  userProperty: string;
4
6
  };
5
7
  //# sourceMappingURL=config.default.d.ts.map
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.passport = void 0;
4
4
  exports.passport = {
5
5
  session: true,
6
+ assignProperty: true,
7
+ sessionUserProperty: 'user',
6
8
  userProperty: 'user',
7
9
  };
8
10
  //# sourceMappingURL=config.default.js.map
@@ -12,20 +12,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.PassportConfiguration = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
14
  const DefaultConfig = require("./config/config.default");
15
- const util_1 = require("./util");
16
15
  const core_1 = require("@midwayjs/core");
16
+ const authenticator_1 = require("./passport/authenticator");
17
17
  let PassportConfiguration = class PassportConfiguration {
18
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
- });
19
+ await container.getAsync(authenticator_1.PassportAuthenticator);
29
20
  }
30
21
  };
31
22
  __decorate([
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { PassportConfiguration as Configuration } from './configuration';
2
2
  export * from './decorator/strategy';
3
- export * from './service/passport.service';
3
+ export * from './passport/authenticator';
4
+ export * from './passport/passport.service';
5
+ export * from './interface';
4
6
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -18,5 +18,7 @@ exports.Configuration = void 0;
18
18
  var configuration_1 = require("./configuration");
19
19
  Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.PassportConfiguration; } });
20
20
  __exportStar(require("./decorator/strategy"), exports);
21
- __exportStar(require("./service/passport.service"), exports);
21
+ __exportStar(require("./passport/authenticator"), exports);
22
+ __exportStar(require("./passport/passport.service"), exports);
23
+ __exportStar(require("./interface"), exports);
22
24
  //# sourceMappingURL=index.js.map
@@ -1,5 +1,20 @@
1
- import * as passport from 'passport';
2
1
  import { IMiddleware } from '@midwayjs/core';
2
+ export interface AuthenticateOptions {
3
+ authInfo?: boolean | undefined;
4
+ assignProperty?: string | undefined;
5
+ failureMessage?: boolean | string | undefined;
6
+ failureRedirect?: string | undefined;
7
+ session?: boolean | undefined;
8
+ scope?: string | string[] | undefined;
9
+ successMessage?: boolean | string | undefined;
10
+ successRedirect?: string | undefined;
11
+ successReturnToOrRedirect?: string | undefined;
12
+ state?: string | undefined;
13
+ pauseStream?: boolean | undefined;
14
+ userProperty?: string | undefined;
15
+ passReqToCallback?: boolean | undefined;
16
+ prompt?: string | undefined;
17
+ }
3
18
  export interface IPassportStrategy {
4
19
  validate(...args: any[]): any;
5
20
  getStrategyOptions(): any;
@@ -7,16 +22,55 @@ export interface IPassportStrategy {
7
22
  deserializeUser?(id: any, done: (err: any, user?: any) => void): void;
8
23
  transformAuthInfo?(info: any, done: (err: any, info: any) => void): void;
9
24
  }
10
- export declare abstract class AbstractStrategy implements IPassportStrategy {
11
- abstract validate(...args: any[]): any;
12
- abstract getStrategyOptions(): any;
13
- }
14
25
  export interface IPassportMiddleware extends IMiddleware<any, any> {
15
- authenticate?(options: passport.AuthenticateOptions, callback: Function): any;
26
+ authenticate?(options: AuthenticateOptions, callback: Function): any;
16
27
  }
17
28
  export declare abstract class AbstractPassportMiddleware implements Pick<IPassportMiddleware, 'authenticate'> {
18
- abstract getAuthenticateOptions(): Promise<passport.AuthenticateOptions> | passport.AuthenticateOptions;
19
- authenticate?(options: passport.AuthenticateOptions, callback?: Function): any;
29
+ abstract getAuthenticateOptions(): Promise<AuthenticateOptions> | AuthenticateOptions;
30
+ authenticate?(options: AuthenticateOptions, callback?: Function): any;
20
31
  resolve(): any;
21
32
  }
33
+ export interface StrategyCreatedStatic {
34
+ /**
35
+ * Authenticate `user`, with optional `info`.
36
+ *
37
+ * Strategies should call this function to successfully authenticate a
38
+ * user. `user` should be an object supplied by the application after it
39
+ * has been given an opportunity to verify credentials. `info` is an
40
+ * optional argument containing additional user information. This is
41
+ * useful for third-party authentication strategies to pass profile
42
+ * details.
43
+ */
44
+ success(user: any, info?: Record<string, any>): void;
45
+ /**
46
+ * Fail authentication, with optional `challenge` and `status`, defaulting
47
+ * to 401.
48
+ *
49
+ * Strategies should call this function to fail an authentication attempt.
50
+ */
51
+ fail(challenge?: string | number, status?: number): void;
52
+ /**
53
+ * Redirect to `url` with optional `status`, defaulting to 302.
54
+ *
55
+ * Strategies should call this function to redirect the user (via their
56
+ * user agent) to a third-party website for authentication.
57
+ */
58
+ redirect(url: string, status?: number): void;
59
+ /**
60
+ * Pass without making a success or fail decision.
61
+ *
62
+ * Under most circumstances, Strategies should not need to call this
63
+ * function. It exists primarily to allow previous authentication state
64
+ * to be restored, for example from an HTTP session.
65
+ */
66
+ pass(): void;
67
+ /**
68
+ * Internal error while performing authentication.
69
+ *
70
+ * Strategies should call this function when an internal error occurs
71
+ * during the process of performing authentication; for example, if the
72
+ * user directory is not available.
73
+ */
74
+ error(err: any): void;
75
+ }
22
76
  //# sourceMappingURL=interface.d.ts.map
package/dist/interface.js CHANGED
@@ -1,9 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AbstractPassportMiddleware = exports.AbstractStrategy = void 0;
4
- class AbstractStrategy {
5
- }
6
- exports.AbstractStrategy = AbstractStrategy;
3
+ exports.AbstractPassportMiddleware = void 0;
7
4
  class AbstractPassportMiddleware {
8
5
  resolve() { }
9
6
  }
@@ -0,0 +1,125 @@
1
+ /// <reference types="node" />
2
+ import { AuthenticateOptions } from '../interface';
3
+ import { Strategy } from './strategy';
4
+ import { IMidwayContainer } from '@midwayjs/core';
5
+ import { IncomingMessage } from 'http';
6
+ export declare class PassportAuthenticator {
7
+ private strategies;
8
+ private userProperty;
9
+ private sessionUserProperty;
10
+ _key: string;
11
+ _serializers: any[];
12
+ _deserializers: any[];
13
+ _infoTransformers: any[];
14
+ applicationContext: IMidwayContainer;
15
+ passportConfig: any;
16
+ protected init(): void;
17
+ isExpressMode(): boolean;
18
+ isEnableSession(): boolean;
19
+ getUserProperty(): string;
20
+ getSessionUserProperty(): string;
21
+ use(name: string | Strategy, strategy?: Strategy): this;
22
+ unuse(name: string): this;
23
+ /**
24
+ * Authenticates requests.
25
+ *
26
+ * Applies the `name`ed strategy (or strategies) to the incoming request, in
27
+ * order to authenticate the request. If authentication is successful, the user
28
+ * will be logged in and populated at `req.user` and a session will be
29
+ * established by default. If authentication fails, an unauthorized response
30
+ * will be sent.
31
+ *
32
+ * Options:
33
+ * - `session` Save login state in session, defaults to _true_
34
+ * - `successRedirect` After successful login, redirect to given URL
35
+ * - `successMessage` True to store success message in
36
+ * req.session.messages, or a string to use as override
37
+ * message for success.
38
+ * - `successFlash` True to flash success messages or a string to use as a flash
39
+ * message for success (overrides any from the strategy itself).
40
+ * - `failureRedirect` After failed login, redirect to given URL
41
+ * - `failureMessage` True to store failure message in
42
+ * req.session.messages, or a string to use as override
43
+ * message for failure.
44
+ * - `failureFlash` True to flash failure messages or a string to use as a flash
45
+ * message for failures (overrides any from the strategy itself).
46
+ * - `assignProperty` Assign the object provided by the verify callback to given property
47
+ */
48
+ authenticate(strategies: Strategy[], options?: AuthenticateOptions): (req: any) => Promise<{
49
+ successResult?: {
50
+ user: any;
51
+ info: any;
52
+ } | undefined;
53
+ redirectResult?: {
54
+ url: string;
55
+ status: number;
56
+ } | undefined;
57
+ failResult?: {
58
+ failures: Array<{
59
+ challenge: string;
60
+ status: number;
61
+ }>;
62
+ };
63
+ }>;
64
+ serializeUser(fn: any, req?: any, done?: any): number;
65
+ /**
66
+ * Registers a function used to deserialize user objects out of the session.
67
+ *
68
+ * Examples:
69
+ *
70
+ * passport.deserializeUser(function(id, done) {
71
+ * User.findById(id, function (err, user) {
72
+ * done(err, user);
73
+ * });
74
+ * });
75
+ *
76
+ * @api public
77
+ */
78
+ deserializeUser(fn: any, req?: any, done?: any): number;
79
+ /**
80
+ * Registers a function used to transform auth info.
81
+ *
82
+ * In some circumstances authorization details are contained in authentication
83
+ * credentials or loaded as part of verification.
84
+ *
85
+ * For example, when using bearer tokens for API authentication, the tokens may
86
+ * encode (either directly or indirectly in a database), details such as scope
87
+ * of access or the client to which the token was issued.
88
+ *
89
+ * Such authorization details should be enforced separately from authentication.
90
+ * Because Passport deals only with the latter, this is the responsiblity of
91
+ * middleware or routes further along the chain. However, it is not optimal to
92
+ * decode the same data or execute the same database query later. To avoid
93
+ * this, Passport accepts optional `info` along with the authenticated `user`
94
+ * in a strategy's `success()` action. This info is set at `req.authInfo`,
95
+ * where said later middlware or routes can access it.
96
+ *
97
+ * Optionally, applications can register transforms to proccess this info,
98
+ * which take effect prior to `req.authInfo` being set. This is useful, for
99
+ * example, when the info contains a client ID. The transform can load the
100
+ * client from the database and include the instance in the transformed info,
101
+ * allowing the full set of client properties to be convieniently accessed.
102
+ *
103
+ * If no transforms are registered, `info` supplied by the strategy will be left
104
+ * unmodified.
105
+ *
106
+ * Examples:
107
+ *
108
+ * passport.transformAuthInfo(function(info, done) {
109
+ * Client.findById(info.clientID, function (err, client) {
110
+ * info.client = client;
111
+ * done(err, info);
112
+ * });
113
+ * });
114
+ *
115
+ * @api public
116
+ */
117
+ transformAuthInfo(fn: any, req?: any, done?: any): number;
118
+ logInToSession(req: IncomingMessage & {
119
+ session: any;
120
+ }, user: any): Promise<void>;
121
+ logOutFromSession(req: any, options?: {
122
+ keepSessionInfo?: boolean;
123
+ }): Promise<void>;
124
+ }
125
+ //# sourceMappingURL=authenticator.d.ts.map