Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
fix: add missing client credential json
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Apr 19, 2024
1 parent 07585c8 commit e06e624
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class GetPasskeyViewModel: ViewModel() {
val requestOptions = PublicKeyCredentialRequestOptions(option.requestJson)
val credIdEnc = requestInfo!!.getString("credentialId")
val credId = Base64.decode(credIdEnc!!)
// val clientDataHash = option.requestData.getByteArray("androidx.credentials.BUNDLE_KEY_CLIENT_DATA_HASH")
val clientDataHash = option.requestData.getByteArray("androidx.credentials.BUNDLE_KEY_CLIENT_DATA_HASH")
val packageName = request.callingAppInfo.packageName
val userHandle = requestInfo.getString("userHandle")
val origin = credentialRepository.appInfoToOrigin(request.callingAppInfo)
Expand All @@ -96,7 +96,7 @@ class GetPasskeyViewModel: ViewModel() {
bs = true,
userHandle = userHandle!!.toByteArray(),
packageName = packageName,
// clientDataHash = clientDataHash!!
clientDataHash = clientDataHash!!
)
val keyPair = credentialRepository.getKeyPair(context, credId)

Expand All @@ -105,17 +105,38 @@ class GetPasskeyViewModel: ViewModel() {
sig.initSign(keyPair.private as ECPrivateKey )
sig.update(response.dataToSign())
response.signature = sig.sign()
val options = request.credentialOptions[0] as GetPublicKeyCredentialOption
val json = options.requestJson
val requestJson = JSONObject(json)


val credential = FidoPublicKeyCredential(
rawId = credId, response = response, authenticatorAttachment = "platform"
)
// add clientDataJSON to the response
val clientDataJSONb64 = getClientDataJSONb64(origin, requestJson.get("challenge") as String)
val delimiter = "response\":{"
val credentialJson = credential.json().substringBeforeLast(delimiter)+ delimiter +
"\"clientDataJSON\":\"$clientDataJSONb64\","+
credential.json().substringAfterLast(delimiter)
Log.d(TAG, credential.toString())
val result = Intent()
val passkeyCredential = PublicKeyCredential(credential.json())
val passkeyCredential = PublicKeyCredential(credentialJson)
PendingIntentHandler.setGetCredentialResponse(
result, GetCredentialResponse(passkeyCredential)
)
return result
}
@OptIn(ExperimentalEncodingApi::class)
private fun getClientDataJSONb64(origin: String, challenge:String): String {

val origin = origin.replace(Regex("/$"), "")

val jsonString =
"{\"type\":\"webauthn.get\",\"challenge\":\"$challenge\",\"origin\":\"$origin\",\"crossOrigin\":false}"
val jsonByteArray = jsonString.toByteArray()
Log.d("MainActivity","+++ ClientDataJSON: $jsonString")
return Base64.encode(jsonByteArray)
}

}

0 comments on commit e06e624

Please sign in to comment.