Improve security of automation (#73)

Require length of automation key at least 20
Random automation key generation
This commit is contained in:
BinTianqi
2024-09-08 09:00:04 +08:00
parent 3d5b9efd96
commit 47a965e6c0
3 changed files with 45 additions and 10 deletions

View File

@@ -17,9 +17,6 @@ class AutomationReceiver: BroadcastReceiver() {
fun handleTask(context: Context, intent: Intent): String { fun handleTask(context: Context, intent: Intent): String {
val sharedPrefs = context.getSharedPreferences("data", Context.MODE_PRIVATE) val sharedPrefs = context.getSharedPreferences("data", Context.MODE_PRIVATE)
val key = sharedPrefs.getString("automation_key", "") ?: "" val key = sharedPrefs.getString("automation_key", "") ?: ""
if(key.length < 6) {
return "Key length must longer than 6"
}
if(key != intent.getStringExtra("key")) { if(key != intent.getStringExtra("key")) {
return "Wrong key" return "Wrong key"
} }

View File

@@ -4,19 +4,32 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build.VERSION import android.os.Build.VERSION
import android.util.Base64
import android.widget.Toast import android.widget.Toast
import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme.typography import androidx.compose.material3.MaterialTheme.typography
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextField import androidx.compose.runtime.Composable
import androidx.compose.runtime.* import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
@@ -24,7 +37,11 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import com.bintianqi.owndroid.ui.* import com.bintianqi.owndroid.ui.Animations
import com.bintianqi.owndroid.ui.SubPageItem
import com.bintianqi.owndroid.ui.SwitchItem
import com.bintianqi.owndroid.ui.TopBar
import java.security.SecureRandom
@Composable @Composable
fun AppSetting(navCtrl:NavHostController, materialYou: MutableState<Boolean>, blackTheme: MutableState<Boolean>) { fun AppSetting(navCtrl:NavHostController, materialYou: MutableState<Boolean>, blackTheme: MutableState<Boolean>) {
@@ -144,16 +161,28 @@ private fun Automation() {
Text(text = stringResource(R.string.automation_api), style = typography.headlineLarge) Text(text = stringResource(R.string.automation_api), style = typography.headlineLarge)
Spacer(Modifier.padding(vertical = 5.dp)) Spacer(Modifier.padding(vertical = 5.dp))
var key by remember { mutableStateOf("") } var key by remember { mutableStateOf("") }
TextField( OutlinedTextField(
value = key, onValueChange = { key = it }, label = { Text("Key")}, value = key, onValueChange = { key = it }, label = { Text("Key")},
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth(),
trailingIcon = {
IconButton(
onClick = {
val charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
val sr = SecureRandom()
key = (1..20).map { charset[sr.nextInt(charset.length)] }.joinToString("")
}
) {
Icon(painter = painterResource(R.drawable.casino_fill0), contentDescription = "Random")
}
}
) )
Button( Button(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
onClick = { onClick = {
sharedPref.edit().putString("automation_key", key).apply() sharedPref.edit().putString("automation_key", key).apply()
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
} },
enabled = key.length >= 20
) { ) {
Text(stringResource(R.string.apply)) Text(stringResource(R.string.apply))
} }

View File

@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M300,720q25,0 42.5,-17.5T360,660q0,-25 -17.5,-42.5T300,600q-25,0 -42.5,17.5T240,660q0,25 17.5,42.5T300,720ZM300,360q25,0 42.5,-17.5T360,300q0,-25 -17.5,-42.5T300,240q-25,0 -42.5,17.5T240,300q0,25 17.5,42.5T300,360ZM480,540q25,0 42.5,-17.5T540,480q0,-25 -17.5,-42.5T480,420q-25,0 -42.5,17.5T420,480q0,25 17.5,42.5T480,540ZM660,720q25,0 42.5,-17.5T720,660q0,-25 -17.5,-42.5T660,600q-25,0 -42.5,17.5T600,660q0,25 17.5,42.5T660,720ZM660,360q25,0 42.5,-17.5T720,300q0,-25 -17.5,-42.5T660,240q-25,0 -42.5,17.5T600,300q0,25 17.5,42.5T660,360ZM200,840q-33,0 -56.5,-23.5T120,760v-560q0,-33 23.5,-56.5T200,120h560q33,0 56.5,23.5T840,200v560q0,33 -23.5,56.5T760,840L200,840ZM200,760h560v-560L200,200v560ZM200,200v560,-560Z"
android:fillColor="#000000"/>
</vector>