Validate endpoint

This commit is contained in:
Adria Navarro 2025-03-19 15:43:43 +01:00
parent 81db09fc7f
commit e6612b2a9c
4 changed files with 60 additions and 0 deletions

View File

@ -7,6 +7,8 @@ import {
RequiredKeys,
OAuth2ConfigResponse,
PASSWORD_REPLACEMENT,
ValidateConfigResponse,
ValidateConfigRequest,
} from "@budibase/types"
import sdk from "../../sdk"
@ -75,3 +77,18 @@ export async function remove(
await sdk.oauth2.remove(configToRemove)
ctx.status = 204
}
export async function validate(
ctx: Ctx<ValidateConfigRequest, ValidateConfigResponse>
) {
const { body } = ctx.request
const config = {
url: body.url,
clientId: body.clientId,
clientSecret: body.clientSecret,
}
const validation = await sdk.oauth2.validateConfig(config)
ctx.status = 201
ctx.body = validation
}

View File

@ -38,5 +38,10 @@ router.delete(
authorized(PermissionType.BUILDER),
controller.remove
)
router.post(
"/api/oauth2/:id/validate",
authorized(PermissionType.BUILDER),
controller.validate
)
export default router

View File

@ -31,3 +31,30 @@ export async function generateToken(id: string) {
return `${jsonResponse.token_type} ${jsonResponse.access_token}`
}
export async function validateConfig(config: {
url: string
clientId: string
clientSecret: string
}): Promise<{ valid: boolean; message?: string }> {
const resp = await fetch(config.url, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
grant_type: "client_credentials",
client_id: config.clientId,
client_secret: config.clientSecret,
}),
redirect: "follow",
})
const jsonResponse = await resp.json()
if (!resp.ok) {
const message = jsonResponse.error_description ?? resp.statusText
return { valid: false, message }
}
return { valid: true }
}

View File

@ -20,3 +20,14 @@ export interface UpsertOAuth2ConfigRequest {
export interface UpsertOAuth2ConfigResponse {
config: OAuth2ConfigResponse
}
export interface ValidateConfigRequest {
url: string
clientId: string
clientSecret: string
}
export interface ValidateConfigResponse {
valid: boolean
message?: string
}