@@ -8,11 +8,13 @@ package org.microg.gms.fido.core
88import android.content.ContentValues
99import android.content.Context
1010import android.database.sqlite.SQLiteDatabase
11- import android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE
1211import android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE
1312import android.database.sqlite.SQLiteOpenHelper
13+ import android.util.Log
1414import androidx.core.database.getLongOrNull
15+ import androidx.core.database.getStringOrNull
1516import org.microg.gms.fido.core.transport.Transport
17+ import org.microg.gms.fido.core.ui.TAG
1618
1719class Database (context : Context ) : SQLiteOpenHelper(context, " fido.db" , null , VERSION ) {
1820
@@ -31,6 +33,23 @@ class Database(context: Context) : SQLiteOpenHelper(context, "fido.db", null, VE
3133 }
3234 }
3335
36+ fun getKnownRegistrationInfo (rpId : String ) = readableDatabase.use {
37+ val cursor = it.query(
38+ TABLE_KNOWN_REGISTRATIONS , arrayOf(COLUMN_CREDENTIAL_ID , COLUMN_REGISTER_USER , COLUMN_TRANSPORT ), " $COLUMN_RP_ID =?" , arrayOf(rpId), null , null , null
39+ )
40+ val result = mutableListOf<CredentialUserInfo >()
41+ cursor.use { c ->
42+ while (c.moveToNext()) {
43+ val credentialId = c.getString(0 )
44+ val userJson = c.getStringOrNull(1 ) ? : continue
45+ val transport = c.getStringOrNull(2 ) ? : continue
46+ Log .d(TAG , " getKnownRegistrationInfo: credential: $credentialId user: $userJson transport: $transport " )
47+ result.add(CredentialUserInfo (credentialId, userJson, Transport .valueOf(transport)))
48+ }
49+ }
50+ result
51+ }
52+
3453 fun insertPrivileged (packageName : String , signatureDigest : String ) = writableDatabase.use {
3554 it.insertWithOnConflict(TABLE_PRIVILEGED_APPS , null , ContentValues ().apply {
3655 put(COLUMN_PACKAGE_NAME , packageName)
@@ -39,13 +58,33 @@ class Database(context: Context) : SQLiteOpenHelper(context, "fido.db", null, VE
3958 }, CONFLICT_REPLACE )
4059 }
4160
42- fun insertKnownRegistration (rpId : String , credentialId : String , transport : Transport ) = writableDatabase.use {
43- it.insertWithOnConflict( TABLE_KNOWN_REGISTRATIONS , null , ContentValues (). apply {
44- put( COLUMN_RP_ID , rpId)
61+ fun insertKnownRegistration (rpId : String , credentialId : String , transport : Transport , userJson : String? = null ) = writableDatabase.use {
62+ Log .d( TAG , " insertKnownRegistration: $rpId $credentialId $transport $userJson " )
63+ val values = ContentValues (). apply {
4564 put(COLUMN_CREDENTIAL_ID , credentialId)
4665 put(COLUMN_TRANSPORT , transport.name)
4766 put(COLUMN_TIMESTAMP , System .currentTimeMillis())
48- }, CONFLICT_REPLACE )
67+ if (userJson != null ) {
68+ put(COLUMN_REGISTER_USER , userJson)
69+ }
70+ }
71+
72+ val updated = if (userJson == null ) {
73+ it.update(TABLE_KNOWN_REGISTRATIONS , values, " $COLUMN_RP_ID = ? AND $COLUMN_CREDENTIAL_ID = ?" , arrayOf(rpId, credentialId))
74+ } else {
75+ it.update(TABLE_KNOWN_REGISTRATIONS , values, " $COLUMN_RP_ID = ? AND $COLUMN_REGISTER_USER = ?" , arrayOf(rpId, userJson))
76+ }
77+
78+ if (updated == 0 ) {
79+ val insertValues = ContentValues ().apply {
80+ put(COLUMN_RP_ID , rpId)
81+ put(COLUMN_CREDENTIAL_ID , credentialId)
82+ put(COLUMN_TRANSPORT , transport.name)
83+ put(COLUMN_TIMESTAMP , System .currentTimeMillis())
84+ userJson?.let { json -> put(COLUMN_REGISTER_USER , json) }
85+ }
86+ it.insert(TABLE_KNOWN_REGISTRATIONS , null , insertValues)
87+ }
4988 }
5089
5190 override fun onCreate (db : SQLiteDatabase ) {
@@ -59,10 +98,13 @@ class Database(context: Context) : SQLiteOpenHelper(context, "fido.db", null, VE
5998 if (oldVersion < 2 ) {
6099 db.execSQL(" CREATE TABLE $TABLE_KNOWN_REGISTRATIONS ($COLUMN_RP_ID TEXT, $COLUMN_CREDENTIAL_ID TEXT, $COLUMN_TRANSPORT TEXT, $COLUMN_TIMESTAMP INT, UNIQUE($COLUMN_RP_ID , $COLUMN_CREDENTIAL_ID ) ON CONFLICT REPLACE)" )
61100 }
101+ if (oldVersion < 3 ) {
102+ db.execSQL(" ALTER TABLE $TABLE_KNOWN_REGISTRATIONS ADD COLUMN $COLUMN_REGISTER_USER TEXT" )
103+ }
62104 }
63105
64106 companion object {
65- const val VERSION = 2
107+ const val VERSION = 3
66108 private const val TABLE_PRIVILEGED_APPS = " privileged_apps"
67109 private const val TABLE_KNOWN_REGISTRATIONS = " known_registrations"
68110 private const val COLUMN_PACKAGE_NAME = " package_name"
@@ -71,6 +113,7 @@ class Database(context: Context) : SQLiteOpenHelper(context, "fido.db", null, VE
71113 private const val COLUMN_RP_ID = " rp_id"
72114 private const val COLUMN_CREDENTIAL_ID = " credential_id"
73115 private const val COLUMN_TRANSPORT = " transport"
116+ private const val COLUMN_REGISTER_USER = " register_user"
74117 }
75118}
76119
0 commit comments