Many fixed and improvements related to asking passwords

This commit is contained in:
Juha Heinanen
2023-03-14 12:37:03 +02:00
parent 26f670422e
commit e1fdb8ecbd
6 changed files with 64 additions and 111 deletions

View File

@ -87,7 +87,7 @@ class Account(val accp: Long) {
if (authUser != "") res += ";auth_user=\"${authUser}\""
if ((authPass != "") && !MainActivity.aorPasswords.containsKey(aor))
if ((authPass != "") && !BaresipService.aorPasswords.containsKey(aor))
res += ";auth_pass=\"${authPass}\""
if (outbound.size > 0) {
@ -142,7 +142,8 @@ class Account(val accp: Long) {
if (!callHistory)
extra += ";call_history=no"
extra += ";tel_provider=${URLEncoder.encode(telProvider, "UTF-8")}"
if (telProvider != "")
extra += ";tel_provider=${URLEncoder.encode(telProvider, "UTF-8")}"
if (countryCode != "")
extra += ";country_code=$countryCode"

View File

@ -227,7 +227,7 @@ class AccountActivity : AppCompatActivity() {
displayName.setText(acc.displayName)
authUser.setText(acc.authUser)
if (MainActivity.aorPasswords.containsKey(aor) || acc.authPass == NO_AUTH_PASS)
if (BaresipService.aorPasswords[aor] != null || acc.authPass == NO_AUTH_PASS)
authPass.setText("")
else
authPass.setText(acc.authPass)
@ -351,7 +351,6 @@ class AccountActivity : AppCompatActivity() {
vmUri.setText(acc.vmUri)
defaultCheck.isChecked = uaIndex == 0
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -431,31 +430,31 @@ class AccountActivity : AppCompatActivity() {
if (ap != "") {
if (ap != acc.authPass) {
if (Account.checkAuthPass(ap)) {
setAuthPass(acc, ap)
MainActivity.aorPasswords.remove(aor)
if (Api.account_set_auth_pass(acc.accp, ap) == 0) {
acc.authPass = Api.account_auth_pass(acc.accp)
save = true
if (acc.regint > 0)
reRegister = true
} else {
Log.e(TAG, "Setting of auth pass failed")
}
BaresipService.aorPasswords.remove(acc.aor)
} else {
Utils.alertView(this, getString(R.string.notice),
String.format(getString(R.string.invalid_authentication_password), ap))
return false
}
} else {
if (MainActivity.aorPasswords.containsKey(aor)) {
MainActivity.aorPasswords.remove(aor)
save = true
}
BaresipService.aorPasswords.remove(acc.aor)
}
} else { // ap == ""
if (acc.authUser != "") {
if (!MainActivity.aorPasswords.containsKey(aor)) {
setAuthPass(acc, "")
MainActivity.aorPasswords[aor] = ""
if (acc.authPass != NO_AUTH_PASS &&
acc.authPass != BaresipService.aorPasswords[acc.aor])
if (Api.account_set_auth_pass(acc.accp, "") == 0) {
acc.authPass = NO_AUTH_PASS
BaresipService.aorPasswords[aor] = NO_AUTH_PASS
save = true
}
} else {
if (acc.authPass != "") {
setAuthPass(acc, "")
MainActivity.aorPasswords.remove(aor)
}
}
}
val ob = ArrayList<String>()
@ -505,8 +504,7 @@ class AccountActivity : AppCompatActivity() {
(regCheck.isChecked && newConfiguredRegInt != acc.configuredRegInt)
if (reReg) {
if (Api.account_set_regint(acc.accp,
if (regCheck.isChecked) newConfiguredRegInt else 0
) != 0) {
if (regCheck.isChecked) newConfiguredRegInt else 0) != 0) {
Log.e(TAG, "Setting of regint failed")
} else {
acc.regint = Api.account_regint(acc.accp)
@ -833,17 +831,6 @@ class AccountActivity : AppCompatActivity() {
finish()
}
private fun setAuthPass(acc: Account, ap: String) {
if (Api.account_set_auth_pass(acc.accp, ap) == 0) {
acc.authPass = Api.account_auth_pass(acc.accp)
MainActivity.aorPasswords.remove(acc.aor)
save = true
reRegister = true
} else {
Log.e(TAG, "Setting of auth pass failed")
}
}
private fun checkOutboundUri(uri: String): Boolean {
if (!uri.startsWith("sip:")) return false
return Utils.checkHostPortParams(uri.substring(4))

View File

@ -3,15 +3,11 @@ package com.tutpro.baresip
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.tutpro.baresip.databinding.ActivityAccountsBinding
class AccountsActivity : AppCompatActivity() {
@ -41,15 +37,7 @@ class AccountsActivity : AppCompatActivity() {
listView.adapter = alAdapter
val accountRequest =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
val aor = it.data!!.getStringExtra("aor")!!
val ua = UserAgent.ofAor(aor)!!
if (MainActivity.aorPasswords.containsKey(aor) &&
MainActivity.aorPasswords[aor] == "")
askPassword(ua)
}
}
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {}
val addAccountButton = binding.addAccount
val newAorView = binding.newAor
@ -95,35 +83,6 @@ class AccountsActivity : AppCompatActivity() {
}
private fun askPassword(ua: UserAgent) {
val layout = LayoutInflater.from(this)
.inflate(R.layout.password_dialog, findViewById(android.R.id.content),
false)
val titleView = layout.findViewById(R.id.title) as TextView
titleView.text = getString(R.string.authentication_password)
val messageView = layout.findViewById(R.id.message) as TextView
val message = getString(R.string.account) + " " + Utils.plainAor(ua.account.aor)
messageView.text = message
val input = layout.findViewById(R.id.password) as EditText
with (MaterialAlertDialogBuilder(this, R.style.AlertDialogTheme)) {
setView(layout)
setPositiveButton(android.R.string.ok) { dialog, _ ->
dialog.dismiss()
var password = input.text.toString().trim()
if (!Account.checkAuthPass(password)) {
Utils.alertView(this@AccountsActivity, getString(R.string.notice),
String.format(getString(R.string.invalid_authentication_password), password))
password = ""
}
setAuthPass(ua, password)
}
setNeutralButton(android.R.string.cancel) { dialog, _ ->
dialog.cancel()
}
show()
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (BaresipService.activities.indexOf("accounts,$aor") == -1)
@ -180,7 +139,7 @@ class AccountsActivity : AppCompatActivity() {
accounts = accounts + a.print() + "\n"
}
Utils.putFileContents(BaresipService.filesPath + "/accounts", accounts.toByteArray())
// Log.d(TAG, "Saved accounts '${accounts}' to '${BaresipService.filesPath}/accounts'")
// Log.e(TAG, "Saved accounts '${accounts}' to '${BaresipService.filesPath}/accounts'")
}
fun noAccounts(): Boolean {
@ -188,17 +147,6 @@ class AccountsActivity : AppCompatActivity() {
return contents == null || contents.isEmpty()
}
fun setAuthPass(ua: UserAgent, ap: String) {
val acc = ua.account
if (Api.account_set_auth_pass(acc.accp, ap) == 0) {
acc.authPass = Api.account_auth_pass(acc.accp)
MainActivity.aorPasswords[acc.aor] = ap
if ((ap != "") && (acc.regint > 0))
Api.ua_register(ua.uap)
} else {
Log.e(TAG, "Setting of auth pass failed")
}
}
}
}

View File

@ -564,14 +564,16 @@ class BaresipService: Service() {
val acc = ua.account
if (acc.authUser != "" && acc.authPass == NO_AUTH_PASS) {
val password = if (MainActivity.aorPasswords[acc.aor] != null)
MainActivity.aorPasswords[acc.aor]!!
else
""
Api.account_set_auth_pass(acc.accp, password)
acc.authPass = Api.account_auth_pass(ua.account.accp)
val password = aorPasswords[acc.aor]
if (password != null) {
Api.account_set_auth_pass(acc.accp, password)
acc.authPass = password
} else {
Api.account_set_auth_pass(acc.accp, NO_AUTH_PASS)
}
}
Log.d(TAG, "got uaEvent $event/${acc.aor}")
return
}
@ -1527,6 +1529,8 @@ class BaresipService: Service() {
var dnsServers = listOf<InetAddress>()
val serviceEvent = MutableLiveData<Event<Long>>()
val serviceEvents = mutableListOf<ServiceEvent>()
// <aor, password> of those accounts that have auth username without auth password
val aorPasswords = mutableMapOf<String, String>()
private var audioFocusRequest: AudioFocusRequestCompat? = null
private var btAdapter: BluetoothAdapter? = null

View File

@ -175,9 +175,6 @@ class CallsActivity : AppCompatActivity() {
onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
File("${BaresipService.filesPath}/recordings").walk().forEach { f ->
Log.d(TAG, "***** recording ${f.name}")
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {

View File

@ -11,7 +11,6 @@ import android.content.Intent.ACTION_CALL
import android.content.pm.PackageManager
import android.content.res.Configuration.ORIENTATION_PORTRAIT
import android.media.AudioAttributes
import android.media.AudioDeviceInfo
import android.media.AudioManager
import android.net.Uri
import android.os.*
@ -250,7 +249,7 @@ class MainActivity : AppCompatActivity() {
val ua = UserAgent.ofAor(activityAor)!!
updateIcons(ua.account)
if (it.resultCode == Activity.RESULT_OK)
if (aorPasswords.containsKey(activityAor) && aorPasswords[activityAor] == "")
if (BaresipService.aorPasswords[activityAor] == NO_AUTH_PASS)
askPassword(getString(R.string.authentication_password), ua)
}
@ -288,11 +287,11 @@ class MainActivity : AppCompatActivity() {
val ua = UserAgent.ofAor(aorSpinner.tag.toString())
if (ua != null) {
val acc = ua.account
if (Api.account_regint(acc.accp) == REGISTRATION_INTERVAL) {
if (Api.account_regint(acc.accp) > 0) {
Api.account_set_regint(acc.accp, 0)
Api.ua_unregister(ua.uap)
} else {
Api.account_set_regint(acc.accp, REGISTRATION_INTERVAL)
Api.account_set_regint(acc.accp, acc.configuredRegInt)
Api.ua_register(ua.uap)
}
acc.regint = Api.account_regint(acc.accp)
@ -1580,7 +1579,6 @@ class MainActivity : AppCompatActivity() {
}
val input = layout.findViewById(R.id.password) as EditText
input.requestFocus()
val context = this
with (MaterialAlertDialogBuilder(this, R.style.AlertDialogTheme)) {
setView(layout)
setPositiveButton(android.R.string.ok) { dialog, _ ->
@ -1588,8 +1586,11 @@ class MainActivity : AppCompatActivity() {
dialog.dismiss()
var password = input.text.toString().trim()
if (!Account.checkAuthPass(password)) {
Utils.alertView(context, getString(R.string.notice),
String.format(getString(R.string.invalid_authentication_password), password))
Toast.makeText(
applicationContext,
String.format(getString(R.string.invalid_authentication_password), password),
Toast.LENGTH_SHORT
).show()
password = ""
}
when (title) {
@ -1598,7 +1599,17 @@ class MainActivity : AppCompatActivity() {
getString(R.string.decrypt_password) ->
if (password != "") restore(password)
else ->
AccountsActivity.setAuthPass(ua!!, password)
if (password == "") {
askPassword(title, ua!!)
} else {
Api.account_set_auth_pass(ua!!.account.accp, password)
ua.account.authPass = Api.account_auth_pass(ua.account.accp)
BaresipService.aorPasswords[ua.account.aor] = ua.account.authPass
if (ua.account.regint == 0)
Api.ua_unregister(ua.uap)
else
Api.ua_register(ua.uap)
}
}
}
setNeutralButton(android.R.string.cancel) { dialog, _ ->
@ -1606,6 +1617,7 @@ class MainActivity : AppCompatActivity() {
dialog.cancel()
}
val dialog = this.create()
dialog.setCanceledOnTouchOutside(false)
dialog.window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
dialog.show()
}
@ -1628,28 +1640,34 @@ class MainActivity : AppCompatActivity() {
messageView.text = message
val input = layout.findViewById(R.id.password) as EditText
input.requestFocus()
val context = this
with (MaterialAlertDialogBuilder(this, R.style.AlertDialogTheme)) {
setView(layout)
setPositiveButton(android.R.string.ok) { dialog, _ ->
imm.hideSoftInputFromWindow(input.windowToken, 0)
dialog.dismiss()
val password = input.text.toString().trim()
if (!Account.checkAuthPass(password)) {
Utils.alertView(context, getString(R.string.notice),
String.format(getString(R.string.invalid_authentication_password), password))
Toast.makeText(
applicationContext,
String.format(getString(R.string.invalid_authentication_password),
password),
Toast.LENGTH_SHORT
).show()
accounts.add(0, account)
} else {
aorPasswords[aor] = password
BaresipService.aorPasswords[aor] = password
}
askPasswords(accounts)
}
setNeutralButton(android.R.string.cancel) { dialog, _ ->
imm.hideSoftInputFromWindow(input.windowToken, 0)
dialog.cancel()
aorPasswords[aor] = ""
BaresipService.aorPasswords[aor] = NO_AUTH_PASS
askPasswords(accounts)
}
val dialog = this.create()
dialog.setCanceledOnTouchOutside(false)
dialog.window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
dialog.show()
}
@ -2199,8 +2217,6 @@ class MainActivity : AppCompatActivity() {
var accountRequest: ActivityResultLauncher<Intent>? = null
var activityAor = ""
// <aor, password> of those accounts that have auth username without auth password
val aorPasswords = mutableMapOf<String, String>()
}