Skip to content

Commit 7a01cc3

Browse files
authored
feat(cognito): integrate OpenID Connect discovery for improved OAuth flow
* feat(cognito): integrate OpenID Connect discovery for improved OAuth flow * feat(cognito): enhance OAuth flow by including client secret in discovery process * chore: update lockfile
1 parent ac61ae5 commit 7a01cc3

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"hookable": "^5.5.3",
4343
"ofetch": "^1.4.1",
4444
"ohash": "^1.1.4",
45+
"openid-client": "^6.1.4",
4546
"pathe": "^1.1.2",
4647
"scule": "^1.3.0",
4748
"uncrypto": "^0.1.3"

pnpm-lock.yaml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import type { OAuthConfig } from '#auth-utils'
2+
import { useRuntimeConfig } from '#imports'
3+
import { defu } from 'defu'
14
import type { H3Event } from 'h3'
25
import { eventHandler, getQuery, sendRedirect } from 'h3'
6+
import { discovery } from 'openid-client'
37
import { withQuery } from 'ufo'
4-
import { defu } from 'defu'
5-
import { handleMissingConfiguration, handleAccessTokenErrorResponse, getOAuthRedirectURL, requestAccessToken } from '../utils'
6-
import { useRuntimeConfig } from '#imports'
7-
import type { OAuthConfig } from '#auth-utils'
8+
import { getOAuthRedirectURL, handleAccessTokenErrorResponse, handleMissingConfiguration, requestAccessToken } from '../utils'
89

910
export interface OAuthCognitoConfig {
1011
/**
@@ -42,11 +43,6 @@ export interface OAuthCognitoConfig {
4243
* @default process.env.NUXT_OAUTH_COGNITO_REDIRECT_URL or current URL
4344
*/
4445
redirectURL?: string
45-
/**
46-
* AWS Cognito App Custom Domain – some pool configurations require this
47-
* @default ''
48-
*/
49-
domain?: string
5046
}
5147

5248
export function defineOAuthCognitoEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthCognitoConfig>) {
@@ -59,11 +55,16 @@ export function defineOAuthCognitoEventHandler({ config, onSuccess, onError }: O
5955
return handleMissingConfiguration(event, 'cognito', ['clientId', 'clientSecret', 'userPoolId', 'region'], onError)
6056
}
6157

62-
const urlBase = config?.domain || `${config.userPoolId}.auth.${config.region}.amazoncognito.com`
63-
64-
const authorizationURL = `https://${urlBase}/oauth2/authorize`
65-
const tokenURL = `https://${urlBase}/oauth2/token`
66-
58+
const congitoDiscoveryUrl = new URL(`https://cognito-idp.${config.region}.amazonaws.com/${config.userPoolId}/.well-known/openid-configuration`)
59+
const issuer = await discovery(congitoDiscoveryUrl, config.clientId, config.clientSecret)
60+
const {
61+
authorization_endpoint: authorizationURL,
62+
token_endpoint: tokenURL,
63+
userinfo_endpoint: userinfoURL,
64+
// TODO: implement logout
65+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
66+
end_session_endpoint: logoutURL,
67+
} = issuer.serverMetadata()
6768
const query = getQuery<{ code?: string }>(event)
6869
const redirectURL = config.redirectURL || getOAuthRedirectURL(event)
6970

@@ -101,9 +102,8 @@ export function defineOAuthCognitoEventHandler({ config, onSuccess, onError }: O
101102

102103
const tokenType = tokens.token_type
103104
const accessToken = tokens.access_token
104-
// TODO: improve typing
105-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
106-
const user: any = await $fetch(`https://${urlBase}/oauth2/userInfo`, {
105+
// TODO: improve typing of user profile
106+
const user: unknown = await $fetch(userinfoURL as string, {
107107
headers: {
108108
Authorization: `${tokenType} ${accessToken}`,
109109
},

0 commit comments

Comments
 (0)