Skip to content

Commit

Permalink
Added option to disable SSL verify
Browse files Browse the repository at this point in the history
  • Loading branch information
JetpackDuba committed Nov 22, 2023
1 parent bcaea8a commit 5bc52bf
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/com/jetpackduba/gitnuro/Icons.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ object AppIcons {
const val REMOVE_DONE = "remove_done.svg"
const val REVERT = "revert.svg"
const val SEARCH = "search.svg"
const val SECURITY = "security.svg"
const val SETTINGS = "settings.svg"
const val SIGN = "sign.svg"
const val SOURCE = "source.svg"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package com.jetpackduba.gitnuro.credentials

import com.jetpackduba.gitnuro.exceptions.NotSupportedHelper
import com.jetpackduba.gitnuro.git.remote_operations.CredentialsCache
import com.jetpackduba.gitnuro.logging.printLog
import com.jetpackduba.gitnuro.managers.IShellManager
import com.jetpackduba.gitnuro.preferences.AppSettings
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.internal.JGitText
import org.eclipse.jgit.lib.Config
import org.eclipse.jgit.transport.CredentialItem
import org.eclipse.jgit.transport.CredentialItem.Password
import org.eclipse.jgit.transport.CredentialItem.Username
import org.eclipse.jgit.transport.CredentialItem.YesNoType
import org.eclipse.jgit.transport.CredentialsProvider
import org.eclipse.jgit.transport.URIish
import java.io.*
import java.util.concurrent.TimeUnit
import kotlin.coroutines.cancellation.CancellationException

private const val TIMEOUT_MIN = 1L
private const val TAG = "HttpCredentialsProvider"

class HttpCredentialsProvider @AssistedInject constructor(
private val credentialsStateManager: CredentialsStateManager,
Expand All @@ -33,22 +39,42 @@ class HttpCredentialsProvider @AssistedInject constructor(

override fun supports(vararg items: CredentialItem?): Boolean {
val fields = items.map { credentialItem -> credentialItem?.promptText }
return if (fields.isEmpty()) {
true
} else
fields.size == 2 &&
fields.contains("Username") &&
fields.contains("Password")
val isEmpty = fields.isEmpty()

val isUserPasswordAuth = fields.size == 2 &&
fields.contains("Username") &&
fields.contains("Password")

val isAskingForSslDisable = items.any { it is YesNoType }

return isEmpty || isUserPasswordAuth || isAskingForSslDisable
}

override fun get(uri: URIish, vararg items: CredentialItem): Boolean {
val userItem = items.firstOrNull { it.promptText == "Username" }
val passwordItem = items.firstOrNull { it.promptText == "Password" }
val itemsMap = items.map { "${it::class.simpleName} - ${it.promptText}" }

printLog(TAG, "Items are $itemsMap")

if (userItem !is CredentialItem.Username || passwordItem !is CredentialItem.Password) {
val sslTrustNowItem = items
.filterIsInstance<YesNoType>()
.firstOrNull { it.promptText.contains(JGitText.get().sslTrustNow) }

val userItem = items
.filterIsInstance<Username>()
.firstOrNull()

val passwordItem = items
.filterIsInstance<Password>()
.firstOrNull()

if (userItem == null || passwordItem == null) {
return false
}

if (sslTrustNowItem != null) {
sslTrustNowItem.value = appSettings.verifySsl
}

val externalCredentialsHelper = getExternalCredentialsHelper(uri, git)

if (externalCredentialsHelper == null) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/com/jetpackduba/gitnuro/preferences/AppSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ private const val PREF_GIT_FF_MERGE = "gitFFMerge"
private const val PREF_GIT_PULL_REBASE = "gitPullRebase"
private const val PREF_GIT_PUSH_WITH_LEASE = "gitPushWithLease"

private const val PREF_VERIFY_SSL = "verifySsl"

private const val DEFAULT_COMMITS_LIMIT = 1000
private const val DEFAULT_COMMITS_LIMIT_ENABLED = true
private const val DEFAULT_SWAP_UNCOMMITTED_CHANGES = false
private const val DEFAULT_CACHE_CREDENTIALS_IN_MEMORY = true
private const val DEFAULT_VERIFY_SSL = true
const val DEFAULT_UI_SCALE = -1f

@Singleton
Expand All @@ -67,6 +70,9 @@ class AppSettings @Inject constructor() {
private val _cacheCredentialsInMemoryFlow = MutableStateFlow(cacheCredentialsInMemory)
val cacheCredentialsInMemoryFlow = _cacheCredentialsInMemoryFlow.asStateFlow()

private val _verifySslFlow = MutableStateFlow(cacheCredentialsInMemory)
val verifySslFlow = _cacheCredentialsInMemoryFlow.asStateFlow()

private val _ffMergeFlow = MutableStateFlow(ffMerge)
val ffMergeFlow = _ffMergeFlow.asStateFlow()

Expand Down Expand Up @@ -168,6 +174,15 @@ class AppSettings @Inject constructor() {
_cacheCredentialsInMemoryFlow.value = value
}

var verifySsl: Boolean
get() {
return preferences.getBoolean(PREF_VERIFY_SSL, DEFAULT_VERIFY_SSL)
}
set(value) {
preferences.putBoolean(PREF_VERIFY_SSL, value)
_verifySslFlow.value = value
}

var scaleUi: Float
get() {
return preferences.getFloat(PREF_UI_SCALE, DEFAULT_UI_SCALE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ val settings = listOf(
SettingsEntry.Section("Network"),
SettingsEntry.Entry(AppIcons.NETWORK, "Proxy") { Proxy(it) },
SettingsEntry.Entry(AppIcons.PASSWORD, "Authentication") { Authentication(it) },
SettingsEntry.Entry(AppIcons.SECURITY, "Security") { Security(it) },

SettingsEntry.Section("Tools"),
SettingsEntry.Entry(AppIcons.TERMINAL, "Terminal") { Terminal(it) },
Expand Down Expand Up @@ -355,6 +356,20 @@ private fun Authentication(settingsViewModel: SettingsViewModel) {
)
}

@Composable
private fun Security(settingsViewModel: SettingsViewModel) {
val verifySsl by settingsViewModel.verifySslFlow.collectAsState()

SettingToggle(
title = "Do not verify SSL security",
subtitle = "If active, you may connect to the remote server via insecure HTTPS connection",
value = !verifySsl,
onValueChanged = { value ->
settingsViewModel.verifySsl = !value
}
)
}

@Composable
fun Terminal(settingsViewModel: SettingsViewModel) {
var commitsLimit by remember { mutableStateOf(settingsViewModel.terminalPath) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SettingsViewModel @Inject constructor(
val commitsLimitEnabledFlow = appSettings.commitsLimitEnabledFlow
val swapUncommittedChangesFlow = appSettings.swapUncommittedChangesFlow
val cacheCredentialsInMemoryFlow = appSettings.cacheCredentialsInMemoryFlow
val verifySslFlow = appSettings.verifySslFlow
val terminalPathFlow = appSettings.terminalPathFlow

var scaleUi: Float
Expand Down Expand Up @@ -61,6 +62,12 @@ class SettingsViewModel @Inject constructor(
appSettings.cacheCredentialsInMemory = value
}

var verifySsl: Boolean
get() = appSettings.verifySsl
set(value) {
appSettings.verifySsl = value
}

var pullRebase: Boolean
get() = appSettings.pullRebase
set(value) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/security.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5bc52bf

Please sign in to comment.