Browse Source

fix reviews

pull/18768/head
Sinan Öztürk 2 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 { from, of } from 'rxjs';
import { AuthFlowStrategy } from './auth-flow-strategy';
import { RememberMeService, isTokenExpired } from '../utils';
import { isTokenExpired } from '../utils';
export class AuthCodeFlowStrategy extends AuthFlowStrategy {
readonly isInternalAuth = false;
private rememberMe = 'remember_me'
rememberMeService = new RememberMeService(this.injector);
readonly #rememberMe = 'remember_me'
async init() {
this.checkRememberMeOption();
@ -21,22 +20,22 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
private checkRememberMeOption() {
const accessToken = this.oAuthService.getAccessToken();
const isTokenExpire = isTokenExpired(this.oAuthService);
let rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe()));
let rememberMe = this.rememberMeService.get();
if (accessToken && !rememberMe) {
let parsedToken = JSON.parse(atob(accessToken.split(".")[1]));
const rememberMeValue = Boolean(parsedToken[this.rememberMe]);
const rememberMeValue = Boolean(parsedToken[this.#rememberMe]);
if (rememberMeValue) {
this.rememberMeService.setRememberMe(true);
this.rememberMeService.set(true);
} else {
this.rememberMeService.setRememberMe(false)
this.rememberMeService.set(false)
}
}
rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe()));
rememberMe = this.rememberMeService.get();
if (accessToken && isTokenExpire && !rememberMe) {
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
this.oAuthService.logOut();
}
}
@ -57,7 +56,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
}
logout(queryParams?: Params) {
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
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 { OAuthErrorFilterService } from '../services';
import { isTokenExpired } from '../utils';
import { RememberMeService } from '../services/remember-me.service';
export abstract class AuthFlowStrategy {
abstract readonly isInternalAuth: boolean;
@ -36,6 +37,7 @@ export abstract class AuthFlowStrategy {
protected oAuthConfig!: AuthConfig;
protected sessionState: SessionStateService;
protected localStorageService: AbpLocalStorageService;
protected rememberMeService: RememberMeService;
protected tenantKey: string;
protected router: Router;
@ -62,6 +64,7 @@ export abstract class AuthFlowStrategy {
this.tenantKey = injector.get(TENANT_KEY);
this.router = injector.get(Router);
this.oAuthErrorFilterService = injector.get(OAuthErrorFilterService);
this.rememberMeService = injector.get(RememberMeService);
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 { HttpHeaders } from '@angular/common/http';
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 { clearOAuthStorage } from '../utils/clear-o-auth-storage';
export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
readonly isInternalAuth = true;
rememberMeService = new RememberMeService(this.injector);
private listenToTokenExpiration() {
this.oAuthService.events
@ -26,7 +25,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
this.refreshToken();
} else {
this.oAuthService.logOut();
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
this.configState.refreshAppState().subscribe();
}
});
@ -41,9 +40,9 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
private checkRememberMeOption(localStorageService: AbpLocalStorageService) {
const accessToken = this.oAuthService.getAccessToken();
const isTokenExpire = isTokenExpired(this.oAuthService);
const rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe()))
const rememberMe = this.rememberMeService.get();
if (accessToken && isTokenExpire && !rememberMe) {
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
this.oAuthService.logOut();
}
}
@ -76,7 +75,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
switchMap(() => this.configState.refreshAppState()),
tap(() => {
router.navigateByUrl('/');
this.rememberMeService.removeRememberMe();
this.rememberMeService.remove();
}),
);
}
@ -84,7 +83,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
protected refreshToken() {
return this.oAuthService.refreshToken().catch(() => {
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,
LoginParams,
PipeToLoginFn,
AbpLocalStorageService,
} from '@abp/ng.core';
import { OAuthService } from 'angular-oauth2-oidc';
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);
}
}
import { RememberMeService } from '../services/remember-me.service';
export const pipeToLogin: PipeToLoginFn = function (
params: Pick<LoginParams, 'redirectUrl' | 'rememberMe'>,
@ -35,11 +16,11 @@ export const pipeToLogin: PipeToLoginFn = function (
) {
const configState = injector.get(ConfigStateService);
const router = injector.get(Router);
const rememberMeService = new RememberMeService(injector);
const rememberMeService = injector.get(RememberMeService);
return pipe(
switchMap(() => configState.refreshAppState()),
tap(() => {
rememberMeService.setRememberMe(params.rememberMe);
rememberMeService.set(params.rememberMe);
if (params.redirectUrl) router.navigate([params.redirectUrl]);
}),
);

Loading…
Cancel
Save