-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Extract login logic #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cec97c7
feat: Add ServerError InternalTranslatedErrorCode for authentication …
LunarX 80bb5d0
feat: Start extracting shared logic to Core module
LunarX 856c6c2
refactor: Split cross app independent login logic to its own independ…
LunarX e6f485f
refactor: Move login logic to Auth module and remove other modules
LunarX e73fb72
refactor: Clean LoginUtils
LunarX ebf529f
refactor: Adapt code now that getUsersByToken is private
LunarX a17dbd7
feat: Rethrow cancelation exceptions
LunarX 6706f3f
refactor: Access the new static ApiRepositoryCore.getUserProfile() me…
LunarX 5ddec9a
refactor: Move models to their correct package
LunarX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
24 changes: 24 additions & 0 deletions
24
Auth/src/main/kotlin/com/infomaniak/core/auth/models/AuthCodeResult.kt
sirambd marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,24 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2025 Infomaniak Network SA | ||
| * | ||
| * 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.infomaniak.core.auth.models | ||
|
|
||
| internal sealed interface AuthCodeResult { | ||
| data class Success(val code: String) : AuthCodeResult | ||
| data class Error(val message: String) : AuthCodeResult | ||
| data object Canceled : AuthCodeResult | ||
| } |
25 changes: 25 additions & 0 deletions
25
Auth/src/main/kotlin/com/infomaniak/core/auth/models/UserLoginResult.kt
LunarX marked this conversation as resolved.
Show resolved
Hide resolved
LunarX marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,25 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2025 Infomaniak Network SA | ||
| * | ||
| * 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.infomaniak.core.auth.models | ||
|
|
||
| import com.infomaniak.core.auth.models.user.User | ||
|
|
||
| sealed interface UserLoginResult { | ||
| data class Success(val user: User) : UserLoginResult | ||
| data class Failure(val errorMessage: String) : UserLoginResult | ||
| } |
36 changes: 36 additions & 0 deletions
36
Auth/src/main/kotlin/com/infomaniak/core/auth/models/UserResult.kt
sirambd marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,36 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2025 Infomaniak Network SA | ||
| * | ||
| * 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.infomaniak.core.auth.models | ||
|
|
||
| import com.infomaniak.core.auth.models.user.User | ||
| import com.infomaniak.core.network.api.ApiController.toApiError | ||
| import com.infomaniak.core.network.api.InternalTranslatedErrorCode | ||
| import com.infomaniak.core.network.models.ApiResponse | ||
| import com.infomaniak.core.network.models.ApiResponseStatus | ||
|
|
||
| internal sealed interface UserResult { | ||
| data class Success(val user: User) : UserResult | ||
| open class Failure(val apiResponse: ApiResponse<*>) : UserResult { | ||
| data object Unknown : Failure( | ||
| ApiResponse<Unit>( | ||
| result = ApiResponseStatus.ERROR, | ||
| error = InternalTranslatedErrorCode.UnknownError.toApiError() | ||
| ) | ||
| ) | ||
| } | ||
| } |
154 changes: 154 additions & 0 deletions
154
Auth/src/main/kotlin/com/infomaniak/core/auth/utils/LoginUtils.kt
LunarX marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,154 @@ | ||
| /* | ||
| * Infomaniak Core - Android | ||
| * Copyright (C) 2025 Infomaniak Network SA | ||
| * | ||
| * 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.infomaniak.core.auth.utils | ||
|
|
||
| import android.content.Context | ||
| import androidx.activity.result.ActivityResult | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import com.infomaniak.core.auth.CredentialManager | ||
| import com.infomaniak.core.auth.TokenAuthenticator.Companion.changeAccessToken | ||
| import com.infomaniak.core.auth.api.ApiRepositoryCore | ||
| import com.infomaniak.core.auth.models.AuthCodeResult | ||
| import com.infomaniak.core.auth.models.UserLoginResult | ||
| import com.infomaniak.core.auth.models.UserResult | ||
| import com.infomaniak.core.auth.utils.LoginUtils.getLoginResultAfterWebView | ||
| import com.infomaniak.core.auth.utils.LoginUtils.getLoginResultsAfterCrossApp | ||
| import com.infomaniak.core.cancellable | ||
| import com.infomaniak.core.network.api.ApiController.toApiError | ||
| import com.infomaniak.core.network.api.InternalTranslatedErrorCode | ||
| import com.infomaniak.core.network.models.ApiResponse | ||
| import com.infomaniak.core.network.models.ApiResponseStatus | ||
| import com.infomaniak.core.network.networking.HttpClient | ||
| import com.infomaniak.core.network.utils.ApiErrorCode.Companion.translateError | ||
| import com.infomaniak.lib.login.ApiToken | ||
| import com.infomaniak.lib.login.InfomaniakLogin | ||
| import com.infomaniak.lib.login.InfomaniakLogin.ErrorStatus | ||
| import com.infomaniak.lib.login.InfomaniakLogin.TokenResult | ||
|
|
||
| object LoginUtils { | ||
| /** | ||
| * Logs the user based on the ActivityResult of our login WebView. | ||
| * | ||
| * @return a [UserLoginResult] or null if the user canceled the login webview without going through to the end | ||
| */ | ||
| suspend fun getLoginResultAfterWebView( | ||
| result: ActivityResult, | ||
| context: Context, | ||
| infomaniakLogin: InfomaniakLogin, | ||
| credentialManager: CredentialManager, | ||
| ): UserLoginResult? { | ||
| val authCodeResult = result.toAuthCodeResult(context) | ||
| when (authCodeResult) { | ||
| is AuthCodeResult.Error -> return UserLoginResult.Failure(authCodeResult.message) | ||
| is AuthCodeResult.Canceled -> return null | ||
| is AuthCodeResult.Success -> Unit | ||
| } | ||
|
|
||
| val tokenResult = infomaniakLogin.getToken(okHttpClient = HttpClient.okHttpClient, code = authCodeResult.code) | ||
| when (tokenResult) { | ||
| is TokenResult.Error -> return UserLoginResult.Failure(context.formatAuthErrorMessage(tokenResult.errorStatus)) | ||
| is TokenResult.Success -> Unit | ||
| } | ||
|
|
||
| val userResult = getUsersByToken(listOf(tokenResult.apiToken), credentialManager).single() | ||
| return when (userResult) { | ||
| is UserResult.Failure -> { | ||
| UserLoginResult.Failure(context.getString(userResult.apiResponse.translateError())) | ||
| } | ||
| is UserResult.Success -> UserLoginResult.Success(userResult.user) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Logs the user based on the [ApiToken]s returned by the cross app login logic. | ||
| * | ||
| * @return a [UserLoginResult] or null if the user canceled the login webview without going through to the end | ||
| */ | ||
| suspend fun getLoginResultsAfterCrossApp( | ||
| apiTokens: List<ApiToken>, | ||
| context: Context, | ||
| credentialManager: CredentialManager, | ||
| ): List<UserLoginResult> = getUsersByToken(apiTokens, credentialManager).map { result -> | ||
| when (result) { | ||
| is UserResult.Success -> UserLoginResult.Success(result.user) | ||
| is UserResult.Failure -> UserLoginResult.Failure(context.getString(result.apiResponse.translateError())) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This method is the basis on which [getLoginResultAfterWebView] and its alternative [getLoginResultsAfterCrossApp] are built. | ||
| */ | ||
| private suspend fun getUsersByToken( | ||
| apiTokens: List<ApiToken>, | ||
| credentialManager: CredentialManager, | ||
| ): List<UserResult> = apiTokens.map { apiToken -> | ||
| runCatching { | ||
| authenticateUser(apiToken, credentialManager) | ||
| }.cancellable().getOrDefault(UserResult.Failure.Unknown) | ||
| } | ||
|
|
||
| private fun ActivityResult.toAuthCodeResult(context: Context): AuthCodeResult { | ||
| if (resultCode != AppCompatActivity.RESULT_OK) return AuthCodeResult.Canceled | ||
|
|
||
| val authCode = data?.getStringExtra(InfomaniakLogin.CODE_TAG) | ||
| val translatedError = data?.getStringExtra(InfomaniakLogin.ERROR_TRANSLATED_TAG) | ||
|
|
||
| return when { | ||
| translatedError?.isNotBlank() == true -> AuthCodeResult.Error(translatedError) | ||
| authCode?.isNotBlank() == true -> AuthCodeResult.Success(authCode) | ||
| else -> AuthCodeResult.Error(context.getString(InternalTranslatedErrorCode.UnknownError.translateRes)) | ||
| } | ||
| } | ||
|
|
||
| private fun Context.formatAuthErrorMessage(errorStatus: ErrorStatus): String { | ||
| return getString( | ||
| when (errorStatus) { | ||
| ErrorStatus.SERVER -> InternalTranslatedErrorCode.ServerError | ||
| ErrorStatus.CONNECTION -> InternalTranslatedErrorCode.ConnectionError | ||
| else -> InternalTranslatedErrorCode.UnknownError | ||
| }.translateRes | ||
| ) | ||
| } | ||
|
|
||
| private suspend fun authenticateUser(apiToken: ApiToken, credentialManager: CredentialManager): UserResult { | ||
| if (credentialManager.getUserById(apiToken.userId) != null) return UserResult.Failure( | ||
| getErrorResponse(InternalTranslatedErrorCode.UserAlreadyPresent) | ||
| ) | ||
|
|
||
| val okhttpClient = HttpClient.okHttpClient.newBuilder().addInterceptor { chain -> | ||
| val newRequest = changeAccessToken(chain.request(), apiToken) | ||
| chain.proceed(newRequest) | ||
| }.build() | ||
|
|
||
| val userProfileResponse = ApiRepositoryCore.getUserProfile(okhttpClient) | ||
|
|
||
| if (userProfileResponse.result == ApiResponseStatus.ERROR) return UserResult.Failure(userProfileResponse) | ||
| val userData = userProfileResponse.data ?: return UserResult.Failure.Unknown | ||
|
|
||
| val user = userData.apply { | ||
| this.apiToken = apiToken | ||
| this.organizations = arrayListOf() | ||
| } | ||
|
|
||
| return UserResult.Success(user) | ||
| } | ||
|
|
||
| private fun getErrorResponse(error: InternalTranslatedErrorCode): ApiResponse<Unit> { | ||
| return ApiResponse(result = ApiResponseStatus.ERROR, error = error.toApiError()) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.