Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
only issue when grant type refresh
Thiago Araujo da Silva7ac27f910fe0
diff --git a/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts b/backend/e2e-test/routes/v1/oauth-token-exchange.spec.tsindex 08d8911ac0..55e61d4611 100644--- a/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts+++ b/backend/e2e-test/routes/v1/oauth-token-exchange.spec.ts@@ -28,6 +28,14 @@ const rotateClientSecret = async (clientDbId: string) =>headers: { authorization: `Bearer ${jwtAuthToken}` }});+const consent = async (body: Record<string, string>) =>+ testServer.inject({+ method: "POST",+ url: "/api/v1/oauth/authorize/consent",+ headers: { authorization: `Bearer ${jwtAuthToken}` },+ body+ });+const postToken = async (body: Record<string, string>) =>testServer.inject({method: "POST",@@ -305,3 +313,61 @@ describe("Full delegation marker on a delegated OAuth token", async () => {expect(res.statusCode).toBe(403);});});++// A refresh token is only usable if the application holds the refresh_token grant, so issuing one to an+// application without it hands back a credential the token endpoint rejects on use.+describe("Refresh token issuance follows the registered grants", async () => {+ const REDIRECT_URI = "https://app.example.com/callback";++ const runCodeFlow = async (grantTypes: OauthGrantType[]) => {+ const created = await createClient({+ name: `e2e-refresh-issuance-${grantTypes.length}`,+ grantTypes,+ redirectUris: [REDIRECT_URI]+ });++ const { client, clientSecret } = JSON.parse(created.payload) as {+ client: { id: string; clientId: string };+ clientSecret: string;+ };++ const consented = await consent({ client_id: client.clientId, redirect_uri: REDIRECT_URI });+ expect(consented.statusCode).toBe(200);++ const { callbackUrl } = JSON.parse(consented.payload) as { callbackUrl: string };+ const code = new URL(callbackUrl).searchParams.get("code") as string;++ const token = await postToken({+ grant_type: OauthGrantType.AuthorizationCode,+ code,+ redirect_uri: REDIRECT_URI,+ client_id: client.clientId,+ client_secret: clientSecret+ });++ return { clientDbId: client.id, token };+ };++ test("issues a refresh token when the application holds the refresh_token grant", async () => {+ const { clientDbId, token } = await runCodeFlow([+ OauthGrantType.AuthorizationCode,+ OauthGrantType.RefreshToken+ ]);++ expect(token.statusCode).toBe(200);+ expect(JSON.parse(token.payload)).toHaveProperty("refresh_token");++ await deleteClient(clientDbId);+ });++ test("omits the refresh token when the application does not", async () => {+ const { clientDbId, token } = await runCodeFlow([OauthGrantType.AuthorizationCode]);++ expect(token.statusCode).toBe(200);+ const body = JSON.parse(token.payload) as Record<string, unknown>;+ expect(body).not.toHaveProperty("refresh_token");+ expect(body).toHaveProperty("access_token");++ await deleteClient(clientDbId);+ });+});diff --git a/backend/src/services/oauth-client/oauth-client-service.ts b/backend/src/services/oauth-client/oauth-client-service.tsindex e7beb7a236..084c304e73 100644--- a/backend/src/services/oauth-client/oauth-client-service.ts+++ b/backend/src/services/oauth-client/oauth-client-service.ts@@ -368,8 +368,11 @@ export const oauthClientServiceFactory = ({return { client: sanitizeOauthClient(updatedClient), clientSecret };};+ const isGrantEnabled = (client: TOauthClients, grantType: OauthGrantType) =>+ (client.grantTypes as OauthGrantType[]).includes(grantType);+const assertGrantEnabled = (client: TOauthClients, grantType: OauthGrantType) => {- if (!(client.grantTypes as OauthGrantType[]).includes(grantType)) {+ if (!isGrantEnabled(client, grantType)) {throw new UnauthorizedError({message: `This application is not registered for the '${grantType}' grant type`});@@ -703,21 +706,23 @@ export const oauthClientServiceFactory = ({accessTokenExpiresIn);- const refreshToken = signOauthToken(- {- ...sharedClaims,- tokenType: AuthTokenType.REFRESH_TOKEN,- version: tokenSession.refreshVersion,- oauthClientId: client.clientId- },- refreshTokenExpiresIn- );+ const refreshToken = isGrantEnabled(client, OauthGrantType.RefreshToken)+ ? signOauthToken(+ {+ ...sharedClaims,+ tokenType: AuthTokenType.REFRESH_TOKEN,+ version: tokenSession.refreshVersion,+ oauthClientId: client.clientId+ },+ refreshTokenExpiresIn+ )+ : undefined;return {access_token: accessToken,token_type: "Bearer" as const,expires_in: expiresInToSeconds(accessTokenExpiresIn),- refresh_token: refreshToken,+ ...(refreshToken ? { refresh_token: refreshToken } : {}),scope: grantedScopes.join(" ")};}diff --git a/docs/documentation/platform/oauth-applications.mdx b/docs/documentation/platform/oauth-applications.mdxindex f083c4cabf..c1dc1bb5e2 100644--- a/docs/documentation/platform/oauth-applications.mdx+++ b/docs/documentation/platform/oauth-applications.mdx@@ -140,6 +140,8 @@ The response contains:Access token lifetime follows your organization's user token expiration setting (falling back to the platform default). Refresh tokens rotate on use, and a refresh never broadens the scopes originally granted.+`refresh_token` is present only when the application is registered for the `refresh_token` grant, which applications created in the UI always are. If you registered one through the API with `grant_type` limited to `authorization_code`, the field is omitted and the client should exchange a new authorization code when the access token expires.+### Validation endpoint```