Send client id and secret

This commit is contained in:
Adria Navarro 2025-03-12 18:08:33 +01:00
parent 29cf7288fe
commit 42717b397e
3 changed files with 17 additions and 2 deletions

View File

@ -45,13 +45,15 @@ describe("oauth2 utils", () => {
const oauthConfig = await sdk.oauth2.create({ const oauthConfig = await sdk.oauth2.create({
name: generator.guid(), name: generator.guid(),
url: `${keycloakUrl}/realms/myrealm/protocol/openid-connect/token`, url: `${keycloakUrl}/realms/myrealm/protocol/openid-connect/token`,
clientId: "my-client",
clientSecret: "my-secret",
}) })
const response = await generateToken(oauthConfig.id) const response = await generateToken(oauthConfig.id)
return response return response
}) })
expect(response).toBe("") expect(response).toBe(expect.stringMatching(/^Bearer .+/))
}) })
}) })
}) })

View File

@ -8,7 +8,18 @@ export async function generateToken(id: string) {
throw new HttpError(`oAuth config ${id} count not be found`) throw new HttpError(`oAuth config ${id} count not be found`)
} }
const resp = await fetch(config.url, { method: "post" }) 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",
})
if (resp.status !== 200) { if (resp.status !== 200) {
throw new Error(`Error fetching oauth2 token: ${resp.statusText}`) throw new Error(`Error fetching oauth2 token: ${resp.statusText}`)
} }

View File

@ -4,6 +4,8 @@ export interface OAuth2Config {
id: string id: string
name: string name: string
url: string url: string
clientId: string
clientSecret: string
} }
export interface OAuth2Configs extends Document { export interface OAuth2Configs extends Document {