Skip to content

Commit f679450

Browse files
committed
support client secret
1 parent 42ac681 commit f679450

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ export default defineNuxtModule<ModuleOptions>({
498498
// OIDC OAuth
499499
runtimeConfig.oauth.oidc = defu(runtimeConfig.oauth.oidc, {
500500
clientId: '',
501+
clientSecret: '',
501502
openidConfig: '',
502503
redirectUrl: '',
503504
scope: [],

src/runtime/server/lib/oauth/oidc.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export interface OAuthOidcConfig {
1313
* @default process.env.NUXT_OAUTH_OIDC_CLIENT_ID
1414
*/
1515
clientId?: string
16+
/**
17+
* OAuth Client secret.
18+
* If unset, PKCE will be used where no client secret is needed.
19+
*
20+
* @default process.env.NUXT_OAUTH_OIDC_CLIENT_SECRET
21+
*/
22+
clientSecret?: string
1623
/**
1724
* OpenID configuration. If a string is passed, it is considered to be the full URL to the OpenID configuration endpoint
1825
* where all required endpoints are listed and fetched from automatically.
@@ -257,7 +264,9 @@ export function defineOAuthOidcEventHandler<TUser = OidcUser>({ config, onSucces
257264

258265
const redirectURL = config.redirectURL || getOAuthRedirectURL(event)
259266
const state = await handleState(event)
260-
const verifier = await handlePkceVerifier(event)
267+
268+
// if no client secret is provided, we will use PKCE so no client secret is needed
269+
const verifier = !config.clientSecret ? await handlePkceVerifier(event) : undefined
261270

262271
if (!query.code) {
263272
config.scope = config.scope || []
@@ -270,8 +279,8 @@ export function defineOAuthOidcEventHandler<TUser = OidcUser>({ config, onSucces
270279
scope: config.scope.join(' '),
271280
state,
272281
response_type: 'code',
273-
code_challenge: verifier.code_challenge,
274-
code_challenge_method: verifier.code_challenge_method,
282+
code_challenge: verifier?.code_challenge,
283+
code_challenge_method: verifier?.code_challenge_method,
275284
...config.parameters?.authorization_endpoint,
276285
}),
277286
)
@@ -285,9 +294,10 @@ export function defineOAuthOidcEventHandler<TUser = OidcUser>({ config, onSucces
285294
body: {
286295
grant_type: 'authorization_code',
287296
client_id: config.clientId,
297+
client_secret: config.clientSecret,
288298
redirect_uri: redirectURL,
289299
code: query.code,
290-
code_verifier: verifier.code_verifier,
300+
code_verifier: verifier?.code_verifier,
291301
...config.parameters?.token_endpoint,
292302
},
293303
})

0 commit comments

Comments
 (0)