Create AccessTokenProvider
This commit is contained in:
parent
6c9c3e5cb3
commit
f489265ce7
|
@ -12,6 +12,7 @@
|
||||||
<w>fdroid</w>
|
<w>fdroid</w>
|
||||||
<w>gplay</w>
|
<w>gplay</w>
|
||||||
<w>hmac</w>
|
<w>hmac</w>
|
||||||
|
<w>homeserver</w>
|
||||||
<w>ktlint</w>
|
<w>ktlint</w>
|
||||||
<w>linkified</w>
|
<w>linkified</w>
|
||||||
<w>linkify</w>
|
<w>linkify</w>
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.matrix.android.internal.di
|
||||||
|
|
||||||
|
import javax.inject.Qualifier
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(AnnotationRetention.RUNTIME)
|
||||||
|
annotation class HomeserverAccessToken
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(AnnotationRetention.RUNTIME)
|
||||||
|
annotation class IdentityServerAccessToken
|
||||||
|
|
|
@ -16,20 +16,16 @@
|
||||||
|
|
||||||
package im.vector.matrix.android.internal.network
|
package im.vector.matrix.android.internal.network
|
||||||
|
|
||||||
import im.vector.matrix.android.internal.auth.SessionParamsStore
|
import im.vector.matrix.android.internal.network.token.AccessTokenProvider
|
||||||
import im.vector.matrix.android.internal.di.SessionId
|
|
||||||
import okhttp3.Interceptor
|
import okhttp3.Interceptor
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
internal class AccessTokenInterceptor @Inject constructor(
|
internal class AccessTokenInterceptor(private val accessTokenProvider: AccessTokenProvider) : Interceptor {
|
||||||
@SessionId private val sessionId: String,
|
|
||||||
private val sessionParamsStore: SessionParamsStore) : Interceptor {
|
|
||||||
|
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
var request = chain.request()
|
var request = chain.request()
|
||||||
|
|
||||||
accessToken?.let {
|
accessTokenProvider.getToken()?.let {
|
||||||
val newRequestBuilder = request.newBuilder()
|
val newRequestBuilder = request.newBuilder()
|
||||||
// Add the access token to all requests if it is set
|
// Add the access token to all requests if it is set
|
||||||
newRequestBuilder.addHeader(HttpHeaders.Authorization, "Bearer $it")
|
newRequestBuilder.addHeader(HttpHeaders.Authorization, "Bearer $it")
|
||||||
|
@ -38,7 +34,4 @@ internal class AccessTokenInterceptor @Inject constructor(
|
||||||
|
|
||||||
return chain.proceed(request)
|
return chain.proceed(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val accessToken
|
|
||||||
get() = sessionParamsStore.get(sessionId)?.credentials?.accessToken
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.matrix.android.internal.network.token
|
||||||
|
|
||||||
|
interface AccessTokenProvider {
|
||||||
|
fun getToken(): String?
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.matrix.android.internal.network.token
|
||||||
|
|
||||||
|
import im.vector.matrix.android.internal.auth.SessionParamsStore
|
||||||
|
|
||||||
|
internal class HomeserverAccessTokenProvider(
|
||||||
|
private val sessionId: String,
|
||||||
|
private val sessionParamsStore: SessionParamsStore
|
||||||
|
) : AccessTokenProvider {
|
||||||
|
override fun getToken() = sessionParamsStore.get(sessionId)?.credentials?.accessToken
|
||||||
|
}
|
|
@ -36,6 +36,7 @@ import im.vector.matrix.android.api.session.accountdata.AccountDataService
|
||||||
import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilitiesService
|
import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilitiesService
|
||||||
import im.vector.matrix.android.api.session.securestorage.SecureStorageService
|
import im.vector.matrix.android.api.session.securestorage.SecureStorageService
|
||||||
import im.vector.matrix.android.api.session.securestorage.SharedSecretStorageService
|
import im.vector.matrix.android.api.session.securestorage.SharedSecretStorageService
|
||||||
|
import im.vector.matrix.android.internal.auth.SessionParamsStore
|
||||||
import im.vector.matrix.android.internal.crypto.secrets.DefaultSharedSecretStorageService
|
import im.vector.matrix.android.internal.crypto.secrets.DefaultSharedSecretStorageService
|
||||||
import im.vector.matrix.android.internal.crypto.verification.VerificationMessageLiveObserver
|
import im.vector.matrix.android.internal.crypto.verification.VerificationMessageLiveObserver
|
||||||
import im.vector.matrix.android.internal.database.LiveEntityObserver
|
import im.vector.matrix.android.internal.database.LiveEntityObserver
|
||||||
|
@ -43,6 +44,7 @@ import im.vector.matrix.android.internal.database.RealmKeysUtils
|
||||||
import im.vector.matrix.android.internal.database.SessionRealmConfigurationFactory
|
import im.vector.matrix.android.internal.database.SessionRealmConfigurationFactory
|
||||||
import im.vector.matrix.android.internal.di.Authenticated
|
import im.vector.matrix.android.internal.di.Authenticated
|
||||||
import im.vector.matrix.android.internal.di.DeviceId
|
import im.vector.matrix.android.internal.di.DeviceId
|
||||||
|
import im.vector.matrix.android.internal.di.HomeserverAccessToken
|
||||||
import im.vector.matrix.android.internal.di.IdentityDatabase
|
import im.vector.matrix.android.internal.di.IdentityDatabase
|
||||||
import im.vector.matrix.android.internal.di.SessionCacheDirectory
|
import im.vector.matrix.android.internal.di.SessionCacheDirectory
|
||||||
import im.vector.matrix.android.internal.di.SessionDatabase
|
import im.vector.matrix.android.internal.di.SessionDatabase
|
||||||
|
@ -60,6 +62,8 @@ import im.vector.matrix.android.internal.network.NetworkConnectivityChecker
|
||||||
import im.vector.matrix.android.internal.network.PreferredNetworkCallbackStrategy
|
import im.vector.matrix.android.internal.network.PreferredNetworkCallbackStrategy
|
||||||
import im.vector.matrix.android.internal.network.RetrofitFactory
|
import im.vector.matrix.android.internal.network.RetrofitFactory
|
||||||
import im.vector.matrix.android.internal.network.interceptors.CurlLoggingInterceptor
|
import im.vector.matrix.android.internal.network.interceptors.CurlLoggingInterceptor
|
||||||
|
import im.vector.matrix.android.internal.network.token.AccessTokenProvider
|
||||||
|
import im.vector.matrix.android.internal.network.token.HomeserverAccessTokenProvider
|
||||||
import im.vector.matrix.android.internal.session.group.GroupSummaryUpdater
|
import im.vector.matrix.android.internal.session.group.GroupSummaryUpdater
|
||||||
import im.vector.matrix.android.internal.session.homeserver.DefaultHomeServerCapabilitiesService
|
import im.vector.matrix.android.internal.session.homeserver.DefaultHomeServerCapabilitiesService
|
||||||
import im.vector.matrix.android.internal.session.identity.db.IdentityRealmModule
|
import im.vector.matrix.android.internal.session.identity.db.IdentityRealmModule
|
||||||
|
@ -195,14 +199,14 @@ internal abstract class SessionModule {
|
||||||
@SessionScope
|
@SessionScope
|
||||||
@Authenticated
|
@Authenticated
|
||||||
fun providesOkHttpClient(@Unauthenticated okHttpClient: OkHttpClient,
|
fun providesOkHttpClient(@Unauthenticated okHttpClient: OkHttpClient,
|
||||||
accessTokenInterceptor: AccessTokenInterceptor): OkHttpClient {
|
@Authenticated accessTokenProvider: AccessTokenProvider): OkHttpClient {
|
||||||
return okHttpClient.newBuilder()
|
return okHttpClient.newBuilder()
|
||||||
.apply {
|
.apply {
|
||||||
// Remove the previous CurlLoggingInterceptor, to add it after the accessTokenInterceptor
|
// Remove the previous CurlLoggingInterceptor, to add it after the accessTokenInterceptor
|
||||||
val existingCurlInterceptors = interceptors().filterIsInstance<CurlLoggingInterceptor>()
|
val existingCurlInterceptors = interceptors().filterIsInstance<CurlLoggingInterceptor>()
|
||||||
interceptors().removeAll(existingCurlInterceptors)
|
interceptors().removeAll(existingCurlInterceptors)
|
||||||
|
|
||||||
addInterceptor(accessTokenInterceptor)
|
addInterceptor(AccessTokenInterceptor(accessTokenProvider))
|
||||||
|
|
||||||
// Re add eventually the curl logging interceptors
|
// Re add eventually the curl logging interceptors
|
||||||
existingCurlInterceptors.forEach {
|
existingCurlInterceptors.forEach {
|
||||||
|
@ -212,6 +216,14 @@ internal abstract class SessionModule {
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
@Provides
|
||||||
|
@Authenticated
|
||||||
|
fun providesAccessTokenProvider(@SessionId sessionId: String,
|
||||||
|
sessionParamsStore: SessionParamsStore): AccessTokenProvider {
|
||||||
|
return HomeserverAccessTokenProvider(sessionId, sessionParamsStore)
|
||||||
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@Provides
|
@Provides
|
||||||
@SessionScope
|
@SessionScope
|
||||||
|
@ -253,6 +265,10 @@ internal abstract class SessionModule {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Binds
|
||||||
|
@HomeserverAccessToken
|
||||||
|
abstract fun bindAccessTokenProvider(provider: HomeserverAccessTokenProvider): AccessTokenProvider
|
||||||
|
|
||||||
@Binds
|
@Binds
|
||||||
abstract fun bindSession(session: DefaultSession): Session
|
abstract fun bindSession(session: DefaultSession): Session
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue