-
Notifications
You must be signed in to change notification settings - Fork 50
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 all 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| // filepath: c:\repos\android-complete\common\common\src\main\java\com\microsoft\identity\common\internal\providers\oauth2\js-bridge.js | ||
| // | ||
| // WebAuthn JavaScript Bridge for Android Credential Manager | ||
| // | ||
| // This JavaScript code is injected into WebViews to intercept WebAuthn API calls | ||
| // (navigator.credentials.create/get) and bridge them to the Android Credential Manager. | ||
| // It handles the communication protocol between the web page and the native Android code. | ||
| // | ||
| // ⚠️ IMPORTANT: MINIFICATION REQUIRED ⚠️ | ||
| // Any modifications to this file MUST be minified and the minified output MUST be | ||
| // updated in PasskeyWebListener.kt in the constant WEB_AUTHN_INTERFACE_JS_MINIFIED. | ||
| // | ||
| // Steps to update: | ||
| // 1. Make changes to this file (js-bridge.js) | ||
| // 2. Minify the code using a JavaScript minifier (e.g., https://www.toptal.com/developers/javascript-minifier, https://minify-js.com/) | ||
| // 3. Update the WEB_AUTHN_INTERFACE_JS_MINIFIED constant in PasskeyWebListener.kt | ||
| // 4. Test thoroughly to ensure the minified version works correctly | ||
| // | ||
| // Failure to update the minified version will result in the changes NOT being | ||
| // reflected in the actual WebView injection. | ||
| // | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| var __webauthn_interface__; | ||
| var __webauthn_hooks__; | ||
| (function (__webauthn_hooks__) { | ||
|
|
||
| //Adding event listener to the interface for replies by default | ||
| __webauthn_interface__.addEventListener('message', onReply); | ||
| // pendingResolveGet/Create is the thunk to resolve an outstanding get request. | ||
| var pendingResolveGet = null; | ||
| var pendingResolveCreate = null; | ||
| // pendingRejectGet/Create is the thunk to fail an outstanding request. | ||
| var pendingRejectGet = null; | ||
| var pendingRejectCreate = null; | ||
| // create overrides 'navigator.credentials.create' which proxies webauthn requests | ||
| // to the create embedder | ||
| function create(request) { | ||
| if (!("publicKey" in request)) { | ||
| return __webauthn_hooks__.originalCreateFunction(request); | ||
| } | ||
| var ret = new Promise(function (resolve, reject) { | ||
| pendingResolveCreate = resolve; | ||
| pendingRejectCreate = reject; | ||
| }); | ||
| var temppk = request.publicKey; | ||
| if (temppk.hasOwnProperty('challenge')) { | ||
| var str = CM_base64url_encode(temppk.challenge); | ||
| temppk.challenge = str; | ||
| } | ||
| if (temppk.hasOwnProperty('user') && temppk.user.hasOwnProperty('id')) { | ||
| var encodedString = CM_base64url_encode(temppk.user.id); | ||
| temppk.user.id = encodedString; | ||
| } | ||
| // Encode all excludeCredentials ids if provided | ||
| if (temppk.hasOwnProperty('excludeCredentials') && Array.isArray(temppk.excludeCredentials) && temppk.excludeCredentials.length > 0) { | ||
| for (var i = 0; i < temppk.excludeCredentials.length; i++) { | ||
| var cred = temppk.excludeCredentials[i]; | ||
| if (cred && cred.hasOwnProperty('id')) { | ||
| cred.id = CM_base64url_encode(cred.id); | ||
| } | ||
| } | ||
| } | ||
| var jsonObj = {"type":"create", "request":temppk}; | ||
|
|
||
| var json = JSON.stringify(jsonObj); | ||
| __webauthn_interface__.postMessage(json); | ||
| return ret; | ||
| } | ||
| __webauthn_hooks__.create = create; | ||
| // get overrides `navigator.credentials.get` and proxies any WebAuthn | ||
| // requests to the get embedder. | ||
| function get(request) { | ||
| if (!("publicKey" in request)) { | ||
| return __webauthn_hooks__.originalGetFunction(request); | ||
| } | ||
| var ret = new Promise(function (resolve, reject) { | ||
| pendingResolveGet = resolve; | ||
| pendingRejectGet = reject; | ||
| }); | ||
| var temppk = request.publicKey; | ||
| if (temppk.hasOwnProperty('challenge')) { | ||
| var str = CM_base64url_encode(temppk.challenge); | ||
| temppk.challenge = str; | ||
| } | ||
| var jsonObj = {"type":"get", "request":temppk} | ||
|
|
||
| var json = JSON.stringify(jsonObj); | ||
| __webauthn_interface__.postMessage(json); | ||
| return ret; | ||
| } | ||
| __webauthn_hooks__.get = get; | ||
|
|
||
| // The embedder gives replies back here, caught by the event listener. | ||
| function onReply(msg) { | ||
| console.log(msg.data); | ||
| var reply = JSON.parse(msg.data); | ||
| if(reply.type === "get") { | ||
| onReplyGet(reply); | ||
| } else if (reply.type === "create") { | ||
| onReplyCreate(reply); | ||
| } else { | ||
| console.log("Incorrect response format for reply: " + reply.type); | ||
| } | ||
| } | ||
|
|
||
| // Resolves what is expected for get, called when the embedder is ready | ||
| function onReplyGet(reply) { | ||
| if (pendingResolveGet === null || pendingRejectGet === null) { | ||
| console.log("Reply failure: Resolve: " + pendingResolveCreate + | ||
| " and reject: " + pendingRejectCreate); | ||
| return; | ||
| } | ||
| if (reply.status != 'success') { | ||
| var reject = pendingRejectGet; | ||
| pendingResolveGet = null; | ||
| pendingRejectGet = null; | ||
| reject(new DOMException(reply.data.domExceptionMessage, reply.data.domExceptionName)); | ||
| return; | ||
| } | ||
| var cred = credentialManagerDecode(reply.data); | ||
| var resolve = pendingResolveGet; | ||
| pendingResolveGet = null; | ||
| pendingRejectGet = null; | ||
| resolve(cred); | ||
| } | ||
| __webauthn_hooks__.onReplyGet = onReplyGet; | ||
| // This a specific decoder for expected types contained in PublicKeyCredential json | ||
| function CM_base64url_decode(value) { | ||
| var m = value.length % 4; | ||
| return Uint8Array.from(atob(value.replace(/-/g, '+') | ||
| .replace(/_/g, '/') | ||
| .padEnd(value.length + (m === 0 ? 0 : 4 - m), '=')), function (c) | ||
| { return c.charCodeAt(0); }).buffer; | ||
| } | ||
| __webauthn_hooks__.CM_base64url_decode = CM_base64url_decode; | ||
| function CM_base64url_encode(buffer) { | ||
| return btoa(Array.from(new Uint8Array(buffer), function (b) | ||
| { return String.fromCharCode(b); }).join('')) | ||
| .replace(/\+/g, '-') | ||
| .replace(/\//g, '_') | ||
| .replace(/=+$/, ''); | ||
| } | ||
| __webauthn_hooks__.CM_base64url_encode = CM_base64url_encode; | ||
| // Resolves what is expected for create, called when the embedder is ready | ||
| function onReplyCreate(reply) { | ||
| if (pendingResolveCreate === null || pendingRejectCreate === null) { | ||
| console.log("Reply failure: Resolve: " + pendingResolveCreate + | ||
| " and reject: " + pendingRejectCreate); | ||
| return; | ||
| } | ||
|
|
||
| if (reply.status != 'success') { | ||
| var reject = pendingRejectCreate; | ||
| pendingResolveCreate = null; | ||
| pendingRejectCreate = null; | ||
| reject(new DOMException(reply.data.domExceptionMessage, reply.data.domExceptionName)); | ||
| return; | ||
| } | ||
| var cred = credentialManagerDecode(reply.data); | ||
| var resolve = pendingResolveCreate; | ||
| pendingResolveCreate = null; | ||
| pendingRejectCreate = null; | ||
| resolve(cred); | ||
| } | ||
| __webauthn_hooks__.onReplyCreate = onReplyCreate; | ||
| /** | ||
| * This decodes the output from the credential manager flow to parse back into URL format. Both | ||
| * get and create flows ultimately return a PublicKeyCredential object. | ||
| * @param json_result | ||
| */ | ||
| function credentialManagerDecode(decoded_reply) { | ||
| decoded_reply.rawId = CM_base64url_decode(decoded_reply.rawId); | ||
| decoded_reply.response.clientDataJSON = CM_base64url_decode(decoded_reply.response.clientDataJSON); | ||
| if (decoded_reply.response.hasOwnProperty('attestationObject')) { | ||
| decoded_reply.response.attestationObject = CM_base64url_decode(decoded_reply.response.attestationObject); | ||
| } | ||
| if (decoded_reply.response.hasOwnProperty('authenticatorData')) { | ||
| decoded_reply.response.authenticatorData = CM_base64url_decode(decoded_reply.response.authenticatorData); | ||
| } | ||
| if (decoded_reply.response.hasOwnProperty('signature')) { | ||
| decoded_reply.response.signature = CM_base64url_decode(decoded_reply.response.signature); | ||
| } | ||
| if (decoded_reply.response.hasOwnProperty('userHandle')) { | ||
| decoded_reply.response.userHandle = CM_base64url_decode(decoded_reply.response.userHandle); | ||
| } | ||
| decoded_reply.getClientExtensionResults = function getClientExtensionResults() { return {}; }; | ||
| decoded_reply.response.getTransports = function getTransports() { | ||
| if (decoded_reply.response.hasOwnProperty('transports')) { return decoded_reply.response.transports; } | ||
| return []; | ||
| }; | ||
| return decoded_reply; | ||
| } | ||
| })(__webauthn_hooks__ || (__webauthn_hooks__ = {})); | ||
| __webauthn_hooks__.originalGetFunction = navigator.credentials.get; | ||
| __webauthn_hooks__.originalCreateFunction = navigator.credentials.create; | ||
| navigator.credentials.get = __webauthn_hooks__.get; | ||
| navigator.credentials.create = __webauthn_hooks__.create; | ||
| // Some sites test that `typeof window.PublicKeyCredential` is | ||
| // `function`. | ||
| window.PublicKeyCredential = (function () { }); | ||
| window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable = | ||
| function () { | ||
| return Promise.resolve(true); | ||
| }; |
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
86 changes: 86 additions & 0 deletions
86
.../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,86 @@ | ||
| // 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 | ||
|
|
||
| /** | ||
| * Handler class to encapsulate Credential Manager APIs for passkey operations. | ||
| * | ||
| * @property activity The activity context used for credential operations. | ||
| */ | ||
| class CredentialManagerHandler(private val activity: Activity) { | ||
|
|
||
| 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.") | ||
| } | ||
| } | ||
| } | ||
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.