Created
May 1, 2018 17:12
-
-
Save DanFlor/fd47492590bca56847dc66d1ac1af722 to your computer and use it in GitHub Desktop.
OkHttp Basic Auth Interceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.obidan.api | |
import okhttp3.Credentials | |
import okhttp3.Interceptor | |
import okhttp3.Response | |
/** | |
* BasicAuthInterceptor.kt | |
* | |
* A simple OkHttp Interceptor to rewrite the authorization header. | |
* Credentials may be set at runtime to allow for them to change without | |
* having to rebuild any injection dependencies. | |
* | |
* Created by obidan on 2/10/18. | |
*/ | |
class BasicAuthInterceptor(): Interceptor { | |
private var credentials: String = "" | |
fun setCredentials(user:String, password: String) { | |
this.credentials = Credentials.basic(user, password) | |
} | |
override fun intercept(chain: Interceptor.Chain?): Response { | |
val request = chain!!.request() | |
val authenticatedRequest = request.newBuilder() | |
.header("Authorization", credentials).build() | |
return chain.proceed(authenticatedRequest) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment