Commit 8c9307f9 authored by Eugene Shapovalov's avatar Eugene Shapovalov 💬

Fix bug with parsing auth tokens.

parent ee5ee290
......@@ -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.ServerError
/**
* @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 ServerError(401)
}
}
return chain.proceed(request)
......
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