Commit 1a3dcd9a authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖

Merge branch 'fix/custom_auth_bugs' into 'feature/prepare_to_release'

Fix custom server auth bugs.

See merge request terrakok/gitlab-client!83
parents c1be4079 f40d86f1
package ru.terrakok.gitlabclient.model.data.server
/**
* @author Eugene Shapovalov (@CraggyHaggy) on 14.09.18.
*/
class TokenInvalidError : RuntimeException()
\ No newline at end of file
......@@ -3,6 +3,7 @@ package ru.terrakok.gitlabclient.model.data.server.interceptor
import okhttp3.Interceptor
import okhttp3.Response
import ru.terrakok.gitlabclient.model.data.auth.AuthHolder
import ru.terrakok.gitlabclient.model.data.server.TokenInvalidError
/**
* @author Konstantin Tskhovrebov (aka terrakok) on 23.04.17.
......@@ -10,11 +11,17 @@ import ru.terrakok.gitlabclient.model.data.auth.AuthHolder
class AuthHeaderInterceptor(private val authData: AuthHolder) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
authData.token?.let {
if (authData.isOAuthToken) {
request = request.newBuilder().addHeader("Authorization", "Bearer " + it).build()
} else {
request = request.newBuilder().addHeader("PRIVATE-TOKEN", it).build()
val token = authData.token
if (token != null) {
try {
request = if (authData.isOAuthToken) {
request.newBuilder().addHeader("Authorization", "Bearer $token").build()
} else {
request.newBuilder().addHeader("PRIVATE-TOKEN", token).build()
}
} catch (e: IllegalArgumentException) {
// If token can't be parsed, just logout user to change it with correct value.
throw TokenInvalidError()
}
}
return chain.proceed(request)
......
......@@ -62,13 +62,14 @@ class AuthInteractor(
}
}
fun login(customServerPath: String, privateToken: String) = Completable.fromAction {
var serverPath = customServerPath
if (!customServerPath.endsWith("/")) serverPath += "/"
fun login(customServerPath: String, privateToken: String) =
Completable.fromAction {
var serverPath = customServerPath
if (!customServerPath.endsWith("/")) serverPath += "/"
authRepository.saveAuthData(privateToken, serverPath, false)
switchServerIfNeeded(customServerPath)
}
authRepository.saveAuthData(privateToken, serverPath, false)
switchServerIfNeeded(serverPath)
}
fun logout() {
authRepository.clearAuthData()
......
......@@ -5,6 +5,7 @@ import ru.terrakok.cicerone.Router
import ru.terrakok.gitlabclient.Screens
import ru.terrakok.gitlabclient.extension.userMessage
import ru.terrakok.gitlabclient.model.data.server.ServerError
import ru.terrakok.gitlabclient.model.data.server.TokenInvalidError
import ru.terrakok.gitlabclient.model.interactor.auth.AuthInteractor
import ru.terrakok.gitlabclient.model.system.ResourceManager
import ru.terrakok.gitlabclient.model.system.SchedulersProvider
......@@ -30,13 +31,13 @@ class ErrorHandler @Inject constructor(
fun proceed(error: Throwable, messageListener: (String) -> Unit = {}) {
Timber.e(error)
if (error is ServerError) {
when (error.errorCode) {
when (error) {
is ServerError -> when (error.errorCode) {
401 -> authErrorRelay.accept(true)
else -> messageListener(error.userMessage(resourceManager))
}
} else {
messageListener(error.userMessage(resourceManager))
is TokenInvalidError -> authErrorRelay.accept(true)
else -> messageListener(error.userMessage(resourceManager))
}
}
......
......@@ -6,6 +6,7 @@ import android.support.design.widget.BottomSheetDialogFragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.webkit.URLUtil
import android.widget.Toast
import kotlinx.android.synthetic.main.fragment_custom_server_auth.*
......@@ -36,6 +37,14 @@ class CustomServerAuthFragment : BottomSheetDialogFragment() {
serverPathValue.setText(BuildConfig.ORIGIN_GITLAB_ENDPOINT)
}
privateTokenValue.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
login()
true
} else {
false
}
}
loginButton.setOnClickListener { login() }
cancelButton.setOnClickListener { dismiss() }
}
......
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/customAuthTitle"
......@@ -12,14 +12,14 @@
android:text="@string/custom_authorization"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25"/>
app:layout_constraintGuide_percent="0.25" />
<EditText
android:id="@+id/serverPathValue"
......@@ -33,7 +33,7 @@
android:textSize="16sp"
app:layout_constraintLeft_toRightOf="@id/guideline"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/customAuthTitle"/>
app:layout_constraintTop_toBottomOf="@id/customAuthTitle" />
<TextView
android:id="@+id/serverPathLabel"
......@@ -42,22 +42,23 @@
android:text="@string/server_path"
android:textSize="16sp"
app:layout_constraintBaseline_toBaselineOf="@id/serverPathValue"
app:layout_constraintEnd_toStartOf="@+id/guideline"/>
app:layout_constraintEnd_toStartOf="@+id/guideline" />
<EditText
android:id="@+id/privateTokenValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:gravity="start"
android:hint="@string/private_token_hint"
android:imeOptions="actionDone"
android:inputType="textShortMessage"
android:lines="1"
android:textSize="16sp"
app:layout_constraintLeft_toRightOf="@id/guideline"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/serverPathValue"/>
app:layout_constraintTop_toBottomOf="@id/serverPathValue" />
<TextView
android:id="@+id/privateTokenLabel"
......@@ -66,7 +67,7 @@
android:text="@string/private_token"
android:textSize="16sp"
app:layout_constraintBaseline_toBaselineOf="@id/privateTokenValue"
app:layout_constraintEnd_toStartOf="@+id/guideline"/>
app:layout_constraintEnd_toStartOf="@+id/guideline" />
<TextView
android:id="@+id/loginButton"
......@@ -80,7 +81,7 @@
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/privateTokenValue"/>
app:layout_constraintTop_toBottomOf="@id/privateTokenValue" />
<TextView
android:id="@+id/cancelButton"
......@@ -94,6 +95,6 @@
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="@id/loginButton"
app:layout_constraintTop_toBottomOf="@id/privateTokenValue"/>
app:layout_constraintTop_toBottomOf="@id/privateTokenValue" />
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment