From fc10fe1fd09c26b5ce0d81b8b19d82f2f6bdce66 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Tue, 1 Jun 2021 14:54:44 +0300 Subject: [PATCH 1/2] create auth utils and move some methods to there --- .../packages/core/src/lib/models/auth.ts | 6 +++ .../packages/core/src/lib/models/index.ts | 1 + .../core/src/lib/services/auth.service.ts | 7 +--- .../src/lib/strategies/auth-flow.strategy.ts | 41 +++++++------------ .../packages/core/src/lib/utils/auth-utils.ts | 41 +++++++++++++++++++ .../packages/core/src/lib/utils/index.ts | 1 + 6 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 npm/ng-packs/packages/core/src/lib/models/auth.ts create mode 100644 npm/ng-packs/packages/core/src/lib/utils/auth-utils.ts diff --git a/npm/ng-packs/packages/core/src/lib/models/auth.ts b/npm/ng-packs/packages/core/src/lib/models/auth.ts new file mode 100644 index 0000000000..5621dc8655 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/models/auth.ts @@ -0,0 +1,6 @@ +export interface LoginParams { + username: string; + password: string; + rememberMe?: boolean; + redirectUrl?: string; +} diff --git a/npm/ng-packs/packages/core/src/lib/models/index.ts b/npm/ng-packs/packages/core/src/lib/models/index.ts index ea58251ef8..f9f2bccb3f 100644 --- a/npm/ng-packs/packages/core/src/lib/models/index.ts +++ b/npm/ng-packs/packages/core/src/lib/models/index.ts @@ -1,4 +1,5 @@ export * from './application-configuration'; +export * from './auth'; export * from './common'; export * from './config'; export * from './dtos'; diff --git a/npm/ng-packs/packages/core/src/lib/services/auth.service.ts b/npm/ng-packs/packages/core/src/lib/services/auth.service.ts index 752721e41a..ea73adce49 100644 --- a/npm/ng-packs/packages/core/src/lib/services/auth.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/auth.service.ts @@ -2,11 +2,8 @@ import { Injectable, Injector } from '@angular/core'; import { Params } from '@angular/router'; import { from, Observable } from 'rxjs'; import { filter, map, switchMap, take, tap } from 'rxjs/operators'; -import { - AuthFlowStrategy, - AUTH_FLOW_STRATEGY, - LoginParams, -} from '../strategies/auth-flow.strategy'; +import { LoginParams } from '../models/auth'; +import { AuthFlowStrategy, AUTH_FLOW_STRATEGY } from '../strategies/auth-flow.strategy'; import { EnvironmentService } from './environment.service'; @Injectable({ diff --git a/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts b/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts index 1d79311f9c..dc6b072d4f 100644 --- a/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts +++ b/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts @@ -9,22 +9,17 @@ import { OAuthService, OAuthStorage, } from 'angular-oauth2-oidc'; -import { from, Observable, of } from 'rxjs'; +import { from, Observable, of, pipe } from 'rxjs'; import { filter, switchMap, tap } from 'rxjs/operators'; import { RestOccurError } from '../actions/rest.actions'; +import { LoginParams } from '../models/auth'; import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; import { ConfigStateService } from '../services/config-state.service'; import { EnvironmentService } from '../services/environment.service'; import { SessionStateService } from '../services/session-state.service'; +import { removeRememberMe, setRememberMe } from '../utils/auth-utils'; import { noop } from '../utils/common-utils'; -export interface LoginParams { - username: string; - password: string; - rememberMe?: boolean; - redirectUrl?: string; -} - export const oAuthStorage = localStorage; export abstract class AuthFlowStrategy { @@ -145,7 +140,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { this.refreshToken(); } else { this.oAuthService.logOut(); - this.removeRememberMe(); + removeRememberMe(); this.appConfigService.get().subscribe(res => { this.configState.setState(res); }); @@ -153,19 +148,6 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { }); } - private setRememberMe(remember: boolean) { - this.removeRememberMe(); - localStorage.setItem(this.storageKey, 'true'); - document.cookie = `${this.cookieKey}=true; path=/${ - remember ? ' ;expires=Fri, 31 Dec 9999 23:59:59 GMT' : '' - }`; - } - - private removeRememberMe() { - localStorage.removeItem(this.storageKey); - document.cookie = this.cookieKey + '= ; path=/; expires = Thu, 01 Jan 1970 00:00:00 GMT'; - } - async init() { if (!getCookieValueByName(this.cookieKey) && localStorage.getItem(this.storageKey)) { this.oAuthService.logOut(); @@ -184,7 +166,6 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { } login(params: LoginParams): Observable { - const router = this.injector.get(Router); const tenant = this.sessionState.getTenant(); return from( @@ -193,11 +174,17 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { params.password, new HttpHeaders({ ...(tenant && tenant.id && { __tenant: tenant.id }) }), ), - ).pipe( + ).pipe(this.pipeToLogin(params)); + } + + pipeToLogin(params: Pick) { + const router = this.injector.get(Router); + + return pipe( switchMap(() => this.appConfigService.get()), tap(res => { this.configState.setState(res); - this.setRememberMe(params.rememberMe); + setRememberMe(params.rememberMe); if (params.redirectUrl) router.navigate([params.redirectUrl]); }), ); @@ -211,7 +198,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { tap(res => { this.configState.setState(res); router.navigateByUrl('/'); - this.removeRememberMe(); + removeRememberMe(); }), ); } @@ -219,7 +206,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { protected refreshToken() { return this.oAuthService.refreshToken().catch(() => { clearOAuthStorage(); - this.removeRememberMe(); + removeRememberMe(); }); } } diff --git a/npm/ng-packs/packages/core/src/lib/utils/auth-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/auth-utils.ts new file mode 100644 index 0000000000..59c1c9438b --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/utils/auth-utils.ts @@ -0,0 +1,41 @@ +import { Injector } from '@angular/core'; +import { Router } from '@angular/router'; +import { pipe } from 'rxjs'; +import { switchMap, tap } from 'rxjs/operators'; +import { LoginParams } from '../models/auth'; +import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; +import { ConfigStateService } from '../services/config-state.service'; + +const cookieKey = 'rememberMe'; +const storageKey = 'passwordFlow'; + +export function pipeToLogin( + params: Pick, + injector: Injector, +) { + const configState = injector.get(ConfigStateService); + const appConfigService = injector.get(AbpApplicationConfigurationService); + const router = injector.get(Router); + + return pipe( + switchMap(() => appConfigService.get()), + tap(res => { + configState.setState(res); + setRememberMe(params.rememberMe); + if (params.redirectUrl) router.navigate([params.redirectUrl]); + }), + ); +} + +export function setRememberMe(remember: boolean) { + removeRememberMe(); + localStorage.setItem(storageKey, 'true'); + document.cookie = `${cookieKey}=true; path=/${ + remember ? ' ;expires=Fri, 31 Dec 9999 23:59:59 GMT' : '' + }`; +} + +export function removeRememberMe() { + localStorage.removeItem(storageKey); + document.cookie = cookieKey + '= ; path=/; expires = Thu, 01 Jan 1970 00:00:00 GMT'; +} diff --git a/npm/ng-packs/packages/core/src/lib/utils/index.ts b/npm/ng-packs/packages/core/src/lib/utils/index.ts index d50fa7712f..e7fe63deb0 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/index.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/index.ts @@ -1,4 +1,5 @@ export * from './array-utils'; +export * from './auth-utils'; export * from './common-utils'; export * from './date-utils'; export * from './environment-utils'; From c7ce181d87b4b6c3ad03665f145ad60c143cb0c0 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Tue, 1 Jun 2021 14:54:55 +0300 Subject: [PATCH 2/2] add WebHttpUrlEncodingCodec class to http-utils --- .../packages/core/src/lib/utils/http-utils.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/npm/ng-packs/packages/core/src/lib/utils/http-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/http-utils.ts index 5b821aa04c..032674acec 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/http-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/http-utils.ts @@ -1,4 +1,24 @@ +import { HttpParameterCodec } from '@angular/common/http'; + export function getPathName(url: string): string { const { pathname } = new URL(url, window.location.origin); return pathname; } + +export class WebHttpUrlEncodingCodec implements HttpParameterCodec { + encodeKey(k: string): string { + return encodeURIComponent(k); + } + + encodeValue(v: string): string { + return encodeURIComponent(v); + } + + decodeKey(k: string): string { + return decodeURIComponent(k); + } + + decodeValue(v: string) { + return decodeURIComponent(v); + } +}