-
Notifications
You must be signed in to change notification settings - Fork 70
/
user-validation.provider.ts
50 lines (48 loc) · 1.94 KB
/
user-validation.provider.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
import {BindingScope, Provider, inject, injectable} from '@loopback/core';
import {AnyObject} from '@loopback/repository';
import {ILogger, LOGGER} from '@sourceloop/core';
import {AuthenticationProviderFn, UserValidationFn} from '..';
import {SignUpProviderKey} from '../enums/sign-up-provider.enum';
import {AuthRefreshTokenRequest} from '../modules/auth';
import {UserValidationServiceBindings} from './keys';
@injectable({scope: BindingScope.TRANSIENT})
export class UserValidationProvider implements Provider<UserValidationFn> {
constructor(
@inject(LOGGER.LOGGER_INJECT) public logger: ILogger,
@inject(UserValidationServiceBindings.GOOGLE_AUTHENTICATION)
private googleAuthenticationProvider: AuthenticationProviderFn,
@inject(UserValidationServiceBindings.KEYCLOAK_AUTHENTICATION)
private keycloakAuthenticationProvider: AuthenticationProviderFn,
@inject(UserValidationServiceBindings.DEFAULT_AUTHENTICATION)
private defaultAuthneticationProvider: AuthenticationProviderFn,
) {}
value(): UserValidationFn {
return async (
req: AuthRefreshTokenRequest,
payload: AnyObject,
signUpProvider: string,
token?: string,
) => this.isAuthenticated(payload, signUpProvider, token);
}
async isAuthenticated(
payload: AnyObject,
signUpProvider?: string,
token?: string,
) {
let isAuthenticated = true;
const accessToken = payload.refreshPayload.externalAuthToken;
switch (signUpProvider) {
case SignUpProviderKey.Google:
isAuthenticated = await this.googleAuthenticationProvider(accessToken);
break;
case SignUpProviderKey.Keycloak:
isAuthenticated =
await this.keycloakAuthenticationProvider(accessToken);
break;
default:
isAuthenticated = await this.defaultAuthneticationProvider(accessToken);
// If none of the cases apply, the default case will set isAuthenticated to true
}
return isAuthenticated;
}
}