/
types.ts
69 lines (62 loc) · 2.3 KB
/
types.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
// Copyright IBM Corp. 2018,2019. All Rights Reserved.
// Node module: @loopback/authentication
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {addExtension, Constructor, Context} from '@loopback/core';
import {Request} from '@loopback/rest';
import {UserProfile} from '@loopback/security';
import {AuthenticationBindings} from './keys';
/**
* interface definition of a function which accepts a request
* and returns an authenticated user
*/
export interface AuthenticateFn {
(request: Request): Promise<UserProfile | undefined>;
}
/**
* An interface that describes the common authentication strategy.
*
* An authentication strategy is a class with an
* 'authenticate' method that verifies a user's credentials and
* returns the corresponding user profile.
*
*/
export interface AuthenticationStrategy {
/**
* The 'name' property is a unique identifier for the
* authentication strategy ( for example : 'basic', 'jwt', etc)
*/
name: string;
/**
* The 'authenticate' method takes in a given request and returns a user profile
* which is an instance of 'UserProfile'.
* (A user profile is a minimal subset of a user object)
* If the user credentials are valid, this method should return a 'UserProfile' instance.
* If the user credentials are invalid, this method should throw an error
* If the user credentials are missing, this method should throw an error, or return 'undefined'
* and let the authentication action deal with it.
*
* @param request - Express request object
*/
authenticate(request: Request): Promise<UserProfile | undefined>;
}
export const AUTHENTICATION_STRATEGY_NOT_FOUND =
'AUTHENTICATION_STRATEGY_NOT_FOUND';
export const USER_PROFILE_NOT_FOUND = 'USER_PROFILE_NOT_FOUND';
/**
* Registers an authentication strategy as an extension of the AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME extension point.
*/
export function registerAuthenticationStrategy(
context: Context,
strategyClass: Constructor<AuthenticationStrategy>,
) {
addExtension(
context,
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
strategyClass,
{
namespace:
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
},
);
}