Skip to content
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

Fix incorrect PKCE code challenge generation #187

Merged
merged 3 commits into from
Jan 5, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Changed
* Fixed issue with sending scheduled messages
* Fixed incorrect PKCE code challenge generation

## [2.0.0-beta.3] - Released 2023-12-18

Expand Down
15 changes: 13 additions & 2 deletions src/main/kotlin/com/nylas/resources/Auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ class Auth(private val client: NylasClient) {
val urlBuilder = urlAuthBuilder(config)
val secret = UUID.randomUUID().toString()

val sha256Digest = MessageDigest.getInstance("SHA-256").digest(secret.toByteArray())
val secretHash = Base64.getEncoder().encodeToString(sha256Digest)
val secretHash = hashPkceSecret(secret)

urlBuilder
.addQueryParameter("response_type", "code")
Expand Down Expand Up @@ -143,6 +142,18 @@ class Auth(private val client: NylasClient) {
return client.executePost(path, responseType, queryParams = params)
}

/**
* Hash a plain text secret for use in PKCE
* @param secret The plain text secret to hash
* @return The hashed secret with base64 encoding (without padding)
*/
private fun hashPkceSecret(secret: String): String {
val sha256Digest = MessageDigest.getInstance("SHA-256")
sha256Digest.update(secret.toByteArray())
val hexString = sha256Digest.digest().joinToString(separator = "") { eachByte -> "%02x".format(eachByte) }
return Base64.getEncoder().withoutPadding().encodeToString(hexString.toByteArray())
}

/**
* Underlying function to build the Hosted Authentication URL
* @param config The configuration for building the URL
Expand Down
Loading