-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from cerberauth/create-testid-client
feat: create testid client for testing purposes
- Loading branch information
Showing
13 changed files
with
967 additions
and
1,786 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Use openssl rand -base64 33 to generate a secret | ||
AUTH_SECRET=secret | ||
|
||
AUTH_CLIENT_ID= | ||
AUTH_CLIENT_SECRET= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { handlers } from '@/auth' | ||
|
||
export const runtime = 'edge' | ||
export const { GET, POST } = handlers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import type { NextApiResponse } from 'next' | ||
import { auth } from '@/auth' | ||
import { GrantType, TokenEndpointAuthMethod } from '@/lib/consts' | ||
|
||
export const runtime = 'edge' | ||
|
||
export async function POST(req: Request, res: NextApiResponse) { | ||
const clientData = await req.json() as OAuthClient | ||
if (!clientData) { | ||
return res.status(400).send('Missing client data') | ||
} | ||
|
||
const session = await auth() | ||
if (!session?.token) { | ||
return new Response('You must be logged in.', { status: 401 }) | ||
} | ||
|
||
let method = clientData.tokenEndpointAuthMethod?.[0]; | ||
switch (method) { | ||
case TokenEndpointAuthMethod.clientSecretPost: | ||
method = 'client_secret_post' | ||
case TokenEndpointAuthMethod.none: | ||
method = 'none' | ||
case TokenEndpointAuthMethod.clientSecretBasic: | ||
default: | ||
method = 'client_secret_basic' | ||
} | ||
|
||
const response = await fetch('https://testid.cerberauth.com/oauth2/register', { | ||
method: 'POST', | ||
headers: { | ||
'Authorization': `Bearer ${session.token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
grant_types: clientData.grantTypes.map((type) => { | ||
switch (type) { | ||
case GrantType.authorizationCodeWithPKCE: | ||
return 'authorization_code'; | ||
case 'refreshToken': | ||
return 'refresh_token'; | ||
case 'clientCredentials': | ||
return 'client_credentials'; | ||
case 'deviceCode': | ||
return 'urn:ietf:params:oauth:grant-type:device_code'; | ||
default: | ||
return type; | ||
} | ||
}), | ||
token_endpoint_auth_method: method, | ||
|
||
client_name: clientData.name, | ||
allowed_cors_origins: clientData.allowedCorsOrigins, | ||
scope: clientData.scopes.join(' '), | ||
audience: clientData.audiences, | ||
redirect_uris: clientData.redirectUris, | ||
post_logout_redirect_uris: clientData.postLogoutRedirectUris, | ||
|
||
contacts: clientData.contacts, | ||
client_uri: clientData.uri, | ||
policy_uri: clientData.policyUri, | ||
tos_uri: clientData.tosUri, | ||
logo_uri: clientData.logoUri, | ||
}), | ||
}) | ||
if (!response.ok) { | ||
throw new Error(`Failed to register client: ${response.statusText}`) | ||
} | ||
const data = await response.json() | ||
|
||
return Response.json({ | ||
clientId: data.client_id, | ||
clientSecret: data.client_secret, | ||
client: clientData, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.