android: use EditPrefs instead of passing UpdatePrefs to start

This allows us to precisely set the options we need during login
and avoid wiping away defaults like AllowSingleHosts.

Updates tailscale/tailscale#11731

Signed-off-by: Percy Wegmann <percy@tailscale.com>
pull/371/head
Percy Wegmann 7 months ago committed by Percy Wegmann
parent 9703d48f1a
commit 31b0ec8865

@ -85,8 +85,11 @@ open class IpnViewModel : ViewModel() {
// Login/Logout // Login/Logout
fun login(options: Ipn.Options = Ipn.Options(), completionHandler: (Result<Unit>) -> Unit = {}) { fun login(
maskedPrefs: Ipn.MaskedPrefs? = null,
authKey: String? = null,
completionHandler: (Result<Unit>) -> Unit = {}
) {
MDMSettings.loginURL.flow.value?.let { MDMSettings.loginURL.flow.value?.let {
Log.d(TAG, "Using MDM derived control URL: $it") Log.d(TAG, "Using MDM derived control URL: $it")
loginWithCustomControlURL(it, completionHandler) loginWithCustomControlURL(it, completionHandler)
@ -102,21 +105,23 @@ open class IpnViewModel : ViewModel() {
} }
} }
Client(viewModelScope).start(options) { start -> val startAction = {
Client(viewModelScope).start(Ipn.Options(AuthKey = authKey)) { start ->
start.onFailure { completionHandler(Result.failure(it)) }.onSuccess { loginAction() } start.onFailure { completionHandler(Result.failure(it)) }.onSuccess { loginAction() }
} }
} }
fun loginWithAuthKey(authKey: String, completionHandler: (Result<Unit>) -> Unit = {}) { maskedPrefs?.let { prefs ->
val prefs = Notifier.prefs.value Client(viewModelScope).editPrefs(prefs) { result ->
if (prefs == null) { result.onFailure { completionHandler(Result.failure(it)) }.onSuccess { startAction() }
completionHandler(Result.failure(Error("no prefs"))) }
return } ?: run { startAction() }
} }
fun loginWithAuthKey(authKey: String, completionHandler: (Result<Unit>) -> Unit = {}) {
val prefs = Ipn.MaskedPrefs()
prefs.WantRunning = true prefs.WantRunning = true
val options = Ipn.Options(AuthKey = authKey, UpdatePrefs = prefs) login(prefs, authKey = authKey, completionHandler)
login(options, completionHandler)
} }
fun loginWithCustomControlURL( fun loginWithCustomControlURL(
@ -125,21 +130,14 @@ open class IpnViewModel : ViewModel() {
) { ) {
val fail: (Throwable) -> Unit = { completionHandler(Result.failure(it)) } val fail: (Throwable) -> Unit = { completionHandler(Result.failure(it)) }
// We need to have the current prefs to set them back with the new control URL
val prefs = Notifier.prefs.value
if (prefs == null) {
fail(Error("no prefs"))
return
}
// The flow for logging in with a custom control URL is to add a profile, // The flow for logging in with a custom control URL is to add a profile,
// call start with prefs that include the control URL, then // call start with prefs that include the control URL, then
// start an interactive login. // start an interactive login.
Client(viewModelScope).addProfile { addProfile -> Client(viewModelScope).addProfile { profile ->
addProfile.onFailure(fail).onSuccess { profile.onFailure(fail).onSuccess {
val prefs = Ipn.MaskedPrefs()
prefs.ControlURL = controlURL prefs.ControlURL = controlURL
val options = Ipn.Options(UpdatePrefs = prefs) login(prefs, authKey = null, completionHandler)
login(options, completionHandler)
} }
} }
} }

Loading…
Cancel
Save