Skip to content

Commit

Permalink
Include auth_time in token
Browse files Browse the repository at this point in the history
  • Loading branch information
darthmaim committed Dec 16, 2024
1 parent f8a9583 commit a7a2b51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
26 changes: 22 additions & 4 deletions apps/web/app/api/(oauth)/token/openid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,40 @@ import { createSigner } from 'fast-jwt';
import { RequestAuthentication } from '../auth';
import { getBaseUrlFromHeaders } from '@/lib/url';
import { ACCESS_TOKEN_EXPIRATION } from './token';
import { db } from '@/lib/db';
import { assert } from '@/lib/oauth/assert';
import { OAuth2ErrorCode } from '@/lib/oauth/error';

type IdTokenOptions = {
clientId: string,
requestAuthentication: RequestAuthentication
userId: string,
authTime: Date,
nonce: string,
}

Check warning on line 14 in apps/web/app/api/(oauth)/token/openid.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon.
export async function createIdToken({ userId, clientId, requestAuthentication, authTime, nonce }: IdTokenOptions) {
export async function createIdToken({ userId, clientId, requestAuthentication, nonce }: IdTokenOptions) {
const { origin: issuer } = await getBaseUrlFromHeaders();
const issuedAt = toTimestamp(new Date());

const issuedAt = Math.floor(Date.now() / 1000);
const user = await db.user.findUnique({
where: { id: userId },
select: {
// get latest created session to use as auth_time
sessions: {
select: { createdAt: true },
orderBy: { createdAt: 'desc' },
take: 1
}
}
});
assert(user, OAuth2ErrorCode.server_error, 'user not found');

const idToken = {
iss: issuer,
sub: userId,
aud: [clientId],
exp: issuedAt + ACCESS_TOKEN_EXPIRATION,
iat: issuedAt,
auth_time: authTime.valueOf(),
auth_time: toTimestamp(user.sessions[0].createdAt),
nonce: nonce

Check warning on line 39 in apps/web/app/api/(oauth)/token/openid.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected property shorthand.
};

Expand All @@ -36,3 +50,7 @@ export async function createIdToken({ userId, clientId, requestAuthentication, a
return jwt;
}
}

function toTimestamp(date: Date): number {
return Math.floor(date.valueOf() / 1000);
}
2 changes: 1 addition & 1 deletion apps/web/app/api/(oauth)/token/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export async function handleTokenRequest(headers: Headers, params: Record<string

// create id_token
const id_token = client.type === 'Confidential' && scope.includes(Scope.OpenID)
? await createIdToken({ userId, clientId, requestAuthentication, nonce: 'TODO', authTime: new Date() })
? await createIdToken({ userId, clientId, requestAuthentication, nonce: 'TODO' })
: undefined;

return {
Expand Down

0 comments on commit a7a2b51

Please sign in to comment.