-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refresh access token
- Loading branch information
Showing
25 changed files
with
166 additions
and
145 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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { decrypt } from '@/utils/encryption'; | ||
import { jwtDecode } from 'jwt-decode'; | ||
import { Account, getServerSession } from 'next-auth'; | ||
import type { JWT } from 'next-auth/jwt'; | ||
import { ExtendedSession } from './auth.types'; | ||
import { authOptions } from './config'; | ||
|
||
export async function getToken(tokenType: 'access_token' | 'id_token') { | ||
const session = (await getServerSession(authOptions)) as ExtendedSession; | ||
if (session) { | ||
const tokenDecrypted = decrypt(session[tokenType]!); | ||
return tokenDecrypted; | ||
} | ||
return null; | ||
} | ||
|
||
export function completeTokenWithAccountInfo(token: JWT, account: Account): JWT { | ||
return { | ||
...token, | ||
decoded: jwtDecode(account.access_token!) as string, | ||
access_token: account.access_token as string, | ||
id_token: account.id_token as string, | ||
refresh_token: account.refresh_token as string, | ||
expires_at: account.expires_at as number, | ||
}; | ||
} | ||
|
||
export async function refreshAccessToken(token: JWT) { | ||
const response = await fetch(`${process.env.REFRESH_TOKEN_URL}`, { | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
body: new URLSearchParams({ | ||
client_id: `${process.env.KEYCLOAK_CLIENT_ID}`, | ||
client_secret: `${process.env.KEYCLOAK_CLIENT_SECRET}`, | ||
grant_type: 'refresh_token', | ||
refresh_token: token.refresh_token as string, | ||
}), | ||
method: 'POST', | ||
cache: 'no-cache', | ||
}); | ||
const refreshToken = await response.json(); | ||
|
||
return { | ||
...token, | ||
access_token: refreshToken.access_token, | ||
id_token: refreshToken.id_token, | ||
decoded: jwtDecode(refreshToken.access_token), | ||
expires_at: Math.floor(Date.now() / 1000) + refreshToken.expires_in, | ||
refresh_token: refreshToken.refresh_token, | ||
}; | ||
} |
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,18 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Account, Session } from 'next-auth'; | ||
import { JWT } from 'next-auth/jwt'; | ||
|
||
export type ExtendedSession = Session & { id_token: string; access_token: string; error?: string }; | ||
|
||
export type JWTCallbackEntry = { | ||
token: JWT; | ||
account: Account | null; | ||
}; | ||
|
||
export type SessionCallbackEntry = { | ||
token: JWT; | ||
session: Session; | ||
}; |
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,52 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { encrypt } from '@/utils/encryption'; | ||
import { keycloackSessionLogOut } from '@/utils/logout'; | ||
import type { NextAuthOptions } from 'next-auth'; | ||
import Keycloack from 'next-auth/providers/keycloak'; | ||
import { signOut } from 'next-auth/react'; | ||
import { completeTokenWithAccountInfo, refreshAccessToken } from './auth'; | ||
import { JWTCallbackEntry, SessionCallbackEntry } from './auth.types'; | ||
|
||
export const authOptions: NextAuthOptions = { | ||
providers: [ | ||
Keycloack({ | ||
clientId: `${process.env.KEYCLOAK_CLIENT_ID}`, | ||
clientSecret: `${process.env.KEYCLOAK_CLIENT_SECRET}`, | ||
issuer: process.env.KEYCLOAK_ISSUER_URL, | ||
authorization: { params: { scope: 'openid profile email elixir_id' } }, | ||
}), | ||
], | ||
callbacks: { | ||
async jwt({ token, account }: JWTCallbackEntry) { | ||
const currTimestamp = Math.floor(Date.now() / 1000); | ||
const isTokenExpired = (token?.expires_at as number) < currTimestamp; | ||
|
||
if (account) { | ||
return completeTokenWithAccountInfo(token, account); | ||
} else if (isTokenExpired) { | ||
try { | ||
const refreshedToken = await refreshAccessToken(token); | ||
return refreshedToken; | ||
} catch (error) { | ||
keycloackSessionLogOut().then(() => signOut({ callbackUrl: '/' })); | ||
throw new Error('Could not refresh the token. Logging out...'); | ||
} | ||
} else { | ||
return token; | ||
} | ||
}, | ||
|
||
async session({ session, token }: SessionCallbackEntry) { | ||
return { | ||
...session, | ||
access_token: encrypt(token.access_token as string), | ||
id_token: encrypt(token.id_token as string), | ||
roles: (token.decoded as { realm_access?: { roles?: string[] } }).realm_access?.roles, | ||
error: token.error, | ||
}; | ||
}, | ||
}, | ||
}; |
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
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
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
Oops, something went wrong.