Skip to content

Commit d51958d

Browse files
committed
add clientSecret as an optional
1 parent 46ec68b commit d51958d

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type { H3Event } from 'h3'
22
import { eventHandler, getQuery, sendRedirect } from 'h3'
33
import { withQuery } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken, handleState, handlePkceVerifier } from '../utils'
5+
import type { RequestAccessTokenOptions } from '../utils'
6+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken, handleState, handlePkceVerifier, handleInvalidState } from '../utils'
67
import { useRuntimeConfig, createError } from '#imports'
78
import type { OAuthConfig } from '#auth-utils'
89

@@ -12,6 +13,11 @@ export interface OAuthZitadelConfig {
1213
* @default process.env.NUXT_OAUTH_ZITADEL_CLIENT_ID
1314
*/
1415
clientId?: string
16+
/**
17+
* ZITADEL OAuth Client Secret
18+
* @default process.env.NUXT_OAUTH_ZITADEL_CLIENT_SECRET
19+
*/
20+
clientSecret?: string
1521
/**
1622
* ZITADEL OAuth Domain
1723
* @example <your-zitadel-instance>.zitadel.cloud
@@ -90,15 +96,25 @@ export function defineOAuthZitadelEventHandler({ config, onSuccess, onError }: O
9096
handleInvalidState(event, 'zitadel', onError)
9197
}
9298

93-
const tokens = await requestAccessToken(tokenURL, {
99+
const request: RequestAccessTokenOptions = {
94100
body: {
95101
grant_type: 'authorization_code',
96102
client_id: config.clientId,
97103
redirect_uri: redirectURL,
98104
code: query.code,
99105
code_verifier: verifier.code_verifier,
100106
},
101-
})
107+
}
108+
109+
if (config.clientSecret) {
110+
const basicAuthorization = Buffer.from(`${config.clientId}:${config.clientSecret}`).toString('base64')
111+
request.headers = {
112+
'Authorization': `Basic ${basicAuthorization}`,
113+
'Content-Type': 'application/x-www-form-urlencoded',
114+
}
115+
}
116+
117+
const tokens = await requestAccessToken(tokenURL, request)
102118

103119
if (tokens.error) {
104120
return handleAccessTokenErrorResponse(event, 'zitadel', tokens, onError)

src/runtime/server/lib/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface RequestAccessTokenBody {
2727
[key: string]: string | undefined
2828
}
2929

30-
interface RequestAccessTokenOptions {
30+
export interface RequestAccessTokenOptions {
3131
body?: RequestAccessTokenBody
3232
params?: Record<string, string | undefined>
3333
headers?: Record<string, string>

0 commit comments

Comments
 (0)