-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
strategy-adapter.ts
64 lines (55 loc) · 2.21 KB
/
strategy-adapter.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
// Copyright IBM Corp. 2017,2018. 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 {HttpErrors, Request} from '@loopback/rest';
import {Strategy} from 'passport';
import {UserProfile} from './types';
const passportRequestMixin = require('passport/lib/http/request');
/**
* Adapter class to invoke passport-strategy
* 1. provides express dependencies to the passport strategies
* 2. provides shimming of requests for passport authentication
* 3. provides lifecycle similar to express to the passport-strategy
* 3. provides state methods to the strategy instance
* see: https://github.com/jaredhanson/passport
*/
export class StrategyAdapter {
/**
* @param strategy instance of a class which implements a passport-strategy;
* @description http://passportjs.org/
*/
constructor(private readonly strategy: Strategy) {}
/**
* The function to invoke the contained passport strategy.
* 1. Create an instance of the strategy
* 2. add success and failure state handlers
* 3. authenticate using the strategy
* @param request The incoming request.
*/
authenticate(request: Request): Promise<UserProfile> {
return new Promise<UserProfile>((resolve, reject) => {
// mix-in passport additions like req.logIn and req.logOut
for (const key in passportRequestMixin) {
// tslint:disable-next-line:no-any
(request as any)[key] = passportRequestMixin[key];
}
// create a prototype chain of an instance of a passport strategy
const strategy = Object.create(this.strategy);
// add success state handler to strategy instance
strategy.success = function(user: UserProfile) {
resolve(user);
};
// add failure state handler to strategy instance
strategy.fail = function(challenge: string) {
reject(new HttpErrors.Unauthorized(challenge));
};
// add error state handler to strategy instance
strategy.error = function(error: string) {
reject(new HttpErrors.InternalServerError(error));
};
// authenticate
strategy.authenticate(request);
});
}
}