Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions controlplane/src/core/auth-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type AuthUtilsOptions = {
};
};

const tokenExpirationWindowSkew = 60 * 5;
const pkceMaxAgeSec = 60 * 15; // 15 minutes
const pkceCodeAlgorithm = 'S256';
const scope = 'openid profile email';
Expand Down Expand Up @@ -305,10 +306,13 @@ export default class AuthUtils {

// Check if the access token is expired
const parsedAccessToken = decodeJWT(userSession.accessToken);
if (parsedAccessToken.exp && parsedAccessToken.exp < Date.now() / 1000) {
const parsedRefreshToken = decodeJWT(userSession.accessToken);
if (parsedAccessToken.exp && parsedAccessToken.exp < Date.now() / 1000 + tokenExpirationWindowSkew) {
if (!userSession.refreshToken) {
throw new AuthenticationError(EnumStatusCode.ERROR_NOT_AUTHENTICATED, 'No refresh token');
}

// Check if the refresh token is valid to issue a new access token
const parsedRefreshToken = decodeJWT(userSession.refreshToken);
if (parsedRefreshToken.exp && parsedRefreshToken.exp < Date.now() / 1000) {
throw new AuthenticationError(EnumStatusCode.ERROR_NOT_AUTHENTICATED, 'Refresh token expired');
}
Expand Down Expand Up @@ -343,6 +347,7 @@ export default class AuthUtils {
const jwt = await encrypt<UserSession>({
maxAgeInSeconds: sessionExpiresIn,
token: {
iss: userSession.userId,
sessionId: newUserSession.id,
},
secret: this.opts.jwtSecret,
Expand Down