-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement automatic token refresh mechanism
- Loading branch information
1 parent
8a9f81e
commit 6cf54d9
Showing
9 changed files
with
175 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,18 @@ | ||
|
||
import { v2GetUserResponse } from '~/composables/aruna_api_json' | ||
import {v2GetUserResponse} from '~/composables/aruna_api_json' | ||
import {ArunaError} from "~/composables/ArunaError"; | ||
|
||
export default defineEventHandler(async event => { | ||
const userId = getQuery(event)['userId'] | ||
const baseUrl = useRuntimeConfig().serverHostUrl | ||
const fetchUrl = userId ? `${baseUrl}/v2/user?userId=${userId}` : `${baseUrl}/v2/user` | ||
const response = await $fetch<v2GetUserResponse>(fetchUrl, { | ||
headers: { | ||
'Authorization': `Bearer ${event.context.access_token}` | ||
} | ||
}).catch((error) => { | ||
if (error.data.message === "Not registered") { | ||
return "not_registered" as string | ||
} | ||
return error.data.message as string | ||
}) | ||
const userId = getQuery(event)['userId'] | ||
const baseUrl = useRuntimeConfig().serverHostUrl | ||
const fetchUrl = `${baseUrl}/v2/user` | ||
|
||
if (typeof response === "string") { | ||
return response | ||
}else{ | ||
return response.user | ||
return await $fetch<v2GetUserResponse>(fetchUrl, { | ||
headers: { | ||
'Authorization': `Bearer ${event.context.access_token}` | ||
} | ||
}).then(response => { | ||
return response.user ? response.user : new ArunaError(15, 'Returned user is undefined') | ||
}).catch(error => { | ||
return new ArunaError(error.data.code, error.data.message) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
export default defineEventHandler(async (event) => { | ||
if (process.client) { | ||
return | ||
} | ||
|
||
const config = useRuntimeConfig().provider.local; | ||
const realmURL = `${config.serverUrl}/realms/${config.realm}` | ||
const tokenURL = `${realmURL}/protocol/openid-connect/token` | ||
const refresh_token = getCookie(event, 'refresh_token') | ||
const access_token = getCookie(event, 'access_token') | ||
|
||
if (refresh_token) { | ||
const refresh_expiry = parseJwt(refresh_token).exp | ||
const access_expiry = access_token ? parseJwt(access_token).exp : 0 | ||
const current_timestamp = Math.floor(Date.now() / 1000) | ||
const access_expired = access_expiry - current_timestamp <= 60 // Only one minute left or less | ||
|
||
console.log(`${refresh_expiry} - ${access_expiry} - ${current_timestamp} - ${access_expiry - current_timestamp} - ${access_expired}`) | ||
|
||
if (refresh_expiry > current_timestamp && access_expired) { | ||
const tokens: any = await $fetch(tokenURL, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
body: new URLSearchParams({ | ||
client_id: config.clientId, | ||
client_secret: config.clientSecret, | ||
grant_type: 'refresh_token', | ||
refresh_token: refresh_token, | ||
}).toString(), | ||
}).catch((error) => { | ||
console.log('error', error) | ||
return {error} | ||
}) | ||
|
||
// Set cookie values with refreshed tokens | ||
if (tokens.access_token) { | ||
setCookie(event, 'access_token', tokens.access_token, | ||
{ | ||
httpOnly: false, | ||
secure: true, | ||
sameSite: 'none', | ||
maxAge: tokens.expires_in, | ||
}) | ||
} | ||
if (tokens.refresh_token) { | ||
setCookie(event, 'refresh_token', tokens.refresh_token, { | ||
httpOnly: false, | ||
secure: true, | ||
sameSite: 'none', | ||
maxAge: tokens.expires_in, | ||
}) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
export function parseJwt(token: any) { | ||
return JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()); | ||
} |
Oops, something went wrong.