Skip to content

Commit

Permalink
fix(fetching): 🐛 Fixed a nuxt resolve bug with custom fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
itpropro committed Jan 1, 2025
1 parent a9e393b commit 9f720c5
Show file tree
Hide file tree
Showing 13 changed files with 614 additions and 1,580 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"@nuxt/devtools-kit": "^1.7.0",
"@nuxt/devtools-ui-kit": "^1.7.0",
"@nuxt/kit": "^3.15.0",
"consola": "^3.3.3",
"defu": "^6.1.4",
"h3": "^1.13.0",
Expand All @@ -51,7 +52,6 @@
"@antfu/eslint-config": "^3.12.1",
"@nuxt/devtools": "^1.7.0",
"@nuxt/eslint-config": "^0.7.4",
"@nuxt/kit": "^3.15.0",
"@nuxt/module-builder": "^0.8.4",
"@nuxt/schema": "^3.15.0",
"@nuxt/test-utils": "^3.15.1",
Expand Down
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineNuxtConfig } from 'nuxt/config'

export default defineNuxtConfig({
modules: [
'nuxt-oidc-auth',
'../src/module',
'@unocss/nuxt',
'@nuxtjs/color-mode',
],
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"generate": "nuxi generate"
},
"dependencies": {
"nuxt": "^3.13.2",
"nuxt": "^3.15.0",
"vue": "^3.5.12"
},
"devDependencies": {
Expand Down
2,146 changes: 590 additions & 1,556 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/runtime/providers/auth0.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ofetch } from 'ofetch'
import { normalizeURL, withHttps, withoutTrailingSlash } from 'ufo'
import { createProviderFetch } from '../server/utils/oidc'
import { defineOidcProvider } from '../server/utils/provider'
import { createProviderFetch, defineOidcProvider } from '../server/utils/provider'

interface Auth0ProviderConfig {
/**
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/providers/entra.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ofetch } from 'ofetch'
import { parseURL } from 'ufo'
import { createProviderFetch } from '../server/utils/oidc'
import { defineOidcProvider } from '../server/utils/provider'
import { createProviderFetch, defineOidcProvider } from '../server/utils/provider'

type EntraIdRequiredFields = 'clientId' | 'clientSecret' | 'authorizationUrl' | 'tokenUrl' | 'redirectUri'

Expand Down
3 changes: 1 addition & 2 deletions src/runtime/providers/keycloak.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ofetch } from 'ofetch'
import { generateProviderUrl } from '../server/utils/config'
import { createProviderFetch } from '../server/utils/oidc'
import { defineOidcProvider } from '../server/utils/provider'
import { createProviderFetch, defineOidcProvider } from '../server/utils/provider'

type KeycloakRequiredFields = 'baseUrl' | 'clientId' | 'clientSecret' | 'redirectUri'

Expand Down
3 changes: 1 addition & 2 deletions src/runtime/providers/zitadel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ofetch } from 'ofetch'
import { normalizeURL, withHttps, withoutTrailingSlash } from 'ufo'
import { createProviderFetch } from '../server/utils/oidc'
import { defineOidcProvider, type OidcProviderConfig } from '../server/utils/provider'
import { createProviderFetch, defineOidcProvider, type OidcProviderConfig } from '../server/utils/provider'

type ZitadelRequiredFields = 'baseUrl' | 'clientId' | 'clientSecret'

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/handler/callback.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { H3Event } from 'h3'
import type { OAuthConfig, PersistentSession, ProviderKeys, TokenRequest, TokenRespose, Tokens, UserSession } from '../../types'
import type { OidcProviderConfig } from '../utils/provider'
// @ts-expect-error - Missing Nitro type exports in Nuxt
import { useRuntimeConfig, useStorage } from '#imports'
import { deleteCookie, eventHandler, getQuery, getRequestURL, readBody, sendRedirect } from 'h3'
import { normalizeURL, parseURL } from 'ufo'
import { textToBase64 } from 'undio'
import * as providerPresets from '../../providers'
import { validateConfig } from '../utils/config'
import { configMerger, convertObjectToSnakeCase, convertTokenRequestToType, createProviderFetch, oidcErrorHandler, useOidcLogger } from '../utils/oidc'
import { configMerger, convertObjectToSnakeCase, convertTokenRequestToType, oidcErrorHandler, useOidcLogger } from '../utils/oidc'
import { createProviderFetch, type OidcProviderConfig } from '../utils/provider'
import { encryptToken, type JwtPayload, parseJwtToken, validateToken } from '../utils/security'
import { getUserSessionId, setUserSession, useAuthSession } from '../utils/session'

Expand Down
3 changes: 3 additions & 0 deletions src/runtime/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../../.nuxt/tsconfig.server.json"
}
12 changes: 1 addition & 11 deletions src/runtime/server/utils/oidc.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import type { H3Event } from 'h3'
import type { RefreshTokenRequest, TokenRequest, TokenRespose, UserSession } from '../../types'
import type { OidcProviderConfig } from './provider'
import { createConsola } from 'consola'
import { createDefu } from 'defu'
import { sendRedirect } from 'h3'
import { ofetch } from 'ofetch'
import { snakeCase } from 'scule'
import { normalizeURL } from 'ufo'
import { ProxyAgent } from 'undici'
import { textToBase64 } from 'undio'
import { createProviderFetch, type OidcProviderConfig } from './provider'
import { parseJwtToken } from './security'
import { clearUserSession } from './session'

Expand All @@ -24,14 +22,6 @@ export const configMerger = createDefu((obj, key, value) => {
}
})

export function createProviderFetch(config: OidcProviderConfig) {
if (config.proxy) {
const proxyAgent = config.ignoreProxyCertificateErrors ? new ProxyAgent({ uri: config.proxy, requestTls: { rejectUnauthorized: false } }) : new ProxyAgent({ uri: config.proxy })
return ofetch.create({ dispatcher: proxyAgent })
}
return ofetch
}

export async function refreshAccessToken(refreshToken: string, config: OidcProviderConfig) {
const logger = useOidcLogger()
const customFetch = createProviderFetch(config)
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/server/utils/provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ProviderSessionConfig } from '../../types'
import { createDefu } from 'defu'
import { ofetch } from 'ofetch'
import { ProxyAgent } from 'undici'

type MakePropertiesRequired<T, K extends keyof T> = T & Required<Pick<T, K>>

Expand Down Expand Up @@ -262,3 +264,11 @@ export function defineOidcProvider<TConfig, TRequired extends keyof (OidcProvide
const mergedConfig = configMerger(config, defaults)
return mergedConfig as MakePropertiesRequired<Partial<typeof mergedConfig>, TRequired & 'redirectUri'>
}

export function createProviderFetch(config: OidcProviderConfig) {
if (config.proxy) {
const proxyAgent = config.ignoreProxyCertificateErrors ? new ProxyAgent({ uri: config.proxy, requestTls: { rejectUnauthorized: false } }) : new ProxyAgent({ uri: config.proxy })
return ofetch.create({ dispatcher: proxyAgent })
}
return ofetch
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./.nuxt/tsconfig.json",
"exclude": [
"client",
"node_modules",
"playground",
"dist",
"docs"
Expand Down

0 comments on commit 9f720c5

Please sign in to comment.