Skip to content

Feat: add support to refresh token absolute lifetime #168

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

Merged
merged 8 commits into from
May 31, 2024
Merged
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ type TAuthConfig = {
tokenExpiresIn?: number // default: null
// Can be used if auth provider doesn't return refresh token expiration time in token response
refreshTokenExpiresIn?: number // default: null
// Defines the expiration strategy for the refresh token.
// - 'renewable': The refresh token's expiration time is renewed each time it is used, getting a new validity period.
// - 'absolute': The refresh token's expiration time is fixed from its initial issuance and does not change, regardless of how many times it is used.
refreshTokenExpiryStrategy?: 'renewable' | 'absolute' // default: renewable
// Whether or not to post 'scope' when refreshing the access token
refreshWithScope?: boolean // default: true
}
Expand Down
4 changes: 3 additions & 1 deletion src/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ export const AuthProvider = ({ authConfig, children }: IAuthProvider) => {
const refreshTokenExpiresIn = config.refreshTokenExpiresIn ?? getRefreshExpiresIn(tokenExpiresIn, response)
if (response.refresh_token) {
setRefreshToken(response.refresh_token)
setRefreshTokenExpire(epochAtSecondsFromNow(refreshTokenExpiresIn))
if (!refreshTokenExpire || config.refreshTokenExpiryStrategy !== 'absolute') {
setRefreshTokenExpire(epochAtSecondsFromNow(refreshTokenExpiresIn))
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/authConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function createInternalConfig(passedConfig: TAuthConfig): TInternalConfig
storage = 'local',
storageKeyPrefix = 'ROCP_',
refreshWithScope = true,
refreshTokenExpiryStrategy = 'renewable',
}: TAuthConfig = passedConfig

const config: TInternalConfig = {
Expand All @@ -32,6 +33,7 @@ export function createInternalConfig(passedConfig: TAuthConfig): TInternalConfig
storage: storage,
storageKeyPrefix: storageKeyPrefix,
refreshWithScope: refreshWithScope,
refreshTokenExpiryStrategy: refreshTokenExpiryStrategy,
}
validateConfig(config)
return config
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type TAuthConfig = {
extraLogoutParameters?: TPrimitiveRecord
tokenExpiresIn?: number
refreshTokenExpiresIn?: number
refreshTokenExpiryStrategy?: 'renewable' | 'absolute'
storage?: 'session' | 'local'
storageKeyPrefix?: string
refreshWithScope?: boolean
Expand Down Expand Up @@ -111,6 +112,7 @@ export type TInternalConfig = {
extraLogoutParameters?: TPrimitiveRecord
tokenExpiresIn?: number
refreshTokenExpiresIn?: number
refreshTokenExpiryStrategy: 'renewable' | 'absolute'
storage: 'session' | 'local'
storageKeyPrefix: string
refreshWithScope: boolean
Expand Down
1 change: 1 addition & 0 deletions tests/auth-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const authConfig: TInternalConfig = {
scope: 'someScope openid',
clearURL: false,
storage: 'local',
refreshTokenExpiryStrategy: 'renewable',
storageKeyPrefix: 'ROCP_',
refreshWithScope: true,
extraAuthParams: {
Expand Down
Loading