1
+ import type { OAuthConfig } from '#auth-utils'
2
+ import { useRuntimeConfig } from '#imports'
3
+ import { defu } from 'defu'
1
4
import type { H3Event } from 'h3'
2
5
import { eventHandler , getQuery , sendRedirect } from 'h3'
6
+ import { discovery } from 'openid-client'
3
7
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'
8
9
9
10
export interface OAuthCognitoConfig {
10
11
/**
@@ -42,11 +43,6 @@ export interface OAuthCognitoConfig {
42
43
* @default process.env.NUXT_OAUTH_COGNITO_REDIRECT_URL or current URL
43
44
*/
44
45
redirectURL ?: string
45
- /**
46
- * AWS Cognito App Custom Domain – some pool configurations require this
47
- * @default ''
48
- */
49
- domain ?: string
50
46
}
51
47
52
48
export function defineOAuthCognitoEventHandler ( { config, onSuccess, onError } : OAuthConfig < OAuthCognitoConfig > ) {
@@ -59,11 +55,16 @@ export function defineOAuthCognitoEventHandler({ config, onSuccess, onError }: O
59
55
return handleMissingConfiguration ( event , 'cognito' , [ 'clientId' , 'clientSecret' , 'userPoolId' , 'region' ] , onError )
60
56
}
61
57
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 ( )
67
68
const query = getQuery < { code ?: string } > ( event )
68
69
const redirectURL = config . redirectURL || getOAuthRedirectURL ( event )
69
70
@@ -101,9 +102,8 @@ export function defineOAuthCognitoEventHandler({ config, onSuccess, onError }: O
101
102
102
103
const tokenType = tokens . token_type
103
104
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 , {
107
107
headers : {
108
108
Authorization : `${ tokenType } ${ accessToken } ` ,
109
109
} ,
0 commit comments