Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.auth0.android.embedded

import com.auth0.android.Auth0
import com.auth0.android.Auth0Exception
import com.auth0.android.embedded.authorize.AdvancingRequest
import com.auth0.android.embedded.authorize.AuthorizeCode
import com.auth0.android.embedded.authorize.EmbeddedAuthState
Expand All @@ -18,6 +19,7 @@ import com.auth0.android.request.internal.GsonAdapter
import com.auth0.android.request.internal.GsonProvider
import com.auth0.android.request.internal.RequestFactory
import com.auth0.android.result.Credentials
import com.auth0.android.util.AlgorithmHelper
import com.google.gson.Gson
import okhttp3.HttpUrl.Companion.toHttpUrl

Expand All @@ -32,7 +34,12 @@ import okhttp3.HttpUrl.Companion.toHttpUrl
* **Note**
* Embedded Authorization is currently in [Beta](https://auth0.com/docs/troubleshoot/product-lifecycle/product-release-stages#beta). Please reach out to Auth0 support to get it enabled for your tenant.
*/
public class EmbeddedAuthClient(private val auth0: Auth0) {
public class EmbeddedAuthClient internal constructor(
private val auth0: Auth0,
private val algorithmHelper: AlgorithmHelper
) {

public constructor(auth0: Auth0) : this(auth0, AlgorithmHelper())

/** Used for both `/e/authorize` and the `/oauth/token` exchange. */
private val factory: RequestFactory<EmbeddedAuthException> =
Expand All @@ -54,6 +61,9 @@ public class EmbeddedAuthClient(private val auth0: Auth0) {
* [EmbeddedAuthError.InsufficientAuthorization.nextActions] on [EmbeddedAuthException.error]
* to learn which step to call next. A terminal error is reported on the same failure channel.
*
* The flow is protected with PKCE (RFC 7636, `S256`): a new code challenge is sent here, and its
* verifier is sent when the authorization code is exchanged for tokens.
*
* @param connection name of the connection to authenticate against.
* @param scope space-separated scopes to request. Must include `openid` for the terminal token
* exchange to return an ID token; defaults to `"openid profile email offline_access"`.
Expand All @@ -68,12 +78,28 @@ public class EmbeddedAuthClient(private val auth0: Auth0) {
capabilities: Set<EmbeddedCapability> = DEFAULT_CAPABILITIES
): Request<Void?, EmbeddedAuthException> {
transactionState = null
val verifier = algorithmHelper.generateCodeVerifier()
val challenge = try {
algorithmHelper.generateCodeChallenge(verifier)
} catch (e: IllegalStateException) {
return FailedRequest(
EmbeddedAuthException(
Auth0Exception.UNKNOWN_ERROR,
"Failed to create the PKCE code challenge.",
error = EmbeddedAuthError.Unknown,
cause = e
)
)
}
transactionState = EmbeddedAuthState(codeVerifier = verifier)
val request = factory.post(authorizeUrl)
.addParameters(buildMap {
put(CLIENT_ID_KEY, clientId)
put(CONNECTION_KEY, connection)
put(SCOPE_KEY, scope)
audience?.let { put(AUDIENCE_KEY, it) }
put(CODE_CHALLENGE_KEY, challenge)
put(CODE_CHALLENGE_METHOD_KEY, CODE_CHALLENGE_METHOD_S256)
})
.addParameter(CAPABILITIES_KEY, capabilities.map { it.value })
return stepping(request)
Expand Down Expand Up @@ -281,7 +307,8 @@ public class EmbeddedAuthClient(private val auth0: Auth0) {
action: EmbeddedCapability,
addPayload: Request<AuthorizeCode, EmbeddedAuthException>.() -> Unit = {}
): Request<Credentials, EmbeddedAuthException> {
val session = transactionState?.authSession ?: return noActiveSession()
val state = transactionState
val session = state?.authSession ?: return noActiveSession()
val request = factory.post(authorizeUrl, authorizeCodeAdapter(gson))
.addParameters(
mapOf(
Expand All @@ -291,7 +318,7 @@ public class EmbeddedAuthClient(private val auth0: Auth0) {
)
)
request.addPayload()
return advancing(request)
return advancing(request, state.codeVerifier)
}

private fun <T> noActiveSession(): Request<T, EmbeddedAuthException> = FailedRequest<T>(
Expand All @@ -306,28 +333,35 @@ public class EmbeddedAuthClient(private val auth0: Auth0) {
request: Request<Void?, EmbeddedAuthException>
): Request<Void?, EmbeddedAuthException> = StepRequest(request, ::updateSessionFromFailure)

/**
* Wraps a final step. The verifier is captured here because the session state is cleared as soon as
* the server issues the authorization code, before the code is exchanged.
*/
private fun advancing(
request: Request<AuthorizeCode, EmbeddedAuthException>
request: Request<AuthorizeCode, EmbeddedAuthException>,
codeVerifier: String
): Request<Credentials, EmbeddedAuthException> = AdvancingRequest(
authorize = request,
exchange = ::exchange,
exchange = { code -> exchange(code, codeVerifier) },
onStepFailure = ::updateSessionFromFailure,
onFlowComplete = { transactionState = null }
)

private fun exchange(authorizationCode: String): Request<Credentials, EmbeddedAuthException> {
private fun exchange(
authorizationCode: String,
codeVerifier: String
): Request<Credentials, EmbeddedAuthException> {
val url = auth0.getDomainUrl().toHttpUrl().newBuilder()
.addPathSegment(OAUTH_PATH)
.addPathSegment(TOKEN_PATH)
.build()
return factory.post(url.toString(), GsonAdapter(Credentials::class.java, gson))
.addParameters(
mapOf(
CLIENT_ID_KEY to clientId,
GRANT_TYPE_KEY to GRANT_TYPE_AUTHORIZATION_CODE,
CODE_KEY to authorizationCode
)
)
.addParameters(buildMap {
put(CLIENT_ID_KEY, clientId)
put(GRANT_TYPE_KEY, GRANT_TYPE_AUTHORIZATION_CODE)
put(CODE_KEY, authorizationCode)
put(CODE_VERIFIER_KEY, codeVerifier)
})
}

private val authorizeUrl: String by lazy {
Expand All @@ -343,7 +377,7 @@ public class EmbeddedAuthClient(private val auth0: Auth0) {

when (error.error) {
is EmbeddedAuthError.InsufficientAuthorization ->
error.authSession?.let { transactionState = EmbeddedAuthState(it) }
error.authSession?.let { session -> transactionState = transactionState?.copy(authSession = session) }

is EmbeddedAuthError.Network -> Unit
else -> transactionState = null
Expand Down Expand Up @@ -373,6 +407,10 @@ public class EmbeddedAuthClient(private val auth0: Auth0) {
private const val TYPE_KEY = "type"
private const val GRANT_TYPE_KEY = "grant_type"
private const val CODE_KEY = "code"
private const val CODE_CHALLENGE_KEY = "code_challenge"
private const val CODE_CHALLENGE_METHOD_KEY = "code_challenge_method"
private const val CODE_CHALLENGE_METHOD_S256 = "S256"
private const val CODE_VERIFIER_KEY = "code_verifier"
private const val GRANT_TYPE_AUTHORIZATION_CODE = "authorization_code"
private const val NO_ACTIVE_SESSION_ERROR = "no_active_session"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.auth0.android.embedded.authorize

/**
* The state of the flow in progress, created by `authorize()` with the PKCE [codeVerifier] that is sent with
* the token exchange. [authSession] is the latest session from the server, and is `null` until the first
* response arrives. Neither is ever logged.
*/
internal data class EmbeddedAuthState(
val authSession: String
)
val codeVerifier: String,
val authSession: String? = null
) {
override fun toString(): String = "EmbeddedAuthState(codeVerifier='xxxxx', authSession='xxxxx')"
}

This file was deleted.

1 change: 1 addition & 0 deletions auth0/src/main/java/com/auth0/android/provider/PKCE.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.auth0.android.callback.Callback;
import com.auth0.android.request.Request;
import com.auth0.android.result.Credentials;
import com.auth0.android.util.AlgorithmHelper;

import java.net.ConnectException;
import java.net.UnknownHostException;
Expand Down
50 changes: 50 additions & 0 deletions auth0/src/main/java/com/auth0/android/util/AlgorithmHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.auth0.android.util

import android.util.Base64
import android.util.Log
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.security.SecureRandom

/**
* Generates the PKCE (RFC 7636) code verifier and its S256 code challenge. Shared by the web-auth and
* embedded flows.
*/
internal open class AlgorithmHelper {

open fun getASCIIBytes(value: String): ByteArray = value.toByteArray(StandardCharsets.US_ASCII)

/**
* @throws IllegalStateException when the SHA-256 algorithm is not available.
*/
open fun getSHA256(input: ByteArray): ByteArray {
try {
val md = MessageDigest.getInstance(SHA_256)
md.update(input, 0, input.size)
return md.digest()
} catch (e: NoSuchAlgorithmException) {
Log.e(TAG, "Failed to get SHA-256 signature", e)
throw IllegalStateException("Failed to get SHA-256 signature", e)
}
}

/** A random, base64url-encoded code verifier made of 32 random bytes. */
open fun generateCodeVerifier(): String {
val code = ByteArray(32)
SecureRandom().nextBytes(code)
return encode(code)
}

/** The S256 code challenge for [codeVerifier]: base64url(SHA-256(ASCII(codeVerifier))). */
open fun generateCodeChallenge(codeVerifier: String): String =
encode(getSHA256(getASCIIBytes(codeVerifier)))

private fun encode(source: ByteArray): String =
Base64.encodeToString(source, Base64.URL_SAFE or Base64.NO_WRAP or Base64.NO_PADDING)

private companion object {
private val TAG = AlgorithmHelper::class.java.simpleName
private const val SHA_256 = "SHA-256"
}
}
Loading
Loading