-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: oidc token refresher middleware
- Loading branch information
1 parent
a2fe59c
commit dfe02c6
Showing
9 changed files
with
82 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
PROD=0 | ||
|
||
TZ=UTC | ||
PORT=3333 | ||
HOST=localhost | ||
|
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,13 @@ | ||
export async function oidcRenewTokens(refresh_token: string) { | ||
return await fetch(`${process.env.OIDC_TOKEN_ENDPOINT}`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Authorization': `Basic ${btoa(`${process.env.OIDC_CLIENT_ID}:${process.env.OIDC_CLIENT_SECRET}`)}`, | ||
}, | ||
body: new URLSearchParams({ | ||
grant_type: 'refresh_token', | ||
refresh_token: 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,38 @@ | ||
import type { HttpContext } from '@adonisjs/core/http' | ||
import type { NextFn } from '@adonisjs/core/types/http' | ||
import * as jose from 'jose' | ||
|
||
import { oidcRenewTokens } from '../lib/oidc.js'; | ||
|
||
export default class OidcTokenRefresherMiddleware { | ||
async handle(ctx: HttpContext, next: NextFn) { | ||
const { request } = ctx; | ||
|
||
const refresh_token = jose.decodeJwt(request.cookie("refresh_token")); | ||
const access_token = jose.decodeJwt(request.cookie("access_token")); | ||
|
||
const refresh_token_expired = new Date(refresh_token.exp * 1000) < new Date(); | ||
const access_token_expired = new Date(access_token.exp * 1000) < new Date(); | ||
|
||
if ((refresh_token && access_token) && (refresh_token_expired || access_token_expired)) { | ||
try { | ||
const res = await oidcRenewTokens(refresh_token.refresh_token); | ||
|
||
if (res.ok) { | ||
const newTokens = await res.json(); | ||
|
||
ctx.response | ||
.cookie('access_token', newTokens.access_token, { expires: new Date((new Date()).getTime() + (newTokens.expires_in)) }) | ||
.cookie('refresh_token', newTokens.refresh_token, { expires: new Date((new Date()).getTime() + (newTokens.expires_in)) }) | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
/** | ||
* Call next method in the pipeline and return its output | ||
*/ | ||
await next(); | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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