Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remember me implementation #18768

Merged
merged 11 commits into from
Jan 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { noop } from '@abp/ng.core';
import { Params } from '@angular/router';
import { from, of } from 'rxjs';
import { AuthFlowStrategy } from './auth-flow-strategy';
import { getRememberMe, removeRememberMe, setRememberMe } from '../utils';
import { RememberMeService, isTokenExpired } from '../utils';

export class AuthCodeFlowStrategy extends AuthFlowStrategy {
readonly isInternalAuth = false;
private remember_me = 'remember_me'
private rememberMe = 'remember_me'
Sinan997 marked this conversation as resolved.
Show resolved Hide resolved
rememberMeService = new RememberMeService(this.injector);
Sinan997 marked this conversation as resolved.
Show resolved Hide resolved

async init() {
this.checkRememberMeOption();
Expand All @@ -19,24 +20,23 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {

private checkRememberMeOption() {
const accessToken = this.oAuthService.getAccessToken();
const expireDate = this.oAuthService.getAccessTokenExpiration();
const currentDate = new Date().getTime();
let rememberMe = getRememberMe(this.localStorageService);
const isTokenExpire = isTokenExpired(this.oAuthService);
let rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe()));

if (accessToken && rememberMe === null) {
if (accessToken && !rememberMe) {
let parsedToken = JSON.parse(atob(accessToken.split(".")[1]));
let rememberMeValue = parsedToken[this.remember_me];
const rememberMeValue = Boolean(parsedToken[this.rememberMe]);

if (rememberMeValue && (rememberMeValue === 'True' || rememberMeValue === 'true')) {
setRememberMe(true, this.localStorageService);
if (rememberMeValue) {
this.rememberMeService.setRememberMe(true);
} else {
setRememberMe(false, this.localStorageService)
this.rememberMeService.setRememberMe(false)
}
Sinan997 marked this conversation as resolved.
Show resolved Hide resolved
}

rememberMe = getRememberMe(this.localStorageService);
if (accessToken && expireDate < currentDate && rememberMe !== 'true') {
removeRememberMe(this.localStorageService);
rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe()));
if (accessToken && isTokenExpire && !rememberMe) {
this.rememberMeService.removeRememberMe();
this.oAuthService.logOut();
}
}
Expand All @@ -57,7 +57,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy {
}

logout(queryParams?: Params) {
removeRememberMe(this.localStorageService);
this.rememberMeService.removeRememberMe();
return from(this.oAuthService.revokeTokenAndLogout(this.getCultureParams(queryParams)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { clearOAuthStorage } from '../utils/clear-o-auth-storage';
import { oAuthStorage } from '../utils/oauth-storage';
import { OAuthErrorFilterService } from '../services';
import { isTokenExpired } from '../utils';

export abstract class AuthFlowStrategy {
abstract readonly isInternalAuth: boolean;
Expand Down Expand Up @@ -78,11 +79,12 @@ export abstract class AuthFlowStrategy {
return this.oAuthService
.loadDiscoveryDocument()
.then(() => {
if (this.oAuthService.hasValidAccessToken() || !this.oAuthService.getRefreshToken()) {
return Promise.resolve();
const isTokenExpire = isTokenExpired(this.oAuthService);
if (!isTokenExpire || this.oAuthService.getRefreshToken()) {
return this.refreshToken();
}

return this.refreshToken();
return Promise.resolve();
})
.catch(this.catchError);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,46 @@ import { Params, Router } from '@angular/router';
import { from, Observable } from 'rxjs';
import { HttpHeaders } from '@angular/common/http';
import { AuthFlowStrategy } from './auth-flow-strategy';
import { getRememberMe, pipeToLogin, removeRememberMe } from '../utils/auth-utils';
import { RememberMeService, 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;
private cookieKey = 'rememberMe';
private storageKey = 'passwordFlow';
rememberMeService = new RememberMeService(this.injector);

private listenToTokenExpiration() {
this.oAuthService.events
.pipe(
filter(
event => {
return event instanceof OAuthInfoEvent &&
event => event instanceof OAuthInfoEvent &&
event.type === 'token_expires' &&
event.info === 'access_token'
}
),
)
.subscribe(() => {
if (this.oAuthService.getRefreshToken()) {
this.refreshToken();
} else {
this.oAuthService.logOut();
removeRememberMe(this.localStorageService);
this.rememberMeService.removeRememberMe();
this.configState.refreshAppState().subscribe();
}
});
}

async init() {
this.checkRememberMeOption(this.localStorageService);
this.checkRememberMeOption(this.localStorageService);

return super.init().then(() => this.listenToTokenExpiration());
}

private checkRememberMeOption(localStorageService: AbpLocalStorageService) {
const accessToken = this.oAuthService.getAccessToken();
const expireDate = this.oAuthService.getAccessTokenExpiration();
const currentDate = new Date().getTime();
const rememberMe = getRememberMe(localStorageService);

if (accessToken && expireDate < currentDate && rememberMe !== 'true') {
removeRememberMe(this.localStorageService);
const isTokenExpire = isTokenExpired(this.oAuthService);
const rememberMe = Boolean(JSON.parse(this.rememberMeService.getRememberMe()))
if (accessToken && isTokenExpire && !rememberMe) {
this.rememberMeService.removeRememberMe();
this.oAuthService.logOut();
}
}
Expand All @@ -73,23 +68,23 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy {
),
).pipe(pipeToLogin(params, this.injector));
}

logout() {
const router = this.injector.get(Router);
const noRedirectToLogoutUrl = true;
return from(this.oAuthService.revokeTokenAndLogout(noRedirectToLogoutUrl)).pipe(
switchMap(() => this.configState.refreshAppState()),
tap(() => {
router.navigateByUrl('/');
removeRememberMe(this.localStorageService);
this.rememberMeService.removeRememberMe();
}),
);
}

protected refreshToken() {
return this.oAuthService.refreshToken().catch(() => {
clearOAuthStorage();
removeRememberMe(this.localStorageService);
this.rememberMeService.removeRememberMe();
});
}
}
41 changes: 25 additions & 16 deletions npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,45 @@ import {
PipeToLoginFn,
AbpLocalStorageService,
} from '@abp/ng.core';
import { OAuthService } from 'angular-oauth2-oidc';

const remember_me = 'remember_me';
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);
Sinan997 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Sinan997 marked this conversation as resolved.
Show resolved Hide resolved

export const pipeToLogin: PipeToLoginFn = function (
params: Pick<LoginParams, 'redirectUrl' | 'rememberMe'>,
injector: Injector,
) {
const configState = injector.get(ConfigStateService);
const router = injector.get(Router);
const localStorage = injector.get(AbpLocalStorageService);
const rememberMeService = new RememberMeService(injector);
return pipe(
switchMap(() => configState.refreshAppState()),
tap(() => {
setRememberMe(params.rememberMe, localStorage);
rememberMeService.setRememberMe(params.rememberMe);
if (params.redirectUrl) router.navigate([params.redirectUrl]);
}),
);
};

export function setRememberMe(
remember: boolean,
localStorageService: AbpLocalStorageService,
) {
localStorageService.setItem(remember_me, JSON.stringify(remember));
}

export function removeRememberMe(localStorageService: AbpLocalStorageService) {
localStorageService.removeItem(remember_me);
export function isTokenExpired(oAuthService: OAuthService): boolean {
const expireDate = oAuthService.getAccessTokenExpiration();
const currentDate = new Date().getTime();
return expireDate < currentDate;
}
masum-ulu marked this conversation as resolved.
Show resolved Hide resolved

export function getRememberMe(localStorageService: AbpLocalStorageService){
return localStorageService.getItem(remember_me);
}
Loading