-
Notifications
You must be signed in to change notification settings - Fork 47
Add passkey registration support for WebView, Fixes AB#3385532 #2769
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
Merged
Merged
Changes from 29 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
2f6fc36
Add passkey support with CredentialManager and WebView integration
p3dr0rv a43bfef
debug
p3dr0rv a02b892
Log success response in PasskeyWebListener and update JavaScript mess…
p3dr0rv 963d700
Merge branch 'dev' into pedroro/passkey-reg-prototype
p3dr0rv 7e2e2f3
Merge branch 'dev' into pedroro/passkey-reg-prototype
p3dr0rv be90629
Enhance WebView message handling and update Gradle configurations
p3dr0rv ef52c35
Refactor Passkey protocol handling and enhance WebView integration
p3dr0rv 150aea2
Add passkey registration flight control and update WebView header han…
p3dr0rv 89e0f50
Refactor PasskeyWebListener and WebViewAuthorizationFragment for impr…
p3dr0rv a6ec873
Refactor CredentialManagerHandler to improve passkey creation and ret…
p3dr0rv 83b9c49
Refactor imports in CredentialManagerHandler for improved clarity and…
p3dr0rv 578242c
Implement Passkey functionality with WebAuthn support
p3dr0rv 5c8bdbb
Refactor PasskeyReplyChannel and related components for improved erro…
p3dr0rv 036fd39
Refactor build.gradle to set project archivesBaseName correctly; upda…
p3dr0rv 8304ba5
Add WebView support for Passkey functionality; update dependencies an…
p3dr0rv 8b31fd0
Update minified JavaScript code in PasskeyWebListener; add detailed i…
p3dr0rv 6f5e568
Merge branch 'dev' into pedroro/passkey-reg-prototype
p3dr0rv 1fc5328
Refactor request header handling in WebViewAuthorizationFragment; ext…
p3dr0rv 89f6544
Update dependencies in build.gradle; replace hardcoded webkit version…
p3dr0rv f172ccb
Remove passkey feature flight toggle; set passkey registration to dis…
p3dr0rv d87a5cc
Enhance Passkey protocol header handling in WebViewAuthorizationFragm…
p3dr0rv 0c0cbfc
Merge branch 'dev' into pedroro/passkey-reg-prototype
p3dr0rv f206ee5
Refactor createPasskey and getPasskey methods in CredentialManagerHan…
p3dr0rv 2fb63ad
Merge branch 'pedroro/passkey-reg-prototype' of https://github.com/Az…
p3dr0rv 7117010
Add passkey registration support for WebView in changelog
p3dr0rv 8e13fde
Update default dependencies size to 16 MB in build.gradle
p3dr0rv 6c20ed8
Add support for dynamic passkey protocol version validation
p3dr0rv 8bbf3d4
Refactor code for improved readability and formatting in PasskeyReply…
p3dr0rv d611ac0
Enhance passkey functionality by adding span context retrieval, impro…
p3dr0rv 49c9101
Refactor WebViewAuthorizationFragment for improved code clarity and m…
p3dr0rv 2b1088d
Update common/src/main/java/com/microsoft/identity/common/internal/pr…
p3dr0rv 7c0d8ec
Update common/src/main/java/com/microsoft/identity/common/internal/fi…
p3dr0rv be46370
Update common/src/main/java/com/microsoft/identity/common/internal/pr…
p3dr0rv 999011b
update WebViewAuthorizationFragment logging methods for improved privacy
p3dr0rv 12a2d84
Add WebAuthn JavaScript bridge and enhance PasskeyWebListener for deb…
p3dr0rv d4c5cf3
Update common/src/main/assets/js-bridge.js
p3dr0rv 2bf51b7
Update WebAuthn interface name and modify user verification promise r…
p3dr0rv eda9cca
Merge branch 'dev' into pedroro/passkey-reg-prototype
p3dr0rv 78a51db
Enhance JsScriptRecord to validate sovereign cloud URLs and require '…
p3dr0rv 475e918
Add JsScriptRecord validation for sovereign cloud URLs and implement …
p3dr0rv 43408f5
Merge branch 'dev' into pedroro/passkey-reg-prototype
p3dr0rv 914cfee
Clarify comment in JsScriptRecord.isAllowedForUrl to specify checking…
p3dr0rv 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
This file contains hidden or 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 hidden or 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 hidden or 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
81 changes: 81 additions & 0 deletions
81
.../java/com/microsoft/identity/common/internal/providers/oauth2/CredentialManagerHandler.kt
This file contains hidden or 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,81 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // All rights reserved. | ||
| // | ||
| // This code is licensed under the MIT License. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files(the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions : | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
| package com.microsoft.identity.common.internal.providers.oauth2 | ||
|
|
||
| import android.app.Activity | ||
| import android.os.Build | ||
| import androidx.credentials.CreatePublicKeyCredentialRequest | ||
| import androidx.credentials.CreatePublicKeyCredentialResponse | ||
| import androidx.credentials.CredentialManager | ||
| import androidx.credentials.GetCredentialRequest | ||
| import androidx.credentials.GetCredentialResponse | ||
| import androidx.credentials.GetPublicKeyCredentialOption | ||
| import com.microsoft.identity.common.logging.Logger | ||
|
|
||
| class CredentialManagerHandler(private val activity: Activity) { | ||
p3dr0rv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| companion object { | ||
| const val TAG = "CredentialManagerHandler" | ||
| } | ||
|
|
||
| private val mCredMan = CredentialManager.create(activity.applicationContext) | ||
|
|
||
| /** | ||
| * Encapsulates the create passkey API for credential manager in a less error-prone manner. | ||
| * | ||
| * @param request a create public key credential request JSON required by [CreatePublicKeyCredentialRequest]. | ||
| * @return [CreatePublicKeyCredentialResponse] containing the result of the credential creation. | ||
| */ | ||
| suspend fun createPasskey(request: String): CreatePublicKeyCredentialResponse { | ||
| val methodTag = "$TAG:createPasskey" | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
| val createRequest = CreatePublicKeyCredentialRequest(request) | ||
| return (mCredMan.createCredential( | ||
| activity, | ||
| createRequest | ||
| ) as CreatePublicKeyCredentialResponse).also { | ||
| Logger.info(methodTag, "Passkey created successfully.") | ||
| } | ||
| } else { | ||
| Logger.warn( | ||
| methodTag, | ||
| "Passkey creation is not supported on Android versions below 9 (Pie). Current version: ${Build.VERSION.SDK_INT}" | ||
| ) | ||
| throw UnsupportedOperationException("Passkey creation requires Android 9 or higher.") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Encapsulates the get passkey API for credential manager in a less error-prone manner. | ||
| * | ||
| * @param request a get public key credential request JSON required by [GetCredentialRequest]. | ||
| * @return [GetCredentialResponse] containing the result of the credential retrieval. | ||
| */ | ||
| suspend fun getPasskey(request: String): GetCredentialResponse { | ||
| val methodTag = "$TAG:getPasskey" | ||
| val getRequest = GetCredentialRequest(listOf(GetPublicKeyCredentialOption(request))) | ||
| return mCredMan.getCredential(activity, getRequest).also { | ||
| Logger.info(methodTag, "Passkey retrieved successfully.") | ||
| } | ||
| } | ||
| } | ||
229 changes: 229 additions & 0 deletions
229
.../main/java/com/microsoft/identity/common/internal/providers/oauth2/PasskeyReplyChannel.kt
This file contains hidden or 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,229 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // All rights reserved. | ||
| // | ||
| // This code is licensed under the MIT License. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files(the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions : | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
| package com.microsoft.identity.common.internal.providers.oauth2 | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import androidx.credentials.exceptions.CreateCredentialCancellationException | ||
| import androidx.credentials.exceptions.CreateCredentialInterruptedException | ||
| import androidx.credentials.exceptions.CreateCredentialProviderConfigurationException | ||
| import androidx.credentials.exceptions.CreateCredentialUnknownException | ||
| import androidx.credentials.exceptions.GetCredentialCancellationException | ||
| import androidx.credentials.exceptions.GetCredentialInterruptedException | ||
| import androidx.credentials.exceptions.GetCredentialProviderConfigurationException | ||
| import androidx.credentials.exceptions.GetCredentialUnknownException | ||
| import androidx.credentials.exceptions.NoCredentialException | ||
| import androidx.webkit.JavaScriptReplyProxy | ||
| import com.microsoft.identity.common.logging.Logger | ||
| import org.json.JSONObject | ||
| import kotlin.jvm.Throws | ||
|
|
||
|
|
||
| /** | ||
| * Communication channel for sending WebAuthn responses back to JavaScript via [JavaScriptReplyProxy]. | ||
| * | ||
| * Formats messages as JSON containing status, data, and request type for WebAuthn credential operations. | ||
| * | ||
| * @property replyProxy Proxy for sending messages to JavaScript. | ||
| * @property requestType Type of WebAuthn request (e.g., "create", "get"). Defaults to "unknown". | ||
| */ | ||
| class PasskeyReplyChannel( | ||
| private val replyProxy: JavaScriptReplyProxy, | ||
| private val requestType: String = "unknown" | ||
| ) { | ||
| companion object { | ||
| const val TAG = "PasskeyReplyChannel" | ||
|
|
||
| // JSON message structure keys | ||
| const val STATUS_KEY = "status" | ||
| const val DATA_KEY = "data" | ||
| const val TYPE_KEY = "type" | ||
|
|
||
| // DOMException error details keys | ||
| const val DOM_EXCEPTION_MESSAGE_KEY = "domExceptionMessage" | ||
| const val DOM_EXCEPTION_NAME_KEY = "domExceptionName" | ||
|
|
||
| // Message status values | ||
| const val SUCCESS_STATUS = "success" | ||
| const val ERROR_STATUS = "error" | ||
|
|
||
| // DOMException names per W3C WebAuthn specification | ||
| const val DOM_EXCEPTION_NOT_ALLOWED_ERROR = "NotAllowedError" | ||
| const val DOM_EXCEPTION_ABORT_ERROR = "AbortError" | ||
| const val DOM_EXCEPTION_NOT_SUPPORTED_ERROR = "NotSupportedError" | ||
| const val DOM_EXCEPTION_UNKNOWN_ERROR = "UnknownError" | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Sealed class representing messages sent to JavaScript. | ||
| */ | ||
| sealed class ReplyMessage { | ||
| abstract val type: String | ||
| abstract val status: String | ||
| abstract val data: JSONObject | ||
|
|
||
| /** | ||
| * Success message containing credential data. | ||
| * | ||
| * @property json JSON string with credential response data. | ||
| * @property type Request type that succeeded. | ||
| */ | ||
| class Success(val json: String, override val type: String) : ReplyMessage() { | ||
| override val status = SUCCESS_STATUS | ||
| override val data: JSONObject = | ||
| runCatching { JSONObject(json) }.getOrElse { JSONObject() } | ||
fadidurah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Error message with DOMException details. | ||
| * | ||
| * @property domExceptionMessage Error description. | ||
| * @property domExceptionName DOMException name per W3C spec. | ||
| * @property type Request type that failed. | ||
| */ | ||
| class Error( | ||
| private val domExceptionMessage: String, | ||
| private val domExceptionName: String = DOM_EXCEPTION_NOT_ALLOWED_ERROR, | ||
| override val type: String | ||
| ) : ReplyMessage() { | ||
| override val status = ERROR_STATUS | ||
| override val data: JSONObject | ||
| get() { | ||
| return JSONObject().apply { | ||
| put(DOM_EXCEPTION_MESSAGE_KEY, domExceptionMessage) | ||
| put(DOM_EXCEPTION_NAME_KEY, domExceptionName) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Serializes the message to JSON string. */ | ||
| override fun toString(): String { | ||
| return JSONObject().apply { | ||
| put(STATUS_KEY, status) | ||
| put(DATA_KEY, data) | ||
| put(TYPE_KEY, type) | ||
| }.toString() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Posts a success message with credential data. | ||
| * | ||
| * @param json JSON string containing the credential response. | ||
| */ | ||
| fun postSuccess(json: String) { | ||
| val methodTag = "$TAG:postSuccess" | ||
| send(ReplyMessage.Success(json, requestType)) | ||
| Logger.info(methodTag, "RequestType: $requestType, was successful.") | ||
| } | ||
|
|
||
| /** | ||
| * Posts an error message with a custom error description. | ||
| * | ||
| * @param errorMessage Error description to send. | ||
| */ | ||
| fun postError(errorMessage: String) { | ||
| postErrorInternal( | ||
| ReplyMessage.Error(domExceptionMessage = errorMessage, type = requestType) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Posts an error message based on a thrown exception. | ||
| * | ||
| * Maps credential exceptions to appropriate DOMException types. | ||
| * | ||
| * @param throwable Exception to convert and send. | ||
| */ | ||
| fun postError(throwable: Throwable) { | ||
| postErrorInternal(throwableToErrorMessage(throwable)) | ||
| } | ||
|
|
||
| /** | ||
| * Internal method to send error messages and log them. | ||
| */ | ||
| private fun postErrorInternal(errorMessage: ReplyMessage.Error) { | ||
| val methodTag = "$TAG:postError" | ||
| send(errorMessage) | ||
| Logger.error(methodTag, "RequestType: $requestType, failed with error: $errorMessage", null) | ||
| } | ||
|
|
||
| /** | ||
| * Maps credential exceptions to DOMException error messages. | ||
| * | ||
| * Conversion table: | ||
| * - Cancellation/No credential → NotAllowedError | ||
| * - Interruption → AbortError | ||
| * - Configuration → NotSupportedError | ||
| * - Unknown → UnknownError | ||
| * | ||
| * @param throwable Exception to map. | ||
| * @return Error message with appropriate DOMException details. | ||
| */ | ||
| private fun throwableToErrorMessage(throwable: Throwable): ReplyMessage.Error { | ||
| val errorMessage = throwable.message ?: "Unknown error (empty message)" | ||
|
|
||
| val exceptionName = when (throwable) { | ||
| // Cancellation exceptions - User cancelled | ||
| is CreateCredentialCancellationException, | ||
| is GetCredentialCancellationException, | ||
| is NoCredentialException -> DOM_EXCEPTION_NOT_ALLOWED_ERROR | ||
|
|
||
| // Interruption exceptions - Operation aborted | ||
| is CreateCredentialInterruptedException, | ||
| is GetCredentialInterruptedException -> DOM_EXCEPTION_ABORT_ERROR | ||
|
|
||
| // Provider configuration exceptions - Not supported | ||
| is CreateCredentialProviderConfigurationException, | ||
| is GetCredentialProviderConfigurationException -> DOM_EXCEPTION_NOT_SUPPORTED_ERROR | ||
|
|
||
| // Unknown exceptions | ||
| is CreateCredentialUnknownException, | ||
| is GetCredentialUnknownException -> DOM_EXCEPTION_UNKNOWN_ERROR | ||
|
|
||
| // Default case for other exceptions | ||
| else -> DOM_EXCEPTION_NOT_ALLOWED_ERROR | ||
| } | ||
|
|
||
| return ReplyMessage.Error( | ||
| domExceptionMessage = errorMessage, | ||
| domExceptionName = exceptionName, | ||
| type = requestType | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Sends a message to JavaScript via the reply proxy. | ||
| */ | ||
| @Throws (Throwable::class) | ||
p3dr0rv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @SuppressLint("RequiresFeature", "Only called when feature is available") | ||
| private fun send(message: ReplyMessage) { | ||
| val methodTag = "$TAG:send" | ||
| try { | ||
| replyProxy.postMessage(message.toString()) | ||
| } catch (t: Throwable) { | ||
| Logger.error(methodTag, "Reply message failed", t) | ||
p3dr0rv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw t | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.