-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Update RemoteDataSource for CTXSpend
- Loading branch information
1 parent
8d2fdc3
commit 7da3ecd
Showing
5 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
70 changes: 70 additions & 0 deletions
70
...rc/main/java/org/dash/wallet/features/exploredash/repository/remote/TokenAuthenticator.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2021 Dash Core Group. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.dash.wallet.features.exploredash.repository.remote | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import okhttp3.Authenticator | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okhttp3.Route | ||
import org.dash.wallet.common.data.ResponseResource | ||
import org.dash.wallet.common.data.safeApiCall | ||
import org.dash.wallet.features.exploredash.data.ctxspend.model.RefreshTokenResponse | ||
import org.dash.wallet.features.exploredash.network.service.ctxspend.CTXSpendTokenApi | ||
import org.dash.wallet.features.exploredash.utils.CTXSpendConfig | ||
import javax.inject.Inject | ||
|
||
class TokenAuthenticator @Inject constructor( | ||
private val tokenApi: CTXSpendTokenApi, | ||
private val config: CTXSpendConfig | ||
) : Authenticator { | ||
|
||
// For multiple call to refresh token sync | ||
private val tokenMutex = Mutex() | ||
|
||
override fun authenticate(route: Route?, response: Response): Request? { | ||
return runBlocking { | ||
tokenMutex.withLock { | ||
when (val tokenResponse = getUpdatedToken()) { | ||
is ResponseResource.Success -> { | ||
tokenResponse.value?.let { | ||
config.set(CTXSpendConfig.PREFS_KEY_ACCESS_TOKEN, it.accessToken?: "") | ||
config.set(CTXSpendConfig.PREFS_KEY_REFRESH_TOKEN, it.refreshToken?: "") | ||
response.request.newBuilder() | ||
.header("Authorization", "Bearer ${it.accessToken}") | ||
.build() | ||
} | ||
} | ||
|
||
else -> { | ||
config.set(CTXSpendConfig.PREFS_KEY_ACCESS_TOKEN, "") | ||
config.set(CTXSpendConfig.PREFS_KEY_REFRESH_TOKEN, "") | ||
null | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun getUpdatedToken(): ResponseResource<RefreshTokenResponse?> { | ||
val accessToken = config.get(CTXSpendConfig.PREFS_KEY_ACCESS_TOKEN) ?: "" | ||
val refreshToken = config.get(CTXSpendConfig.PREFS_KEY_REFRESH_TOKEN) ?: "" | ||
return safeApiCall { tokenApi.refreshToken(refreshToken = refreshToken) } | ||
} | ||
} |
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