Skip to content

Commit

Permalink
Support biometric login
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Oct 26, 2022
1 parent e076cb2 commit 8892734
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 5 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 32
compileSdkVersion 33

defaultConfig {
applicationId 'org.traccar.manager'
minSdkVersion 19
targetSdkVersion 32
targetSdkVersion 33
versionCode 33
versionName '3.2'
multiDexEnabled true
Expand All @@ -29,6 +29,7 @@ dependencies {
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0'
implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.biometric:biometric-ktx:1.2.0-alpha05'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
googleImplementation platform('com.google.firebase:firebase-bom:29.0.3')
googleImplementation 'com.google.firebase:firebase-core'
Expand Down
3 changes: 1 addition & 2 deletions app/src/google/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.traccar.manager">
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".GoogleMainApplication"
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/org/traccar/manager/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,19 @@ class MainFragment : WebViewFragment() {
@JavascriptInterface
fun postMessage(message: String) {
if (message.startsWith("login")) {
if (message.length > 6) {
SecurityManager.saveToken(activity, message.substring(6))
}
broadcastManager.sendBroadcast(Intent(EVENT_LOGIN))
} else if (message.startsWith("authentication")) {
SecurityManager.readToken(activity) { token ->
if (token != null) {
val code = "handleLoginToken && handleLoginToken('$token')"
webView.evaluateJavascript(code, null)
}
}
} else if (message.startsWith("logout")) {
SecurityManager.deleteToken(activity)
} else if (message.startsWith("server")) {
val url = message.substring(7)
PreferenceManager.getDefaultSharedPreferences(activity)
Expand Down
75 changes: 75 additions & 0 deletions app/src/main/java/org/traccar/manager/SecurityManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2022 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.manager

import android.app.Activity
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import androidx.preference.PreferenceManager

object SecurityManager {

fun saveToken(activity: Activity, token: String) {
PreferenceManager.getDefaultSharedPreferences(activity)
.edit().putString(KEY_TOKEN, token).apply()
}

fun readToken(activity: Activity, onResult: (String?) -> Unit) {
val token = PreferenceManager.getDefaultSharedPreferences(activity)
.getString(KEY_TOKEN, null)
if (token != null) {
authenticate(activity) {
onResult(if (it) token else null)
}
}
onResult(null)
}

fun deleteToken(activity: Activity) {
PreferenceManager.getDefaultSharedPreferences(activity)
.edit().remove(KEY_TOKEN).apply()
}

private fun authenticate(activity: Activity, onResult: (Boolean) -> Unit) {
val executor = ContextCompat.getMainExecutor(activity)
val biometricPrompt = BiometricPrompt(
activity as FragmentActivity,
executor,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
onResult(false)
}

override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
onResult(true)
}

override fun onAuthenticationFailed() {
onResult(false)
}
},
)
val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(activity.getString(R.string.biometric_title))
.setNegativeButtonText(activity.getString(R.string.biometric_cancel))
.build()
activity.runOnUiThread { biometricPrompt.authenticate(promptInfo) }
}

private const val KEY_TOKEN = "managerToken"

}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
<string name="file_browser">File Browser</string>
<string name="notification_channel">Default</string>
<string name="permission_location_rationale">App requires location permission to show current location on the map</string>
<string name="biometric_title">Biometric login</string>
<string name="biometric_cancel">Use account password</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.2'
classpath 'com.android.tools.build:gradle:7.3.1'
classpath 'com.google.gms:google-services:4.3.13'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Expand Down

0 comments on commit 8892734

Please sign in to comment.