Introduce no-auth mode for connections (#768)

Fixes #758
Fixes #526
This commit is contained in:
Harsh Shandilya 2020-05-08 20:50:04 +05:30 committed by GitHub
parent ed3312b303
commit 4c461fb174
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 114 additions and 86 deletions

View file

@ -6,6 +6,7 @@ package com.zeapo.pwdstore
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
import android.view.LayoutInflater import android.view.LayoutInflater
@ -19,12 +20,14 @@ import androidx.appcompat.view.ActionMode
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.observe import androidx.lifecycle.observe
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.zeapo.pwdstore.databinding.PasswordRecyclerViewBinding import com.zeapo.pwdstore.databinding.PasswordRecyclerViewBinding
import com.zeapo.pwdstore.git.BaseGitActivity import com.zeapo.pwdstore.git.BaseGitActivity
import com.zeapo.pwdstore.git.GitOperationActivity import com.zeapo.pwdstore.git.GitOperationActivity
import com.zeapo.pwdstore.git.GitServerConfigActivity import com.zeapo.pwdstore.git.GitServerConfigActivity
import com.zeapo.pwdstore.git.config.ConnectionMode
import com.zeapo.pwdstore.ui.OnOffItemAnimator import com.zeapo.pwdstore.ui.OnOffItemAnimator
import com.zeapo.pwdstore.ui.adapters.PasswordItemRecyclerAdapter import com.zeapo.pwdstore.ui.adapters.PasswordItemRecyclerAdapter
import com.zeapo.pwdstore.ui.dialogs.ItemCreationBottomSheet import com.zeapo.pwdstore.ui.dialogs.ItemCreationBottomSheet
@ -37,6 +40,7 @@ import me.zhanghai.android.fastscroll.FastScrollerBuilder
class PasswordFragment : Fragment() { class PasswordFragment : Fragment() {
private lateinit var recyclerAdapter: PasswordItemRecyclerAdapter private lateinit var recyclerAdapter: PasswordItemRecyclerAdapter
private lateinit var listener: OnFragmentInteractionListener private lateinit var listener: OnFragmentInteractionListener
private lateinit var settings: SharedPreferences
private var recyclerViewStateToRestore: Parcelable? = null private var recyclerViewStateToRestore: Parcelable? = null
private var actionMode: ActionMode? = null private var actionMode: ActionMode? = null
@ -53,6 +57,7 @@ class PasswordFragment : Fragment() {
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
_binding = PasswordRecyclerViewBinding.inflate(inflater, container, false) _binding = PasswordRecyclerViewBinding.inflate(inflater, container, false)
settings = PreferenceManager.getDefaultSharedPreferences(requireContext())
initializePasswordList() initializePasswordList()
binding.fab.setOnClickListener { binding.fab.setOnClickListener {
ItemCreationBottomSheet().apply { ItemCreationBottomSheet().apply {
@ -63,6 +68,9 @@ class PasswordFragment : Fragment() {
} }
private fun initializePasswordList() { private fun initializePasswordList() {
val gitDir = File(PasswordRepository.getRepositoryDirectory(requireContext()), ".git")
val hasGitDir = gitDir.exists() && gitDir.isDirectory && (gitDir.listFiles()?.isNotEmpty() == true)
if (hasGitDir) {
binding.swipeRefresher.setOnRefreshListener { binding.swipeRefresher.setOnRefreshListener {
if (!PasswordRepository.isGitRepo()) { if (!PasswordRepository.isGitRepo()) {
Snackbar.make(binding.root, getString(R.string.clone_git_repo), Snackbar.LENGTH_INDEFINITE) Snackbar.make(binding.root, getString(R.string.clone_git_repo), Snackbar.LENGTH_INDEFINITE)
@ -74,9 +82,16 @@ class PasswordFragment : Fragment() {
.show() .show()
binding.swipeRefresher.isRefreshing = false binding.swipeRefresher.isRefreshing = false
} else { } else {
// When authentication is set to ConnectionMode.None then the only git operation we
// can run is a pull, so automatically fallback to that.
val operationId = when (ConnectionMode.fromString(settings.getString("git_remote_auth", null))) {
ConnectionMode.None -> BaseGitActivity.REQUEST_PULL
else -> BaseGitActivity.REQUEST_SYNC
}
val intent = Intent(context, GitOperationActivity::class.java) val intent = Intent(context, GitOperationActivity::class.java)
intent.putExtra(BaseGitActivity.REQUEST_ARG_OP, BaseGitActivity.REQUEST_SYNC) intent.putExtra(BaseGitActivity.REQUEST_ARG_OP, operationId)
startActivityForResult(intent, BaseGitActivity.REQUEST_SYNC) startActivityForResult(intent, operationId)
}
} }
} }

View file

@ -53,6 +53,7 @@ import com.zeapo.pwdstore.git.GitAsyncTask
import com.zeapo.pwdstore.git.GitOperation import com.zeapo.pwdstore.git.GitOperation
import com.zeapo.pwdstore.git.GitOperationActivity import com.zeapo.pwdstore.git.GitOperationActivity
import com.zeapo.pwdstore.git.GitServerConfigActivity import com.zeapo.pwdstore.git.GitServerConfigActivity
import com.zeapo.pwdstore.git.config.ConnectionMode
import com.zeapo.pwdstore.ui.dialogs.FolderCreationDialogFragment import com.zeapo.pwdstore.ui.dialogs.FolderCreationDialogFragment
import com.zeapo.pwdstore.utils.PasswordItem import com.zeapo.pwdstore.utils.PasswordItem
import com.zeapo.pwdstore.utils.PasswordRepository import com.zeapo.pwdstore.utils.PasswordRepository
@ -201,7 +202,13 @@ class PasswordStore : AppCompatActivity() {
} }
override fun onCreateOptionsMenu(menu: Menu?): Boolean { override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(if (PasswordRepository.isGitRepo()) R.menu.main_menu_git else R.menu.main_menu_non_git, menu) val menuRes = when {
ConnectionMode.fromString(settings.getString("git_remote_auth", null))
== ConnectionMode.None -> R.menu.main_menu_no_auth
PasswordRepository.isGitRepo() -> R.menu.main_menu_git
else -> R.menu.main_menu_non_git
}
menuInflater.inflate(menuRes, menu)
return super.onCreateOptionsMenu(menu) return super.onCreateOptionsMenu(menu)
} }

View file

@ -247,6 +247,9 @@ abstract class GitOperation(fileDir: File, internal val callingActivity: Activit
dialog.show() dialog.show()
} }
} }
ConnectionMode.None -> {
execute()
}
} }
} }

View file

@ -24,7 +24,7 @@ import java.io.IOException
*/ */
class GitServerConfigActivity : BaseGitActivity() { class GitServerConfigActivity : BaseGitActivity() {
lateinit var binding: ActivityGitCloneBinding private lateinit var binding: ActivityGitCloneBinding
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -36,11 +36,10 @@ class GitServerConfigActivity : BaseGitActivity() {
setContentView(binding.root) setContentView(binding.root)
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
val protocolIdToCheck = when (protocol) { binding.cloneProtocolGroup.check(when (protocol) {
Protocol.Ssh -> R.id.clone_protocol_ssh Protocol.Ssh -> R.id.clone_protocol_ssh
Protocol.Https -> R.id.clone_protocol_https Protocol.Https -> R.id.clone_protocol_https
} })
binding.cloneProtocolGroup.check(protocolIdToCheck)
binding.cloneProtocolGroup.addOnButtonCheckedListener { _, checkedId, checked -> binding.cloneProtocolGroup.addOnButtonCheckedListener { _, checkedId, checked ->
if (checked) { if (checked) {
when (checkedId) { when (checkedId) {
@ -51,19 +50,18 @@ class GitServerConfigActivity : BaseGitActivity() {
} }
} }
val connectionModeIdToCheck = when (connectionMode) { binding.connectionModeGroup.check(when (connectionMode) {
ConnectionMode.SshKey -> R.id.connection_mode_ssh_key ConnectionMode.SshKey -> R.id.connection_mode_ssh_key
ConnectionMode.Password -> R.id.connection_mode_password ConnectionMode.Password -> R.id.connection_mode_password
ConnectionMode.OpenKeychain -> R.id.connection_mode_open_keychain ConnectionMode.OpenKeychain -> R.id.connection_mode_open_keychain
} ConnectionMode.None -> R.id.connection_mode_none
binding.connectionModeGroup.check(connectionModeIdToCheck) })
binding.connectionModeGroup.addOnButtonCheckedListener { _, checkedId, checked -> binding.connectionModeGroup.setOnCheckedChangeListener { group, _ ->
if (checked) { when (group.checkedRadioButtonId) {
when (checkedId) {
R.id.connection_mode_ssh_key -> connectionMode = ConnectionMode.SshKey R.id.connection_mode_ssh_key -> connectionMode = ConnectionMode.SshKey
R.id.connection_mode_open_keychain -> connectionMode = ConnectionMode.OpenKeychain R.id.connection_mode_open_keychain -> connectionMode = ConnectionMode.OpenKeychain
R.id.connection_mode_password -> connectionMode = ConnectionMode.Password R.id.connection_mode_password -> connectionMode = ConnectionMode.Password
} R.id.connection_mode_none -> connectionMode = ConnectionMode.None
} }
} }
updateConnectionModeToggleGroup() updateConnectionModeToggleGroup()
@ -121,15 +119,20 @@ class GitServerConfigActivity : BaseGitActivity() {
private fun updateConnectionModeToggleGroup() { private fun updateConnectionModeToggleGroup() {
if (protocol == Protocol.Ssh) { if (protocol == Protocol.Ssh) {
if (binding.connectionModeNone.isChecked)
binding.connectionModeGroup.check(R.id.connection_mode_ssh_key)
binding.connectionModeSshKey.isEnabled = true binding.connectionModeSshKey.isEnabled = true
binding.connectionModeOpenKeychain.isEnabled = true binding.connectionModeOpenKeychain.isEnabled = true
binding.connectionModeNone.isEnabled = false
} else { } else {
// Reset connection mode to the only one possible via HTTPS: password. // Reset connection mode to password if the current value is not valid for HTTPS
// Important note: This has to happen before disabling the other toggle buttons or they // Important note: This has to happen before disabling the other toggle buttons or they
// won't uncheck. // won't uncheck.
if (connectionMode !in listOf(ConnectionMode.None, ConnectionMode.Password))
binding.connectionModeGroup.check(R.id.connection_mode_password) binding.connectionModeGroup.check(R.id.connection_mode_password)
binding.connectionModeSshKey.isEnabled = false binding.connectionModeSshKey.isEnabled = false
binding.connectionModeOpenKeychain.isEnabled = false binding.connectionModeOpenKeychain.isEnabled = false
binding.connectionModeNone.isEnabled = true
} }
} }

View file

@ -7,7 +7,9 @@ package com.zeapo.pwdstore.git.config
enum class ConnectionMode(val pref: String) { enum class ConnectionMode(val pref: String) {
SshKey("ssh-key"), SshKey("ssh-key"),
Password("username/password"), Password("username/password"),
OpenKeychain("OpenKeychain"); OpenKeychain("OpenKeychain"),
None("None"),
;
companion object { companion object {
private val map = values().associateBy(ConnectionMode::pref) private val map = values().associateBy(ConnectionMode::pref)

View file

@ -6,7 +6,8 @@ package com.zeapo.pwdstore.git.config
enum class Protocol(val pref: String) { enum class Protocol(val pref: String) {
Ssh("ssh://"), Ssh("ssh://"),
Https("https://"); Https("https://"),
;
companion object { companion object {
private val map = values().associateBy(Protocol::pref) private val map = values().associateBy(Protocol::pref)

View file

@ -1,9 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
</vector>

View file

@ -156,50 +156,39 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/label_server_path" /> app:layout_constraintTop_toBottomOf="@id/label_server_path" />
<com.google.android.material.button.MaterialButtonToggleGroup <RadioGroup
android:id="@+id/connection_mode_group" android:id="@+id/connection_mode_group"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="8dp" android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@id/label_connection_mode" app:layout_constraintTop_toBottomOf="@id/label_connection_mode"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent">
app:selectionRequired="true"
app:singleSelection="true" >
<com.google.android.material.button.MaterialButton <RadioButton
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/connection_mode_ssh_key" android:id="@+id/connection_mode_ssh_key"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/connection_mode_ssh_key" android:text="@string/connection_mode_ssh_key" />
android:textColor="?android:attr/textColorPrimary"
app:rippleColor="@color/ripple_color"
app:strokeColor="?attr/colorSecondary"
app:backgroundTint="@color/toggle_button_selector" />
<com.google.android.material.button.MaterialButton <RadioButton
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/connection_mode_password" android:id="@+id/connection_mode_password"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/connection_mode_basic_authentication" android:text="@string/connection_mode_basic_authentication" />
android:textColor="?android:attr/textColorPrimary"
app:rippleColor="@color/ripple_color"
app:strokeColor="?attr/colorSecondary"
app:backgroundTint="@color/toggle_button_selector" />
<com.google.android.material.button.MaterialButton <RadioButton
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/connection_mode_open_keychain" android:id="@+id/connection_mode_open_keychain"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/connection_mode_openkeychain" android:text="@string/connection_mode_openkeychain" />
android:textColor="?android:attr/textColorPrimary"
app:rippleColor="@color/ripple_color"
app:strokeColor="?attr/colorSecondary"
app:backgroundTint="@color/toggle_button_selector" />
</com.google.android.material.button.MaterialButtonToggleGroup> <RadioButton
android:id="@+id/connection_mode_none"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/connection_mode_none" />
</RadioGroup>
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button" style="@style/Widget.MaterialComponents.Button"

View file

@ -1,7 +1,6 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto">
tools:context=".pwdstore" >
<item android:id="@+id/action_search" <item android:id="@+id/action_search"
android:title="@string/action_search" android:title="@string/action_search"
@ -10,18 +9,18 @@
app:actionViewClass="androidx.appcompat.widget.SearchView" /> app:actionViewClass="androidx.appcompat.widget.SearchView" />
<item android:id="@+id/git_sync" <item android:id="@+id/git_sync"
android:title="@string/git_sync"/> android:title="@string/git_sync" />
<item android:id="@+id/git_pull" <item android:id="@+id/git_pull"
android:title="@string/git_pull"/> android:title="@string/git_pull" />
<item android:id="@+id/git_push" <item android:id="@+id/git_push"
android:title="@string/git_push"/> android:title="@string/git_push" />
<item android:id="@+id/refresh" <item android:id="@+id/refresh"
android:title="@string/refresh_list" android:title="@string/refresh_list" />
android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="never"/>
<item android:id="@+id/user_pref" <item android:id="@+id/user_pref"
android:title="@string/action_settings" android:title="@string/action_settings"
android:orderInCategory="100"/> android:orderInCategory="100" />
</menu> </menu>

View file

@ -0,0 +1,20 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_search_white_24dp"
app:showAsAction="always|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
<item android:id="@+id/git_pull"
android:title="@string/git_pull"/>
<item android:id="@+id/refresh"
android:title="@string/refresh_list" />
<item android:id="@+id/user_pref"
android:title="@string/action_settings"
android:orderInCategory="100" />
</menu>

View file

@ -1,7 +1,6 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto">
tools:context=".pwdstore" >
<item android:id="@+id/action_search" <item android:id="@+id/action_search"
android:title="@string/action_search" android:title="@string/action_search"
@ -10,11 +9,9 @@
app:actionViewClass="androidx.appcompat.widget.SearchView" /> app:actionViewClass="androidx.appcompat.widget.SearchView" />
<item android:id="@+id/refresh" <item android:id="@+id/refresh"
android:title="@string/refresh_list" android:title="@string/refresh_list" />
android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="never"/>
<item android:id="@+id/user_pref" <item android:id="@+id/user_pref"
android:title="@string/action_settings" android:title="@string/action_settings"
android:orderInCategory="100"/> android:orderInCategory="100" />
</menu> </menu>

View file

@ -354,9 +354,10 @@
<string name="theme_follow_system">System default</string> <string name="theme_follow_system">System default</string>
<string name="clone_protocol_ssh" translatable="false">SSH</string> <string name="clone_protocol_ssh" translatable="false">SSH</string>
<string name="clone_protocol_https" translatable="false">HTTPS</string> <string name="clone_protocol_https" translatable="false">HTTPS</string>
<string name="connection_mode_ssh_key" translatable="false">SSH key</string> <string name="connection_mode_ssh_key">SSH key</string>
<string name="connection_mode_basic_authentication" translatable="false">Password</string> <string name="connection_mode_basic_authentication">Password</string>
<string name="connection_mode_openkeychain" translatable="false">OpenKeychain</string> <string name="connection_mode_openkeychain" translatable="false">OpenKeychain</string>
<string name="connection_mode_none">None</string>
<string name="git_server_config_save_success">Successfully saved configuration</string> <string name="git_server_config_save_success">Successfully saved configuration</string>
<string name="git_server_config_save_failure">Configuration error: please verify your settings and try again</string> <string name="git_server_config_save_failure">Configuration error: please verify your settings and try again</string>
<string name="git_operation_unable_to_open_ssh_key_title">Unable to open the ssh-key</string> <string name="git_operation_unable_to_open_ssh_key_title">Unable to open the ssh-key</string>