From 6ab04bde038f07b711a522e88277c034187df804 Mon Sep 17 00:00:00 2001 From: Jonathan Koren Date: Tue, 28 Sep 2021 14:24:55 -0700 Subject: [PATCH] Add User-Agent header (#39) Change-Id: I9306b1dc140620d9dfb59357e1acc4017d4112b3 --- .../src/main/java/com/example/android/fido2/Fido2App.kt | 5 ++++- .../com/example/android/fido2/api/AddHeaderInterceptor.kt | 4 +++- .../app/src/main/java/com/example/android/fido2/Fido2App.kt | 5 ++++- .../com/example/android/fido2/api/AddHeaderInterceptor.kt | 4 +++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/android/app-start/src/main/java/com/example/android/fido2/Fido2App.kt b/android/app-start/src/main/java/com/example/android/fido2/Fido2App.kt index eeb0de6..f005649 100644 --- a/android/app-start/src/main/java/com/example/android/fido2/Fido2App.kt +++ b/android/app-start/src/main/java/com/example/android/fido2/Fido2App.kt @@ -17,6 +17,7 @@ package com.example.android.fido2 import android.app.Application +import android.os.Build import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.PreferenceDataStoreFactory import androidx.datastore.preferences.core.Preferences @@ -43,8 +44,10 @@ object AppModule { @Singleton @Provides fun provideOkHttpClient() : OkHttpClient { + val userAgent = "${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} " + + "(Android ${Build.VERSION.RELEASE}; ${Build.MODEL}; ${Build.BRAND})" return OkHttpClient.Builder() - .addInterceptor(AddHeaderInterceptor()) + .addInterceptor(AddHeaderInterceptor(userAgent)) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(40, TimeUnit.SECONDS) .connectTimeout(40, TimeUnit.SECONDS) diff --git a/android/app-start/src/main/java/com/example/android/fido2/api/AddHeaderInterceptor.kt b/android/app-start/src/main/java/com/example/android/fido2/api/AddHeaderInterceptor.kt index 31ffa5d..c0c2844 100644 --- a/android/app-start/src/main/java/com/example/android/fido2/api/AddHeaderInterceptor.kt +++ b/android/app-start/src/main/java/com/example/android/fido2/api/AddHeaderInterceptor.kt @@ -19,10 +19,12 @@ package com.example.android.fido2.api import okhttp3.Interceptor import okhttp3.Response -internal class AddHeaderInterceptor : Interceptor { +internal class AddHeaderInterceptor(private val userAgent: String) : Interceptor { + override fun intercept(chain: Interceptor.Chain): Response { return chain.proceed( chain.request().newBuilder() + .header("User-Agent", userAgent) .header("X-Requested-With", "XMLHttpRequest") .build() ) diff --git a/android/app/src/main/java/com/example/android/fido2/Fido2App.kt b/android/app/src/main/java/com/example/android/fido2/Fido2App.kt index b5598bc..006fa11 100644 --- a/android/app/src/main/java/com/example/android/fido2/Fido2App.kt +++ b/android/app/src/main/java/com/example/android/fido2/Fido2App.kt @@ -17,6 +17,7 @@ package com.example.android.fido2 import android.app.Application +import android.os.Build import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.PreferenceDataStoreFactory import androidx.datastore.preferences.core.Preferences @@ -43,8 +44,10 @@ object AppModule { @Singleton @Provides fun provideOkHttpClient() : OkHttpClient { + val userAgent = "${BuildConfig.APPLICATION_ID}/${BuildConfig.VERSION_NAME} " + + "(Android ${Build.VERSION.RELEASE}; ${Build.MODEL}; ${Build.BRAND})" return OkHttpClient.Builder() - .addInterceptor(AddHeaderInterceptor()) + .addInterceptor(AddHeaderInterceptor(userAgent)) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(40, TimeUnit.SECONDS) .connectTimeout(40, TimeUnit.SECONDS) diff --git a/android/app/src/main/java/com/example/android/fido2/api/AddHeaderInterceptor.kt b/android/app/src/main/java/com/example/android/fido2/api/AddHeaderInterceptor.kt index 31ffa5d..c0c2844 100644 --- a/android/app/src/main/java/com/example/android/fido2/api/AddHeaderInterceptor.kt +++ b/android/app/src/main/java/com/example/android/fido2/api/AddHeaderInterceptor.kt @@ -19,10 +19,12 @@ package com.example.android.fido2.api import okhttp3.Interceptor import okhttp3.Response -internal class AddHeaderInterceptor : Interceptor { +internal class AddHeaderInterceptor(private val userAgent: String) : Interceptor { + override fun intercept(chain: Interceptor.Chain): Response { return chain.proceed( chain.request().newBuilder() + .header("User-Agent", userAgent) .header("X-Requested-With", "XMLHttpRequest") .build() )