-
-
Notifications
You must be signed in to change notification settings - Fork 753
/
Copy pathcore.ts
263 lines (226 loc) · 8.76 KB
/
core.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { promisify } from 'util';
import { merge } from 'lodash';
import jsonwebtoken, { SignOptions, Secret, VerifyOptions } from 'jsonwebtoken';
import uuidv4 from 'uuid/v4';
import { NotAuthenticated } from '@feathersjs/errors';
import Debug from 'debug';
import { Application, Params } from '@feathersjs/feathers';
import { IncomingMessage, ServerResponse } from 'http';
import defaultOptions from './options';
const debug = Debug('@feathersjs/authentication/base');
const verifyJWT = promisify(jsonwebtoken.verify);
const createJWT = promisify(jsonwebtoken.sign);
export interface AuthenticationResult {
[key: string]: any;
}
export interface AuthenticationRequest {
strategy?: string;
[key: string]: any;
}
export type ConnectionEvent = 'login' | 'logout' | 'disconnect';
export interface AuthenticationStrategy {
/**
* Implement this method to get access to the AuthenticationService
* @param auth The AuthenticationService
*/
setAuthentication? (auth: AuthenticationBase): void;
/**
* Implement this method to get access to the Feathers application
* @param app The Feathers application instance
*/
setApplication? (app: Application): void;
/**
* Implement this method to get access to the strategy name
* @param name The name of the strategy
*/
setName? (name: string): void;
/**
* Implement this method to verify the current configuration
* and throw an error if it is invalid.
*/
verifyConfiguration? (): void;
/**
* Authenticate an authentication request with this strategy.
* Should throw an error if the strategy did not succeed.
* @param authentication The authentication request
* @param params The service call parameters
*/
authenticate? (authentication: AuthenticationRequest, params: Params): Promise<AuthenticationResult>;
/**
* Update a real-time connection according to this strategy.
*
* @param connection The real-time connection
* @param context The hook context
*/
handleConnection? (event: ConnectionEvent, connection: any, authResult?: AuthenticationResult): Promise<void>;
/**
* Parse a basic HTTP request and response for authentication request information.
* @param req The HTTP request
* @param res The HTTP response
*/
parse? (req: IncomingMessage, res: ServerResponse): Promise<AuthenticationRequest | null>;
}
export interface JwtVerifyOptions extends VerifyOptions {
algorithm?: string | string[];
}
/**
* A base class for managing authentication strategies and creating and verifying JWTs
*/
export class AuthenticationBase {
app: Application;
configKey: string;
strategies: {
[key: string]: AuthenticationStrategy;
};
/**
* Create a new authentication service.
* @param app The Feathers application instance
* @param configKey The configuration key name in `app.get` (default: `authentication`)
* @param options Optional initial options
*/
constructor (app: Application, configKey: string = 'authentication', options = {}) {
if (!app || typeof app.use !== 'function') {
throw new Error('An application instance has to be passed to the authentication service');
}
this.app = app;
this.strategies = {};
this.configKey = configKey;
app.set('defaultAuthentication', app.get('defaultAuthentication') || configKey);
app.set(configKey, merge({}, app.get(configKey), options));
}
/**
* Return the current configuration from the application
*/
get configuration () {
// Always returns a copy of the authentication configuration
return Object.assign({}, defaultOptions, this.app.get(this.configKey));
}
/**
* A list of all registered strategy names
*/
get strategyNames () {
return Object.keys(this.strategies);
}
/**
* Register a new authentication strategy under a given name.
* @param name The name to register the strategy under
* @param strategy The authentication strategy instance
*/
register (name: string, strategy: AuthenticationStrategy) {
// Call the functions a strategy can implement
if (typeof strategy.setName === 'function') {
strategy.setName(name);
}
if (typeof strategy.setApplication === 'function') {
strategy.setApplication(this.app);
}
if (typeof strategy.setAuthentication === 'function') {
strategy.setAuthentication(this);
}
if (typeof strategy.verifyConfiguration === 'function') {
strategy.verifyConfiguration();
}
// Register strategy as name
this.strategies[name] = strategy;
}
/**
* Get the registered authentication strategies for a list of names.
* The return value may contain `undefined` if the strategy does not exist.
* @param names The list or strategy names
*/
getStrategies (...names: string[]) {
// Returns all strategies for a list of names (including undefined)
return names.map(name => this.strategies[name])
.filter(current => !!current);
}
/**
* Create a new access token with payload and options.
* @param payload The JWT payload
* @param optsOverride The options to extend the defaults (`configuration.jwtOptions`) with
* @param secretOverride Use a different secret instead
*/
async createAccessToken (payload: string | Buffer | object, optsOverride?: SignOptions, secretOverride?: Secret) {
const { secret, jwtOptions } = this.configuration;
// Use configuration by default but allow overriding the secret
const jwtSecret = secretOverride || secret;
// Default jwt options merged with additional options
const options = merge({}, jwtOptions, optsOverride);
if (!options.jwtid) {
// Generate a UUID as JWT ID by default
options.jwtid = uuidv4();
}
// @ts-ignore
return createJWT(payload, jwtSecret, options);
}
/**
* Verifies an access token.
* @param accessToken The token to verify
* @param optsOverride The options to extend the defaults (`configuration.jwtOptions`) with
* @param secretOverride Use a different secret instead
*/
async verifyAccessToken (accessToken: string, optsOverride?: JwtVerifyOptions, secretOverride?: Secret) {
const { secret, jwtOptions } = this.configuration;
const jwtSecret = secretOverride || secret;
const options = merge({}, jwtOptions, optsOverride);
const { algorithm } = options;
// Normalize the `algorithm` setting into the algorithms array
if (algorithm && !options.algorithms) {
options.algorithms = Array.isArray(algorithm) ? algorithm : [ algorithm ];
delete options.algorithm;
}
try {
// @ts-ignore
const isValid = await verifyJWT(accessToken, jwtSecret, options);
return isValid;
} catch (error) {
throw new NotAuthenticated(error.message, error);
}
}
/**
* Authenticate a given authentication request against a list of strategies.
* @param authentication The authentication request
* @param params Service call parameters
* @param allowed A list of allowed strategy names
*/
async authenticate (authentication: AuthenticationRequest, params: Params, ...allowed: string[]) {
const { strategy } = authentication || ({} as AuthenticationRequest);
const [ authStrategy ] = this.getStrategies(strategy);
const strategyAllowed = allowed.includes(strategy);
debug('Running authenticate for strategy', strategy, allowed);
if (!authentication || !authStrategy || !strategyAllowed) {
const additionalInfo = (!strategy && ' (no `strategy` set)') ||
(!strategyAllowed && ' (strategy not allowed in authStrategies)') || '';
// If there are no valid strategies or `authentication` is not an object
throw new NotAuthenticated('Invalid authentication information' + additionalInfo);
}
return authStrategy.authenticate(authentication, {
...params,
authenticated: true
});
}
async handleConnection (event: ConnectionEvent, connection: any, authResult?: AuthenticationResult) {
const strategies = this.getStrategies(...Object.keys(this.strategies))
.filter(current => typeof current.handleConnection === 'function');
for (const strategy of strategies) {
await strategy.handleConnection(event, connection, authResult);
}
}
/**
* Parse an HTTP request and response for authentication request information.
* @param req The HTTP request
* @param res The HTTP response
* @param names A list of strategies to use
*/
async parse (req: IncomingMessage, res: ServerResponse, ...names: string[]) {
const strategies = this.getStrategies(...names)
.filter(current => current && typeof current.parse === 'function');
debug('Strategies parsing HTTP header for authentication information', names);
for (const authStrategy of strategies) {
const value = await authStrategy.parse(req, res);
if (value !== null) {
return value;
}
}
return null;
}
}