|
|
@ -1,6 +1,6 @@ |
|
|
import { Injector } from '@angular/core'; |
|
|
import { Injector } from '@angular/core'; |
|
|
import { Store } from '@ngxs/store'; |
|
|
import { Store } from '@ngxs/store'; |
|
|
import { AuthConfig, OAuthService, OAuthSuccessEvent } from 'angular-oauth2-oidc'; |
|
|
import { AuthConfig, OAuthService } from 'angular-oauth2-oidc'; |
|
|
import { ConfigState } from '../states/config.state'; |
|
|
import { ConfigState } from '../states/config.state'; |
|
|
import { CORE_OPTIONS } from '../tokens/options.token'; |
|
|
import { CORE_OPTIONS } from '../tokens/options.token'; |
|
|
import { Router } from '@angular/router'; |
|
|
import { Router } from '@angular/router'; |
|
|
@ -9,12 +9,18 @@ import { RestService } from '../services/rest.service'; |
|
|
import { switchMap } from 'rxjs/operators'; |
|
|
import { switchMap } from 'rxjs/operators'; |
|
|
import { GetAppConfiguration } from '../actions/config.actions'; |
|
|
import { GetAppConfiguration } from '../actions/config.actions'; |
|
|
|
|
|
|
|
|
export abstract class OAuthStrategy { |
|
|
export abstract class AuthFlowStrategy { |
|
|
|
|
|
protected abstract _isInternalAuth: boolean; |
|
|
|
|
|
get isInternalAuth(): boolean { |
|
|
|
|
|
return this._isInternalAuth; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
protected oAuthService: OAuthService; |
|
|
protected oAuthService: OAuthService; |
|
|
protected oAuthConfig: AuthConfig; |
|
|
protected oAuthConfig: AuthConfig; |
|
|
abstract navigateToLogin(): void; |
|
|
abstract checkIfInternalAuth(): boolean; |
|
|
abstract canActivate(): boolean; |
|
|
abstract login(): void; |
|
|
abstract logOut(): Observable<any>; |
|
|
abstract logout(): Observable<any>; |
|
|
|
|
|
abstract destroy(): void; |
|
|
|
|
|
|
|
|
private catchError = err => { |
|
|
private catchError = err => { |
|
|
// TODO: handle the error
|
|
|
// TODO: handle the error
|
|
|
@ -31,7 +37,9 @@ export abstract class OAuthStrategy { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export class OAuthCodeFlowStrategy extends OAuthStrategy { |
|
|
export class AuthCodeFlowStrategy extends AuthFlowStrategy { |
|
|
|
|
|
protected _isInternalAuth = false; |
|
|
|
|
|
|
|
|
async init() { |
|
|
async init() { |
|
|
return super |
|
|
return super |
|
|
.init() |
|
|
.init() |
|
|
@ -39,32 +47,36 @@ export class OAuthCodeFlowStrategy extends OAuthStrategy { |
|
|
.then(() => this.oAuthService.setupAutomaticSilentRefresh()); |
|
|
.then(() => this.oAuthService.setupAutomaticSilentRefresh()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
navigateToLogin() { |
|
|
login() { |
|
|
this.oAuthService.initCodeFlow(); |
|
|
this.oAuthService.initCodeFlow(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
canActivate() { |
|
|
checkIfInternalAuth() { |
|
|
this.oAuthService.initCodeFlow(); |
|
|
this.oAuthService.initCodeFlow(); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logOut() { |
|
|
logout() { |
|
|
this.oAuthService.logOut(); |
|
|
this.oAuthService.logOut(); |
|
|
return of(null); |
|
|
return of(null); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
destroy() {} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export class OAuthPasswordFlowStrategy extends OAuthStrategy { |
|
|
export class AuthPasswordFlowStrategy extends AuthFlowStrategy { |
|
|
navigateToLogin() { |
|
|
protected _isInternalAuth = true; |
|
|
|
|
|
|
|
|
|
|
|
login() { |
|
|
const router = this.injector.get(Router); |
|
|
const router = this.injector.get(Router); |
|
|
router.navigateByUrl('/account/login'); |
|
|
router.navigateByUrl('/account/login'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
canActivate() { |
|
|
checkIfInternalAuth() { |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
logOut() { |
|
|
logout() { |
|
|
const store = this.injector.get(Store); |
|
|
const store = this.injector.get(Store); |
|
|
const rest = this.injector.get(RestService); |
|
|
const rest = this.injector.get(RestService); |
|
|
|
|
|
|
|
|
@ -85,28 +97,15 @@ export class OAuthPasswordFlowStrategy extends OAuthStrategy { |
|
|
}), |
|
|
}), |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
destroy() {} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export const OAUTH_STRATEGY = { |
|
|
export const AUTH_FLOW_STRATEGY = { |
|
|
async Init(injector: Injector) { |
|
|
Code(injector: Injector) { |
|
|
return getOAuthStrategy(injector).init(); |
|
|
return new AuthCodeFlowStrategy(injector); |
|
|
}, |
|
|
}, |
|
|
NavigateToLogin(injector: Injector) { |
|
|
Password(injector: Injector) { |
|
|
return getOAuthStrategy(injector).navigateToLogin(); |
|
|
return new AuthPasswordFlowStrategy(injector); |
|
|
}, |
|
|
|
|
|
CanActivate(injector: Injector) { |
|
|
|
|
|
return getOAuthStrategy(injector).canActivate(); |
|
|
|
|
|
}, |
|
|
|
|
|
LogOut(injector: Injector) { |
|
|
|
|
|
return getOAuthStrategy(injector).logOut(); |
|
|
|
|
|
}, |
|
|
}, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
function getOAuthStrategy(injector: Injector) { |
|
|
|
|
|
const codeFlow = |
|
|
|
|
|
injector |
|
|
|
|
|
.get(Store) |
|
|
|
|
|
.selectSnapshot(ConfigState.getDeep('environment.oAuthConfig.responseType')) === 'code'; |
|
|
|
|
|
|
|
|
|
|
|
return codeFlow ? new OAuthCodeFlowStrategy(injector) : new OAuthPasswordFlowStrategy(injector); |
|
|
|
|
|
} |
|
|
|