-
Notifications
You must be signed in to change notification settings - Fork 23
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
SDKS-1710 WebAuthn Device Management #415
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b44d3e8
SDKS-1710 WebAuthn Device Management
rodrigoareis 44466f9
Addressing Andy's comments
rodrigoareis e49e3e5
Updating existing deleteCredentials method
rodrigoareis 8f45568
Fixing issue on encoding credentialId parameter
rodrigoareis 2aebe1b
Addressing comments from Stoyan
rodrigoareis 0e98223
Deprecating previous deleteCredentials method
rodrigoareis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
forgerock-auth/src/main/java/org/forgerock/android/auth/RemoteWebAuthnRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright (c) 2024 ForgeRock. All rights reserved. | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
package org.forgerock.android.auth | ||
|
||
import android.net.Uri | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlinx.coroutines.withContext | ||
import okhttp3.Call | ||
import okhttp3.Callback | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import org.forgerock.android.auth.exception.ApiException | ||
import org.forgerock.android.auth.webauthn.PublicKeyCredentialSource | ||
import org.forgerock.android.auth.webauthn.WebAuthnRepository | ||
import org.json.JSONObject | ||
import java.io.IOException | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
|
||
private val TAG = RemoteWebAuthnRepository::class.java.simpleName | ||
|
||
internal class RemoteWebAuthnRepository(val serverConfig: ServerConfig = Config.getInstance().serverConfig) : | ||
WebAuthnRepository { | ||
|
||
override suspend fun delete(publicKeyCredentialSource: PublicKeyCredentialSource) { | ||
val credentialId = String(publicKeyCredentialSource.id) | ||
val userId = String(publicKeyCredentialSource.userHandle) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be base64? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
|
||
val findResponse = find(userId, credentialId) | ||
val resourceId = findResponse.getString("uuid") | ||
spetrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (resourceId.isNotEmpty()) { | ||
val deleteResponse = invokeDelete(userId, resourceId) | ||
if (deleteResponse.code != HttpURLConnection.HTTP_OK) { | ||
throw ApiException(deleteResponse.code, deleteResponse.message, | ||
"Failed to delete resources") | ||
} | ||
} | ||
} | ||
|
||
private suspend fun find(userId: String, credentialId: String): JSONObject { | ||
val response = invokeGet(userId, credentialId) | ||
if (response.code != HttpURLConnection.HTTP_OK) { | ||
throw ApiException(response.code, response.message, "Failed to find resource") | ||
} else { | ||
return JSONObject(response.body.toString()) | ||
spetrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
private suspend fun invokeGet(userId: String, credentialId: String): Response = | ||
withContext(Dispatchers.IO) { | ||
suspendCancellableCoroutine { continuation -> | ||
val client = | ||
OkHttpClientProvider.getInstance().lookup(serverConfig) | ||
val url = getUrl(userId, credentialId) | ||
val request = Request.Builder() | ||
.header(ServerConfig.ACCEPT_API_VERSION, "resource=1.0") | ||
.url(url) | ||
.get().build() | ||
val call = client.newCall(request) | ||
call.enqueue(object : Callback { | ||
override fun onFailure(call: Call, e: IOException) { | ||
if (continuation.isCancelled) return | ||
Logger.warn(TAG, e, "Delete webauthn device failed." ) | ||
continuation.resumeWithException(e) | ||
} | ||
|
||
override fun onResponse(call: Call, response: Response) { | ||
continuation.resume(response) | ||
} | ||
}) | ||
continuation.invokeOnCancellation { | ||
try { | ||
call.cancel() | ||
} catch (e: Exception) { | ||
Logger.warn(TAG, e, "Cancel API call failed") | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
private suspend fun invokeDelete(userId: String, resourceId:String): Response = | ||
withContext(Dispatchers.IO) { | ||
suspendCancellableCoroutine { continuation -> | ||
val client = | ||
OkHttpClientProvider.getInstance().lookup(serverConfig) | ||
val url = getUrl(userId, resourceId) | ||
val request = Request.Builder() | ||
.header(ServerConfig.ACCEPT_API_VERSION, "resource=1.0") | ||
.url(url) | ||
.delete().build() | ||
val call = client.newCall(request) | ||
call.enqueue(object : Callback { | ||
override fun onFailure(call: Call, e: IOException) { | ||
if (continuation.isCancelled) return | ||
Logger.warn(TAG, e, "Delete webauthn device failed." ) | ||
continuation.resumeWithException(e) | ||
} | ||
|
||
override fun onResponse(call: Call, response: Response) { | ||
continuation.resume(response) | ||
} | ||
}) | ||
continuation.invokeOnCancellation { | ||
try { | ||
call.cancel() | ||
} catch (e: Exception) { | ||
Logger.warn(TAG, e, "Cancel API call failed") | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
private fun getUrl(userId: String, resourceId:String): URL { | ||
val builder = Uri.parse(serverConfig.url).buildUpon() | ||
builder.appendPath("json") | ||
.appendPath("realms") | ||
.appendPath(serverConfig.realm) | ||
.appendPath("users") | ||
.appendPath(userId) | ||
.appendPath("devices") | ||
.appendPath("2fa") | ||
.appendPath("webauthn") | ||
.appendPath(resourceId) | ||
return URL(builder.build().toString()) | ||
} | ||
|
||
} |
spetrov marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be base64?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it's not required, the
toJson
andfromJson
methods from thePublicKeyCredentialSource
class handles the Base64 decode and decode.