-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: unify access token refreshing logic [WPB-5038] (#2141)
- Loading branch information
1 parent
a71fb6d
commit 2168dee
Showing
38 changed files
with
1,003 additions
and
200 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
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
39 changes: 39 additions & 0 deletions
39
logic/src/commonMain/kotlin/com/wire/kalium/logic/data/session/token/AccessToken.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,39 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* 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 com.wire.kalium.logic.data.session.token | ||
|
||
import kotlin.jvm.JvmInline | ||
|
||
internal data class AccessTokenRefreshResult( | ||
val accessToken: AccessToken, | ||
val refreshToken: RefreshToken | ||
) | ||
|
||
/** | ||
* Represents an access token, which is used for authentication and authorization purposes. | ||
* | ||
* @property value The value of the access token. | ||
* @property tokenType The type of the access token. _e.g._ "Bearer" | ||
*/ | ||
data class AccessToken( | ||
val value: String, | ||
val tokenType: String | ||
) | ||
|
||
@JvmInline | ||
value class RefreshToken(val value: String) |
90 changes: 90 additions & 0 deletions
90
...c/src/commonMain/kotlin/com/wire/kalium/logic/data/session/token/AccessTokenRepository.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,90 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* 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 com.wire.kalium.logic.data.session.token | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.NetworkFailure | ||
import com.wire.kalium.logic.StorageFailure | ||
import com.wire.kalium.logic.data.id.toDao | ||
import com.wire.kalium.logic.data.user.UserId | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.functional.map | ||
import com.wire.kalium.logic.wrapApiRequest | ||
import com.wire.kalium.logic.wrapStorageRequest | ||
import com.wire.kalium.network.api.base.authenticated.AccessTokenApi | ||
import com.wire.kalium.persistence.client.AuthTokenStorage | ||
|
||
internal interface AccessTokenRepository { | ||
/** | ||
* Retrieves a new access token using the provided refresh token and client ID. | ||
* | ||
* If provided, the new token will be associated with this client ID. | ||
* If the client is remotely removed by the user, the tokens will be invalidated. | ||
* Future refreshes will keep the previously associated client ID. | ||
* _i.e._ after the first refresh, the client ID doesn't need to be provided anymore. | ||
* | ||
* @param refreshToken The refresh token to use for obtaining a new access token. | ||
* @param clientId The optional client ID. | ||
* @return Either a [CoreFailure] or the new access token. | ||
*/ | ||
suspend fun getNewAccessToken( | ||
refreshToken: String, | ||
clientId: String? = null | ||
): Either<NetworkFailure, AccessTokenRefreshResult> | ||
|
||
/** | ||
* Persists the access token and refresh token in the repository. | ||
* | ||
* @param accessToken The access token to persist. | ||
* @param refreshToken The refresh token to persist. | ||
* @return Either a [CoreFailure] if the operation fails, or [Unit] if the tokens are successfully persisted. | ||
*/ | ||
suspend fun persistTokens( | ||
accessToken: AccessToken, | ||
refreshToken: RefreshToken | ||
): Either<CoreFailure, Unit> | ||
} | ||
|
||
internal class AccessTokenRepositoryImpl( | ||
private val userId: UserId, | ||
private val accessTokenApi: AccessTokenApi, | ||
private val authTokenStorage: AuthTokenStorage, | ||
) : AccessTokenRepository { | ||
override suspend fun getNewAccessToken( | ||
refreshToken: String, | ||
clientId: String? | ||
): Either<NetworkFailure, AccessTokenRefreshResult> = wrapApiRequest { | ||
accessTokenApi.getToken(refreshToken, clientId) | ||
}.map { (accessTokenDTO, newRefreshToken) -> | ||
val token = AccessToken(accessTokenDTO.value, accessTokenDTO.tokenType) | ||
val resolvedRefreshToken = newRefreshToken?.value ?: refreshToken | ||
AccessTokenRefreshResult(token, RefreshToken(resolvedRefreshToken)) | ||
} | ||
|
||
override suspend fun persistTokens( | ||
accessToken: AccessToken, | ||
refreshToken: RefreshToken | ||
): Either<StorageFailure, Unit> = wrapStorageRequest { | ||
authTokenStorage.updateToken( | ||
userId.toDao(), | ||
accessToken.value, | ||
accessToken.tokenType, | ||
refreshToken.value | ||
) | ||
}.map { } | ||
} |
39 changes: 39 additions & 0 deletions
39
...ommonMain/kotlin/com/wire/kalium/logic/data/session/token/AccessTokenRepositoryFactory.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,39 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* 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 com.wire.kalium.logic.data.session.token | ||
|
||
import com.wire.kalium.logic.data.user.UserId | ||
import com.wire.kalium.network.api.base.authenticated.AccessTokenApi | ||
import com.wire.kalium.persistence.client.AuthTokenStorage | ||
|
||
/** | ||
* Interface for creating an [AccessTokenRepository] instance. | ||
* Allows intaking a dynamic [AccessTokenApi] for its construction. | ||
*/ | ||
internal interface AccessTokenRepositoryFactory { | ||
fun create(tokenApi: AccessTokenApi): AccessTokenRepository | ||
} | ||
|
||
internal class AccessTokenRepositoryFactoryImpl( | ||
private val userId: UserId, | ||
private val tokenStorage: AuthTokenStorage | ||
) : AccessTokenRepositoryFactory { | ||
override fun create(tokenApi: AccessTokenApi): AccessTokenRepository { | ||
return AccessTokenRepositoryImpl(userId, tokenApi, tokenStorage) | ||
} | ||
} |
Oops, something went wrong.