Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions CredentialManager/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ android {
defaultConfig {
applicationId "com.google.credentialmanager.sample"
minSdk 21
targetSdk 35
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -76,7 +77,7 @@ android {
}

composeOptions {
kotlinCompilerExtensionVersion = "1.4.2"
kotlinCompilerExtensionVersion = "1.5.15"
}
packagingOptions {
resources {
Expand All @@ -86,6 +87,7 @@ android {

buildFeatures {
viewBinding true
compose true
}
}

Expand All @@ -107,7 +109,15 @@ dependencies {

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"
implementation "androidx.lifecycle:lifecycle-runtime-compose:2.6.2"

// Integration with activities
implementation 'androidx.activity:activity-compose:1.9.0'
// Compose Bill of Materials
implementation platform('androidx.compose:compose-bom:2024.05.00')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3'
implementation "androidx.navigation:navigation-compose:2.7.7"
// Android Studio Preview support
debugImplementation 'androidx.compose.ui:ui-tooling'
}
2 changes: 1 addition & 1 deletion CredentialManager/app/src/main/assets/AuthFromServer
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"challenge": "HjBbH__fbLuzy95AGR31yEARA0EMtKlY0NrV5oy3NQw",
"timeout": 1800000,
"userVerification": "required",
"rpId": "passkeys-codelab.glitch.me"
"rpId": "project-sesame-426206.appspot.com"
}
4 changes: 2 additions & 2 deletions CredentialManager/app/src/main/assets/RegFromServer
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"challenge": "<challenge>",
"rp": {
"id": "passkeys-codelab.glitch.me",
"name": "CredMan App Test"
"id": "project-sesame-426206.appspot.com",
"name": "project-sesame-426206"
},
"pubKeyCredParams": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2025 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.
*/

package com.google.credentialmanager.sample

import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

sealed class Screen(val route: String) {
object Main : Screen("main")
object SignUp : Screen("signup")
object SignIn : Screen("signin")
object Home : Screen("home")
}

@Composable
fun AppNavHost(
startDestination: String = Screen.Main.route
) {
val navController = rememberNavController()

NavHost(
navController = navController,
startDestination = startDestination
) {
composable(Screen.Main.route) {
MainScreen(navController = navController)
}
composable(Screen.SignUp.route) {
SignUpScreen(navController = navController)
}
composable(Screen.SignIn.route) {
SignInScreen(navController = navController)
}
composable(Screen.Home.route) {
HomeScreen(navController = navController)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2025 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.
*/

package com.google.credentialmanager.sample

import android.app.Activity
import androidx.credentials.CreateCredentialRequest
import androidx.credentials.CreateCredentialResponse
import androidx.credentials.CredentialManager
import androidx.credentials.GetCredentialRequest
import androidx.credentials.GetCredentialResponse

/**
* Creates a credential using the Credential Manager API.
*
* @param activity The activity to use for the create credential request.
* @param request The create credential request.
* @return The create credential response.
*/

suspend fun createCredential(
activity: Activity,
request: CreateCredentialRequest
): CreateCredentialResponse {
val credentialManager = CredentialManager.create(activity)
return credentialManager.createCredential(activity, request)
}

/**
* Gets a credential using the Credential Manager API.
*
* @param activity The activity to use for the get credential request.
* @param request The get credential request.
* @return The get credential response.
*/
suspend fun getCredential(
activity: Activity,
request: GetCredentialRequest
): GetCredentialResponse {
val credentialManager = CredentialManager.create(activity)
return credentialManager.getCredential(activity, request)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2025 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 All @@ -19,6 +19,9 @@ package com.google.credentialmanager.sample
import android.content.Context
import android.content.SharedPreferences

/**
* A data provider that manages the user's signed-in status using SharedPreferences.
*/
object DataProvider {

private lateinit var sharedPreference: SharedPreferences
Expand All @@ -28,28 +31,51 @@ object DataProvider {
private const val IS_SIGNED_IN_THROUGH_PASSKEYS = "isSignedInThroughPasskeys"
private const val PREF_NAME = "CREDMAN_PREF"

/**
* Initializes the SharedPreferences for the data provider.
*
* @param context The application context.
*/
fun initSharedPref(context: Context) {
sharedPreference =
context.applicationContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
editor = sharedPreference.edit()
}

//Set if the user is signed in or not
/**
* Sets the user's signed-in status.
*
* @param flag True if the user is signed in, false otherwise.
*/
fun configureSignedInPref(flag: Boolean) {
editor.putBoolean(IS_SIGNED_IN, flag)
editor.commit()
editor.apply()
}

//Set if signed in through passkeys or not
/**
* Sets whether the user signed in through passkeys.
*
* @param flag True if the user signed in through passkeys, false otherwise.
*/
fun setSignedInThroughPasskeys(flag: Boolean) {
editor.putBoolean(IS_SIGNED_IN_THROUGH_PASSKEYS, flag)
editor.commit()
editor.apply()
}

/**
* Checks if the user is signed in.
*
* @return True if the user is signed in, false otherwise.
*/
fun isSignedIn(): Boolean {
return sharedPreference.getBoolean(IS_SIGNED_IN, false)
}

/**
* Checks if the user signed in through passkeys.
*
* @return True if the user signed in through passkeys, false otherwise.
*/
fun isSignedInThroughPasskeys(): Boolean {
return sharedPreference.getBoolean(IS_SIGNED_IN_THROUGH_PASSKEYS, false)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2025 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 All @@ -16,22 +16,17 @@

package com.google.credentialmanager.sample

import android.app.Activity
import android.content.Context
import androidx.appcompat.app.AlertDialog
import android.content.ContextWrapper

fun Context.readFromAsset(fileName: String): String {
var data = ""
this.assets.open(fileName).bufferedReader().use {
data = it.readText()
}
return data
}

fun Context.showErrorAlert(msg: String) {
AlertDialog.Builder(this)
.setTitle("An error occurred")
.setMessage(msg)
.setNegativeButton("Ok", null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show()
/**
* Finds the activity from a context.
*
* @return The activity if found, null otherwise.
*/
fun Context.findActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}

This file was deleted.

Loading