chore: improve oidc provider support

Adds fields to the Account model to support various pieces
of data returned by OIDC providers such as AzureAD and GitLab.

Additionally passes through the email verification status and handles
retrieving the email for providers such as AzureAD who use a different
claim instead.
This commit is contained in:
Mythie
2024-05-30 22:15:45 +10:00
parent d8d0734680
commit 70eeb1a746
3 changed files with 16 additions and 4 deletions

View File

@ -139,19 +139,24 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
{ {
id: 'oidc', id: 'oidc',
name: 'OIDC', name: 'OIDC',
type: 'oauth',
wellKnown: process.env.NEXT_PRIVATE_OIDC_WELL_KNOWN, wellKnown: process.env.NEXT_PRIVATE_OIDC_WELL_KNOWN,
clientId: process.env.NEXT_PRIVATE_OIDC_CLIENT_ID, clientId: process.env.NEXT_PRIVATE_OIDC_CLIENT_ID,
clientSecret: process.env.NEXT_PRIVATE_OIDC_CLIENT_SECRET, clientSecret: process.env.NEXT_PRIVATE_OIDC_CLIENT_SECRET,
authorization: { params: { scope: 'openid email profile' } }, authorization: { params: { scope: 'openid email profile' } },
idToken: true,
checks: ['pkce', 'state'], checks: ['pkce', 'state'],
type: 'oauth',
idToken: true,
allowDangerousEmailAccountLinking: true, allowDangerousEmailAccountLinking: true,
profile(profile) { profile(profile) {
return { return {
id: Number(profile.sub), id: profile.sub,
email: profile.email, email: profile.email || profile.preferred_username,
name: profile.name || `${profile.given_name} ${profile.family_name}`.trim(), name: profile.name || `${profile.given_name} ${profile.family_name}`.trim(),
emailVerified: profile.email_verified ? new Date().toISOString() : null,
}; };
}, },
}, },

View File

@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "Account" ADD COLUMN "created_at" INTEGER,
ADD COLUMN "ext_expires_in" INTEGER;

View File

@ -233,6 +233,10 @@ model Account {
refresh_token String? @db.Text refresh_token String? @db.Text
access_token String? @db.Text access_token String? @db.Text
expires_at Int? expires_at Int?
// Some providers return created_at so we need to make it optional
created_at Int?
// Stops next-auth from crashing when dealing with AzureAD
ext_expires_in Int?
token_type String? token_type String?
scope String? scope String?
id_token String? @db.Text id_token String? @db.Text