0

I want to add refresh token call inside my authenticate method of HttpAuthenticationInterceptor to manage access_token expiry on 401 unAuthorized response.

I added a synchronous call of refresh_token in authenticate method using serviceClient interface(serviceGateway) of retrofit 2.but i am facing cyclic-dependency problem while injecting serviceGateway in the interceptor. Do i need to make a separate ServiceClient interface for this refresh token call?

Here are my AuthenticationInterceptor and NetworkModule classes

class AuthenticationInterceptor @Inject constructor(private val preferences: SharedPreferencesKeyValueDataSource, private val serviceGateway: ServiceGateway) : Interceptor, Authenticator{

    @Throws(IOException::class)
    override fun authenticate(route: Route?, response: Response): Request?{
        if (response.code() == 401) {
            val credentials = Credentials.basic("dummy", "dummy")
            val refreshCall = serviceGateway.refreshToken(credentials, preferences.getValue(Constants.REFRESH_TOKEN, ""))

            //make it as retrofit synchronous call
            val refreshResponse = refreshCall.execute()
            return if (refreshResponse.code() == 200) {
                //Save the access token and refresh token in preferences
                refreshResponse.body()?.let{
                    preferences.setValue(Constants.ACCESS_TOKEN, it.access_token)
                    preferences.setValue(Constants.REFRESH_TOKEN, it.refresh_token)
                }
                val newAccessToken = refreshResponse.body()?.access_token
                response.request().newBuilder()
                        .header(AuthenticationInterceptor.AUTHORIZATION, "Bearer $newAccessToken")
                        .build()
            } else {
                null
            }
        }
        return null
    }
}

//And Code from NetworkModule is below,

@Provides
    @Singleton
    fun provideAuthenticationInterceptor(preferences: SharedPreferencesKeyValueDataSource, serviceGateway: ServiceGateway) = AuthenticationInterceptor(preferences, serviceGateway)

    @Provides
    @Singleton
    fun provideOkhttpClient(loggingInterceptor: HttpLoggingInterceptor,
                            authenticationInterceptor: AuthenticationInterceptor): OkHttpClient {

        val client = OkHttpClient.Builder()
        client.addInterceptor(loggingInterceptor)
        client.addInterceptor(authenticationInterceptor)
        client.authenticator(authenticationInterceptor)
        client.connectTimeout(15, TimeUnit.SECONDS)
        client.readTimeout(20, TimeUnit.SECONDS)
        return client.build()
    }


    @Provides
    @Singleton
    fun provideRetrofit(gson: Gson, okHttpClient: OkHttpClient,
                        @Named("BASE_URL_CORE") baseUrl: String)
            : Retrofit {

        return Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .build()
    }

    @Provides
    @Singleton
    fun provideGateway(retrofit: Retrofit): ServiceGateway {
        return retrofit.create(ServiceGateway::class.java)
    }

Compiler error is...

  • ServiceGateway is injected at ...NetworkModule.provideAuthenticationInterceptor(…, serviceGateway)
  • AuthenticationInterceptor is injected at
    ...NetworkModule.provideOkhttpClient(…, authenticationInterceptor)
  • okhttp3.OkHttpClient is injected at ...NetworkModule.provideRetrofit(…, okHttpClient, …)
  • retrofit2.Retrofit is injected at ...NetworkModule.provideGateway(retrofit)
Shoaib Mushtaq
  • 595
  • 4
  • 17

1 Answers1

0

I tried Dagger Lazy Injection(import daggger.Lazy) for ServiceGateway and it's working fine without cyclic dependency of Dagger.

class AuthenticationInterceptor @Inject constructor(private val preferences: SharedPreferencesKeyValueDataSource, private val serviceGateway: Lazy<ServiceGateway>) : Interceptor, Authenticator{
}

RefreshToken api is calling now but this results in another issue (cyclic refresh-token call). So to resolve this cyclic refresh-token call issue, it's better to make a separate serviceGateway for refresh-token call like this guys Yasin Kaçmaz answered here. Another solution is to make refresh-token call directly on Okhttp(without using Retrofit service interface)

Thanks Aamir Abro for helping me out to resolve this issue.

Shoaib Mushtaq
  • 595
  • 4
  • 17