diff --git a/IdentityCredentials/.gitignore b/IdentityCredentials/.gitignore new file mode 100644 index 0000000..bbc8263 --- /dev/null +++ b/IdentityCredentials/.gitignore @@ -0,0 +1,10 @@ +*.iml +.gradle +/local.properties +.DS_Store +.idea +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/IdentityCredentials/README.md b/IdentityCredentials/README.md new file mode 100644 index 0000000..89371bf --- /dev/null +++ b/IdentityCredentials/README.md @@ -0,0 +1,12 @@ +Android Identity Credential Sample +=================================== + +This sample demonstrates how to use the Identity Credential API. The Identity Credential API allows your app to store user credentials that it can later retrieve to re-authenticate users on a new device. This sample demonstrates that by allowing you to save text, uninstall the app, reinstall it and retreive that text. + +Running the sample +-When you run the sample you will see a screen with an EditText and a button that says Sign In. +-To save text, type text into the EditText and press register. There will now be a TextView displaying the text you just typed in. +-To test Identity Credential, uninstall the app and reinstall it. Your text will still be displayed in the TextView. +-To clear your text press Sign Out. + +To learn more about Identity check out the documentation: https://developers.google.com/identity/credential-management. diff --git a/IdentityCredentials/app/.gitignore b/IdentityCredentials/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/IdentityCredentials/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/IdentityCredentials/app/build.gradle b/IdentityCredentials/app/build.gradle new file mode 100644 index 0000000..1d7a14d --- /dev/null +++ b/IdentityCredentials/app/build.gradle @@ -0,0 +1,88 @@ +/* + * Copyright 2021 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 { + alias(libs.plugins.android.application) + alias(libs.plugins.kotlin.android) + alias(libs.plugins.hilt.android) + alias(libs.plugins.kotlin.kapt) +} + +android { + compileSdk 35 + namespace "com.google.android.gms.identity.credentials.sample" + defaultConfig { + applicationId "com.google.android.gms.identity.credentials.sample" + minSdk 21 + targetSdk 35 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildFeatures { + viewBinding true + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } + + kotlinOptions { + jvmTarget = '17' + } +} + +dependencies { + implementation libs.kotlin.stdlib + implementation libs.material.design + implementation libs.androidx.activity.ktx + implementation libs.androidx.core.ktx + implementation libs.androidx.appcompat + implementation libs.androidx.constraintlayout + implementation libs.androidx.test.espresso.idling.resource + + implementation libs.google.play.services.identity.credentials + implementation libs.androidx.credentials.play.services.auth + + // Required because the broadcast pending-intent otherwise lacks the intent-flag. + // IllegalArgumentException: Targeting S+ (version 31 and above) requires that one of + // FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. + implementation libs.androidx.work.runtime + + // Lifecycle + implementation libs.bundles.androidx.lifecycle + + // Coroutines + implementation libs.kotlinx.coroutines.play.services + + // Hilt + implementation libs.hilt.android + kapt libs.hilt.android.compiler + implementation libs.hilt.work + kapt libs.hilt.compiler + + testImplementation libs.junit + debugImplementation libs.androidx.test.monitor + androidTestImplementation libs.bundles.androidx.test +} \ No newline at end of file diff --git a/IdentityCredentials/app/proguard-rules.pro b/IdentityCredentials/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/IdentityCredentials/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/IdentityCredentials/app/src/main/AndroidManifest.xml b/IdentityCredentials/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..76447a4 --- /dev/null +++ b/IdentityCredentials/app/src/main/AndroidManifest.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/AppModule.kt b/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/AppModule.kt new file mode 100644 index 0000000..db6321e --- /dev/null +++ b/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/AppModule.kt @@ -0,0 +1,38 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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. + */ +package com.google.android.gms.identity.credentials.sample + +import android.content.Context +import com.google.android.gms.identitycredentials.IdentityCredentialManager +import com.google.android.gms.identitycredentials.IdentityCredentialClient +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.qualifiers.ApplicationContext +import dagger.hilt.components.SingletonComponent +import javax.inject.Singleton + +/** + * Hilt module that provides singleton (application-scoped) objects. + */ +@Module +@InstallIn(SingletonComponent::class) +class AppModule { + @Singleton + @Provides + fun provideIdentityCredentialClient(@ApplicationContext context: Context): IdentityCredentialClient = + IdentityCredentialManager.getClient(context) +} diff --git a/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/IdentityCredentialsRepository.kt b/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/IdentityCredentialsRepository.kt new file mode 100644 index 0000000..420eb50 --- /dev/null +++ b/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/IdentityCredentialsRepository.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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. + */ +package com.google.android.gms.identity.credentials.sample + +import android.os.Bundle +import android.os.ResultReceiver +import com.google.android.gms.identitycredentials.ClearRegistryRequest +import com.google.android.gms.identitycredentials.ClearRegistryResponse +import com.google.android.gms.identitycredentials.CredentialOption +import com.google.android.gms.identitycredentials.GetCredentialRequest +import com.google.android.gms.identitycredentials.IdentityCredentialClient +import com.google.android.gms.identitycredentials.PendingGetCredentialHandle +import com.google.android.gms.identitycredentials.RegistrationRequest +import com.google.android.gms.identitycredentials.RegistrationResponse +import com.google.android.gms.tasks.Task +import javax.inject.Inject + +/** + * The Repository handles data operations and provides a clean API so that + * the rest of the app can retrieve the Identity Credential data easily. + * @see IdentityCredentialClient + * @see CommonStatusCodes + */ +class IdentityCredentialsRepository @Inject constructor( + private val client: IdentityCredentialClient +) { + /** + * Returns a Task which asynchronously generates a RegistrationResponse on success + * or throws an OperationException on failure, when attempting to write to the registry. + * + * eg. com.google.android.gms.common.api.ApiException: 17 means: + * The client attempted to call a method from an API that failed to connect. + */ + fun registerCredentials( + credentials: ByteArray, matcher: ByteArray, type: String, + requestType: String, protocolTypes: List, id: String + ): Task { + return client.registerCredentials( + RegistrationRequest(credentials, matcher, type, requestType, protocolTypes, id) + ) + } + + /** Returns a Task which asynchronously generates a pending intent to get credentials. */ + fun getCredential( + credentialOptions: List, data: Bundle, + origin: String?, resultReceiver: ResultReceiver + ): Task { + return client.getCredential( + GetCredentialRequest(credentialOptions, data, origin, resultReceiver) + ) + } + + /** + * Returns a Task which asynchronously generates a ClearRegistryResponse on success + * or throws an OperationException on failure, when attempting to clear from the registry. + */ + fun clearRegistry(): Task { + return client.clearRegistry( + ClearRegistryRequest() + ) + } +} diff --git a/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/MainActivity.kt b/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/MainActivity.kt new file mode 100644 index 0000000..d3ef671 --- /dev/null +++ b/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/MainActivity.kt @@ -0,0 +1,85 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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. + */ +package com.google.android.gms.identity.credentials.sample + +import android.os.Bundle +import android.os.Handler +import android.os.Looper +import android.os.ResultReceiver +import android.text.Editable +import androidx.activity.viewModels +import androidx.appcompat.app.AppCompatActivity +import androidx.core.view.isVisible +import androidx.lifecycle.lifecycleScope +import com.google.android.gms.identity.credentials.sample.databinding.ActivityMainBinding +import com.google.android.gms.identitycredentials.CredentialOption +import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.launch + +@AndroidEntryPoint +class MainActivity : AppCompatActivity() { + + private val binding: ActivityMainBinding by lazy { + ActivityMainBinding.inflate(layoutInflater) + } + private val viewModel: MainViewModel by viewModels() + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + setContentView(binding.root) + + lifecycleScope.launch { + viewModel.state + .collect { state -> + binding.buttonClearRegistry.isVisible = state.hasCredential + binding.buttonRegisterCredentials.isVisible = !state.hasCredential + if (state.hasCredential) { + binding.nameTextView.text.insert(0, state.credential.toString()) + } + } + } + + binding.buttonRegisterCredentials.setOnClickListener { + val credentialBytes = binding.nameTextView.text.toString().toByteArray() + val matcherBytes = "".toByteArray() + + // IllegalArgumentException: Either type: default, + // or requestType: default and protocolTypes: [] must be specified, + // but all were blank, or for protocolTypes, empty or full of blank elements. + viewModel.registerCredentials( + credentialBytes, + matcherBytes, + "default", + "", + emptyList(), + "1" + ) + } + + binding.buttonGetCredential.setOnClickListener { + val credentialOptions: List = listOf() + val data: Bundle = Bundle() + val origin: String = "" + val receiver: ResultReceiver = ResultReceiver(Handler(Looper.getMainLooper())) + viewModel.launchCredentialSelector(credentialOptions, data, origin, receiver) + } + + binding.buttonClearRegistry.setOnClickListener { + viewModel.clearRegistry() + } + } +} diff --git a/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/MainApplication.kt b/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/MainApplication.kt new file mode 100644 index 0000000..9e7610b --- /dev/null +++ b/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/MainApplication.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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. + */ +package com.google.android.gms.identity.credentials.sample + +import android.app.Application +import dagger.hilt.android.HiltAndroidApp + +/** Application class, needed to enable dependency injection with Hilt. */ +@HiltAndroidApp +class MainApplication : Application() diff --git a/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/MainViewModel.kt b/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/MainViewModel.kt new file mode 100644 index 0000000..cf6d03c --- /dev/null +++ b/IdentityCredentials/app/src/main/java/com/google/android/gms/identity/credentials/sample/MainViewModel.kt @@ -0,0 +1,103 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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. + */ +package com.google.android.gms.identity.credentials.sample + +import android.app.PendingIntent +import android.app.PendingIntent.CanceledException +import android.os.Bundle +import android.os.ResultReceiver +import android.util.Log +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.google.android.gms.identitycredentials.ClearRegistryResponse +import com.google.android.gms.identitycredentials.Credential +import com.google.android.gms.identitycredentials.CredentialOption +import com.google.android.gms.identitycredentials.PendingGetCredentialHandle +import com.google.android.gms.identitycredentials.RegistrationResponse +import com.google.android.gms.tasks.Task +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.launch +import kotlinx.coroutines.tasks.await +import javax.inject.Inject + +data class State( + val credential: Credential? = null +) + +val State.hasCredential get() = credential != null + +/** ViewModel is used to access the Identity Credential Data and to observe changes to it. */ +@HiltViewModel +class MainViewModel @Inject constructor( + private val repository: IdentityCredentialsRepository +) : ViewModel() { + + private val _state = MutableStateFlow(State()) + val state: StateFlow = _state.asStateFlow() + + init { + viewModelScope.launch { + Log.d("", "init") + } + } + + fun registerCredentials( + credentials: ByteArray, matcher: ByteArray, type: String, + requestType: String, protocolTypes: List, id: String + ) { + viewModelScope.launch { + var response: RegistrationResponse = repository.registerCredentials(credentials, matcher, type, requestType, protocolTypes, id).await() + // _state.value = State(credential = result) + } + } + + /** Launch the credential-selector UI. */ + fun launchCredentialSelector( + credentialOptions: List, data: Bundle, + origin: String?, resultReceiver: ResultReceiver + ) { + viewModelScope.launch { + val intent: PendingIntent = getCredentialSelectorPendingIntent( + credentialOptions, data, origin, resultReceiver + ) + try { + intent.send() + } catch (e: CanceledException) { + e.printStackTrace() + } + } + } + + /** Obtain the PendingIntent to launch the credential-selector UI. */ + private suspend fun getCredentialSelectorPendingIntent( + credentialOptions: List, data: Bundle, + origin: String?, resultReceiver: ResultReceiver + ): PendingIntent { + val response: Task = repository.getCredential(credentialOptions, data, origin, resultReceiver) + response.await() + return response.result.pendingIntent + } + + fun clearRegistry() { + viewModelScope.launch { + var response: ClearRegistryResponse = repository.clearRegistry().await() + _state.value = State(credential = null) + } + } +} diff --git a/IdentityCredentials/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/IdentityCredentials/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..f5ba0ea --- /dev/null +++ b/IdentityCredentials/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/IdentityCredentials/app/src/main/res/drawable/ic_launcher_background.xml b/IdentityCredentials/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..873df7a --- /dev/null +++ b/IdentityCredentials/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,182 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IdentityCredentials/app/src/main/res/layout/activity_main.xml b/IdentityCredentials/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..0cdcc51 --- /dev/null +++ b/IdentityCredentials/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,54 @@ + + + + + + +