Skip to content

Commit

Permalink
Merge pull request #42 from hegocre/fix/self-signed
Browse files Browse the repository at this point in the history
OkHttp bug fixes
  • Loading branch information
hegocre authored Nov 25, 2023
2 parents e396307 + 270d88b commit bdb9b85
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 33 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId "com.hegocre.nextcloudpasswords"
minSdk 24
targetSdk 34
versionCode 17
versionName "1.0.5"
versionCode 18
versionName "1.0.6"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
* Class with methods used to interact with [the API](https://git.mdns.eu/nextcloud/passwords/-/wikis/Developers/Api)
Expand Down Expand Up @@ -127,7 +128,7 @@ class ApiController private constructor(context: Context) {
PWDv1ChallengeMasterKeyNeededException::class,
PWDv1ChallengeMasterKeyInvalidException::class
)
suspend fun openSession(masterPassword: String?): Boolean {
suspend fun openSession(masterPassword: String?): Boolean = withContext(Dispatchers.Default) {
decryptCSEv1Keychain(
preferencesManager.getCSEv1Keychain(),
masterPassword
Expand Down Expand Up @@ -165,13 +166,13 @@ class ApiController private constructor(context: Context) {
)
}
}
return false
return@withContext false
}

val secret = if (secretResult is Result.Success) {
secretResult.data
} else {
return if (secretResult is Result.Error && secretResult.code == Error.API_NO_CSE) {
return@withContext if (secretResult is Result.Error && secretResult.code == Error.API_NO_CSE) {
// No encryption, we need no session
// Clear old keychain, if CSE was disabled
preferencesManager.setCSEv1Keychain(null)
Expand Down Expand Up @@ -201,7 +202,7 @@ class ApiController private constructor(context: Context) {
)
}
}
return false
return@withContext false
}

preferencesManager.setCSEv1Keychain(encryptedKeychainJson)
Expand All @@ -215,7 +216,7 @@ class ApiController private constructor(context: Context) {
sessionCode = newSessionCode

_sessionOpen.emit(true)
return true
return@withContext true
}

/**
Expand Down
14 changes: 4 additions & 10 deletions app/src/main/java/com/hegocre/nextcloudpasswords/api/FoldersApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FoldersApi private constructor(private var server: Server) {


val code = apiResponse.code
val body = apiResponse.body?.string()
val body = withContext(Dispatchers.IO) { apiResponse.body?.string() }
withContext(Dispatchers.IO) {
apiResponse.close()
}
Expand Down Expand Up @@ -107,9 +107,7 @@ class FoldersApi private constructor(private var server: Server) {
return Result.Error(Error.API_BAD_RESPONSE)
}

withContext(Dispatchers.Default) {
Result.Success(Unit)
}
Result.Success(Unit)
} catch (e: SocketTimeoutException) {
if (BuildConfig.DEBUG) {
e.printStackTrace()
Expand Down Expand Up @@ -161,9 +159,7 @@ class FoldersApi private constructor(private var server: Server) {
return Result.Error(Error.API_BAD_RESPONSE)
}

withContext(Dispatchers.Default) {
Result.Success(Unit)
}
Result.Success(Unit)
} catch (e: SocketTimeoutException) {
if (BuildConfig.DEBUG) {
e.printStackTrace()
Expand Down Expand Up @@ -214,9 +210,7 @@ class FoldersApi private constructor(private var server: Server) {
return Result.Error(Error.API_BAD_RESPONSE)
}

withContext(Dispatchers.Default) {
Result.Success(Unit)
}
Result.Success(Unit)
} catch (e: SocketTimeoutException) {
if (BuildConfig.DEBUG) {
e.printStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PasswordsApi private constructor(private var server: Server) {
}

val code = apiResponse.code
val body = apiResponse.body?.string()
val body = withContext(Dispatchers.IO) { apiResponse.body?.string() }
withContext(Dispatchers.IO) {
apiResponse.close()
}
Expand Down Expand Up @@ -108,9 +108,7 @@ class PasswordsApi private constructor(private var server: Server) {
return Result.Error(Error.API_BAD_RESPONSE)
}

withContext(Dispatchers.Default) {
Result.Success(Unit)
}
Result.Success(Unit)
} catch (e: SocketTimeoutException) {
if (BuildConfig.DEBUG) {
e.printStackTrace()
Expand Down Expand Up @@ -162,9 +160,7 @@ class PasswordsApi private constructor(private var server: Server) {
return Result.Error(Error.API_BAD_RESPONSE)
}

withContext(Dispatchers.Default) {
Result.Success(Unit)
}
Result.Success(Unit)
} catch (e: SocketTimeoutException) {
if (BuildConfig.DEBUG) {
e.printStackTrace()
Expand Down Expand Up @@ -215,9 +211,7 @@ class PasswordsApi private constructor(private var server: Server) {
return Result.Error(Error.API_BAD_RESPONSE)
}

withContext(Dispatchers.Default) {
Result.Success(Unit)
}
Result.Success(Unit)
} catch (e: SocketTimeoutException) {
if (BuildConfig.DEBUG) {
e.printStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ServiceApi private constructor(private val server: Server) {
}

val code = apiResponse.code
val body = apiResponse.body?.string()
val body = withContext(Dispatchers.IO) { apiResponse.body?.string() }
withContext(Dispatchers.IO) {
apiResponse.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SessionApi private constructor(private var server: Server) {
}

val code = apiResponse.code
val body = apiResponse.body?.string()
val body = withContext(Dispatchers.IO) { apiResponse.body?.string() }

withContext(Dispatchers.IO) {
apiResponse.close()
Expand Down Expand Up @@ -100,7 +100,7 @@ class SessionApi private constructor(private var server: Server) {
)
}

val body = apiResponse.body?.string()
val body = withContext(Dispatchers.IO) { apiResponse.body?.string() }
val code = apiResponse.code

val xSessionCode = apiResponse.header("x-api-session", null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SettingsApi private constructor(private val server: Server) {
}

val code = apiResponse.code
val body = apiResponse.body?.string()
val body = withContext(Dispatchers.IO) { apiResponse.body?.string() }

withContext(Dispatchers.IO) {
apiResponse.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ class OkHttpRequest private constructor() {
val JSON = "application/json; charset=utf-8".toMediaTypeOrNull()

fun getInstance(): OkHttpRequest {
if (instance == null) instance = OkHttpRequest()
return instance as OkHttpRequest
synchronized(this) {
if (instance == null) instance = OkHttpRequest()

return instance as OkHttpRequest
}
}
}
}

0 comments on commit bdb9b85

Please sign in to comment.