Skip to content

Commit 925f688

Browse files
Barbapapazesatinuxautofix-ci[bot]
authored
refactor: request token
* refactor: request token * chore: fix import * up * up * Merge branch 'main' into refactor/request-token * [autofix.ci] apply automated fixes * chore: fix types issue * chore: lint --------- Co-authored-by: Sébastien Chopin <seb@nuxt.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 8d3af7e commit 925f688

File tree

18 files changed

+318
-406
lines changed

18 files changed

+318
-406
lines changed

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

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { H3Event } from 'h3'
2-
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
3-
import { withQuery, parsePath } from 'ufo'
2+
import { eventHandler, getQuery, sendRedirect } from 'h3'
3+
import { withQuery } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleMissingConfiguration, handleAccessTokenErrorResponse } from '../utils'
5+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
66
import { useRuntimeConfig } from '#imports'
77
import type { OAuthConfig } from '#auth-utils'
88

@@ -70,16 +70,17 @@ export function oauthAuth0EventHandler({ config, onSuccess, onError }: OAuthConf
7070
config = defu(config, useRuntimeConfig(event).oauth?.auth0, {
7171
authorizationParams: {},
7272
}) as OAuthAuth0Config
73-
const { code } = getQuery(event)
7473

7574
if (!config.clientId || !config.clientSecret || !config.domain) {
7675
return handleMissingConfiguration(event, 'auth0', ['clientId', 'clientSecret', 'domain'], onError)
7776
}
7877
const authorizationURL = `https://${config.domain}/authorize`
7978
const tokenURL = `https://${config.domain}/oauth/token`
8079

81-
const redirectURL = config.redirectURL || getRequestURL(event).href
82-
if (!code) {
80+
const query = getQuery<{ code?: string }>(event)
81+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event)
82+
83+
if (!query.code) {
8384
config.scope = config.scope || ['openid', 'offline_access']
8485
if (config.emailRequired && !config.scope.includes('email')) {
8586
config.scope.push('email')
@@ -100,25 +101,17 @@ export function oauthAuth0EventHandler({ config, onSuccess, onError }: OAuthConf
100101
)
101102
}
102103

103-
// TODO: improve typing
104-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
105-
const tokens: any = await $fetch(
106-
tokenURL as string,
107-
{
108-
method: 'POST',
109-
headers: {
110-
'Content-Type': 'application/json',
111-
},
112-
body: {
113-
grant_type: 'authorization_code',
114-
client_id: config.clientId,
115-
client_secret: config.clientSecret,
116-
redirect_uri: parsePath(redirectURL).pathname,
117-
code,
118-
},
104+
const tokens = await requestAccessToken(tokenURL as string, {
105+
headers: {
106+
'Content-Type': 'application/json',
107+
},
108+
body: {
109+
grant_type: 'authorization_code',
110+
client_id: config.clientId,
111+
client_secret: config.clientSecret,
112+
redirect_uri: redirectURL,
113+
code: query.code,
119114
},
120-
).catch((error) => {
121-
return { error }
122115
})
123116

124117
if (tokens.error) {

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

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { randomUUID } from 'node:crypto'
21
import type { H3Event } from 'h3'
3-
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
4-
import { withQuery, parsePath } from 'ufo'
2+
import { eventHandler, getQuery, sendRedirect } from 'h3'
3+
import { withQuery } from 'ufo'
54
import { defu } from 'defu'
6-
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
7-
import { useRuntimeConfig } from '#imports'
5+
import { randomUUID } from 'uncrypto'
6+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
7+
import { useRuntimeConfig, createError } from '#imports'
88
import type { OAuthConfig } from '#auth-utils'
99

1010
export interface OAuthBattledotnetConfig {
@@ -62,8 +62,7 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }: OA
6262
authorizationParams: {},
6363
}) as OAuthBattledotnetConfig
6464

65-
const query = getQuery(event)
66-
const { code } = query
65+
const query = getQuery<{ code?: string, error?: string }>(event)
6766

6867
if (query.error) {
6968
const error = createError({
@@ -80,8 +79,9 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }: OA
8079
)
8180
}
8281

83-
const redirectURL = config.redirectURL || getRequestURL(event).href
84-
if (!code) {
82+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event)
83+
84+
if (!query.code) {
8585
config.scope = config.scope || ['openid']
8686
config.region = config.region || 'EU'
8787

@@ -109,27 +109,16 @@ export function oauthBattledotnetEventHandler({ config, onSuccess, onError }: OA
109109
config.scope.push('openid')
110110
}
111111

112-
const authCode = Buffer.from(`${config.clientId}:${config.clientSecret}`).toString('base64')
113-
114-
// TODO: improve typing
115-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
116-
const tokens: any = await $fetch(
117-
config.tokenURL as string,
118-
{
119-
method: 'POST',
120-
headers: {
121-
'Content-Type': 'application/x-www-form-urlencoded',
122-
'Authorization': `Basic ${authCode}`,
123-
},
124-
params: {
125-
code,
126-
grant_type: 'authorization_code',
127-
scope: config.scope.join(' '),
128-
redirect_uri: parsePath(redirectURL).pathname,
129-
},
112+
const tokens = await requestAccessToken(config.tokenURL as string, {
113+
headers: {
114+
Authorization: `Basic ${Buffer.from(`${config.clientId}:${config.clientSecret}`).toString('base64')}`,
115+
},
116+
params: {
117+
grant_type: 'authorization_code',
118+
scope: config.scope.join(' '),
119+
redirect_uri: redirectURL,
120+
code: query.code,
130121
},
131-
).catch((error) => {
132-
return { error }
133122
})
134123

135124
if (tokens.error) {

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { H3Event } from 'h3'
2-
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
3-
import { withQuery, parsePath } from 'ufo'
2+
import { eventHandler, getQuery, sendRedirect } from 'h3'
3+
import { withQuery } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
5+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
66
import { useRuntimeConfig } from '#imports'
77
import type { OAuthConfig } from '#auth-utils'
88

@@ -54,7 +54,6 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }: OAuthCo
5454
config = defu(config, useRuntimeConfig(event).oauth?.cognito, {
5555
authorizationParams: {},
5656
}) as OAuthCognitoConfig
57-
const { code } = getQuery(event)
5857

5958
if (!config.clientId || !config.clientSecret || !config.userPoolId || !config.region) {
6059
return handleMissingConfiguration(event, 'cognito', ['clientId', 'clientSecret', 'userPoolId', 'region'], onError)
@@ -65,8 +64,10 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }: OAuthCo
6564
const authorizationURL = `https://${urlBase}/oauth2/authorize`
6665
const tokenURL = `https://${urlBase}/oauth2/token`
6766

68-
const redirectURL = config.redirectURL || getRequestURL(event).href
69-
if (!code) {
67+
const query = getQuery<{ code?: string }>(event)
68+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event)
69+
70+
if (!query.code) {
7071
config.scope = config.scope || ['openid', 'profile']
7172
// Redirect to Cognito login page
7273
return sendRedirect(
@@ -81,20 +82,18 @@ export function oauthCognitoEventHandler({ config, onSuccess, onError }: OAuthCo
8182
)
8283
}
8384

84-
// TODO: improve typing
85-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
86-
const tokens: any = await $fetch(
85+
const tokens = await requestAccessToken(
8786
tokenURL as string,
8887
{
89-
method: 'POST',
90-
headers: {
91-
'Content-Type': 'application/x-www-form-urlencoded',
88+
body: {
89+
grant_type: 'authorization_code',
90+
client_id: config.clientId,
91+
client_secret: config.clientSecret,
92+
redirect_uri: redirectURL,
93+
code: query.code,
9294
},
93-
body: `grant_type=authorization_code&client_id=${config.clientId}&client_secret=${config.clientSecret}&redirect_uri=${parsePath(redirectURL).pathname}&code=${code}`,
9495
},
95-
).catch((error) => {
96-
return { error }
97-
})
96+
)
9897

9998
if (tokens.error) {
10099
return handleAccessTokenErrorResponse(event, 'cognito', tokens, onError)

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

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { H3Event } from 'h3'
2-
import { eventHandler, getQuery, getRequestURL, sendRedirect } from 'h3'
3-
import { withQuery, parseURL, stringifyParsedURL } from 'ufo'
2+
import { eventHandler, getQuery, sendRedirect } from 'h3'
3+
import { withQuery } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
5+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
66
import { useRuntimeConfig } from '#imports'
77
import type { OAuthConfig } from '#auth-utils'
88

@@ -67,14 +67,15 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }: OAuthCo
6767
profileRequired: true,
6868
authorizationParams: {},
6969
}) as OAuthDiscordConfig
70-
const { code } = getQuery(event)
70+
const query = getQuery<{ code?: string }>(event)
7171

7272
if (!config.clientId || !config.clientSecret) {
7373
return handleMissingConfiguration(event, 'discord', ['clientId', 'clientSecret'], onError)
7474
}
7575

76-
const redirectURL = config.redirectURL || getRequestURL(event).href
77-
if (!code) {
76+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event)
77+
78+
if (!query.code) {
7879
config.scope = config.scope || []
7980
if (config.emailRequired && !config.scope.includes('email')) {
8081
config.scope.push('email')
@@ -96,27 +97,14 @@ export function oauthDiscordEventHandler({ config, onSuccess, onError }: OAuthCo
9697
)
9798
}
9899

99-
const parsedRedirectUrl = parseURL(redirectURL)
100-
parsedRedirectUrl.search = ''
101-
// TODO: improve typing
102-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
103-
const tokens: any = await $fetch(
104-
config.tokenURL as string,
105-
{
106-
method: 'POST',
107-
headers: {
108-
'Content-Type': 'application/x-www-form-urlencoded',
109-
},
110-
body: new URLSearchParams({
111-
client_id: config.clientId,
112-
client_secret: config.clientSecret,
113-
grant_type: 'authorization_code',
114-
redirect_uri: stringifyParsedURL(parsedRedirectUrl),
115-
code: code as string,
116-
}).toString(),
100+
const tokens = await requestAccessToken(config.tokenURL as string, {
101+
body: {
102+
client_id: config.clientId,
103+
client_secret: config.clientSecret,
104+
grant_type: 'authorization_code',
105+
redirect_uri: redirectURL,
106+
code: query.code,
117107
},
118-
).catch((error) => {
119-
return { error }
120108
})
121109

122110
if (tokens.error) {

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import type { H3Event } from 'h3'
2-
import {
3-
eventHandler,
4-
createError,
5-
getQuery,
6-
getRequestURL,
7-
sendRedirect,
8-
} from 'h3'
2+
import { eventHandler, getQuery, sendRedirect } from 'h3'
93
import { withQuery } from 'ufo'
104
import { defu } from 'defu'
11-
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
12-
import { useRuntimeConfig } from '#imports'
5+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
6+
import { useRuntimeConfig, createError } from '#imports'
137
import type { OAuthConfig } from '#auth-utils'
148

159
export interface OAuthFacebookConfig {
@@ -74,7 +68,8 @@ export function oauthFacebookEventHandler({
7468
tokenURL: 'https://graph.facebook.com/v19.0/oauth/access_token',
7569
authorizationParams: {},
7670
}) as OAuthFacebookConfig
77-
const query = getQuery(event)
71+
72+
const query = getQuery<{ code?: string, error?: string }>(event)
7873

7974
if (query.error) {
8075
const error = createError({
@@ -90,7 +85,8 @@ export function oauthFacebookEventHandler({
9085
return handleMissingConfiguration(event, 'facebook', ['clientId'], onError)
9186
}
9287

93-
const redirectURL = config.redirectURL || getRequestURL(event).href
88+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event)
89+
9490
if (!query.code) {
9591
config.scope = config.scope || []
9692
// Redirect to Facebook Oauth page
@@ -104,13 +100,11 @@ export function oauthFacebookEventHandler({
104100
)
105101
}
106102

107-
// TODO: improve typing
108-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
109-
const tokens: any = await $fetch(config.tokenURL as string, {
110-
method: 'POST',
103+
const tokens = await requestAccessToken(config.tokenURL as string, {
111104
body: {
112105
client_id: config.clientId,
113106
client_secret: config.clientSecret,
107+
grant_type: 'authorization_code',
114108
redirect_uri: redirectURL,
115109
code: query.code,
116110
},

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { H3Event } from 'h3'
2-
import { eventHandler, createError, getQuery, getRequestURL, sendRedirect } from 'h3'
2+
import { eventHandler, getQuery, sendRedirect } from 'h3'
33
import { withQuery } from 'ufo'
44
import { defu } from 'defu'
5-
import { handleAccessTokenErrorResponse, handleMissingConfiguration } from '../utils'
6-
import { useRuntimeConfig } from '#imports'
5+
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
6+
import { useRuntimeConfig, createError } from '#imports'
77
import type { OAuthConfig } from '#auth-utils'
88

99
export interface OAuthGitHubConfig {
@@ -64,7 +64,8 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }: OAuthCon
6464
tokenURL: 'https://github.com/login/oauth/access_token',
6565
authorizationParams: {},
6666
}) as OAuthGitHubConfig
67-
const query = getQuery(event)
67+
68+
const query = getQuery<{ code?: string, error?: string }>(event)
6869

6970
if (query.error) {
7071
const error = createError({
@@ -80,13 +81,14 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }: OAuthCon
8081
return handleMissingConfiguration(event, 'github', ['clientId', 'clientSecret'], onError)
8182
}
8283

84+
const redirectURL = config.redirectURL || getOAuthRedirectURL(event)
85+
8386
if (!query.code) {
8487
config.scope = config.scope || []
8588
if (config.emailRequired && !config.scope.includes('user:email')) {
8689
config.scope.push('user:email')
8790
}
88-
// Redirect to GitHub Oauth page
89-
const redirectURL = config.redirectURL || getRequestURL(event).href
91+
9092
return sendRedirect(
9193
event,
9294
withQuery(config.authorizationURL as string, {
@@ -98,19 +100,15 @@ export function oauthGitHubEventHandler({ config, onSuccess, onError }: OAuthCon
98100
)
99101
}
100102

101-
// TODO: improve typing
102-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
103-
const tokens: any = await $fetch(
104-
config.tokenURL as string,
105-
{
106-
method: 'POST',
107-
body: {
108-
client_id: config.clientId,
109-
client_secret: config.clientSecret,
110-
code: query.code,
111-
},
103+
const tokens = await requestAccessToken(config.tokenURL as string, {
104+
body: {
105+
grant_type: 'authorization_code',
106+
client_id: config.clientId,
107+
client_secret: config.clientSecret,
108+
redirect_uri: redirectURL,
109+
code: query.code,
112110
},
113-
)
111+
})
114112

115113
if (tokens.error) {
116114
return handleAccessTokenErrorResponse(event, 'github', tokens, onError)

0 commit comments

Comments
 (0)