Skip to content

Fix OAuth error response mapping for authentication endpoints #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/main/kotlin/com/workos/WorkOS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.workos.common.exceptions.UnauthorizedException
import com.workos.common.exceptions.UnprocessableEntityException
import com.workos.common.http.BadRequestExceptionResponse
import com.workos.common.http.GenericErrorResponse
import com.workos.common.http.OAuthErrorResponse
import com.workos.common.http.RequestConfig
import com.workos.common.http.UnprocessableEntityExceptionResponse
import com.workos.directorysync.DirectorySyncApi
Expand Down Expand Up @@ -359,12 +360,18 @@ class WorkOS(
}

private fun handleResponseError(response: Response, payload: String) {
val requestId = response.header("X-Request-ID").first()
val requestId = response.header("X-Request-ID").firstOrNull() ?: "unknown"

when (val status = response.statusCode) {
400 -> {
val responseData = mapper.readValue(payload, BadRequestExceptionResponse::class.java)
throw BadRequestException(responseData.message, responseData.code, responseData.errors, requestId)
val jsonNode = mapper.readTree(payload)
if (jsonNode.has("error") && (jsonNode.has("error_description") || !jsonNode.has("message"))) {
val oauthError = mapper.treeToValue(jsonNode, OAuthErrorResponse::class.java)
throw BadRequestException(oauthError.errorDescription, oauthError.error, null, requestId)
} else {
val responseData = mapper.treeToValue(jsonNode, BadRequestExceptionResponse::class.java)
throw BadRequestException(responseData.message, responseData.code, responseData.errors, requestId)
}
}

401 -> {
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/com/workos/common/http/OAuthErrorResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.workos.common.http

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty

/**
* Error response format for OAuth/Authentication endpoints.
* These endpoints return errors in the OAuth 2.0 standard format.
*/
internal data class OAuthErrorResponse
@JsonCreator constructor(
@JsonProperty("error")
val error: String? = null,

@JsonProperty("error_description")
val errorDescription: String? = null
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.workos.test.usermanagement

import com.workos.common.exceptions.BadRequestException
import com.workos.common.models.ListMetadata
import com.workos.common.models.Order
import com.workos.test.TestBase
Expand Down Expand Up @@ -494,6 +495,55 @@ class UserManagementApiTest : TestBase() {
assertEquals(listOf("email", "profile"), response.oauthTokens?.scopes)
}

@Test
fun authenticateWithCodeShouldHandleOAuthErrorResponse() {
// Tests fix for issue #287: OAuth error responses have different format
stubResponse(
"/user_management/authenticate",
"""{
"error": "invalid_grant",
"error_description": "The code 'INVALID_CODE' has expired or is invalid."
}""",
responseStatus = 400
)

val exception = assertThrows(BadRequestException::class.java) {
workos.userManagement.authenticateWithCode(
"client_123",
"INVALID_CODE",
null
)
}

// OAuth errors should map error_description to message and error to code
assertEquals("The code 'INVALID_CODE' has expired or is invalid.", exception.message)
assertEquals("invalid_grant", exception.code)
assertEquals(null, exception.errors)
}

@Test
fun authenticateWithCodeShouldHandleOAuthErrorWithoutDescription() {
// OAuth spec allows error_description to be optional
stubResponse(
"/user_management/authenticate",
"""{
"error": "invalid_client"
}""",
responseStatus = 400
)

val exception = assertThrows(BadRequestException::class.java) {
workos.userManagement.authenticateWithCode(
"client_123",
"SOME_CODE",
null
)
}

assertEquals("invalid_client", exception.code)
assertEquals(null, exception.message)
}

@Test
fun authenticateWithPasswordShouldReturnAuthenticationResponse() {
val workos = createWorkOSClient()
Expand Down
Loading