UNPKG

5.87 kBTypeScriptView Raw
1import { SignOptions, Secret, VerifyOptions } from 'jsonwebtoken';
2import { Application, Params } from '@feathersjs/feathers';
3import { IncomingMessage, ServerResponse } from 'http';
4import { AuthenticationConfiguration } from './options';
5export interface AuthenticationResult {
6 [key: string]: any;
7}
8export interface AuthenticationRequest {
9 strategy?: string;
10 [key: string]: any;
11}
12export interface AuthenticationParams extends Params {
13 payload?: {
14 [key: string]: any;
15 };
16 jwtOptions?: SignOptions;
17 authStrategies?: string[];
18 secret?: string;
19 [key: string]: any;
20}
21export type ConnectionEvent = 'login' | 'logout' | 'disconnect';
22export interface AuthenticationStrategy {
23 /**
24 * Implement this method to get access to the AuthenticationService
25 *
26 * @param auth The AuthenticationService
27 */
28 setAuthentication?(auth: AuthenticationBase): void;
29 /**
30 * Implement this method to get access to the Feathers application
31 *
32 * @param app The Feathers application instance
33 */
34 setApplication?(app: Application): void;
35 /**
36 * Implement this method to get access to the strategy name
37 *
38 * @param name The name of the strategy
39 */
40 setName?(name: string): void;
41 /**
42 * Implement this method to verify the current configuration
43 * and throw an error if it is invalid.
44 */
45 verifyConfiguration?(): void;
46 /**
47 * Implement this method to setup this strategy
48 * @param auth The AuthenticationService
49 * @param name The name of the strategy
50 */
51 setup?(auth: AuthenticationBase, name: string): Promise<void>;
52 /**
53 * Authenticate an authentication request with this strategy.
54 * Should throw an error if the strategy did not succeed.
55 *
56 * @param authentication The authentication request
57 * @param params The service call parameters
58 */
59 authenticate?(authentication: AuthenticationRequest, params: AuthenticationParams): Promise<AuthenticationResult>;
60 /**
61 * Update a real-time connection according to this strategy.
62 *
63 * @param connection The real-time connection
64 * @param context The hook context
65 */
66 handleConnection?(event: ConnectionEvent, connection: any, authResult?: AuthenticationResult): Promise<void>;
67 /**
68 * Parse a basic HTTP request and response for authentication request information.
69 *
70 * @param req The HTTP request
71 * @param res The HTTP response
72 */
73 parse?(req: IncomingMessage, res: ServerResponse): Promise<AuthenticationRequest | null>;
74}
75export interface JwtVerifyOptions extends VerifyOptions {
76 algorithm?: string | string[];
77}
78/**
79 * A base class for managing authentication strategies and creating and verifying JWTs
80 */
81export declare class AuthenticationBase {
82 app: Application;
83 strategies: {
84 [key: string]: AuthenticationStrategy;
85 };
86 configKey: string;
87 isReady: boolean;
88 /**
89 * Create a new authentication service.
90 *
91 * @param app The Feathers application instance
92 * @param configKey The configuration key name in `app.get` (default: `authentication`)
93 * @param options Optional initial options
94 */
95 constructor(app: Application, configKey?: string, options?: {});
96 /**
97 * Return the current configuration from the application
98 */
99 get configuration(): AuthenticationConfiguration;
100 /**
101 * A list of all registered strategy names
102 */
103 get strategyNames(): string[];
104 /**
105 * Register a new authentication strategy under a given name.
106 *
107 * @param name The name to register the strategy under
108 * @param strategy The authentication strategy instance
109 */
110 register(name: string, strategy: AuthenticationStrategy): void;
111 /**
112 * Get the registered authentication strategies for a list of names.
113 *
114 * @param names The list or strategy names
115 */
116 getStrategies(...names: string[]): AuthenticationStrategy[];
117 /**
118 * Returns a single strategy by name
119 *
120 * @param name The strategy name
121 * @returns The authentication strategy or undefined
122 */
123 getStrategy(name: string): AuthenticationStrategy;
124 /**
125 * Create a new access token with payload and options.
126 *
127 * @param payload The JWT payload
128 * @param optsOverride The options to extend the defaults (`configuration.jwtOptions`) with
129 * @param secretOverride Use a different secret instead
130 */
131 createAccessToken(payload: string | Buffer | object, optsOverride?: SignOptions, secretOverride?: Secret): Promise<string>;
132 /**
133 * Verifies an access token.
134 *
135 * @param accessToken The token to verify
136 * @param optsOverride The options to extend the defaults (`configuration.jwtOptions`) with
137 * @param secretOverride Use a different secret instead
138 */
139 verifyAccessToken(accessToken: string, optsOverride?: JwtVerifyOptions, secretOverride?: Secret): Promise<any>;
140 /**
141 * Authenticate a given authentication request against a list of strategies.
142 *
143 * @param authentication The authentication request
144 * @param params Service call parameters
145 * @param allowed A list of allowed strategy names
146 */
147 authenticate(authentication: AuthenticationRequest, params: AuthenticationParams, ...allowed: string[]): Promise<AuthenticationResult>;
148 handleConnection(event: ConnectionEvent, connection: any, authResult?: AuthenticationResult): Promise<void>;
149 /**
150 * Parse an HTTP request and response for authentication request information.
151 *
152 * @param req The HTTP request
153 * @param res The HTTP response
154 * @param names A list of strategies to use
155 */
156 parse(req: IncomingMessage, res: ServerResponse, ...names: string[]): Promise<AuthenticationRequest>;
157 setup(): Promise<void>;
158}