Skip to content
1 change: 1 addition & 0 deletions Auth/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ android {
}

dependencies {
implementation(project(":Core"))
implementation(project(":Core:Network"))
implementation(project(":Core:Sentry"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,23 @@ abstract class ApiRepositoryCore {
withEmails: Boolean = false,
withPhones: Boolean = false,
withSecurity: Boolean = false,
): ApiResponse<User> {
var with = ""
if (withEmails) with += "emails"
if (withPhones) with += "phones"
if (withSecurity) with += "security"
if (with.isNotEmpty()) with = "?with=$with"
): ApiResponse<User> = ApiRepositoryCore.getUserProfile(okHttpClient, withEmails, withPhones, withSecurity)

val url = "${ApiRoutesCore.getUserProfile()}$with"
return ApiController.callApi(url, ApiController.ApiMethod.GET, okHttpClient = okHttpClient)
companion object {
suspend fun getUserProfile(
okHttpClient: OkHttpClient,
withEmails: Boolean = false,
withPhones: Boolean = false,
withSecurity: Boolean = false,
): ApiResponse<User> {
var with = ""
if (withEmails) with += "emails"
if (withPhones) with += "phones"
if (withSecurity) with += "security"
if (with.isNotEmpty()) with = "?with=$with"

val url = "${ApiRoutesCore.getUserProfile()}$with"
return ApiController.callApi(url, ApiController.ApiMethod.GET, okHttpClient = okHttpClient)
}
}
}
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
}
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 Auth/src/main/kotlin/com/infomaniak/core/auth/models/UserResult.kt
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 Auth/src/main/kotlin/com/infomaniak/core/auth/utils/LoginUtils.kt
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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ abstract class BaseCrossAppLoginViewModel(applicationId: String, clientId: Strin
createElement = { account -> async { getFirstValidTokenOrError(account) } },
)

private val apiRepository = object : ApiRepositoryCore() {}

private val baseOkHttpClient by lazy {
OkHttpClient.Builder().apply {
addCache()
Expand Down Expand Up @@ -191,7 +189,7 @@ abstract class BaseCrossAppLoginViewModel(applicationId: String, clientId: Strin

val customTokenHttpClient = baseOkHttpClient.addInterceptor(CustomTokenInterceptor(token)).build()

when (apiRepository.getUserProfile(customTokenHttpClient).data) {
when (ApiRepositoryCore.getUserProfile(customTokenHttpClient).data) {
is User -> {
// Put the just checked token first. Will speed up later derivation attempts.
val tokens = setOf(token) + account.tokens
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum class InternalTranslatedErrorCode(
) : ErrorCodeTranslated {
NoConnection("no_connection", R.string.noConnection),
ConnectionError("connection_error", R.string.connectionError),
ServerError("server_error", R.string.serverError),
UnknownError("an_error_has_occurred", R.string.anErrorHasOccurred),
UserLoggedOut("user_logged_out", R.string.anErrorHasOccurred), // We don't display this error
UserAlreadyPresent("user_already_present", R.string.errorUserAlreadyPresent),
Expand Down
1 change: 1 addition & 0 deletions Network/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<string name="connectionError">Problem mit der Internetverbindung</string>
<string name="errorUserAlreadyPresent">Fehler Benutzer bereits vorhanden</string>
<string name="noConnection">Keine Verbindung</string>
<string name="serverError">Server-Fehler</string>
</resources>
1 change: 1 addition & 0 deletions Network/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<string name="connectionError">Problema de conexión a Internet</string>
<string name="errorUserAlreadyPresent">Error, usuario ya registrado</string>
<string name="noConnection">Sin conexión</string>
<string name="serverError">Error del servidor</string>
</resources>
1 change: 1 addition & 0 deletions Network/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<string name="connectionError">Problème de connexion internet</string>
<string name="errorUserAlreadyPresent">Erreur utilisateur déjà présent</string>
<string name="noConnection">Pas de connexion</string>
<string name="serverError">Erreur serveur</string>
</resources>
1 change: 1 addition & 0 deletions Network/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<string name="connectionError">Problema di connessione a Internet</string>
<string name="errorUserAlreadyPresent">Errore utente già esistente</string>
<string name="noConnection">Nessuna connessione</string>
<string name="serverError">Errore del server</string>
</resources>
3 changes: 2 additions & 1 deletion Network/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<resources>
<string name="anErrorHasOccurred">An error has occurred</string>
<string name="connectionError">Internet connection problem</string>
<string name="errorUserAlreadyPresent">Error, User already present</string>
<string name="errorUserAlreadyPresent">Error, user already present</string>
<string name="noConnection">No connection</string>
<string name="serverError">Server Error</string>
</resources>
Loading