Skip to content

Commit

Permalink
convert hashed string to hex before b64
Browse files Browse the repository at this point in the history
  • Loading branch information
mrashed-dev committed Jan 4, 2024
1 parent de6b593 commit 614a995
Showing 1 changed file with 13 additions and 2 deletions.
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

0 comments on commit 614a995

Please sign in to comment.