-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from meik99/manage-student-data
implemented managing student data
- Loading branch information
Showing
10 changed files
with
263 additions
and
70 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/rynkbit/jku/stuka/identity/AccessControlProfile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.rynkbit.jku.stuka.identity | ||
|
||
import androidx.security.identity.AccessControlProfile | ||
import androidx.security.identity.AccessControlProfileId | ||
|
||
class AccessControlProfile { | ||
val accessControlProfileId = AccessControlProfileId(1) | ||
val accessControlProfile = AccessControlProfile.Builder(accessControlProfileId) | ||
.setUserAuthenticationRequired(false) | ||
.build() | ||
} |
2 changes: 1 addition & 1 deletion
2
.../stuka/AgnosticIdentityCredentialStore.kt → ...entity/AgnosticIdentityCredentialStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/rynkbit/jku/stuka/identity/StudentProfile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.rynkbit.jku.stuka.identity | ||
|
||
import androidx.security.identity.PersonalizationData | ||
|
||
const val CREDENTIAL_NAMESPACE = "STUKA" | ||
const val CREDENTIAL_STUDENT_FIRSTNAME = "FIRSTNAME" | ||
const val CREDENTIAL_STUDENT_LASTNAME = "LASTNAME" | ||
const val CREDENTIAL_STUDENT_BIRTHDATE = "BIRTHDATE" | ||
const val CREDENTIAL_STUDENT_MATRICULATION_NUMBER = "MATRICULATION_NUMBER" | ||
const val CREDENTIAL_STUDENT_STUDY_CODE = "STUDY_CODE" | ||
|
||
class StudentProfile( | ||
val firstname: String = "", | ||
val lastname: String = "", | ||
val birthdate: String = "", | ||
val matriculationNumber: String = "", | ||
val studyCode: String = "", | ||
val studentAccessControlProfile: AccessControlProfile = AccessControlProfile(), | ||
) { | ||
fun toPersonalizationData() = | ||
PersonalizationData.Builder() | ||
.addAccessControlProfile(studentAccessControlProfile.accessControlProfile) | ||
.putEntry(CREDENTIAL_STUDENT_FIRSTNAME, firstname) | ||
.putEntry(CREDENTIAL_STUDENT_LASTNAME, lastname) | ||
.putEntry(CREDENTIAL_STUDENT_BIRTHDATE, birthdate) | ||
.putEntry(CREDENTIAL_STUDENT_MATRICULATION_NUMBER, matriculationNumber) | ||
.putEntry(CREDENTIAL_STUDENT_STUDY_CODE, studyCode) | ||
.build() | ||
|
||
private fun PersonalizationData.Builder.putEntry( | ||
key: String, | ||
value: String | ||
): PersonalizationData.Builder = | ||
this.putEntryString( | ||
CREDENTIAL_NAMESPACE, | ||
key, | ||
listOf(studentAccessControlProfile.accessControlProfileId), | ||
value | ||
) | ||
} |
101 changes: 101 additions & 0 deletions
101
app/src/main/java/com/rynkbit/jku/stuka/identity/StudentStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.rynkbit.jku.stuka.identity | ||
|
||
import android.content.Context | ||
import androidx.security.identity.IdentityCredential | ||
import androidx.security.identity.IdentityCredentialStore | ||
import androidx.security.identity.ResultData | ||
import java.lang.UnsupportedOperationException | ||
|
||
const val CREDENTIAL_NAME = "STUDENT" | ||
const val DOC_TYPE = "CARD" | ||
const val IDENTITY_FEATURE = "android.hardware.identity_credential" | ||
|
||
class StudentStore( | ||
context: Context, | ||
private val agnosticIdentityCredentialStore: AgnosticIdentityCredentialStore = AgnosticIdentityCredentialStore( | ||
context | ||
) | ||
) { | ||
|
||
init { | ||
|
||
} | ||
|
||
fun store(student: StudentProfile) { | ||
val credentialStore = agnosticIdentityCredentialStore.credentialStore | ||
val credentials = credentialStore.getCredentialByName( | ||
CREDENTIAL_NAME, | ||
IdentityCredentialStore.CIPHERSUITE_ECDHE_HKDF_ECDSA_WITH_AES_256_GCM_SHA256 | ||
) | ||
|
||
if (credentials == null) { | ||
createStudent(student) | ||
} else { | ||
updateStudent(student, credentials) | ||
} | ||
} | ||
|
||
fun get(): StudentProfile { | ||
val credentialStore = agnosticIdentityCredentialStore.credentialStore | ||
val credentials = credentialStore.getCredentialByName( | ||
CREDENTIAL_NAME, | ||
IdentityCredentialStore.CIPHERSUITE_ECDHE_HKDF_ECDSA_WITH_AES_256_GCM_SHA256 | ||
) | ||
|
||
if (credentials != null) { | ||
val results = credentials.getEntries( | ||
null, | ||
mapOf( | ||
Pair( | ||
CREDENTIAL_NAMESPACE, listOf( | ||
CREDENTIAL_STUDENT_FIRSTNAME, | ||
CREDENTIAL_STUDENT_LASTNAME, | ||
CREDENTIAL_STUDENT_BIRTHDATE, | ||
CREDENTIAL_STUDENT_MATRICULATION_NUMBER, | ||
CREDENTIAL_STUDENT_STUDY_CODE | ||
) | ||
) | ||
), | ||
null | ||
) | ||
return results.toStudent() | ||
} | ||
|
||
return StudentProfile() | ||
} | ||
|
||
private fun createStudent(student: StudentProfile) { | ||
val credentialStore = agnosticIdentityCredentialStore.credentialStore | ||
val credentials = credentialStore.createCredential(CREDENTIAL_NAME, DOC_TYPE) | ||
credentials.personalize(student.toPersonalizationData()) | ||
} | ||
|
||
private fun updateStudent(student: StudentProfile, credentials: IdentityCredential) { | ||
val credentialStore = agnosticIdentityCredentialStore.credentialStore | ||
|
||
when { | ||
credentialStore.capabilities.isUpdateSupported -> { | ||
credentials.update(student.toPersonalizationData()) | ||
} | ||
credentialStore.capabilities.isDeleteSupported -> { | ||
deleteStudent(credentials) | ||
createStudent(student) | ||
} | ||
else -> { | ||
throw UnsupportedOperationException() | ||
} | ||
} | ||
} | ||
|
||
private fun deleteStudent(credentials: IdentityCredential) { | ||
credentials.delete(ByteArray(1)) | ||
} | ||
|
||
private fun ResultData.toStudent(): StudentProfile = StudentProfile( | ||
getEntryString(CREDENTIAL_NAMESPACE, CREDENTIAL_STUDENT_FIRSTNAME) ?: "", | ||
getEntryString(CREDENTIAL_NAMESPACE, CREDENTIAL_STUDENT_LASTNAME) ?: "", | ||
getEntryString(CREDENTIAL_NAMESPACE, CREDENTIAL_STUDENT_BIRTHDATE) ?: "", | ||
getEntryString(CREDENTIAL_NAMESPACE, CREDENTIAL_STUDENT_MATRICULATION_NUMBER) ?: "", | ||
getEntryString(CREDENTIAL_NAMESPACE, CREDENTIAL_STUDENT_STUDY_CODE) ?: "" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/rynkbit/jku/stuka/ui/edit/EditDataViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,25 @@ | ||
package com.rynkbit.jku.stuka.ui.edit | ||
|
||
import androidx.lifecycle.ViewModel | ||
import com.rynkbit.jku.stuka.identity.StudentProfile | ||
|
||
class EditDataViewModel : ViewModel() { | ||
var firstname: String = "" | ||
var lastname: String = "" | ||
var birthdate: String = "" | ||
var matriculationNumber: String = "" | ||
var studyCode: String = "" | ||
|
||
val studentProfile | ||
get() = StudentProfile( | ||
firstname, lastname, birthdate, matriculationNumber, studyCode | ||
) | ||
|
||
fun applyStudentProfile(studentProfile: StudentProfile) { | ||
firstname = studentProfile.firstname | ||
lastname = studentProfile.lastname | ||
birthdate = studentProfile.birthdate | ||
matriculationNumber = studentProfile.matriculationNumber | ||
studyCode = studentProfile.studyCode | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
app/src/main/java/com/rynkbit/jku/stuka/ui/main/MainViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package com.rynkbit.jku.stuka.ui.main | ||
|
||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import com.rynkbit.jku.stuka.identity.StudentProfile | ||
|
||
class MainViewModel : ViewModel() { | ||
var student = MutableLiveData<StudentProfile>() | ||
} |
Oops, something went wrong.