WebView でユーザーを認証する

このドキュメントでは、WebView を使用する Android アプリに Credential Manager API を統合する方法について説明します。

概要

統合プロセスの説明に入る前に、ネイティブ Android コード、WebView 内にレンダリングされてアプリの認証を管理するウェブ コンポーネント、バックエンドの間の通信フローを理解することが重要です。このフローには、登録(認証情報の作成)と認証(既存の認証情報の取得)が含まれます。

登録(パスキーの作成)

  1. バックエンドは初期の登録 JSON を生成し、WebView 内にレンダリングされたウェブページに送信します。
  2. ウェブページは navigator.credentials.create() を使用して新しい認証情報を登録します。挿入された JavaScript を使用して、後のステップでこのメソッドをオーバーライドし、リクエストを Android アプリに送信します。
  3. Android アプリは、Credential Manager API を使用して認証情報のリクエストを作成し、createCredential に使用します。
  4. Credential Manager API が公開鍵認証情報をアプリと共有します。
  5. アプリは公開鍵認証情報をウェブページに送り返し、挿入された JavaScript がレスポンスを解析できるようにします。
  6. ウェブページが公開鍵をバックエンドに送信します。バックエンドは公開鍵を検証して保存します。
パスキー登録フローを示すチャート
図 1. パスキー登録フロー。

認証(パスキーの取得)

  1. バックエンドは、認証 JSON を生成して認証情報を取得し、WebView クライアントにレンダリングされたウェブページに送信します。
  2. ウェブページは navigator.credentials.get を使用します。挿入された JavaScript を使用してこのメソッドをオーバーライドし、リクエストを Android アプリにリダイレクトします。
  3. アプリは、getCredential を呼び出して Credential Manager API を使用し、認証情報を取得します。
  4. Credential Manager API がアプリに認証情報を返します。
  5. アプリは秘密鍵のデジタル署名を取得してウェブページに送信し、挿入された JavaScript がレスポンスを解析できるようにします。
  6. その後、ウェブページがそれをサーバーに送信し、サーバーは公開鍵を使用してデジタル署名を検証します。
パスキー認証フローを示すチャート
図 2. パスキー認証フロー。

パスワードやフェデレーション ID システムにも同じフローを使用できます。

前提条件

Credential Manager API を使用するには、認証情報マネージャー ガイドの前提条件セクションに記載されている手順を実施し、次の操作を行ってください。

JavaScript の通信

WebView の JavaScript とネイティブ Android コードが相互に通信できるようにするには、2 つの環境間でメッセージを送信し、リクエストを処理する必要があります。そのためには、カスタムの JavaScript コードを WebView に挿入します。すると、ウェブ コンテンツの動作を変更したり、ネイティブ Android コードとやり取りしたりできるようになります。

JavaScript の挿入

以下の JavaScript コードは、WebView と Android アプリの間の通信を確立します。これは、先述した登録と認証のフローで WebAuthn API が使用する navigator.credentials.create() メソッドと navigator.credentials.get() メソッドをオーバーライドします。

アプリではこの JavaScript コードの圧縮版を使用します。

パスキーのリスナーを作成する

JavaScript との通信を処理する PasskeyWebListener クラスをセットアップします。このクラスは WebViewCompat.WebMessageListener から継承する必要があります。このクラスは、JavaScript からメッセージを受け取り、Android アプリで必要なアクションを実行します。

以降のセクションでは、PasskeyWebListener クラスの構造と、リクエストとレスポンスの処理について説明します。

認証リクエストを処理する

JavaScript コードが Android アプリにメッセージを送信すると、WebAuthn の navigator.credentials.create() オペレーションまたは navigator.credentials.get() オペレーションのリクエストを処理するために、PasskeyWebListener クラスの onPostMessage メソッドが呼び出されます。

// The class talking to Javascript should inherit:
class PasskeyWebListener(
  private val activity: Activity,
  private val coroutineScope: CoroutineScope,
  private val credentialManagerHandler: CredentialManagerHandler
) : WebViewCompat.WebMessageListener {
  /** havePendingRequest is true if there is an outstanding WebAuthn request.
  There is only ever one request outstanding at a time. */
  private var havePendingRequest = false

  /** pendingRequestIsDoomed is true if the WebView has navigated since
  starting a request. The FIDO module cannot be canceled, but the response
  will never be delivered in this case. */
  private var pendingRequestIsDoomed = false

  /** replyChannel is the port that the page is listening for a response on.
  It is valid if havePendingRequest is true. */
  private var replyChannel: ReplyChannel? = null

  /**
   * Called by the page during a WebAuthn request.
   *
   * @param view Creates the WebView.
   * @param message The message sent from the client using injected JavaScript.
   * @param sourceOrigin The origin of the HTTPS request. Should not be null.
   * @param isMainFrame Should be set to true. Embedded frames are not
  supported.
   * @param replyProxy Passed in by JavaScript. Allows replying when wrapped in
  the Channel.
   * @return The message response.
   */
  @UiThread
  override fun onPostMessage(
    view: WebView,
    message: WebMessageCompat,
    sourceOrigin: Uri,
    isMainFrame: Boolean,
    replyProxy: JavaScriptReplyProxy,
  ) {
    val messageData = message.data ?: return
    onRequest(
      messageData,
      sourceOrigin,
      isMainFrame,
      JavaScriptReplyChannel(replyProxy)
    )
  }

  private fun onRequest(
    msg: String,
    sourceOrigin: Uri,
    isMainFrame: Boolean,
    reply: ReplyChannel,
  ) {
    msg?.let {
      val jsonObj = JSONObject(msg);
      val type = jsonObj.getString(TYPE_KEY)
      val message = jsonObj.getString(REQUEST_KEY)

      if (havePendingRequest) {
        postErrorMessage(reply, "The request already in progress", type)
        return
      }

      replyChannel = reply
      if (!isMainFrame) {
        reportFailure("Requests from subframes are not supported", type)
        return
      }
      val originScheme = sourceOrigin.scheme
      if (originScheme == null || originScheme.lowercase() != "https") {
        reportFailure("WebAuthn not permitted for current URL", type)
        return
      }

      // Verify that origin belongs to your website,
      // it's because the unknown origin may gain credential info.
      // if (isUnknownOrigin(originScheme)) {
      // return
      // }

      havePendingRequest = true
      pendingRequestIsDoomed = false

      // Use a temporary "replyCurrent" variable to send the data back, while
      // resetting the main "replyChannel" variable to null so it’s ready for
      // the next request.
      val replyCurrent = replyChannel
      if (replyCurrent == null) {
        Log.i(TAG, "The reply channel was null, cannot continue")
        return;
      }

      when (type) {
        CREATE_UNIQUE_KEY ->
          this.coroutineScope.launch {
            handleCreateFlow(credentialManagerHandler, message, replyCurrent)
          }

        GET_UNIQUE_KEY -> this.coroutineScope.launch {
          handleGetFlow(credentialManagerHandler, message, replyCurrent)
        }

        else -> Log.i(TAG, "Incorrect request json")
      }
    }
  }

  private suspend fun handleCreateFlow(
    credentialManagerHandler: CredentialManagerHandler,
    message: String,
    reply: ReplyChannel,
  ) {
    try {
      havePendingRequest = false
      pendingRequestIsDoomed = false
      val response = credentialManagerHandler.createPasskey(message)
      val successArray = ArrayList<Any>();
      successArray.add("success");
      successArray.add(JSONObject(response.registrationResponseJson));
      successArray.add(CREATE_UNIQUE_KEY);
      reply.send(JSONArray(successArray).toString())
      replyChannel = null // setting initial replyChannel for the next request
    } catch (e: CreateCredentialException) {
      reportFailure(
        "Error: ${e.errorMessage} w type: ${e.type} w obj: $e",
        CREATE_UNIQUE_KEY
      )
    } catch (t: Throwable) {
      reportFailure("Error: ${t.message}", CREATE_UNIQUE_KEY)
    }
  }

  companion object {
    /** INTERFACE_NAME is the name of the MessagePort that must be injected into pages. */
    const val INTERFACE_NAME = "__webauthn_interface__"
    const val TYPE_KEY = "type"
    const val REQUEST_KEY = "request"
    const val CREATE_UNIQUE_KEY = "create"
    const val GET_UNIQUE_KEY = "get"
    /** INJECTED_VAL is the minified version of the JavaScript code described at this class
     * heading. The non minified form is found at credmanweb/javascript/encode.js.*/
    const val INJECTED_VAL = """
            var __webauthn_interface__,__webauthn_hooks__;!function(e){console.log("In the hook."),__webauthn_interface__.addEventListener("message",function e(n){var r=JSON.parse(n.data),t=r[2];"get"===t?o(r):"create"===t?u(r):console.log("Incorrect response format for reply")});var n=null,r=null,t=null,a=null;function o(e){if(null!==n&&null!==t){if("success"!=e[0]){var r=t;n=null,t=null,r(new DOMException(e[1],"NotAllowedError"));return}var a=i(e[1]),o=n;n=null,t=null,o(a)}}function l(e){var n=e.length%4;return Uint8Array.from(atob(e.replace(/-/g,"+").replace(/_/g,"/").padEnd(e.length+(0===n?0:4-n),"=")),function(e){return e.charCodeAt(0)}).buffer}function s(e){return btoa(Array.from(new Uint8Array(e),function(e){return String.fromCharCode(e)}).join("")).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+${'$'}/,"")}function u(e){if(null===r||null===a){console.log("Here: "+r+" and reject: "+a);return}if(console.log("Output back: "+e),"success"!=e[0]){var n=a;r=null,a=null,n(new DOMException(e[1],"NotAllowedError"));return}var t=i(e[1]),o=r;r=null,a=null,o(t)}function i(e){return console.log("Here is the response from credential manager: "+e),e.rawId=l(e.rawId),e.response.clientDataJSON=l(e.response.clientDataJSON),e.response.hasOwnProperty("attestationObject")&&(e.response.attestationObject=l(e.response.attestationObject)),e.response.hasOwnProperty("authenticatorData")&&(e.response.authenticatorData=l(e.response.authenticatorData)),e.response.hasOwnProperty("signature")&&(e.response.signature=l(e.response.signature)),e.response.hasOwnProperty("userHandle")&&(e.response.userHandle=l(e.response.userHandle)),e.getClientExtensionResults=function e(){return{}},e}e.create=function n(t){if(!("publicKey"in t))return e.originalCreateFunction(t);var o=new Promise(function(e,n){r=e,a=n}),l=t.publicKey;if(l.hasOwnProperty("challenge")){var u=s(l.challenge);l.challenge=u}if(l.hasOwnProperty("user")&&l.user.hasOwnProperty("id")){var i=s(l.user.id);l.user.id=i}var c=JSON.stringify({type:"create",request:l});return __webauthn_interface__.postMessage(c),o},e.get=function r(a){if(!("publicKey"in a))return e.originalGetFunction(a);var o=new Promise(function(e,r){n=e,t=r}),l=a.publicKey;if(l.hasOwnProperty("challenge")){var u=s(l.challenge);l.challenge=u}var i=JSON.stringify({type:"get",request:l});return __webauthn_interface__.postMessage(i),o},e.onReplyGet=o,e.CM_base64url_decode=l,e.CM_base64url_encode=s,e.onReplyCreate=u}(__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,window.PublicKeyCredential=function(){},window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable=function(){return Promise.resolve(!1)};
        """
  }

handleCreateFlowhandleGetFlow については、GitHub の例をご覧ください。

レスポンスを処理する

ネイティブ アプリからウェブページに送信されるレスポンスを処理するには、JavaScriptReplyChannel 内に JavaScriptReplyProxy を追加します。

// The setup for the reply channel allows communication with JavaScript.
private class JavaScriptReplyChannel(private val reply: JavaScriptReplyProxy) :
  ReplyChannel {
  override fun send(message: String?) {
    try {
      reply.postMessage(message!!)
    } catch (t: Throwable) {
      Log.i(TAG, "Reply failure due to: " + t.message);
    }
  }
}

// ReplyChannel is the interface where replies to the embedded site are
// sent. This allows for testing since AndroidX bans mocking its objects.
interface ReplyChannel {
  fun send(message: String?)
}

ネイティブ アプリのエラーは必ず捕捉して JavaScript 側に送り返してください。

WebView と統合する

このセクションでは、WebView の統合をセットアップする方法について説明します。

WebView を初期化する

Android アプリのアクティビティで、WebView を初期化し、付随する WebViewClient をセットアップします。WebViewClient は、WebView に挿入された JavaScript コードとの通信を処理します。

WebView をセットアップし、認証情報マネージャーを呼び出します。

val credentialManagerHandler = CredentialManagerHandler(this)

setContent {
  val coroutineScope = rememberCoroutineScope()
  AndroidView(factory = {
    WebView(it).apply {
      settings.javaScriptEnabled = true

      // Test URL:
      val url = "https://passkeys-codelab.glitch.me/"
      val listenerSupported = WebViewFeature.isFeatureSupported(
        WebViewFeature.WEB_MESSAGE_LISTENER
      )
      if (listenerSupported) {
        // Inject local JavaScript that calls Credential Manager.
        hookWebAuthnWithListener(
          this, this@WebViewMainActivity,
          coroutineScope, credentialManagerHandler
        )
      } else {
        // Fallback routine for unsupported API levels.
      }
      loadUrl(url)
    }
  }
  )
}

新しい WebView クライアント オブジェクトを作成し、ウェブページに JavaScript を挿入します。

val passkeyWebListener = PasskeyWebListener(activity, coroutineScope, credentialManagerHandler)

val webViewClient = object : WebViewClient() {
  override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
    super.onPageStarted(view, url, favicon)
    webView.evaluateJavascript(PasskeyWebListener.INJECTED_VAL, null)
  }
}

webView.webViewClient = webViewClient

ウェブ メッセージ リスナーをセットアップする

JavaScript と Android アプリの間でメッセージを送信できるようにするには、WebViewCompat.addWebMessageListener メソッドでウェブ メッセージ リスナーをセットアップします。

val rules = setOf("*")
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
  WebViewCompat.addWebMessageListener(webView, PasskeyWebListener.INTERFACE_NAME,
    rules, passkeyWebListener)
}

ウェブ インテグレーション

ウェブ インテグレーションの構築方法については、パスワードレス ログイン用のパスキーを作成するフォームの自動入力でパスキーを使用してログインするをご覧ください。

テストとデプロイ

制御された環境でフロー全体を徹底的にテストし、Android アプリ、ウェブページ、バックエンドの間で適切な通信が行われることを確認します。

統合されたソリューションを本番環境にデプロイし、受け取った登録リクエストと認証リクエストをバックエンドで処理できるようにします。バックエンド コードは、登録(create)プロセスと認証(get)プロセス用に初期の JSON を生成する必要があります。また、ウェブページから受け取ったレスポンスの妥当性確認と検証を処理する必要もあります。

実装が UX の推奨事項に対応していることを確認します。

重要な注意点

  • 提供されている JavaScript コードを使用して、navigator.credentials.create() オペレーションと navigator.credentials.get() オペレーションを処理します。
  • PasskeyWebListener クラスは、WebView の JavaScript コードと Android アプリの間のブリッジです。メッセージの受け渡し、通信、必要なアクションの実行を処理します。
  • 提供されているコード スニペットは、プロジェクトの構造、命名規則、特定の要件に合わせて変更してください。
  • ネイティブ アプリ側でエラーを捕捉して JavaScript 側に送り返します。

このガイドに沿って、WebView を使用する Android アプリに Credential Manager API を統合することで、認証情報を効果的に管理しながら、パスキーに対応した安全でシームレスなログイン エクスペリエンスをユーザーに提供できます。