From 6bcfd844caa1174ebc12548f86b6e386925b01e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Thu, 4 Jan 2024 15:37:06 +0300 Subject: [PATCH 01/11] initial commit (for saving) --- .../lib/strategies/auth-code-flow-strategy.ts | 6 ++++ .../strategies/auth-password-flow-strategy.ts | 9 ++---- .../oauth/src/lib/utils/cookie-utils.ts | 29 +++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 npm/ng-packs/packages/oauth/src/lib/utils/cookie-utils.ts diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts index c9708d699ea..c3443427d20 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts @@ -2,11 +2,17 @@ import { noop } from '@abp/ng.core'; import { Params } from '@angular/router'; import { from, of } from 'rxjs'; import { AuthFlowStrategy } from './auth-flow-strategy'; +import { deleteAllCookies } from '../utils/cookie-utils'; export class AuthCodeFlowStrategy extends AuthFlowStrategy { readonly isInternalAuth = false; + private rememberMeKey = 'remember_me' async init() { + console.log('code flow'); + const accessToken = this.oAuthService.getAccessToken(); + let parsedToken = JSON.parse(atob(accessToken.split(".")[1]));; + return super .init() .then(() => this.oAuthService.tryLogin().catch(noop)) diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts index 1958865e232..74ffc3bd305 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts @@ -7,11 +7,7 @@ import { AuthFlowStrategy } from './auth-flow-strategy'; import { pipeToLogin, removeRememberMe } from '../utils/auth-utils'; import { LoginParams } from '@abp/ng.core'; import { clearOAuthStorage } from '../utils/clear-o-auth-storage'; - -function getCookieValueByName(name: string) { - const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); - return match ? match[2] : ''; -} +import { getCookieValueByName } from '../utils/cookie-utils'; export class AuthPasswordFlowStrategy extends AuthFlowStrategy { readonly isInternalAuth = true; @@ -40,7 +36,8 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { } async init() { - if (!getCookieValueByName(this.cookieKey) && localStorage.getItem(this.storageKey)) { + if (!(getCookieValueByName(this.cookieKey) === 'true') && localStorage.getItem(this.storageKey)) { + removeRememberMe(this.localStorageService); this.oAuthService.logOut(); } diff --git a/npm/ng-packs/packages/oauth/src/lib/utils/cookie-utils.ts b/npm/ng-packs/packages/oauth/src/lib/utils/cookie-utils.ts new file mode 100644 index 00000000000..e31d4944ffb --- /dev/null +++ b/npm/ng-packs/packages/oauth/src/lib/utils/cookie-utils.ts @@ -0,0 +1,29 @@ +export function getCookieValueByName(name: string) { + const cookiesArray = document.cookie.split(';'); + + for (let i = 0; i < cookiesArray.length; i++) { + let cookie = cookiesArray[i].trim(); + + if (cookie.startsWith(name + '=')) { + return cookie.substring(name.length + 1); + } + } + return null; +} + +export function deleteAllCookies() { + var cookies = document.cookie.split("; "); + for (var c = 0; c < cookies.length; c++) { + var d = window.location.hostname.split("."); + while (d.length > 0) { + var cookieBase = encodeURIComponent(cookies[c].split(";")[0].split("=")[0]) + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain=' + d.join('.') + ' ;path='; + var p = location.pathname.split('/'); + document.cookie = cookieBase + '/'; + while (p.length > 0) { + document.cookie = cookieBase + p.join('/'); + p.pop(); + }; + d.shift(); + } + } +} \ No newline at end of file From c5eb3f34b58af1586ec2837435819918613a1412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Fri, 5 Jan 2024 17:19:23 +0300 Subject: [PATCH 02/11] add latest updates, will be checked again --- .../dev-app/src/environments/environment.ts | 2 +- .../lib/strategies/auth-code-flow-strategy.ts | 66 ++++++++++++++++--- .../src/lib/strategies/auth-flow-strategy.ts | 13 ++-- .../strategies/auth-password-flow-strategy.ts | 30 ++++++--- .../oauth/src/lib/utils/auth-utils.ts | 16 +++-- .../oauth/src/lib/utils/cookie-utils.ts | 29 -------- 6 files changed, 93 insertions(+), 63 deletions(-) delete mode 100644 npm/ng-packs/packages/oauth/src/lib/utils/cookie-utils.ts diff --git a/npm/ng-packs/apps/dev-app/src/environments/environment.ts b/npm/ng-packs/apps/dev-app/src/environments/environment.ts index 1e014dcaa7d..45b31e8ef08 100644 --- a/npm/ng-packs/apps/dev-app/src/environments/environment.ts +++ b/npm/ng-packs/apps/dev-app/src/environments/environment.ts @@ -14,7 +14,7 @@ export const environment = { issuer: 'https://localhost:44305/', clientId: 'MyProjectName_App', scope: 'offline_access MyProjectName', - responseType: 'code', + // responseType: 'code', redirectUri: baseUrl, }, apis: { diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts index c3443427d20..9825fa953e6 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts @@ -1,22 +1,71 @@ import { noop } from '@abp/ng.core'; import { Params } from '@angular/router'; -import { from, of } from 'rxjs'; +import { filter, from, of } from 'rxjs'; import { AuthFlowStrategy } from './auth-flow-strategy'; -import { deleteAllCookies } from '../utils/cookie-utils'; +import { getRememberMe, removeRememberMe, setRememberMe } from '../utils'; -export class AuthCodeFlowStrategy extends AuthFlowStrategy { +export class AuthCodeFlowStrategy extends AuthFlowStrategy{ readonly isInternalAuth = false; - private rememberMeKey = 'remember_me' + private remember_me = 'remember_me' async init() { - console.log('code flow'); - const accessToken = this.oAuthService.getAccessToken(); - let parsedToken = JSON.parse(atob(accessToken.split(".")[1]));; + this.checkRememberMeOption(); return super .init() .then(() => this.oAuthService.tryLogin().catch(noop)) - .then(() => this.oAuthService.setupAutomaticSilentRefresh({}, 'access_token')); + .then(() => this.oAuthService.setupAutomaticSilentRefresh()) + // .then(() => this.listenToTokenExpiration()); + } + + // private listenToTokenExpiration() { + // this.oAuthService.events + // .pipe( + // filter( + // event => { + // return event instanceof OAuthInfoEvent && + // event.type === 'token_expires' && + // event.info === 'access_token' + // } + // ), + // ) + // .subscribe(() => { + // if (this.oAuthService.getRefreshToken()) { + // console.log('refresh token'); + // this.refreshToken(); + // } else { + // this.oAuthService.logOut(); + // removeRememberMe(this.localStorageService); + // this.configState.refreshAppState().subscribe(); + // } + // }); + // } + + private checkRememberMeOption() { + const accessToken = this.oAuthService.getAccessToken(); + const expireDate = this.oAuthService.getAccessTokenExpiration(); + const currentDate = new Date().getTime(); + let rememberMe = getRememberMe(this.localStorageService); + + if (accessToken && rememberMe === null) { + let parsedToken = JSON.parse(atob(accessToken.split(".")[1])); + + if (parsedToken[this.remember_me]) { + setRememberMe(true, this.localStorageService); + } else { + setRememberMe(false, this.localStorageService) + } + + } + rememberMe = getRememberMe(this.localStorageService); + + if (accessToken && expireDate < currentDate && rememberMe === 'false') { + removeRememberMe(this.localStorageService); + this.oAuthService.logOut(); + }else{ + console.log('try login'); + this.oAuthService.tryLogin().catch(noop) + } } navigateToLogin(queryParams?: Params) { @@ -35,6 +84,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy { } logout(queryParams?: Params) { + removeRememberMe(this.localStorageService); return from(this.oAuthService.revokeTokenAndLogout(this.getCultureParams(queryParams))); } diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts index e5189966bd7..4184cf2c8d0 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts @@ -70,23 +70,20 @@ export abstract class AuthFlowStrategy { const shouldClear = shouldStorageClear(this.oAuthConfig.clientId, oAuthStorage); if (shouldClear) clearOAuthStorage(oAuthStorage); } - this.oAuthService.configure(this.oAuthConfig); - this.oAuthService.events .pipe(filter(event => event.type === 'token_refresh_error')) .subscribe(() => this.navigateToLogin()); - this.navigateToPreviousUrl(); - return this.oAuthService .loadDiscoveryDocument() .then(() => { - if (this.oAuthService.hasValidAccessToken() || !this.oAuthService.getRefreshToken()) { - return Promise.resolve(); + const expireDate = this.oAuthService.getAccessTokenExpiration(); + const currentDate = new Date().getTime(); + if (expireDate > currentDate || this.oAuthService.getRefreshToken()) { + return this.refreshToken(); } - - return this.refreshToken(); + return Promise.resolve(); }) .catch(this.catchError); } diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts index 74ffc3bd305..8936ebd0a5b 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts @@ -1,13 +1,12 @@ import { filter, switchMap, tap } from 'rxjs/operators'; import { OAuthInfoEvent } from 'angular-oauth2-oidc'; import { Params, Router } from '@angular/router'; -import { from, Observable, pipe } from 'rxjs'; +import { from, Observable } from 'rxjs'; import { HttpHeaders } from '@angular/common/http'; import { AuthFlowStrategy } from './auth-flow-strategy'; -import { pipeToLogin, removeRememberMe } from '../utils/auth-utils'; -import { LoginParams } from '@abp/ng.core'; +import { getRememberMe, pipeToLogin, removeRememberMe } from '../utils/auth-utils'; +import { AbpLocalStorageService, LoginParams } from '@abp/ng.core'; import { clearOAuthStorage } from '../utils/clear-o-auth-storage'; -import { getCookieValueByName } from '../utils/cookie-utils'; export class AuthPasswordFlowStrategy extends AuthFlowStrategy { readonly isInternalAuth = true; @@ -18,10 +17,11 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { this.oAuthService.events .pipe( filter( - event => - event instanceof OAuthInfoEvent && + event => { + return event instanceof OAuthInfoEvent && event.type === 'token_expires' && - event.info === 'access_token', + event.info === 'access_token' + } ), ) .subscribe(() => { @@ -36,12 +36,21 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { } async init() { - if (!(getCookieValueByName(this.cookieKey) === 'true') && localStorage.getItem(this.storageKey)) { + 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) === 'true'; + + if (accessToken && expireDate < currentDate && !rememberMe) { removeRememberMe(this.localStorageService); this.oAuthService.logOut(); } - - return super.init().then(() => this.listenToTokenExpiration()); } navigateToLogin(queryParams?: Params) { @@ -77,6 +86,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { } protected refreshToken() { + console.log('token refreshed'); return this.oAuthService.refreshToken().catch(() => { clearOAuthStorage(); removeRememberMe(this.localStorageService); diff --git a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts index bd50e97ee79..9bc14d7e315 100644 --- a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts +++ b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts @@ -11,6 +11,7 @@ import { const cookieKey = 'rememberMe'; const storageKey = 'passwordFlow'; +const remember_me = 'remember_me'; export const pipeToLogin: PipeToLoginFn = function ( params: Pick, @@ -32,14 +33,15 @@ export function setRememberMe( remember: boolean | undefined, localStorageService: AbpLocalStorageService, ) { - removeRememberMe(localStorageService); - localStorageService.setItem(storageKey, 'true'); - document.cookie = `${cookieKey}=true; path=/${ - remember ? ' ;expires=Fri, 31 Dec 9999 23:59:59 GMT' : '' - }`; + console.log('set Remember Me'); + localStorageService.setItem(remember_me, JSON.stringify(remember)); } export function removeRememberMe(localStorageService: AbpLocalStorageService) { - localStorageService.removeItem(storageKey); - document.cookie = cookieKey + '= ; path=/; expires = Thu, 01 Jan 1970 00:00:00 GMT'; + console.log('remove remember Me'); + localStorageService.removeItem(remember_me); +} + +export function getRememberMe(localStorageService: AbpLocalStorageService){ + return localStorageService.getItem(remember_me); } diff --git a/npm/ng-packs/packages/oauth/src/lib/utils/cookie-utils.ts b/npm/ng-packs/packages/oauth/src/lib/utils/cookie-utils.ts deleted file mode 100644 index e31d4944ffb..00000000000 --- a/npm/ng-packs/packages/oauth/src/lib/utils/cookie-utils.ts +++ /dev/null @@ -1,29 +0,0 @@ -export function getCookieValueByName(name: string) { - const cookiesArray = document.cookie.split(';'); - - for (let i = 0; i < cookiesArray.length; i++) { - let cookie = cookiesArray[i].trim(); - - if (cookie.startsWith(name + '=')) { - return cookie.substring(name.length + 1); - } - } - return null; -} - -export function deleteAllCookies() { - var cookies = document.cookie.split("; "); - for (var c = 0; c < cookies.length; c++) { - var d = window.location.hostname.split("."); - while (d.length > 0) { - var cookieBase = encodeURIComponent(cookies[c].split(";")[0].split("=")[0]) + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT; domain=' + d.join('.') + ' ;path='; - var p = location.pathname.split('/'); - document.cookie = cookieBase + '/'; - while (p.length > 0) { - document.cookie = cookieBase + p.join('/'); - p.pop(); - }; - d.shift(); - } - } -} \ No newline at end of file From 1d659c5cbecf6a8bd0ea9ff910c64a189dd82af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Tue, 16 Jan 2024 11:01:30 +0300 Subject: [PATCH 03/11] existing code tested and reviewed by me --- .../dev-app/src/environments/environment.ts | 2 +- .../lib/strategies/auth-code-flow-strategy.ts | 39 +++---------------- .../strategies/auth-password-flow-strategy.ts | 6 +-- .../oauth/src/lib/utils/auth-utils.ts | 8 +--- 4 files changed, 12 insertions(+), 43 deletions(-) diff --git a/npm/ng-packs/apps/dev-app/src/environments/environment.ts b/npm/ng-packs/apps/dev-app/src/environments/environment.ts index 45b31e8ef08..1e014dcaa7d 100644 --- a/npm/ng-packs/apps/dev-app/src/environments/environment.ts +++ b/npm/ng-packs/apps/dev-app/src/environments/environment.ts @@ -14,7 +14,7 @@ export const environment = { issuer: 'https://localhost:44305/', clientId: 'MyProjectName_App', scope: 'offline_access MyProjectName', - // responseType: 'code', + responseType: 'code', redirectUri: baseUrl, }, apis: { diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts index 9825fa953e6..77ceb94dbfa 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts @@ -1,10 +1,10 @@ import { noop } from '@abp/ng.core'; import { Params } from '@angular/router'; -import { filter, from, of } from 'rxjs'; +import { from, of } from 'rxjs'; import { AuthFlowStrategy } from './auth-flow-strategy'; import { getRememberMe, removeRememberMe, setRememberMe } from '../utils'; -export class AuthCodeFlowStrategy extends AuthFlowStrategy{ +export class AuthCodeFlowStrategy extends AuthFlowStrategy { readonly isInternalAuth = false; private remember_me = 'remember_me' @@ -15,32 +15,8 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy{ .init() .then(() => this.oAuthService.tryLogin().catch(noop)) .then(() => this.oAuthService.setupAutomaticSilentRefresh()) - // .then(() => this.listenToTokenExpiration()); } - // private listenToTokenExpiration() { - // this.oAuthService.events - // .pipe( - // filter( - // event => { - // return event instanceof OAuthInfoEvent && - // event.type === 'token_expires' && - // event.info === 'access_token' - // } - // ), - // ) - // .subscribe(() => { - // if (this.oAuthService.getRefreshToken()) { - // console.log('refresh token'); - // this.refreshToken(); - // } else { - // this.oAuthService.logOut(); - // removeRememberMe(this.localStorageService); - // this.configState.refreshAppState().subscribe(); - // } - // }); - // } - private checkRememberMeOption() { const accessToken = this.oAuthService.getAccessToken(); const expireDate = this.oAuthService.getAccessTokenExpiration(); @@ -49,22 +25,19 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy{ if (accessToken && rememberMe === null) { let parsedToken = JSON.parse(atob(accessToken.split(".")[1])); + let rememberMeValue = parsedToken[this.remember_me]; - if (parsedToken[this.remember_me]) { + if (rememberMeValue && (rememberMeValue === 'True' || rememberMeValue === 'true')) { setRememberMe(true, this.localStorageService); } else { setRememberMe(false, this.localStorageService) } - } - rememberMe = getRememberMe(this.localStorageService); - if (accessToken && expireDate < currentDate && rememberMe === 'false') { + rememberMe = getRememberMe(this.localStorageService); + if (accessToken && expireDate < currentDate && rememberMe !== 'true') { removeRememberMe(this.localStorageService); this.oAuthService.logOut(); - }else{ - console.log('try login'); - this.oAuthService.tryLogin().catch(noop) } } diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts index 8936ebd0a5b..f8e585d1700 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts @@ -45,9 +45,9 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { const accessToken = this.oAuthService.getAccessToken(); const expireDate = this.oAuthService.getAccessTokenExpiration(); const currentDate = new Date().getTime(); - const rememberMe = getRememberMe(localStorageService) === 'true'; + const rememberMe = getRememberMe(localStorageService); - if (accessToken && expireDate < currentDate && !rememberMe) { + if (accessToken && expireDate < currentDate && rememberMe !== 'true') { removeRememberMe(this.localStorageService); this.oAuthService.logOut(); } @@ -73,6 +73,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { ), ).pipe(pipeToLogin(params, this.injector)); } + logout() { const router = this.injector.get(Router); const noRedirectToLogoutUrl = true; @@ -86,7 +87,6 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { } protected refreshToken() { - console.log('token refreshed'); return this.oAuthService.refreshToken().catch(() => { clearOAuthStorage(); removeRememberMe(this.localStorageService); diff --git a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts index 9bc14d7e315..0d8fb28fe39 100644 --- a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts +++ b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts @@ -9,8 +9,6 @@ import { AbpLocalStorageService, } from '@abp/ng.core'; -const cookieKey = 'rememberMe'; -const storageKey = 'passwordFlow'; const remember_me = 'remember_me'; export const pipeToLogin: PipeToLoginFn = function ( @@ -30,18 +28,16 @@ export const pipeToLogin: PipeToLoginFn = function ( }; export function setRememberMe( - remember: boolean | undefined, + remember: boolean, localStorageService: AbpLocalStorageService, ) { - console.log('set Remember Me'); localStorageService.setItem(remember_me, JSON.stringify(remember)); } export function removeRememberMe(localStorageService: AbpLocalStorageService) { - console.log('remove remember Me'); localStorageService.removeItem(remember_me); } export function getRememberMe(localStorageService: AbpLocalStorageService){ return localStorageService.getItem(remember_me); -} +} \ No newline at end of file From ed4a0e4359cb5618d0351fe97a9dbc6dab08b1b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Tue, 16 Jan 2024 11:16:47 +0300 Subject: [PATCH 04/11] remove changes in auth-flow-strategy.ts --- .../oauth/src/lib/strategies/auth-flow-strategy.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts index 4184cf2c8d0..594e6e5c89d 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts @@ -78,12 +78,11 @@ export abstract class AuthFlowStrategy { return this.oAuthService .loadDiscoveryDocument() .then(() => { - const expireDate = this.oAuthService.getAccessTokenExpiration(); - const currentDate = new Date().getTime(); - if (expireDate > currentDate || this.oAuthService.getRefreshToken()) { - return this.refreshToken(); + if (this.oAuthService.hasValidAccessToken() || !this.oAuthService.getRefreshToken()) { + return Promise.resolve(); } - return Promise.resolve(); + + return this.refreshToken(); }) .catch(this.catchError); } From 5857c5551bb2412458808ee48e18f48176ad9438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Tue, 16 Jan 2024 21:52:02 +0300 Subject: [PATCH 05/11] fix reviews --- .../lib/strategies/auth-code-flow-strategy.ts | 28 ++++++------- .../src/lib/strategies/auth-flow-strategy.ts | 10 +++-- .../strategies/auth-password-flow-strategy.ts | 29 ++++++------- .../oauth/src/lib/utils/auth-utils.ts | 41 +++++++++++-------- 4 files changed, 57 insertions(+), 51 deletions(-) diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts index 77ceb94dbfa..d160a14d331 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts @@ -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' + rememberMeService = new RememberMeService(this.injector); async init() { this.checkRememberMeOption(); @@ -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) } } - 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(); } } @@ -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))); } diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts index 594e6e5c89d..8bafb4a70ad 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts @@ -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; @@ -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); } diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts index f8e585d1700..7a7ee98c65d 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts @@ -4,24 +4,21 @@ 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(() => { @@ -29,26 +26,24 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { 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(); } } @@ -73,7 +68,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { ), ).pipe(pipeToLogin(params, this.injector)); } - + logout() { const router = this.injector.get(Router); const noRedirectToLogoutUrl = true; @@ -81,7 +76,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { switchMap(() => this.configState.refreshAppState()), tap(() => { router.navigateByUrl('/'); - removeRememberMe(this.localStorageService); + this.rememberMeService.removeRememberMe(); }), ); } @@ -89,7 +84,7 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { protected refreshToken() { return this.oAuthService.refreshToken().catch(() => { clearOAuthStorage(); - removeRememberMe(this.localStorageService); + this.rememberMeService.removeRememberMe(); }); } } diff --git a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts index 0d8fb28fe39..7c163a40c7c 100644 --- a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts +++ b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts @@ -8,8 +8,26 @@ 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); + } +} export const pipeToLogin: PipeToLoginFn = function ( params: Pick, @@ -17,27 +35,18 @@ export const pipeToLogin: PipeToLoginFn = function ( ) { 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; } - -export function getRememberMe(localStorageService: AbpLocalStorageService){ - return localStorageService.getItem(remember_me); -} \ No newline at end of file From 4049971cfff42809444e291da05c20328841eed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Wed, 17 Jan 2024 15:54:09 +0300 Subject: [PATCH 06/11] fix reviews --- .../src/lib/services/remember-me.service.ts | 24 ++++++++++++++++++ .../lib/strategies/auth-code-flow-strategy.ts | 19 +++++++------- .../src/lib/strategies/auth-flow-strategy.ts | 3 +++ .../strategies/auth-password-flow-strategy.ts | 13 +++++----- .../oauth/src/lib/utils/auth-utils.ts | 25 +++---------------- 5 files changed, 45 insertions(+), 39 deletions(-) create mode 100644 npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts diff --git a/npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts b/npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts new file mode 100644 index 00000000000..cc1c78af8a3 --- /dev/null +++ b/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))); + } +} \ No newline at end of file diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts index d160a14d331..a616d60770e 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts +++ b/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))); } diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts index 8bafb4a70ad..80b63ec77c2 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts +++ b/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(); } diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts index 7a7ee98c65d..65198536a8e 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts +++ b/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(); }); } } diff --git a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts index 7c163a40c7c..64f867df7e6 100644 --- a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts +++ b/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, @@ -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]); }), ); From 95a4ccc9c7eb46d6a73f5f7795566ee2dfb3863d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Wed, 17 Jan 2024 17:13:28 +0300 Subject: [PATCH 07/11] fix reviews --- .../src/lib/services/remember-me.service.ts | 19 +++++++++++-------- .../lib/strategies/auth-code-flow-strategy.ts | 6 ++---- .../src/lib/strategies/auth-flow-strategy.ts | 4 ++-- .../strategies/auth-password-flow-strategy.ts | 8 ++++---- .../oauth/src/lib/utils/auth-utils.ts | 4 +--- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts b/npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts index cc1c78af8a3..7c099c5166d 100644 --- a/npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts +++ b/npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts @@ -1,24 +1,27 @@ import { AbpLocalStorageService } from "@abp/ng.core"; -import { Injectable, Injector } from "@angular/core"; - -const rememberMe = 'remember_me'; +import { Injectable, inject } from "@angular/core"; @Injectable({ providedIn: 'root' }) export class RememberMeService { - constructor(private injector: Injector) { } - localStorageService = this.injector.get(AbpLocalStorageService); + readonly #rememberMe = 'remember_me' + protected readonly localStorageService = inject(AbpLocalStorageService); set(remember: boolean) { - this.localStorageService.setItem(rememberMe, JSON.stringify(remember)); + this.localStorageService.setItem(this.#rememberMe, JSON.stringify(remember)); } remove() { - this.localStorageService.removeItem(rememberMe); + this.localStorageService.removeItem(this.#rememberMe); } get() { - return Boolean(JSON.parse(this.localStorageService.getItem(rememberMe))); + return Boolean(JSON.parse(this.localStorageService.getItem(this.#rememberMe))); + } + + getFromToken(accessToken: string) { + let parsedToken = JSON.parse(atob(accessToken.split(".")[1])); + return Boolean(parsedToken[this.#rememberMe]); } } \ No newline at end of file diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts index a616d60770e..5a1b2c3d056 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts @@ -6,7 +6,6 @@ import { isTokenExpired } from '../utils'; export class AuthCodeFlowStrategy extends AuthFlowStrategy { readonly isInternalAuth = false; - readonly #rememberMe = 'remember_me' async init() { this.checkRememberMeOption(); @@ -19,12 +18,11 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy { private checkRememberMeOption() { const accessToken = this.oAuthService.getAccessToken(); - const isTokenExpire = isTokenExpired(this.oAuthService); + const isTokenExpire = isTokenExpired(this.oAuthService.getAccessTokenExpiration()); let rememberMe = this.rememberMeService.get(); if (accessToken && !rememberMe) { - let parsedToken = JSON.parse(atob(accessToken.split(".")[1])); - const rememberMeValue = Boolean(parsedToken[this.#rememberMe]); + const rememberMeValue = this.rememberMeService.getFromToken(accessToken); if (rememberMeValue) { this.rememberMeService.set(true); diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts index 80b63ec77c2..8a75a23b8b6 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-flow-strategy.ts @@ -82,11 +82,11 @@ export abstract class AuthFlowStrategy { return this.oAuthService .loadDiscoveryDocument() .then(() => { - const isTokenExpire = isTokenExpired(this.oAuthService); + const isTokenExpire = isTokenExpired(this.oAuthService.getAccessTokenExpiration()); if (!isTokenExpire || this.oAuthService.getRefreshToken()) { return this.refreshToken(); } - + return Promise.resolve(); }) .catch(this.catchError); diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts index 65198536a8e..eddf38a03ee 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts @@ -5,7 +5,7 @@ import { from, Observable } from 'rxjs'; import { HttpHeaders } from '@angular/common/http'; import { AuthFlowStrategy } from './auth-flow-strategy'; import { isTokenExpired, pipeToLogin } from '../utils/auth-utils'; -import { AbpLocalStorageService, LoginParams } from '@abp/ng.core'; +import { LoginParams } from '@abp/ng.core'; import { clearOAuthStorage } from '../utils/clear-o-auth-storage'; export class AuthPasswordFlowStrategy extends AuthFlowStrategy { @@ -32,14 +32,14 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { } async init() { - this.checkRememberMeOption(this.localStorageService); + this.checkRememberMeOption(); return super.init().then(() => this.listenToTokenExpiration()); } - private checkRememberMeOption(localStorageService: AbpLocalStorageService) { + private checkRememberMeOption() { const accessToken = this.oAuthService.getAccessToken(); - const isTokenExpire = isTokenExpired(this.oAuthService); + const isTokenExpire = isTokenExpired(this.oAuthService.getAccessTokenExpiration()); const rememberMe = this.rememberMeService.get(); if (accessToken && isTokenExpire && !rememberMe) { this.rememberMeService.remove(); diff --git a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts index 64f867df7e6..8742136b1f5 100644 --- a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts +++ b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts @@ -7,7 +7,6 @@ import { LoginParams, PipeToLoginFn, } from '@abp/ng.core'; -import { OAuthService } from 'angular-oauth2-oidc'; import { RememberMeService } from '../services/remember-me.service'; export const pipeToLogin: PipeToLoginFn = function ( @@ -26,8 +25,7 @@ export const pipeToLogin: PipeToLoginFn = function ( ); }; -export function isTokenExpired(oAuthService: OAuthService): boolean { - const expireDate = oAuthService.getAccessTokenExpiration(); +export function isTokenExpired(expireDate: number): boolean { const currentDate = new Date().getTime(); return expireDate < currentDate; } From 3214c802fb087fee11b16acadee61ad5b550730d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Fri, 19 Jan 2024 11:16:52 +0300 Subject: [PATCH 08/11] remove remember_me from localstorage before routing and use double exclamation --- .../oauth/src/lib/strategies/auth-code-flow-strategy.ts | 4 ++-- .../oauth/src/lib/strategies/auth-password-flow-strategy.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts index 5a1b2c3d056..ced281253df 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts @@ -25,9 +25,9 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy { const rememberMeValue = this.rememberMeService.getFromToken(accessToken); if (rememberMeValue) { - this.rememberMeService.set(true); + this.rememberMeService.set(!!rememberMeValue); } else { - this.rememberMeService.set(false) + this.rememberMeService.set(!!rememberMeValue) } } diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts index eddf38a03ee..9c38c64e27e 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-password-flow-strategy.ts @@ -74,8 +74,8 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { return from(this.oAuthService.revokeTokenAndLogout(noRedirectToLogoutUrl)).pipe( switchMap(() => this.configState.refreshAppState()), tap(() => { - router.navigateByUrl('/'); this.rememberMeService.remove(); + router.navigateByUrl('/'); }), ); } From 254ff8c52e3291a65ab90747448f71a4466856d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Fri, 19 Jan 2024 11:52:06 +0300 Subject: [PATCH 09/11] remove unnecesarry if block --- .../oauth/src/lib/strategies/auth-code-flow-strategy.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts index ced281253df..f648be47c60 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts @@ -24,11 +24,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy { if (accessToken && !rememberMe) { const rememberMeValue = this.rememberMeService.getFromToken(accessToken); - if (rememberMeValue) { - this.rememberMeService.set(!!rememberMeValue); - } else { - this.rememberMeService.set(!!rememberMeValue) - } + this.rememberMeService.set(!!rememberMeValue); } rememberMe = this.rememberMeService.get(); From 165885e5813e03d4ac21ae99272218dcc1c68674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinan=20=C3=96zt=C3=BCrk?= Date: Tue, 23 Jan 2024 14:14:58 +0300 Subject: [PATCH 10/11] prettify --- .../src/lib/services/remember-me.service.ts | 38 +++++++++---------- .../lib/strategies/auth-code-flow-strategy.ts | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts b/npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts index 7c099c5166d..84d40d982a0 100644 --- a/npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts +++ b/npm/ng-packs/packages/oauth/src/lib/services/remember-me.service.ts @@ -1,27 +1,27 @@ -import { AbpLocalStorageService } from "@abp/ng.core"; -import { Injectable, inject } from "@angular/core"; +import { AbpLocalStorageService } from '@abp/ng.core'; +import { Injectable, inject } from '@angular/core'; @Injectable({ - providedIn: 'root' + providedIn: 'root', }) export class RememberMeService { - readonly #rememberMe = 'remember_me' - protected readonly localStorageService = inject(AbpLocalStorageService); + readonly #rememberMe = 'remember_me'; + protected readonly localStorageService = inject(AbpLocalStorageService); - set(remember: boolean) { - this.localStorageService.setItem(this.#rememberMe, JSON.stringify(remember)); - } + set(remember: boolean) { + this.localStorageService.setItem(this.#rememberMe, JSON.stringify(remember)); + } - remove() { - this.localStorageService.removeItem(this.#rememberMe); - } + remove() { + this.localStorageService.removeItem(this.#rememberMe); + } - get() { - return Boolean(JSON.parse(this.localStorageService.getItem(this.#rememberMe))); - } + get() { + return Boolean(JSON.parse(this.localStorageService.getItem(this.#rememberMe))); + } - getFromToken(accessToken: string) { - let parsedToken = JSON.parse(atob(accessToken.split(".")[1])); - return Boolean(parsedToken[this.#rememberMe]); - } -} \ No newline at end of file + getFromToken(accessToken: string) { + const parsedToken = JSON.parse(atob(accessToken.split('.')[1])); + return Boolean(parsedToken[this.#rememberMe]); + } +} diff --git a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts index f648be47c60..7b1e0c108ac 100644 --- a/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts +++ b/npm/ng-packs/packages/oauth/src/lib/strategies/auth-code-flow-strategy.ts @@ -13,7 +13,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy { return super .init() .then(() => this.oAuthService.tryLogin().catch(noop)) - .then(() => this.oAuthService.setupAutomaticSilentRefresh()) + .then(() => this.oAuthService.setupAutomaticSilentRefresh()); } private checkRememberMeOption() { From 0b63b88b8a7fb6dabea68f4f996823f49ed59299 Mon Sep 17 00:00:00 2001 From: Masum ULU <49063256+masumulu28@users.noreply.github.com> Date: Wed, 24 Jan 2024 11:20:19 +0300 Subject: [PATCH 11/11] Add issue ref to auth-utils --- npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts index 8742136b1f5..bc76609f5a6 100644 --- a/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts +++ b/npm/ng-packs/packages/oauth/src/lib/utils/auth-utils.ts @@ -25,6 +25,7 @@ export const pipeToLogin: PipeToLoginFn = function ( ); }; +//Ref: https://github.com/manfredsteyer/angular-oauth2-oidc/issues/1214 export function isTokenExpired(expireDate: number): boolean { const currentDate = new Date().getTime(); return expireDate < currentDate;