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

Follow RFC7636 to generate code verifier #189

Merged
merged 1 commit into from
Apr 15, 2024
Merged
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
14 changes: 7 additions & 7 deletions sdk/src/main/java/com/oursky/authgear/AuthgearCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -635,20 +635,20 @@ internal class AuthgearCore(
}

private fun generateCodeVerifier(): Verifier {
// https://datatracker.ietf.org/doc/html/rfc7636#section-4.1
// It is RECOMMENDED that the output of
// a suitable random number generator be used to create a 32-octet
// sequence. The octet sequence is then base64url-encoded to produce a
// 43-octet URL safe string to use as the code verifier.
val bytes = ByteArray(32)
SecureRandom().nextBytes(bytes)
val verifier = bytes.joinToString(separator = "") {
it.toString(16).padStart(2, '0')
}
val verifier = base64UrlEncode(bytes)
return Verifier(verifier, computeCodeChallenge(verifier))
}

private fun computeCodeChallenge(verifier: String): String {
val hash = sha256(verifier)
return String(
Base64.encode(hash, Base64.URL_SAFE or Base64.NO_PADDING or Base64.NO_WRAP),
StandardCharsets.UTF_8
)
return base64UrlEncode(hash)
}

private fun sha256(input: String): ByteArray {
Expand Down
Loading