UNPKG

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