Browse Source

fix reviews

pull/18768/head
Sinan Öztürk 3 years ago
parent
commit
4049971cff
  1. 24
      npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts
  2. 19
      npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts
  3. 3
      npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts
  4. 13
      npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts
  5. 25
      npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts

24
npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts

@ -0,0 +1,24 @@
import { AbpLocalStorageService } from "@abp/ng.core";
import { Injectable, Injector } from "@angular/core";
const rememberMe = 'remember_me';
@Injectable({
providedIn: 'root'
})
export class RememberMeService {
constructor(private injector: Injector) { }
localStorageService = this.injector.get(AbpLocalStorageService);
set(remember: boolean) {
this.localStorageService.setItem(rememberMe, JSON.stringify(remember));
}
remove() {
this.localStorageService.removeItem(rememberMe);
}
get() {
return Boolean(JSON.parse(this.localStorageService.getItem(rememberMe)));
}
}

19
npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts

@ -2,12 +2,11 @@ import { noop } from '@abp/ng.core';
import { Params } from '@angular/router'; import { Params } from '@angular/router';
import { from, of } from 'rxjs'; import { from, of } from 'rxjs';
import { AuthFlowStrategy } from './auth-flow-strategy'; import { AuthFlowStrategy } from './auth-flow-strategy';
import { RememberMeService, isTokenExpired } from '../utils'; import { isTokenExpired } from '../utils';
export class AuthCodeFlowStrategy extends AuthFlowStrategy { export class AuthCodeFlowStrategy extends AuthFlowStrategy {
readonly isInternalAuth = false; readonly isInternalAuth = false;
private rememberMe = 'remember_me' readonly #rememberMe = 'remember_me'
rememberMeService = new RememberMeService(this.injector);
async init() { async init() {
this.checkRememberMeOption(); this.checkRememberMeOption();
@ -21,22 +20,22 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
private checkRememberMeOption() { private checkRememberMeOption() {
const accessToken = this.oAuthService.getAccessToken(); const accessToken = this.oAuthService.getAccessToken();
const isTokenExpire = isTokenExpired(this.oAuthService); const isTokenExpire = isTokenExpired(this.oAuthService);
let rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe())); let rememberMe = this.rememberMeService.get();
if (accessToken && !rememberMe) { if (accessToken && !rememberMe) {
let parsedToken = JSON.parse(atob(accessToken.split(".")[1])); let parsedToken = JSON.parse(atob(accessToken.split(".")[1]));
const rememberMeValue = Boolean(parsedToken[this.rememberMe]); const rememberMeValue = Boolean(parsedToken[this.#rememberMe]);
if (rememberMeValue) { if (rememberMeValue) {
this.rememberMeService.setRememberMe(true); this.rememberMeService.set(true);
} else { } else {
this.rememberMeService.setRememberMe(false) this.rememberMeService.set(false)
} }
} }
rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe())); rememberMe = this.rememberMeService.get();
if (accessToken && isTokenExpire && !rememberMe) { if (accessToken && isTokenExpire && !rememberMe) {
this.rememberMeService.removeRememberMe(); this.rememberMeService.remove();
this.oAuthService.logOut(); this.oAuthService.logOut();
} }
} }
@ -57,7 +56,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
} }
logout(queryParams?: Params) { logout(queryParams?: Params) {
this.rememberMeService.removeRememberMe(); this.rememberMeService.remove();
return from(this.oAuthService.revokeTokenAndLogout(this.getCultureParams(queryParams))); return from(this.oAuthService.revokeTokenAndLogout(this.getCultureParams(queryParams)));
} }

3
npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts

@ -25,6 +25,7 @@ import { clearOAuthStorage } from '../utils/clear-o-auth-storage';
import { oAuthStorage } from '../utils/oauth-storage'; import { oAuthStorage } from '../utils/oauth-storage';
import { OAuthErrorFilterService } from '../services'; import { OAuthErrorFilterService } from '../services';
import { isTokenExpired } from '../utils'; import { isTokenExpired } from '../utils';
import { RememberMeService } from '../services/remember-me.service';
export abstract class AuthFlowStrategy { export abstract class AuthFlowStrategy {
abstract readonly isInternalAuth: boolean; abstract readonly isInternalAuth: boolean;
@ -36,6 +37,7 @@ export abstract class AuthFlowStrategy {
protected oAuthConfig!: AuthConfig; protected oAuthConfig!: AuthConfig;
protected sessionState: SessionStateService; protected sessionState: SessionStateService;
protected localStorageService: AbpLocalStorageService; protected localStorageService: AbpLocalStorageService;
protected rememberMeService: RememberMeService;
protected tenantKey: string; protected tenantKey: string;
protected router: Router; protected router: Router;
@ -62,6 +64,7 @@ export abstract class AuthFlowStrategy {
this.tenantKey = injector.get(TENANT_KEY); this.tenantKey = injector.get(TENANT_KEY);
this.router = injector.get(Router); this.router = injector.get(Router);
this.oAuthErrorFilterService = injector.get(OAuthErrorFilterService); this.oAuthErrorFilterService = injector.get(OAuthErrorFilterService);
this.rememberMeService = injector.get(RememberMeService);
this.listenToOauthErrors(); this.listenToOauthErrors();
} }

13
npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts

@ -4,13 +4,12 @@ import { Params, Router } from '@angular/router';
import { from, Observable } from 'rxjs'; import { from, Observable } from 'rxjs';
import { HttpHeaders } from '@angular/common/http'; import { HttpHeaders } from '@angular/common/http';
import { AuthFlowStrategy } from './auth-flow-strategy'; import { AuthFlowStrategy } from './auth-flow-strategy';
import { RememberMeService, isTokenExpired, pipeToLogin } from '../utils/auth-utils'; import { isTokenExpired, pipeToLogin } from '../utils/auth-utils';
import { AbpLocalStorageService, LoginParams } from '@abp/ng.core'; import { AbpLocalStorageService, LoginParams } from '@abp/ng.core';
import { clearOAuthStorage } from '../utils/clear-o-auth-storage'; import { clearOAuthStorage } from '../utils/clear-o-auth-storage';
export class AuthPasswordFlowStrategy extends AuthFlowStrategy { export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
readonly isInternalAuth = true; readonly isInternalAuth = true;
rememberMeService = new RememberMeService(this.injector);
private listenToTokenExpiration() { private listenToTokenExpiration() {
this.oAuthService.events this.oAuthService.events
@ -26,7 +25,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
this.refreshToken(); this.refreshToken();
} else { } else {
this.oAuthService.logOut(); this.oAuthService.logOut();
this.rememberMeService.removeRememberMe(); this.rememberMeService.remove();
this.configState.refreshAppState().subscribe(); this.configState.refreshAppState().subscribe();
} }
}); });
@ -41,9 +40,9 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
private checkRememberMeOption(localStorageService: AbpLocalStorageService) { private checkRememberMeOption(localStorageService: AbpLocalStorageService) {
const accessToken = this.oAuthService.getAccessToken(); const accessToken = this.oAuthService.getAccessToken();
const isTokenExpire = isTokenExpired(this.oAuthService); const isTokenExpire = isTokenExpired(this.oAuthService);
const rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe())) const rememberMe = this.rememberMeService.get();
if (accessToken && isTokenExpire && !rememberMe) { if (accessToken && isTokenExpire && !rememberMe) {
this.rememberMeService.removeRememberMe(); this.rememberMeService.remove();
this.oAuthService.logOut(); this.oAuthService.logOut();
} }
} }
@ -76,7 +75,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
switchMap(() => this.configState.refreshAppState()), switchMap(() => this.configState.refreshAppState()),
tap(() => { tap(() => {
router.navigateByUrl('/'); router.navigateByUrl('/');
this.rememberMeService.removeRememberMe(); this.rememberMeService.remove();
}), }),
); );
} }
@ -84,7 +83,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
protected refreshToken() { protected refreshToken() {
return this.oAuthService.refreshToken().catch(() => { return this.oAuthService.refreshToken().catch(() => {
clearOAuthStorage(); clearOAuthStorage();
this.rememberMeService.removeRememberMe(); this.rememberMeService.remove();
}); });
} }
} }

25
npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts

@ -6,28 +6,9 @@ import {
ConfigStateService, ConfigStateService,
LoginParams, LoginParams,
PipeToLoginFn, PipeToLoginFn,
AbpLocalStorageService,
} from '@abp/ng.core'; } from '@abp/ng.core';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService } from 'angular-oauth2-oidc';
import { RememberMeService } from '../services/remember-me.service';
const rememberMe = 'remember_me';
export class RememberMeService {
constructor(private injector: Injector) { }
localStorageService = this.injector.get(AbpLocalStorageService);
setRememberMe(remember: boolean) {
this.localStorageService.setItem(rememberMe, JSON.stringify(remember));
}
removeRememberMe() {
this.localStorageService.removeItem(rememberMe);
}
getRememberMe() {
return this.localStorageService.getItem(rememberMe);
}
}
export const pipeToLogin: PipeToLoginFn = function ( export const pipeToLogin: PipeToLoginFn = function (
params: Pick<LoginParams, 'redirectUrl' | 'rememberMe'>, params: Pick<LoginParams, 'redirectUrl' | 'rememberMe'>,
@ -35,11 +16,11 @@ export const pipeToLogin: PipeToLoginFn = function (
) { ) {
const configState = injector.get(ConfigStateService); const configState = injector.get(ConfigStateService);
const router = injector.get(Router); const router = injector.get(Router);
const rememberMeService = new RememberMeService(injector); const rememberMeService = injector.get(RememberMeService);
return pipe( return pipe(
switchMap(() => configState.refreshAppState()), switchMap(() => configState.refreshAppState()),
tap(() => { tap(() => {
rememberMeService.setRememberMe(params.rememberMe); rememberMeService.set(params.rememberMe);
if (params.redirectUrl) router.navigate([params.redirectUrl]); if (params.redirectUrl) router.navigate([params.redirectUrl]);
}), }),
); );

Loading…
Cancel
Save