BIT-685: Add request headers to all network requests in the app (#300)

This commit is contained in:
David Perez
2023-11-30 09:49:55 -06:00
committed by Álison Fernandes
parent 798fdf3e19
commit 9fcd2b1690
8 changed files with 149 additions and 15 deletions

View File

@@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.platform.datasource.network.di
import com.x8bit.bitwarden.data.platform.datasource.network.authenticator.RefreshAuthenticator
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.AuthTokenInterceptor
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.BaseUrlInterceptors
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.HeadersInterceptor
import com.x8bit.bitwarden.data.platform.datasource.network.retrofit.Retrofits
import com.x8bit.bitwarden.data.platform.datasource.network.retrofit.RetrofitsImpl
import com.x8bit.bitwarden.data.platform.datasource.network.serializer.LocalDateTimeSerializer
@@ -37,6 +38,10 @@ object PlatformNetworkModule {
@Singleton
fun providesAuthTokenInterceptor(): AuthTokenInterceptor = AuthTokenInterceptor()
@Provides
@Singleton
fun providesHeadersInterceptor(): HeadersInterceptor = HeadersInterceptor()
@Provides
@Singleton
fun providesRefreshAuthenticator(): RefreshAuthenticator = RefreshAuthenticator()
@@ -46,12 +51,14 @@ object PlatformNetworkModule {
fun provideRetrofits(
authTokenInterceptor: AuthTokenInterceptor,
baseUrlInterceptors: BaseUrlInterceptors,
headersInterceptor: HeadersInterceptor,
refreshAuthenticator: RefreshAuthenticator,
json: Json,
): Retrofits =
RetrofitsImpl(
authTokenInterceptor = authTokenInterceptor,
baseUrlInterceptors = baseUrlInterceptors,
headersInterceptor = headersInterceptor,
refreshAuthenticator = refreshAuthenticator,
json = json,
)

View File

@@ -0,0 +1,27 @@
package com.x8bit.bitwarden.data.platform.datasource.network.interceptor
import com.x8bit.bitwarden.data.platform.datasource.network.util.HEADER_KEY_CLIENT_NAME
import com.x8bit.bitwarden.data.platform.datasource.network.util.HEADER_KEY_CLIENT_VERSION
import com.x8bit.bitwarden.data.platform.datasource.network.util.HEADER_KEY_DEVICE_TYPE
import com.x8bit.bitwarden.data.platform.datasource.network.util.HEADER_KEY_USER_AGENT
import com.x8bit.bitwarden.data.platform.datasource.network.util.HEADER_VALUE_CLIENT_NAME
import com.x8bit.bitwarden.data.platform.datasource.network.util.HEADER_VALUE_CLIENT_VERSION
import com.x8bit.bitwarden.data.platform.datasource.network.util.HEADER_VALUE_DEVICE_TYPE
import com.x8bit.bitwarden.data.platform.datasource.network.util.HEADER_VALUE_USER_AGENT
import okhttp3.Interceptor
import okhttp3.Response
/**
* Interceptor responsible for adding various headers to all API requests.
*/
class HeadersInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response = chain.proceed(
chain.request()
.newBuilder()
.header(HEADER_KEY_USER_AGENT, HEADER_VALUE_USER_AGENT)
.header(HEADER_KEY_CLIENT_NAME, HEADER_VALUE_CLIENT_NAME)
.header(HEADER_KEY_CLIENT_VERSION, HEADER_VALUE_CLIENT_VERSION)
.header(HEADER_KEY_DEVICE_TYPE, HEADER_VALUE_DEVICE_TYPE)
.build(),
)
}

View File

@@ -6,6 +6,7 @@ import com.x8bit.bitwarden.data.platform.datasource.network.core.ResultCallAdapt
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.AuthTokenInterceptor
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.BaseUrlInterceptor
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.BaseUrlInterceptors
import com.x8bit.bitwarden.data.platform.datasource.network.interceptor.HeadersInterceptor
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
@@ -18,6 +19,7 @@ import retrofit2.Retrofit
class RetrofitsImpl(
authTokenInterceptor: AuthTokenInterceptor,
baseUrlInterceptors: BaseUrlInterceptors,
headersInterceptor: HeadersInterceptor,
refreshAuthenticator: RefreshAuthenticator,
json: Json,
) : Retrofits {
@@ -70,6 +72,7 @@ class RetrofitsImpl(
private val baseOkHttpClient: OkHttpClient =
OkHttpClient.Builder()
.addInterceptor(headersInterceptor)
.build()
private val authenticatedOkHttpClient: OkHttpClient by lazy {

View File

@@ -1,5 +1,8 @@
package com.x8bit.bitwarden.data.platform.datasource.network.util
import android.os.Build
import com.x8bit.bitwarden.BuildConfig
/**
* The bearer prefix used for the 'authorization' headers value.
*/
@@ -9,3 +12,45 @@ const val HEADER_VALUE_BEARER_PREFIX: String = "Bearer "
* The key used for the 'authorization' headers.
*/
const val HEADER_KEY_AUTHORIZATION: String = "Authorization"
/**
* The key used for the 'user-agent' headers.
*/
const val HEADER_KEY_USER_AGENT: String = "User-Agent"
/**
* The value used for the 'user-agent' headers.
*/
@Suppress("MaxLineLength")
val HEADER_VALUE_USER_AGENT: String =
"Bitwarden_Mobile/${BuildConfig.VERSION_NAME} (Android ${Build.VERSION.RELEASE}; SDK ${Build.VERSION.SDK_INT}; Model ${Build.MODEL})"
/**
* The key used for the 'bitwarden-client-name' headers.
*/
const val HEADER_KEY_CLIENT_NAME: String = "Bitwarden-Client-Name"
/**
* The value used for the 'bitwarden-client-name' headers.
*/
const val HEADER_VALUE_CLIENT_NAME: String = "mobile"
/**
* The key used for the 'bitwarden-client-version' headers.
*/
const val HEADER_KEY_CLIENT_VERSION: String = "Bitwarden-Client-Version"
/**
* The value used for the 'bitwarden-client-version' headers.
*/
const val HEADER_VALUE_CLIENT_VERSION: String = BuildConfig.VERSION_NAME
/**
* The key used for the 'device-type' headers.
*/
const val HEADER_KEY_DEVICE_TYPE: String = "Device-Type"
/**
* The value used for the 'device-type' headers.
*/
const val HEADER_VALUE_DEVICE_TYPE: String = "0"