Browse Source

add password flow strategy

pull/7861/head
mehmet-erim 6 years ago
parent
commit
aea58a19f0
  1. 8
      npm/ng-packs/packages/core/src/lib/services/auth.service.ts
  2. 49
      npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts

8
npm/ng-packs/packages/core/src/lib/services/auth.service.ts

@ -20,13 +20,17 @@ export class AuthService {
} }
private setStrategy = () => { private setStrategy = () => {
const flow = this.environment.getEnvironment().oAuthConfig.responseType || 'password'; const flow =
this.environment.getEnvironment().oAuthConfig.responseType === 'code' ? 'code' : 'password';
if (this.flow === flow) return; if (this.flow === flow) return;
if (this.strategy) this.strategy.destroy(); if (this.strategy) this.strategy.destroy();
this.flow = flow; this.flow = flow;
this.strategy = AUTH_FLOW_STRATEGY.Code(this.injector); this.strategy =
flow === 'code'
? AUTH_FLOW_STRATEGY.Code(this.injector)
: AUTH_FLOW_STRATEGY.Password(this.injector);
}; };
private listenToSetEnvironment() { private listenToSetEnvironment() {

49
npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts

@ -1,10 +1,14 @@
import { Injector } from '@angular/core'; import { Injector } from '@angular/core';
import { Router } from '@angular/router';
import { Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import { AuthConfig, OAuthService, OAuthStorage } from 'angular-oauth2-oidc'; import { AuthConfig, OAuthService, OAuthStorage } from 'angular-oauth2-oidc';
import { Observable, of } from 'rxjs'; import { from, Observable, of } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { RestOccurError } from '../actions/rest.actions'; import { RestOccurError } from '../actions/rest.actions';
import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service';
import { ConfigStateService } from '../services/config-state.service'; import { ConfigStateService } from '../services/config-state.service';
import { EnvironmentService } from '../services/environment.service'; import { EnvironmentService } from '../services/environment.service';
import { RestService } from '../services/rest.service';
export const oAuthStorage = localStorage; export const oAuthStorage = localStorage;
@ -70,9 +74,43 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
} }
logout() { logout() {
this.oAuthService.revokeTokenAndLogout(); return from(this.oAuthService.revokeTokenAndLogout());
// TODO: no need to return of(null). It may be removed in v5.0. }
return of(null);
destroy() {}
}
export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
readonly isInternalAuth = true;
private appConfigService = this.injector.get(AbpApplicationConfigurationService);
login() {
const router = this.injector.get(Router);
router.navigateByUrl('/account/login');
}
checkIfInternalAuth() {
return true;
}
logout() {
const rest = this.injector.get(RestService);
const issuer = this.configState.getDeep('environment.oAuthConfig.issuer');
return rest
.request(
{
method: 'GET',
url: '/api/account/logout',
},
null,
issuer,
)
.pipe(
switchMap(() => from(this.oAuthService.revokeTokenAndLogout())),
switchMap(() => this.appConfigService.get()),
tap(res => this.configState.setState(res)),
);
} }
destroy() {} destroy() {}
@ -82,6 +120,9 @@ export const AUTH_FLOW_STRATEGY = {
Code(injector: Injector) { Code(injector: Injector) {
return new AuthCodeFlowStrategy(injector); return new AuthCodeFlowStrategy(injector);
}, },
Password(injector: Injector) {
return new AuthPasswordFlowStrategy(injector);
},
}; };
export function clearOAuthStorage(storage: OAuthStorage = oAuthStorage) { export function clearOAuthStorage(storage: OAuthStorage = oAuthStorage) {

Loading…
Cancel
Save