Skip to content

Instantly share code, notes, and snippets.

@DanFlor
Created May 1, 2018 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanFlor/fd47492590bca56847dc66d1ac1af722 to your computer and use it in GitHub Desktop.
Save DanFlor/fd47492590bca56847dc66d1ac1af722 to your computer and use it in GitHub Desktop.
OkHttp Basic Auth Interceptor
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