-
-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05bbdc5
commit 9b65eee
Showing
7 changed files
with
252 additions
and
271 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
38 changes: 38 additions & 0 deletions
38
app/src/main/java/de/westnordost/streetcomplete/screens/user/login/LoginViewModel.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,38 @@ | ||
package de.westnordost.streetcomplete.screens.user.login | ||
|
||
import androidx.lifecycle.ViewModel | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
abstract class LoginViewModel : ViewModel() { | ||
abstract val unsyncedChangesCount: StateFlow<Int> | ||
|
||
abstract val loginState: StateFlow<LoginState> | ||
|
||
abstract val authorizationRequestUrl: String | ||
|
||
/** Starts the OAuth2 based login flow. */ | ||
abstract fun startLogin() | ||
|
||
/** Call when the web view / browser received an error when loading the (authorization) page */ | ||
abstract fun failAuthorization(url: String, errorCode: Int, description: String?) | ||
|
||
/** Returns whether the url is a redirect url destined for this OAuth authorization flow */ | ||
abstract fun isAuthorizationResponseUrl(url: String): Boolean | ||
|
||
/** Continues OAuth authorization flow with given redirect url */ | ||
abstract fun finishAuthorization(authorizationResponseUrl: String) | ||
|
||
/** Resets the login state to LoggedOut. Only works if current state is LoginError */ | ||
abstract fun resetLogin() | ||
} | ||
|
||
sealed interface LoginState | ||
data object LoggedOut : LoginState | ||
data object RequestingAuthorization : LoginState | ||
data object RetrievingAccessToken : LoginState | ||
enum class LoginError : LoginState { | ||
RequiredPermissionsNotGranted, | ||
CommunicationError | ||
} | ||
data object LoggedIn : LoginState | ||
|
105 changes: 105 additions & 0 deletions
105
app/src/main/java/de/westnordost/streetcomplete/screens/user/login/LoginViewModelImpl.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,105 @@ | ||
package de.westnordost.streetcomplete.screens.user.login | ||
|
||
import de.westnordost.streetcomplete.data.UnsyncedChangesCountSource | ||
import de.westnordost.streetcomplete.data.user.OAUTH2_AUTHORIZATION_URL | ||
import de.westnordost.streetcomplete.data.user.OAUTH2_CLIENT_ID | ||
import de.westnordost.streetcomplete.data.user.OAUTH2_REDIRECT_URI | ||
import de.westnordost.streetcomplete.data.user.OAUTH2_REQUESTED_SCOPES | ||
import de.westnordost.streetcomplete.data.user.OAUTH2_REQUIRED_SCOPES | ||
import de.westnordost.streetcomplete.data.user.OAUTH2_TOKEN_URL | ||
import de.westnordost.streetcomplete.data.user.UserLoginStatusController | ||
import de.westnordost.streetcomplete.data.user.UserUpdater | ||
import de.westnordost.streetcomplete.data.user.oauth.OAuthAuthorizationParams | ||
import de.westnordost.streetcomplete.data.user.oauth.OAuthException | ||
import de.westnordost.streetcomplete.data.user.oauth.OAuthService | ||
import de.westnordost.streetcomplete.data.user.oauth.extractAuthorizationCode | ||
import de.westnordost.streetcomplete.util.ktx.launch | ||
import de.westnordost.streetcomplete.util.logs.Log | ||
import kotlinx.coroutines.Dispatchers.IO | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.withContext | ||
|
||
class LoginViewModelImpl( | ||
private val unsyncedChangesCountSource: UnsyncedChangesCountSource, | ||
private val userLoginStatusController: UserLoginStatusController, | ||
private val oAuthService: OAuthService, | ||
private val userUpdater: UserUpdater | ||
) : LoginViewModel() { | ||
override val loginState = MutableStateFlow<LoginState>(LoggedOut) | ||
override val unsyncedChangesCount = MutableStateFlow(0) | ||
|
||
override val authorizationRequestUrl: String get() = oAuth.authorizationRequestUrl | ||
|
||
private val oAuth = OAuthAuthorizationParams( | ||
OAUTH2_AUTHORIZATION_URL, | ||
OAUTH2_TOKEN_URL, | ||
OAUTH2_CLIENT_ID, | ||
OAUTH2_REQUESTED_SCOPES, | ||
OAUTH2_REDIRECT_URI | ||
) | ||
|
||
init { | ||
launch(IO) { | ||
unsyncedChangesCount.update { unsyncedChangesCountSource.getCount() } | ||
} | ||
} | ||
|
||
override fun startLogin() { | ||
loginState.compareAndSet(LoggedOut, RequestingAuthorization) | ||
} | ||
|
||
override fun failAuthorization(url: String, errorCode: Int, description: String?) { | ||
Log.e(TAG, "Error for URL " + url + if (description != null) ": $description" else "") | ||
loginState.compareAndSet(RequestingAuthorization, LoginError.CommunicationError) | ||
} | ||
|
||
override fun isAuthorizationResponseUrl(url: String): Boolean = | ||
oAuth.itsForMe(url) | ||
|
||
override fun finishAuthorization(authorizationResponseUrl: String) { | ||
launch { | ||
val accessToken = retrieveAccessToken(authorizationResponseUrl) | ||
if (accessToken != null) { | ||
login(accessToken) | ||
} | ||
} | ||
} | ||
|
||
private suspend fun retrieveAccessToken(authorizationResponseUrl: String): String? { | ||
try { | ||
loginState.value = RetrievingAccessToken | ||
val authorizationCode = extractAuthorizationCode(authorizationResponseUrl) | ||
val accessTokenResponse = withContext(IO) { | ||
oAuthService.retrieveAccessToken(oAuth, authorizationCode) | ||
} | ||
if (accessTokenResponse.grantedScopes?.containsAll(OAUTH2_REQUIRED_SCOPES) == false) { | ||
loginState.value = LoginError.RequiredPermissionsNotGranted | ||
return null | ||
} | ||
return accessTokenResponse.accessToken | ||
} catch (e: Exception) { | ||
if (e is OAuthException && e.error == "access_denied") { | ||
loginState.value = LoginError.RequiredPermissionsNotGranted | ||
} else { | ||
Log.e(TAG, "Error during authorization", e) | ||
loginState.value = LoginError.CommunicationError | ||
} | ||
return null | ||
} | ||
} | ||
|
||
private suspend fun login(accessToken: String) { | ||
loginState.value = LoggedIn | ||
userLoginStatusController.logIn(accessToken) | ||
userUpdater.update() | ||
} | ||
|
||
override fun resetLogin() { | ||
if (loginState.value is LoginError) loginState.value = LoggedOut | ||
} | ||
|
||
companion object { | ||
private const val TAG = "Login" | ||
} | ||
} |
Oops, something went wrong.