-
-
Notifications
You must be signed in to change notification settings - Fork 269
feat: Allow sign in with Steam Guard TOTP code #1241
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
base: master
Are you sure you want to change the base?
Changes from all commits
6a8c875
b4b81ae
0a6217c
b0a147e
f190827
0a7664f
e9d520f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package app.gamenative.service | ||
|
|
||
| import app.gamenative.enums.LoginResult | ||
| import app.gamenative.enums.LoginScreen | ||
| import app.gamenative.ui.data.UserLoginState | ||
| import `in`.dragonbra.javasteam.steam.authentication.IAuthenticator | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.update | ||
| import kotlinx.coroutines.launch | ||
| import timber.log.Timber | ||
| import kotlinx.coroutines.channels.Channel | ||
| import java.util.concurrent.CompletableFuture | ||
|
|
||
| class SteamAuthenticator(var loginState : MutableStateFlow<UserLoginState>, | ||
| var submitChannel : Channel<String>, | ||
| var viewModelScope : CoroutineScope) : IAuthenticator { | ||
|
|
||
| var useGuardTotp: Boolean = false | ||
|
|
||
| override fun acceptDeviceConfirmation(): CompletableFuture<Boolean> { | ||
| Timber.tag("UserLoginViewModel").i("Two-Factor, device confirmation") | ||
|
|
||
| if (useGuardTotp) { | ||
| useGuardTotp = false | ||
| return CompletableFuture.completedFuture(false) | ||
| } | ||
|
|
||
| loginState.update { currentState -> | ||
| currentState.copy( | ||
| loginResult = LoginResult.DeviceConfirm, | ||
| loginScreen = LoginScreen.TWO_FACTOR, | ||
| isLoggingIn = false, | ||
| lastTwoFactorMethod = "steam_guard", | ||
| ) | ||
| } | ||
|
|
||
| return CompletableFuture.completedFuture(true) | ||
| } | ||
|
|
||
| override fun getDeviceCode(previousCodeWasIncorrect: Boolean): CompletableFuture<String> { | ||
| Timber.tag("UserLoginViewModel").d("Two-Factor, device code") | ||
|
|
||
| loginState.update { currentState -> | ||
| currentState.copy( | ||
| loginResult = LoginResult.DeviceAuth, | ||
| loginScreen = LoginScreen.TWO_FACTOR, | ||
| isLoggingIn = false, | ||
| previousCodeIncorrect = previousCodeWasIncorrect, | ||
| lastTwoFactorMethod = "authenticator_code", | ||
| ) | ||
| } | ||
|
|
||
| return CompletableFuture<String>().apply { | ||
| viewModelScope.launch { | ||
|
peanut250 marked this conversation as resolved.
|
||
| val code = submitChannel.receive() | ||
| complete(code) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun getEmailCode( | ||
| email: String?, | ||
| previousCodeWasIncorrect: Boolean, | ||
| ): CompletableFuture<String> { | ||
| Timber.tag("UserLoginViewModel").d("Two-Factor, asking for email code") | ||
|
|
||
| loginState.update { currentState -> | ||
| currentState.copy( | ||
| loginResult = LoginResult.EmailAuth, | ||
| loginScreen = LoginScreen.TWO_FACTOR, | ||
| isLoggingIn = false, | ||
| email = email, | ||
| previousCodeIncorrect = previousCodeWasIncorrect, | ||
| lastTwoFactorMethod = "email_code", | ||
| ) | ||
| } | ||
|
|
||
| return CompletableFuture<String>().apply { | ||
| viewModelScope.launch { | ||
| val code = submitChannel.receive() | ||
| complete(code) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,11 @@ import app.gamenative.enums.LoginResult | |
| import app.gamenative.enums.LoginScreen | ||
| import app.gamenative.events.AndroidEvent | ||
| import app.gamenative.events.SteamEvent | ||
| import app.gamenative.service.SteamAuthenticator | ||
| import app.gamenative.service.SteamService | ||
| import app.gamenative.ui.data.UserLoginState | ||
| import app.gamenative.PrefManager | ||
| import com.posthog.PostHog | ||
| import `in`.dragonbra.javasteam.steam.authentication.IAuthenticator | ||
| import java.util.concurrent.CompletableFuture | ||
| import kotlinx.coroutines.channels.Channel | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
|
|
@@ -32,68 +31,7 @@ class UserLoginViewModel : ViewModel() { | |
|
|
||
| private val submitChannel = Channel<String>() | ||
|
|
||
| private val authenticator = object : IAuthenticator { | ||
| override fun acceptDeviceConfirmation(): CompletableFuture<Boolean> { | ||
| Timber.tag("UserLoginViewModel").i("Two-Factor, device confirmation") | ||
|
|
||
| _loginState.update { currentState -> | ||
| currentState.copy( | ||
| loginResult = LoginResult.DeviceConfirm, | ||
| loginScreen = LoginScreen.TWO_FACTOR, | ||
| isLoggingIn = false, | ||
| lastTwoFactorMethod = "steam_guard", | ||
| ) | ||
| } | ||
|
|
||
| return CompletableFuture.completedFuture(true) | ||
| } | ||
|
|
||
| override fun getDeviceCode(previousCodeWasIncorrect: Boolean): CompletableFuture<String> { | ||
| Timber.tag("UserLoginViewModel").d("Two-Factor, device code") | ||
|
|
||
| _loginState.update { currentState -> | ||
| currentState.copy( | ||
| loginResult = LoginResult.DeviceAuth, | ||
| loginScreen = LoginScreen.TWO_FACTOR, | ||
| isLoggingIn = false, | ||
| previousCodeIncorrect = previousCodeWasIncorrect, | ||
| lastTwoFactorMethod = "authenticator_code", | ||
| ) | ||
| } | ||
|
|
||
| return CompletableFuture<String>().apply { | ||
| viewModelScope.launch { | ||
| val code = submitChannel.receive() | ||
| complete(code) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun getEmailCode( | ||
| email: String?, | ||
| previousCodeWasIncorrect: Boolean, | ||
| ): CompletableFuture<String> { | ||
| Timber.tag("UserLoginViewModel").d("Two-Factor, asking for email code") | ||
|
|
||
| _loginState.update { currentState -> | ||
| currentState.copy( | ||
| loginResult = LoginResult.EmailAuth, | ||
| loginScreen = LoginScreen.TWO_FACTOR, | ||
| isLoggingIn = false, | ||
| email = email, | ||
| previousCodeIncorrect = previousCodeWasIncorrect, | ||
| lastTwoFactorMethod = "email_code", | ||
| ) | ||
| } | ||
|
|
||
| return CompletableFuture<String>().apply { | ||
| viewModelScope.launch { | ||
| val code = submitChannel.receive() | ||
| complete(code) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
-35
to
-96
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was the reason to pull this out into a file?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just organizational taste/readability since it is slightly expanded to allow for the TOTP code method. |
||
| private val authenticator = SteamAuthenticator(_loginState, submitChannel, viewModelScope) | ||
|
|
||
| private val onSteamConnected: (SteamEvent.Connected) -> Unit = { | ||
| Timber.i("Received is connected") | ||
|
|
@@ -321,6 +259,21 @@ class UserLoginViewModel : ViewModel() { | |
| } | ||
| } | ||
|
|
||
| fun useGuardTotp() { | ||
| authenticator.useGuardTotp = true | ||
|
peanut250 marked this conversation as resolved.
|
||
|
|
||
| with(_loginState.value) { | ||
| viewModelScope.launch { | ||
| SteamService.startLoginWithCredentials( | ||
| username = username, | ||
| password = password, | ||
| rememberSession = rememberSession, | ||
| authenticator = authenticator, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
peanut250 marked this conversation as resolved.
|
||
|
|
||
| fun retryConnection(context: Context) { | ||
| // Reset error/login state if needed | ||
| _loginState.update { currentState -> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.