Browse Source

save email and password to encrypted preference

pull/46/head
Jannik 2 years ago
parent
commit
ea0caea91e
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 1
      app/build.gradle
  2. 2
      app/src/main/java/org/mosad/seil0/projectlaogai/MainActivity.kt
  3. 87
      app/src/main/java/org/mosad/seil0/projectlaogai/controller/login/LoginController.kt
  4. 7
      app/src/main/java/org/mosad/seil0/projectlaogai/fragments/SettingsFragment.kt
  5. 4
      app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/dialogs/AddSubjectDialog.kt
  6. 3
      app/src/main/res/values/strings.xml

1
app/build.gradle

@ -53,6 +53,7 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-rc1'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'androidx.security:security-crypto:1.1.0-alpha02'
implementation 'com.google.android.material:material:1.2.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.afollestad:aesthetic:1.0.0-beta05'

2
app/src/main/java/org/mosad/seil0/projectlaogai/MainActivity.kt

@ -48,6 +48,7 @@ import org.mosad.seil0.projectlaogai.controller.NFCMensaCard
import org.mosad.seil0.projectlaogai.controller.PreferencesController
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cColorAccent
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cColorPrimary
import org.mosad.seil0.projectlaogai.controller.login.LoginController
import org.mosad.seil0.projectlaogai.fragments.*
import kotlin.system.measureTimeMillis
@ -174,6 +175,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
val startupTime = measureTimeMillis {
PreferencesController.load(this) // load the settings, must be finished before doing anything else
CacheController(this) // load the cache
LoginController.load(this)
}
Log.i(className, "startup completed in $startupTime ms")
}

87
app/src/main/java/org/mosad/seil0/projectlaogai/controller/login/LoginController.kt

@ -22,6 +22,89 @@
package org.mosad.seil0.projectlaogai.controller.login
class LoginController {
// TODO implement
import android.content.Context
import android.content.SharedPreferences
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.util.Log
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import org.mosad.seil0.projectlaogai.R
object LoginController {
var email = ""
internal set
/**
* initially load the users email
*/
fun load(context: Context) {
with(getEncryptedPreferences(context)) {
email = this?.getString(
context.getString(R.string.save_key_user_email),
context.getString(R.string.sample_user)
).toString()
}
}
/**
* save user email and password to encrypted preference
*/
fun saveCredentials(email: String, password: String, context: Context) {
this.email = email
with (getEncryptedPreferences(context)?.edit()) {
this?.putString(context.getString(R.string.save_key_user_email), email)
this?.putString(context.getString(R.string.save_key_user_password), password)
this?.apply()
}
}
/**
* read user email and password from encrypted preference
*/
fun readCredentials(context: Context): Pair<String, String> {
return with (getEncryptedPreferences(context)) {
email = this?.getString(context.getString(R.string.save_key_user_email), "").toString()
Pair(
this?.getString(context.getString(R.string.save_key_user_email), "").toString(),
this?.getString(context.getString(R.string.save_key_user_password), "").toString()
)
}
}
/**
* create a encrypted shared preference
*/
private fun getEncryptedPreferences(context: Context): SharedPreferences? {
return try {
val spec = KeyGenParameterSpec.Builder(MasterKey.DEFAULT_MASTER_KEY_ALIAS,
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setKeySize(MasterKey.DEFAULT_AES_GCM_MASTER_KEY_SIZE)
.build()
val masterKey = MasterKey.Builder(context)
.setKeyGenParameterSpec(spec)
.build()
EncryptedSharedPreferences.create(
context,
context.getString(R.string.encrypted_preference_file_key),
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
} catch (ex: Exception) {
Log.e(javaClass.name, "Could not create encrypted shared preference.", ex)
null
}
}
}

7
app/src/main/java/org/mosad/seil0/projectlaogai/fragments/SettingsFragment.kt

@ -55,6 +55,7 @@ import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cCourse
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cShowBuffet
import org.mosad.seil0.projectlaogai.controller.cache.TimetableController
import org.mosad.seil0.projectlaogai.controller.login.LoginController
import org.mosad.seil0.projectlaogai.uicomponents.dialogs.LoginDialog
import org.mosad.seil0.projectlaogai.util.DataTypes
import java.util.*
@ -104,6 +105,7 @@ class SettingsFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
// initialize the settings gui
txtView_User.text = LoginController.email
txtView_Course.text = cCourse.courseName
txtView_AboutDesc.text = resources.getString(R.string.about_version, BuildConfig.VERSION_NAME, getString(R.string.build_time))
switch_buffet.isChecked = cShowBuffet // init switch
@ -139,10 +141,11 @@ class SettingsFragment : Fragment() {
LoginDialog(context!!)
.positiveButton {
println("Test: $password")
LoginController.saveCredentials(email, password, context)
}
.show {
email = resources.getString(R.string.sample_user)
password = "Test123"
email = LoginController.email
password = ""
}
}

4
app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/dialogs/AddSubjectDialog.kt

@ -50,8 +50,8 @@ class AddSubjectDialog(val context: Context) {
private val dialog = MaterialDialog(context, BottomSheet())
private var spinnerCourses: Spinner
private var spinnerSubjects: Spinner
private val spinnerCourses: Spinner
private val spinnerSubjects: Spinner
private val subjectsList = ArrayList<String>()
private val courseNamesList = getCourseNames()

3
app/src/main/res/values/strings.xml

@ -98,6 +98,7 @@
<!-- save keys -->
<string name="preference_file_key" translatable="false">org.mosad.seil0.projectlaogai_preferences</string>
<string name="encrypted_preference_file_key" translatable="false">org.mosad.seil0.projectlaogai_encrypted_preferences</string>
<string name="save_key_course" translatable="false">org.mosad.seil0.projectlaogai.course</string>
<string name="save_key_courseTTLink" translatable="false">org.mosad.seil0.projectlaogai.courseTTLink</string>
<string name="save_key_colorPrimary" translatable="false">org.mosad.seil0.projectlaogai.colorPrimary</string>
@ -106,6 +107,8 @@
<string name="save_key_coursesCacheTime" translatable="false">org.mosad.seil0.projectlaogai.coursesCacheTime</string>
<string name="save_key_mensaCacheTime" translatable="false">org.mosad.seil0.projectlaogai.mensaCacheTime</string>
<string name="save_key_timetableCacheTime" translatable="false">org.mosad.seil0.projectlaogai.timetableCacheTime</string>
<string name="save_key_user_email" translatable="false">org.mosad.seil0.projectlaogai.user_email</string>
<string name="save_key_user_password" translatable="false">org.mosad.seil0.projectlaogai.user_password</string>
<string-array name="courses">

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close