Created
October 24, 2018 17:04
-
-
Save lalitsharma1607/518cbbeb2d6d3e771de525858fd39a5d to your computer and use it in GitHub Desktop.
Authentication Intercepter
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.taxiqq.driver.network; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import java.io.IOException; | |
import okhttp3.Credentials; | |
import okhttp3.Interceptor; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
public class AuthInterceptor implements Interceptor { | |
private String credentials; | |
private static AuthInterceptor instance; | |
private AuthInterceptor() {} | |
public static AuthInterceptor getInstance() { | |
if (instance == null) instance = new AuthInterceptor(); | |
return instance; | |
} | |
public void setCredentials(Context context, String user, String password) { | |
this.credentials = Credentials.basic(user, password); | |
save(context); | |
} | |
public void logout(Context context) { | |
SharedPreferences preferences = context.getSharedPreferences("myPreferences",Context.MODE_PRIVATE); | |
preferences.edit().putString("authCredentials",null).apply(); | |
} | |
private void save(Context context) { | |
SharedPreferences preferences = context.getSharedPreferences("myPreferences",Context.MODE_PRIVATE); | |
preferences.edit().putString("authCredentials",credentials).apply(); | |
} | |
public Boolean retrieveAuth(Context context) { | |
SharedPreferences preferences = context.getSharedPreferences("myPreferences", Context.MODE_PRIVATE); | |
String credentials = preferences.getString("authCredentials", null); | |
if (credentials != null && credentials.length() > 4) { | |
this.credentials = credentials; | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
Request request = chain.request(); | |
Request authenticatedRequest = request.newBuilder() | |
.header("Authorization", credentials).build(); | |
return chain.proceed(authenticatedRequest); | |
} | |
public String getCredentials() { | |
return credentials; | |
} | |
} |
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 ai.saal.saalmilitary.base | |
import com.taxiqq.driver.BuildConfig | |
import com.taxiqq.driver.network.AuthInterceptor | |
import com.taxiqq.driver.network.APIService | |
import okhttp3.OkHttpClient | |
import okhttp3.logging.HttpLoggingInterceptor | |
import retrofit2.Retrofit | |
import retrofit2.converter.gson.GsonConverterFactory | |
import java.util.concurrent.TimeUnit | |
open class BaseAPI { | |
private val timeout = BuildConfig.Timeout.toLong() | |
private var retrofit : Retrofit | |
init { | |
val okHttpClient = OkHttpClient.Builder() | |
.addInterceptor(AuthInterceptor.getInstance())//.addInterceptor(ThreatAnalysisMockClient( MyApplication.instance.applicationContext as Context)) | |
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) | |
.readTimeout(timeout, TimeUnit.SECONDS) | |
.connectTimeout(timeout, TimeUnit.SECONDS) | |
.build() | |
retrofit = Retrofit.Builder() | |
.baseUrl(getBaseURL()) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.client(okHttpClient) | |
.build() | |
} | |
open fun getBaseURL() : String {return ""} | |
open fun getRetrofit() : APIService { | |
return retrofit.create(APIService::class.java) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment