UNPKG

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