Use basic authentication for tasks.org

pull/1244/head
Alex Baker 4 years ago
parent 3a34721b12
commit 806b74f03a

@ -63,15 +63,16 @@ class SignInViewModel @ViewModelInject constructor(
val auth = authService.authStateManager.current
val tokenString = auth.accessToken ?: return null
val idToken = auth.idToken?.let { IdToken(it) } ?: return null
val username = "${authService.iss}_${idToken.sub}"
try {
val homeSet = provider
.forUrl(
context.getString(R.string.tasks_caldav_url),
token = tokenString
username,
tokenString
)
.setForeground()
.homeSet(token = tokenString)
val username = "${authService.iss}_${idToken.sub}"
.homeSet(username, tokenString)
val password = encryption.encrypt(tokenString)
return caldavDao.getAccount(CaldavAccount.TYPE_TASKS, username)
?.apply {

@ -89,8 +89,7 @@ class CaldavClient(
@Throws(IOException::class, DavException::class, NoSuchAlgorithmException::class, KeyManagementException::class)
suspend fun homeSet(
username: String? = null,
password: String? = null,
token: String? = null
password: String? = null
): String = withContext(Dispatchers.IO) {
var principal: String? = null
try {
@ -107,8 +106,7 @@ class CaldavClient(
provider.forUrl(
(if (isNullOrEmpty(principal)) httpUrl else httpUrl!!.resolve(principal!!)).toString(),
username,
password,
token)
password)
.findHomeset()
}

@ -12,6 +12,7 @@ import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.internal.tls.OkHostnameVerifier
import org.tasks.DebugNetworkInterceptor
import org.tasks.R
import org.tasks.billing.Inventory
import org.tasks.data.CaldavAccount
import org.tasks.preferences.Preferences
@ -27,12 +28,14 @@ class CaldavClientProvider @Inject constructor(
private val interceptor: DebugNetworkInterceptor,
private val inventory: Inventory
) {
private val tasksUrl = context.getString(R.string.tasks_caldav_url)
suspend fun forUrl(
url: String?,
username: String? = null,
password: String? = null,
token: String? = null): CaldavClient {
val auth = getAuthInterceptor(username = username, password = password, token = token)
password: String? = null
): CaldavClient {
val auth = getAuthInterceptor(username, password, url)
val customCertManager = newCertManager()
return CaldavClient(
this,
@ -43,7 +46,11 @@ class CaldavClientProvider @Inject constructor(
}
suspend fun forAccount(account: CaldavAccount, url: String? = account.url): CaldavClient {
val auth = getAuthInterceptor(account)
val auth = getAuthInterceptor(
account.username,
account.getPassword(encryption),
account.url
)
val customCertManager = newCertManager()
return CaldavClient(
this,
@ -58,22 +65,13 @@ class CaldavClientProvider @Inject constructor(
}
private fun getAuthInterceptor(
account: CaldavAccount? = null,
username: String? = account?.username,
password: String? = account?.getPassword(encryption),
token: String? = null
): Interceptor? {
return when {
account?.isTasksOrg == true ->
account.password
?.let { encryption.decrypt(it) }
?.let { TokenInterceptor(it, inventory) }
username?.isNotBlank() == true && password?.isNotBlank() == true ->
BasicDigestAuthHandler(null, username, password)
token?.isNotBlank() == true ->
TokenInterceptor(token, inventory)
else -> null
}
username: String?,
password: String?,
url: String?
): Interceptor? = when {
username.isNullOrBlank() || password.isNullOrBlank() -> null
url?.startsWith(tasksUrl) == true -> TasksBasicAuth(username, password, inventory)
else -> BasicDigestAuthHandler(null, username, password)
}
private fun createHttpClient(auth: Interceptor?, customCertManager: CustomCertManager, foreground: Boolean = false): OkHttpClient {

@ -1,15 +1,19 @@
package org.tasks.caldav
import okhttp3.Credentials
import okhttp3.Interceptor
import okhttp3.Response
import org.tasks.billing.Inventory
class TokenInterceptor(
private val token: String,
class TasksBasicAuth(
user: String,
token: String,
private val inventory: Inventory
) : Interceptor {
private val credentials = Credentials.basic(user, token, Charsets.UTF_8)
override fun intercept(chain: Interceptor.Chain): Response {
val builder = chain.request().newBuilder().header(AUTHORIZATION, "Bearer $token")
val builder = chain.request().newBuilder().header(AUTHORIZATION, credentials)
inventory.subscription?.let {
builder.header(SKU, it.sku)
builder.header(TOKEN, it.purchaseToken)
Loading…
Cancel
Save