Browse Source

feat: remove the password flow

pull/6168/head
mehmet-erim 6 years ago
parent
commit
6ebdecebfa
  1. 22
      npm/ng-packs/packages/core/src/lib/guards/auth.guard.ts
  2. 51
      npm/ng-packs/packages/core/src/lib/services/auth.service.ts
  3. 42
      npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts

22
npm/ng-packs/packages/core/src/lib/guards/auth.guard.ts

@ -1,32 +1,22 @@
import { Injectable, Injector } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivate,
Router,
RouterStateSnapshot,
UrlTree,
} from '@angular/router';
import { Injectable } from '@angular/core';
import { CanActivate, UrlTree } from '@angular/router';
import { OAuthService } from 'angular-oauth2-oidc';
import { Observable } from 'rxjs';
import { AuthService } from '../services/auth.service';
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
constructor(private oauthService: OAuthService, private injector: Injector) {}
canActivate(
_: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
): Observable<boolean> | boolean | UrlTree {
const router = this.injector.get(Router);
constructor(private oauthService: OAuthService, private authService: AuthService) {}
canActivate(): Observable<boolean> | boolean | UrlTree {
const hasValidAccessToken = this.oauthService.hasValidAccessToken();
if (hasValidAccessToken) {
return hasValidAccessToken;
}
router.navigate(['/account/login'], { state: { redirectUrl: state.url } });
this.authService.initLogin();
return true;
}
}

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

@ -1,16 +1,7 @@
import { HttpHeaders } from '@angular/common/http';
import { Inject, Injectable, Injector, Optional } from '@angular/core';
import { Navigate } from '@ngxs/router-plugin';
import { Store } from '@ngxs/store';
import { OAuthService } from 'angular-oauth2-oidc';
import { from, Observable } from 'rxjs';
import { switchMap, take, tap } from 'rxjs/operators';
import snq from 'snq';
import { Injectable, Injector } from '@angular/core';
import { Observable } from 'rxjs';
import { AuthFlowStrategy, AUTH_FLOW_STRATEGY } from '../strategies/auth-flow.strategy';
import { ApplicationConfigurationService } from './application-configuration.service';
import { ConfigStateService } from './config-state.service';
import { EnvironmentService } from './environment.service';
import { SessionStateService } from './session-state.service';
@Injectable({
providedIn: 'root',
@ -23,16 +14,7 @@ export class AuthService {
return this.strategy.isInternalAuth;
}
constructor(
private environment: EnvironmentService,
private injector: Injector,
private oAuthService: OAuthService,
private store: Store,
private sessionState: SessionStateService,
private configState: ConfigStateService,
private appConfigService: ApplicationConfigurationService,
@Optional() @Inject('ACCOUNT_OPTIONS') private options: any,
) {
constructor(private environment: EnvironmentService, private injector: Injector) {
this.setStrategy();
this.listenToSetEnvironment();
}
@ -44,38 +26,13 @@ export class AuthService {
if (this.strategy) this.strategy.destroy();
this.flow = flow;
this.strategy =
this.flow === 'code'
? AUTH_FLOW_STRATEGY.Code(this.injector)
: AUTH_FLOW_STRATEGY.Password(this.injector);
this.strategy = AUTH_FLOW_STRATEGY.Code(this.injector);
};
private listenToSetEnvironment() {
this.environment.createOnUpdateStream(state => state.oAuthConfig).subscribe(this.setStrategy);
}
login(username: string, password: string): Observable<any> {
const tenant = this.sessionState.getTenant();
return from(
this.oAuthService.fetchTokenUsingPasswordFlow(
username,
password,
new HttpHeaders({ ...(tenant && tenant.id && { __tenant: tenant.id }) }),
),
).pipe(
switchMap(() =>
this.appConfigService.getConfiguration().pipe(tap(res => this.configState.setState(res))),
),
tap(() => {
const redirectUrl =
snq(() => window.history.state.redirectUrl) || (this.options || {}).redirectUrl || '/';
this.store.dispatch(new Navigate([redirectUrl]));
}),
take(1),
);
}
async init() {
return await this.strategy.init();
}

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

@ -1,14 +1,11 @@
import { Injector } from '@angular/core';
import { Router } from '@angular/router';
import { Store } from '@ngxs/store';
import { AuthConfig, OAuthService, OAuthStorage } from 'angular-oauth2-oidc';
import { Observable, of } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { RestOccurError } from '../actions/rest.actions';
import { ApplicationConfigurationService } from '../services/application-configuration.service';
import { ConfigStateService } from '../services/config-state.service';
import { EnvironmentService } from '../services/environment.service';
import { RestService } from '../services/rest.service';
export const oAuthStorage = localStorage;
@ -83,49 +80,10 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
destroy() {}
}
export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
readonly isInternalAuth = true;
login() {
const router = this.injector.get(Router);
router.navigateByUrl('/account/login');
}
checkIfInternalAuth() {
return true;
}
logout() {
const rest = this.injector.get(RestService);
const issuer = this.environment.getEnvironment().oAuthConfig.issuer;
return rest
.request(
{
method: 'GET',
url: '/api/account/logout',
},
null,
issuer,
)
.pipe(
tap(() => this.oAuthService.logOut()),
switchMap(() =>
this.appConfigService.getConfiguration().pipe(tap(res => this.configState.setState(res))),
),
);
}
destroy() {}
}
export const AUTH_FLOW_STRATEGY = {
Code(injector: Injector) {
return new AuthCodeFlowStrategy(injector);
},
Password(injector: Injector) {
return new AuthPasswordFlowStrategy(injector);
},
};
export function clearOAuthStorage(storage: OAuthStorage = oAuthStorage) {

Loading…
Cancel
Save