Skip to content

Commit 3889148

Browse files
committed
Extract common code from CredentialManagerUtils
1 parent 203298f commit 3889148

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

Shrine/app/src/main/java/com/authentication/shrine/CredentialManagerUtils.kt

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ class CredentialManagerUtils @Inject constructor(
6262
) {
6363

6464
private val TAG = "CredentialManagerUtils"
65+
66+
private object JSON_KEYS {
67+
const val RP_ID = "rpId"
68+
const val CREDENTIAL_ID = "credentialId"
69+
const val USER_ID = "userId"
70+
const val ALL_ACCEPTED_CREDENTIAL_IDS = "allAcceptedCredentialIds"
71+
const val NAME = "name"
72+
const val DISPLAY_NAME = "displayName"
73+
}
74+
6575
/**
6676
* Retrieves a passkey or password credential from the credential manager.
6777
*
@@ -270,8 +280,8 @@ class CredentialManagerUtils @Inject constructor(
270280
credentialManager.signalCredentialState(
271281
request = SignalUnknownCredentialRequest(
272282
requestJson = JSONObject().apply {
273-
put("rpId", rpId)
274-
put("credentialId", credentialId)
283+
put(JSON_KEYS.RP_ID, rpId)
284+
put(JSON_KEYS.CREDENTIAL_ID, credentialId)
275285
}.toString()
276286
),
277287
)
@@ -282,20 +292,13 @@ class CredentialManagerUtils @Inject constructor(
282292
suspend fun signalAcceptedIds(
283293
credentialIds: List<String>,
284294
) {
285-
val rpId = dataStore.read(RP_ID_KEY)
286-
val userId = dataStore.read(USER_ID_KEY)
287-
288-
if (rpId.isNullOrBlank()) {
289-
Log.e(TAG, "RP ID not present")
290-
} else if (userId.isNullOrBlank()) {
291-
Log.e(TAG, "User ID not present")
292-
} else {
295+
fetchDataAndPerformAction { rpId, userId ->
293296
credentialManager.signalCredentialState(
294297
request = SignalAllAcceptedCredentialIdsRequest(
295298
requestJson = JSONObject().apply {
296-
put("rpId", rpId)
297-
put("userId", userId)
298-
put("allAcceptedCredentialIds", JSONArray(credentialIds))
299+
put(JSON_KEYS.RP_ID, rpId)
300+
put(JSON_KEYS.USER_ID, userId)
301+
put(JSON_KEYS.ALL_ACCEPTED_CREDENTIAL_IDS, JSONArray(credentialIds))
299302
}.toString()
300303
),
301304
)
@@ -306,6 +309,23 @@ class CredentialManagerUtils @Inject constructor(
306309
suspend fun signalUserDetails(
307310
newName: String,
308311
newDisplayName: String,
312+
) {
313+
fetchDataAndPerformAction { rpId, userId ->
314+
credentialManager.signalCredentialState(
315+
request = SignalCurrentUserDetailsRequest(
316+
requestJson = JSONObject().apply {
317+
put(JSON_KEYS.RP_ID, rpId)
318+
put(JSON_KEYS.USER_ID, userId)
319+
put(JSON_KEYS.NAME, newName)
320+
put(JSON_KEYS.DISPLAY_NAME, newDisplayName)
321+
}.toString()
322+
),
323+
)
324+
}
325+
}
326+
327+
suspend fun fetchDataAndPerformAction(
328+
credentialManagerAction: suspend (rpId: String, userId: String) -> Unit
309329
) {
310330
val rpId = dataStore.read(RP_ID_KEY)
311331
val userId = dataStore.read(USER_ID_KEY)
@@ -315,16 +335,7 @@ class CredentialManagerUtils @Inject constructor(
315335
} else if (userId.isNullOrBlank()) {
316336
Log.e(TAG, "User ID not present")
317337
} else {
318-
credentialManager.signalCredentialState(
319-
request = SignalCurrentUserDetailsRequest(
320-
requestJson = JSONObject().apply {
321-
put("rpId", rpId)
322-
put("userId", userId)
323-
put("name", newName)
324-
put("displayName", newDisplayName)
325-
}.toString()
326-
),
327-
)
338+
credentialManagerAction(rpId, userId)
328339
}
329340
}
330341
}

0 commit comments

Comments
 (0)