1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | import { IncomingMessage } from 'http';
|
14 |
|
15 | declare global {
|
16 | namespace Express {
|
17 |
|
18 | interface AuthInfo {}
|
19 |
|
20 | interface User {}
|
21 |
|
22 | interface Request {
|
23 | authInfo?: AuthInfo;
|
24 | user?: User;
|
25 |
|
26 |
|
27 | login(user: User, done: (err: any) => void): void;
|
28 | login(user: User, options: any, done: (err: any) => void): void;
|
29 | logIn(user: User, done: (err: any) => void): void;
|
30 | logIn(user: User, options: any, done: (err: any) => void): void;
|
31 |
|
32 | logout(): void;
|
33 | logOut(): void;
|
34 |
|
35 | isAuthenticated(): this is AuthenticatedRequest;
|
36 | isUnauthenticated(): this is UnauthenticatedRequest;
|
37 | }
|
38 |
|
39 | interface AuthenticatedRequest extends Request {
|
40 | user: User;
|
41 | }
|
42 |
|
43 | interface UnauthenticatedRequest extends Request {
|
44 | user?: undefined;
|
45 | }
|
46 | }
|
47 | }
|
48 |
|
49 | import express = require('express');
|
50 |
|
51 | declare namespace passport {
|
52 | interface AuthenticateOptions {
|
53 | authInfo?: boolean;
|
54 | assignProperty?: string;
|
55 | failureFlash?: string | boolean;
|
56 | failureMessage?: boolean | string;
|
57 | failureRedirect?: string;
|
58 | failWithError?: boolean;
|
59 | session?: boolean;
|
60 | scope?: string | string[];
|
61 | successFlash?: string | boolean;
|
62 | successMessage?: boolean | string;
|
63 | successRedirect?: string;
|
64 | successReturnToOrRedirect?: string;
|
65 | state?: string;
|
66 | pauseStream?: boolean;
|
67 | userProperty?: string;
|
68 | passReqToCallback?: boolean;
|
69 | prompt?: string;
|
70 | }
|
71 |
|
72 | interface Authenticator<InitializeRet = express.Handler, AuthenticateRet = any, AuthorizeRet = AuthenticateRet, AuthorizeOptions = AuthenticateOptions> {
|
73 | use(strategy: Strategy): this;
|
74 | use(name: string, strategy: Strategy): this;
|
75 | unuse(name: string): this;
|
76 | framework<X, Y, Z>(fw: Framework<X, Y, Z>): Authenticator<X, Y, Z>;
|
77 | initialize(options?: { userProperty: string; }): InitializeRet;
|
78 | session(options?: { pauseStream: boolean; }): AuthenticateRet;
|
79 |
|
80 | authenticate(strategy: string | string[] | Strategy, callback?: (...args: any[]) => any): AuthenticateRet;
|
81 | authenticate(strategy: string | string[] | Strategy, options: AuthenticateOptions, callback?: (...args: any[]) => any): AuthenticateRet;
|
82 | authorize(strategy: string | string[], callback?: (...args: any[]) => any): AuthorizeRet;
|
83 | authorize(strategy: string | string[], options: AuthorizeOptions, callback?: (...args: any[]) => any): AuthorizeRet;
|
84 | serializeUser<TID>(fn: (user: Express.User, done: (err: any, id?: TID) => void) => void): void;
|
85 | serializeUser<TID, TR extends IncomingMessage = express.Request>(fn: (req: TR, user: Express.User, done: (err: any, id?: TID) => void) => void): void;
|
86 | deserializeUser<TID>(fn: (id: TID, done: (err: any, user?: Express.User | false | null) => void) => void): void;
|
87 | deserializeUser<TID, TR extends IncomingMessage = express.Request>(fn: (req: TR, id: TID, done: (err: any, user?: Express.User | false | null) => void) => void): void;
|
88 | transformAuthInfo(fn: (info: any, done: (err: any, info: any) => void) => void): void;
|
89 | }
|
90 |
|
91 | interface PassportStatic extends Authenticator {
|
92 | Authenticator: { new(): Authenticator };
|
93 | Passport: PassportStatic["Authenticator"];
|
94 | Strategy: { new(): Strategy & StrategyCreatedStatic };
|
95 | }
|
96 |
|
97 | interface Strategy {
|
98 | name?: string;
|
99 | authenticate(this: StrategyCreated<this>, req: express.Request, options?: any): any;
|
100 | }
|
101 |
|
102 | interface StrategyCreatedStatic {
|
103 | /**
|
104 | * Authenticate `user`, with optional `info`.
|
105 | *
|
106 | * Strategies should call this function to successfully authenticate a
|
107 | * user. `user` should be an object supplied by the application after it
|
108 | * has been given an opportunity to verify credentials. `info` is an
|
109 | * optional argument containing additional user information. This is
|
110 | * useful for third-party authentication strategies to pass profile
|
111 | * details.
|
112 | */
|
113 | success(user: Express.User, info?: object): void;
|
114 | /**
|
115 | * Fail authentication, with optional `challenge` and `status`, defaulting
|
116 | * to 401.
|
117 | *
|
118 | * Strategies should call this function to fail an authentication attempt.
|
119 | */
|
120 | fail(challenge?: string | number, status?: number): void;
|
121 | /**
|
122 | * Redirect to `url` with optional `status`, defaulting to 302.
|
123 | *
|
124 | * Strategies should call this function to redirect the user (via their
|
125 | * user agent) to a third-party website for authentication.
|
126 | */
|
127 | redirect(url: string, status?: number): void;
|
128 | /**
|
129 | * Pass without making a success or fail decision.
|
130 | *
|
131 | * Under most circumstances, Strategies should not need to call this
|
132 | * function. It exists primarily to allow previous authentication state
|
133 | * to be restored, for example from an HTTP session.
|
134 | */
|
135 | pass(): void;
|
136 | /**
|
137 | * Internal error while performing authentication.
|
138 | *
|
139 | * Strategies should call this function when an internal error occurs
|
140 | * during the process of performing authentication; for example, if the
|
141 | * user directory is not available.
|
142 | */
|
143 | error(err: any): void;
|
144 | }
|
145 |
|
146 | type StrategyCreated<T, O = T & StrategyCreatedStatic> = {
|
147 | [P in keyof O]: O[P];
|
148 | };
|
149 |
|
150 | interface Profile {
|
151 | provider: string;
|
152 | id: string;
|
153 | displayName: string;
|
154 | username?: string;
|
155 | name?: {
|
156 | familyName: string;
|
157 | givenName: string;
|
158 | middleName?: string;
|
159 | };
|
160 | emails?: Array<{
|
161 | value: string;
|
162 | type?: string;
|
163 | }>;
|
164 | photos?: Array<{
|
165 | value: string;
|
166 | }>;
|
167 | }
|
168 |
|
169 | interface Framework<InitializeRet = any, AuthenticateRet = any, AuthorizeRet = AuthenticateRet> {
|
170 | initialize(passport: Authenticator<InitializeRet, AuthenticateRet, AuthorizeRet>, options?: any): (...args: any[]) => InitializeRet;
|
171 | authenticate(passport: Authenticator<InitializeRet, AuthenticateRet, AuthorizeRet>, name: string, options?: any, callback?: (...args: any[]) => any): (...args: any[]) => AuthenticateRet;
|
172 | authorize?(passport: Authenticator<InitializeRet, AuthenticateRet, AuthorizeRet>, name: string, options?: any, callback?: (...args: any[]) => any): (...args: any[]) => AuthorizeRet;
|
173 | }
|
174 | }
|
175 |
|
176 | declare const passport: passport.PassportStatic;
|
177 | export = passport;
|