Skip to content

Commit

Permalink
Misc small fixes and update copyright headers
Browse files Browse the repository at this point in the history
Change-Id: Ie386d2a01e019165cc3a15b63ab3aad6103583e8
  • Loading branch information
jdkoren committed Apr 26, 2023
1 parent 735c326 commit d054b7c
Show file tree
Hide file tree
Showing 33 changed files with 459 additions and 68 deletions.
23 changes: 18 additions & 5 deletions CredentialManager/app/build.gradle
@@ -1,3 +1,19 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
Expand All @@ -7,13 +23,10 @@ repositories {
mavenCentral()
}

// Create a variable called keystorePropertiesFile, and initialize it to your
// keystore.properties file, in the rootProject folder.
// Load keystore properties for our signing key
// (see developer.android.com/studio/publish/app-signing)
def keystorePropertiesFile = rootProject.file("keystore.properties")

def keystoreProperties = new Properties()

// Load keystore properties for our signing key (see developer.android.com/studio/publish/app-signing)
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {
Expand Down
16 changes: 16 additions & 0 deletions CredentialManager/app/src/main/AndroidManifest.xml
@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2023 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

Expand Down
8 changes: 4 additions & 4 deletions CredentialManager/app/src/main/assets/RegFromServer
@@ -1,5 +1,5 @@
{
"challenge": "$4",
"challenge": "<challenge>",
"rp": {
"id": "passkeys-codelab.glitch.me",
"name": "CredMan App Test"
Expand All @@ -19,8 +19,8 @@
"residentKey": "required"
},
"user": {
"id": "$3",
"name": "$1",
"displayName": "$2"
"id": "<userId>",
"name": "<userName>",
"displayName": "<userDisplayName>"
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,8 @@ object DataProvider {
private const val PREF_NAME = "CREDMAN_PREF"

fun initSharedPref(context: Context) {
sharedPreference = context.applicationContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
sharedPreference =
context.applicationContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
editor = sharedPreference.edit()
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,4 +35,3 @@ fun Context.showErrorAlert(msg: String) {
.setIcon(android.R.drawable.ic_dialog_alert)
.show()
}

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,12 +56,12 @@ class MainActivity : AppCompatActivity(), MainFragmentCallback, HomeFragmentCall
}

override fun logout() {
supportFragmentManager.popBackStack("home", FragmentManager.POP_BACK_STACK_INCLUSIVE);
supportFragmentManager.popBackStack("home", FragmentManager.POP_BACK_STACK_INCLUSIVE)
loadMainFragment()
}

private fun loadMainFragment() {
supportFragmentManager.popBackStack();
supportFragmentManager.popBackStack()
loadFragment(MainFragment(), false)
}

Expand All @@ -80,7 +80,7 @@ class MainActivity : AppCompatActivity(), MainFragmentCallback, HomeFragmentCall
if (DataProvider.isSignedIn() || supportFragmentManager.backStackEntryCount == 1) {
finish()
} else {
super.onBackPressed();
super.onBackPressed()
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -106,34 +106,36 @@ class SignInFragment : Fragment() {
val getPublicKeyCredentialOption =
GetPublicKeyCredentialOption(fetchAuthJsonFromServer(), null, true)
val getPasswordOption = GetPasswordOption()
try {
val result = credentialManager.getCredential(
val result = try {
credentialManager.getCredential(
GetCredentialRequest(
listOf(
getPublicKeyCredentialOption,
getPasswordOption
)
), requireActivity()
),
requireActivity()
)

if (result.credential is PublicKeyCredential) {
val cred = result.credential as PublicKeyCredential
DataProvider.setSignedInThroughPasskeys(true)
return "Passkey: ${cred.authenticationResponseJson}"
}
if (result.credential is PasswordCredential) {
val cred = result.credential as PasswordCredential
DataProvider.setSignedInThroughPasskeys(false)
return "Got Password - User:${cred.id} Password: ${cred.password}"
}
} catch (e: Exception) {
configureViews(View.INVISIBLE, true)
Log.e("Auth", " Exception Message" + e.message.toString())
Log.e("Auth", "getCredential failed with exception: " + e.message.toString())
activity?.showErrorAlert(
"An error occurred while authenticating through saved credentials. Check logs for additional details"
)
return null
}

if (result.credential is PublicKeyCredential) {
val cred = result.credential as PublicKeyCredential
DataProvider.setSignedInThroughPasskeys(true)
return "Passkey: ${cred.authenticationResponseJson}"
}
if (result.credential is PasswordCredential) {
val cred = result.credential as PasswordCredential
DataProvider.setSignedInThroughPasskeys(false)
return "Got Password - User:${cred.id} Password: ${cred.password}"
}

return null
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -143,10 +143,10 @@ class SignUpFragment : Fragment() {
val response = requireContext().readFromAsset("RegFromServer")

//Update userId, name and Display name in the mock
return response.replace("$1", binding.username.text.toString())
.replace("$4", getEncodedChallenge())
.replace("$3", getEncodedUserId())
.replace("$2", binding.username.text.toString())
return response.replace("<userId>", getEncodedUserId())
.replace("<userName>", binding.username.text.toString())
.replace("<userDisplayName>", binding.username.text.toString())
.replace("<challenge>", getEncodedChallenge())
}

private fun getEncodedUserId(): String {
Expand Down Expand Up @@ -177,7 +177,7 @@ class SignUpFragment : Fragment() {
try {
credentialManager.createCredential(request, requireActivity()) as CreatePasswordResponse
} catch (e: Exception) {
Log.e("Auth", " Exception Message : " + e.message)
Log.e("Auth", "createPassword failed with exception: " + e.message)
}
}

Expand All @@ -191,7 +191,7 @@ class SignUpFragment : Fragment() {
) as CreatePublicKeyCredentialResponse
} catch (e: CreateCredentialException) {
configureProgress(View.INVISIBLE)
handleFailure(e)
handlePasskeyFailure(e)
}
return response
}
Expand All @@ -207,38 +207,30 @@ class SignUpFragment : Fragment() {
binding.circularProgressIndicator.visibility = visibility
}

//Demonstration purpose : These are type of errors during passkey creation, handle this in your code wisely.
private fun handleFailure(e: CreateCredentialException) {
var msg = ""
when (e) {
// These are types of errors that can occur during passkey creation.
private fun handlePasskeyFailure(e: CreateCredentialException) {
val msg = when (e) {
is CreatePublicKeyCredentialDomException -> {
// Handle the passkey DOM errors thrown according to the
// WebAuthn spec using e.domError
msg =
"An error occurred while creating a passkey, please check logs for additional details"
"An error occurred while creating a passkey, please check logs for additional details."
}
is CreateCredentialCancellationException -> {
// The user intentionally canceled the operation and chose not
// to register the credential.
msg =
"The user intentionally canceled the operation and chose not to register the credential. , please check logs for additional details"
"The user intentionally canceled the operation and chose not to register the credential. Check logs for additional details."
}
is CreateCredentialInterruptedException -> {
msg =
"The operation was interrupted, please retry the call. Check logs for additional details"
// Retry-able error. Consider retrying the call.
"The operation was interrupted, please retry the call. Check logs for additional details."
}
is CreateCredentialProviderConfigurationException -> {
// Your app is missing the provider configuration dependency.
// Most likely, you're missing the
// "credentials-play-services-auth" module.
msg =
"Your app is missing the provider configuration dependency. Check logs for additional details"
// Most likely, you're missing "credentials-play-services-auth".
"Your app is missing the provider configuration dependency. Check logs for additional details."
}
is CreateCredentialUnknownException -> {
msg =
"An unknown error occurred while creating passkey. Check logs for additional details"
Log.w("Auth", e.message.toString())
"An unknown error occurred while creating passkey. Check logs for additional details."
}
is CreateCustomCredentialException -> {
// You have encountered an error from a 3rd-party SDK. If you
Expand All @@ -247,12 +239,14 @@ class SignUpFragment : Fragment() {
// should check for any custom exception type constants within
// that SDK to match with e.type. Otherwise, drop or log the
// exception.
msg =
"An unknown error occurred from a 3rd party SDK. Check logs for additional details"
"An unknown error occurred from a 3rd party SDK. Check logs for additional details."
}
else -> {
Log.w("Auth", "Unexpected exception type ${e::class.java.name}")
"An unknown error occurred."
}
else -> Log.w("Auth", "Unexpected exception type ${e::class.java.name}")
}
Log.e("Auth", " Exception Message : " + e.message.toString())
Log.e("Auth", "createPasskey failed with exception: " + e.message.toString())
activity?.showErrorAlert(msg)
}

Expand Down
@@ -1,3 +1,19 @@
<!--
~ Copyright 2023 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
Expand Down Expand Up @@ -27,4 +43,4 @@
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
</vector>
16 changes: 16 additions & 0 deletions CredentialManager/app/src/main/res/drawable-v24/passkey.xml
@@ -1,3 +1,19 @@
<!--
~ Copyright 2023 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
Expand Down
@@ -1,4 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2023 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
Expand Down

0 comments on commit d054b7c

Please sign in to comment.