Skip to content

Commit

Permalink
Made the realm parameter optional in passkey
Browse files Browse the repository at this point in the history
  • Loading branch information
pmathew92 committed Nov 14, 2024
1 parent d58083d commit 85f9718
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,18 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
*
* @param authSession the auth session received from the server as part of the public key challenge request.
* @param authResponse the public key credential authentication response
* @param realm the default connection to use
* @param realm the connection to use. If excluded, the application will use the default connection configured in the tenant
* @return a request to configure and start that will yield [Credentials]
*/
public fun signinWithPasskey(
authSession: String,
authResponse: PublicKeyCredentials,
realm: String
realm: String? = null
): AuthenticationRequest {
val params = ParameterBuilder.newBuilder().apply {
setGrantType(ParameterBuilder.GRANT_TYPE_PASSKEY)
set(AUTH_SESSION_KEY, authSession)
setRealm(realm)
realm?.let { setRealm(it) }
}.asDictionary()

return loginWithToken(params)
Expand Down Expand Up @@ -217,12 +217,12 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
* ```
*
* @param userData user information of the client
* @param realm default connection to use
* @param realm the connection to use. If excluded, the application will use the default connection configured in the tenant
* @return a request to configure and start that will yield [PasskeyRegistrationChallenge]
*/
public fun signupWithPasskey(
userData: UserData,
realm: String
realm: String? = null
): Request<PasskeyRegistrationChallenge, AuthenticationException> {
val user = Gson().toJsonTree(userData)
val url = auth0.getDomainUrl().toHttpUrl().newBuilder()
Expand All @@ -232,7 +232,7 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe

val params = ParameterBuilder.newBuilder().apply {
setClientId(clientId)
setRealm(realm)
realm?.let { setRealm(it) }
}.asDictionary()

val passkeyRegistrationChallengeAdapter: JsonAdapter<PasskeyRegistrationChallenge> =
Expand Down Expand Up @@ -261,11 +261,11 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
* })
* ```
*
* @param realm A default connection name
* @param realm the connection to use. If excluded, the application will use the default connection configured in the tenant
* @return a request to configure and start that will yield [PasskeyChallenge]
*/
public fun passkeyChallenge(
realm: String
realm: String? = null
): Request<PasskeyChallenge, AuthenticationException> {
val url = auth0.getDomainUrl().toHttpUrl().newBuilder()
.addPathSegment(PASSKEY_PATH)
Expand All @@ -274,7 +274,7 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe

val parameters = ParameterBuilder.newBuilder().apply {
setClientId(clientId)
setRealm(realm)
realm?.let { setRealm(it) }
}.asDictionary()

val passkeyChallengeAdapter: JsonAdapter<PasskeyChallenge> = GsonAdapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ internal class PasskeyManager(
callback: Callback<Credentials, AuthenticationException>,
executor: Executor = Executors.newSingleThreadExecutor()
) {

if (realm == null) {
callback.onFailure(AuthenticationException("Realm is required for passkey authentication"))
return
}
authenticationAPIClient.signupWithPasskey(userData, realm)
.addParameters(parameters)
.start(object : Callback<PasskeyRegistrationChallenge, AuthenticationException> {
Expand Down Expand Up @@ -120,10 +115,6 @@ internal class PasskeyManager(
callback: Callback<Credentials, AuthenticationException>,
executor: Executor = Executors.newSingleThreadExecutor()
) {
if (realm == null) {
callback.onFailure(AuthenticationException("Realm is required for passkey authentication"))
return
}
authenticationAPIClient.passkeyChallenge(realm)
.start(object : Callback<PasskeyChallenge, AuthenticationException> {
override fun onSuccess(result: PasskeyChallenge) {
Expand Down
19 changes: 9 additions & 10 deletions sample/src/main/java/com/auth0/sample/DatabaseLoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class DatabaseLoginFragment : Fragment() {
}

binding.btSignupPasskey.setOnClickListener {
passkeySignup()
passkeySignup(binding.textEmail.text.toString())
}

binding.btSignInPasskey.setOnClickListener {
Expand All @@ -129,7 +129,7 @@ class DatabaseLoginFragment : Fragment() {

binding.btSignupPasskeyAsync.setOnClickListener {
launchAsync {
passkeySignupAsync()
passkeySignupAsync(binding.textEmail.text.toString())
}
}

Expand Down Expand Up @@ -486,11 +486,11 @@ class DatabaseLoginFragment : Fragment() {
}
}

private fun passkeySignup() {
private fun passkeySignup(email: String) {
authenticationApiClient.signupWithPasskey(
UserData(
email = "jndoe@email.com"
), "Username-Password-Authentication"
email = email
)
).start(object : Callback<PasskeyRegistrationChallenge, AuthenticationException> {
override fun onSuccess(result: PasskeyRegistrationChallenge) {
val passKeyRegistrationChallenge = result
Expand Down Expand Up @@ -558,7 +558,7 @@ class DatabaseLoginFragment : Fragment() {
}

private fun passkeySignin() {
authenticationApiClient.passkeyChallenge("Username-Password-Authentication")
authenticationApiClient.passkeyChallenge()
.start(object : Callback<PasskeyChallenge, AuthenticationException> {
override fun onSuccess(result: PasskeyChallenge) {
val passkeyChallengeResponse = result
Expand Down Expand Up @@ -631,12 +631,11 @@ class DatabaseLoginFragment : Fragment() {
})
}

private suspend fun passkeySignupAsync() {
private suspend fun passkeySignupAsync(email: String) {

try {
val challenge = authenticationApiClient.signupWithPasskey(
UserData(email = "[email protected]"),
"Username-Password-Authentication"
UserData(email = email)
).await()

val request = CreatePublicKeyCredentialRequest(
Expand Down Expand Up @@ -682,7 +681,7 @@ class DatabaseLoginFragment : Fragment() {
try {

val challenge =
authenticationApiClient.passkeyChallenge("Username-Password-Authentication")
authenticationApiClient.passkeyChallenge()
.await()

val request = GetPublicKeyCredentialOption(Gson().toJson(challenge.authParamsPublicKey))
Expand Down

0 comments on commit 85f9718

Please sign in to comment.