1+ import type { OAuthConfig } from '#auth-utils'
2+ import { useRuntimeConfig } from '#imports'
3+ import { defu } from 'defu'
14import type { H3Event } from 'h3'
25import { eventHandler , getQuery , sendRedirect } from 'h3'
6+ import { discovery } from 'openid-client'
37import { 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
910export 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
5248export 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