UNPKG

7.8 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(): 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
49import express = require('express');
50
51declare namespace passport {
52 interface AuthenticateOptions {
53 authInfo?: boolean | undefined;
54 assignProperty?: string | undefined;
55 failureFlash?: string | boolean | undefined;
56 failureMessage?: boolean | string | undefined;
57 failureRedirect?: string | undefined;
58 failWithError?: boolean | undefined;
59 session?: boolean | undefined;
60 scope?: string | string[] | undefined;
61 successFlash?: string | boolean | undefined;
62 successMessage?: boolean | string | undefined;
63 successRedirect?: string | undefined;
64 successReturnToOrRedirect?: string | undefined;
65 state?: string | undefined;
66 pauseStream?: boolean | undefined;
67 userProperty?: string | undefined;
68 passReqToCallback?: boolean | undefined;
69 prompt?: string | undefined;
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 | undefined;
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 | undefined;
155 name?: {
156 familyName: string;
157 givenName: string;
158 middleName?: string | undefined;
159 } | undefined;
160 emails?: Array<{
161 value: string;
162 type?: string | undefined;
163 }> | undefined;
164 photos?: Array<{
165 value: string;
166 }> | undefined;
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
176declare const passport: passport.PassportStatic;
177export = passport;