Skip to content
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

Login with custom server & preauth key #520

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions android/src/main/java/com/tailscale/ipn/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import com.tailscale.ipn.ui.view.ExitNodePicker
import com.tailscale.ipn.ui.view.HealthView
import com.tailscale.ipn.ui.view.IntroView
import com.tailscale.ipn.ui.view.LoginQRView
import com.tailscale.ipn.ui.view.LoginWithAuthKeyView
import com.tailscale.ipn.ui.view.LoginWithCustomControlURLView
import com.tailscale.ipn.ui.view.MDMSettingsDebugView
import com.tailscale.ipn.ui.view.MainView
Expand Down Expand Up @@ -249,9 +248,6 @@ class MainActivity : ComponentActivity() {
composable("intro", exitTransition = { fadeOut(animationSpec = tween(150)) }) {
IntroView(backTo("main"))
}
composable("loginWithAuthKey") {
LoginWithAuthKeyView(onNavigateHome = backTo("main"), backTo("userSwitcher"))
}
composable("loginWithCustomControl") {
LoginWithCustomControlURLView(
onNavigateHome = backTo("main"), backTo("userSwitcher"))
Expand Down
62 changes: 26 additions & 36 deletions android/src/main/java/com/tailscale/ipn/ui/view/CustomLogin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import androidx.compose.ui.unit.dp
import com.tailscale.ipn.R
import com.tailscale.ipn.ui.theme.listItem
import com.tailscale.ipn.ui.util.set
import com.tailscale.ipn.ui.viewModel.LoginWithAuthKeyViewModel
import com.tailscale.ipn.ui.viewModel.LoginWithCustomControlURLViewModel

data class LoginViewStrings(
Expand Down Expand Up @@ -69,50 +68,20 @@ fun LoginWithCustomControlURLView(
LoginView(
innerPadding = innerPadding,
strings = strings,
onSubmitAction = { viewModel.setControlURL(it, onNavigateHome) })
}
}

@Composable
fun LoginWithAuthKeyView(
onNavigateHome: BackNavigation,
backToSettings: BackNavigation,
viewModel: LoginWithAuthKeyViewModel = LoginWithAuthKeyViewModel()
) {

Scaffold(
topBar = {
Header(
R.string.add_account,
onBack = backToSettings,
onSubmitAction = {it: String, it2: String -> viewModel.setControlURL(it, it2, onNavigateHome) }
)
}) { innerPadding ->
val error by viewModel.errorDialog.collectAsState()
val strings =
LoginViewStrings(
title = stringResource(id = R.string.auth_key_title),
explanation = stringResource(id = R.string.auth_key_explanation),
inputTitle = stringResource(id = R.string.auth_key_input_title),
placeholder = stringResource(id = R.string.auth_key_placeholder),
)
// Show the error overlay if need be
error?.let { ErrorDialog(type = it, action = { viewModel.errorDialog.set(null) }) }

LoginView(
innerPadding = innerPadding,
strings = strings,
onSubmitAction = { viewModel.setAuthKey(it, onNavigateHome) })
}
}

@Composable
fun LoginView(
innerPadding: PaddingValues = PaddingValues(16.dp),
strings: LoginViewStrings,
onSubmitAction: (String) -> Unit,
onSubmitAction: (it: String, it2: String) -> Unit,
) {

var textVal by remember { mutableStateOf("") }
var textVal by remember { mutableStateOf("") }
var textVal2 by remember { mutableStateOf("") }

Column(
modifier =
Expand Down Expand Up @@ -144,12 +113,33 @@ fun LoginView(
)
})

ListItem(
colors = MaterialTheme.colorScheme.listItem,
headlineContent = { Text(text = "Preauth Key") }, // TODO: clean this up
supportingContent = {
OutlinedTextField(
modifier = Modifier.fillMaxWidth(),
colors = TextFieldDefaults.colors(
focusedContainerColor = Color.Transparent,
unfocusedContainerColor = Color.Transparent,
),
textStyle = MaterialTheme.typography.bodyMedium,
value = textVal2,
onValueChange = { textVal2 = it },
placeholder = {
Text("Key", style = MaterialTheme.typography.bodySmall) // TODO: clean this up
},
keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.None)
)
}
)

ListItem(
colors = MaterialTheme.colorScheme.listItem,
headlineContent = {
Box(modifier = Modifier.fillMaxWidth()) {
Button(
onClick = { onSubmitAction(textVal) },
onClick = { onSubmitAction(textVal, textVal2) },
content = { Text(stringResource(id = R.string.add_account_short)) })
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,6 @@ fun FusMenu(
viewModel.showHeaderMenu.set(false)
},
text = stringResource(id = R.string.custom_control_menu))
MenuItem(
onClick = {
onAuthKeyClick()
viewModel.showHeaderMenu.set(false)
},
text = stringResource(id = R.string.auth_key_menu))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,17 @@ open class CustomLoginViewModel : IpnViewModel() {
val errorDialog: StateFlow<ErrorDialogType?> = MutableStateFlow(null)
}

class LoginWithAuthKeyViewModel : CustomLoginViewModel() {
// Sets the auth key and invokes the login flow
fun setAuthKey(authKey: String, onSuccess: () -> Unit) {
// The most basic of checks for auth key syntax
if (authKey.isEmpty()) {
errorDialog.set(ErrorDialogType.INVALID_AUTH_KEY)
return
}
loginWithAuthKey(authKey) {
it.onFailure { errorDialog.set(ErrorDialogType.ADD_PROFILE_FAILED) }
it.onSuccess { onSuccess() }
}
}
}

class LoginWithCustomControlURLViewModel : CustomLoginViewModel() {
// Sets the custom control URL and invokes the login flow
fun setControlURL(urlStr: String, onSuccess: () -> Unit) {
fun setControlURL(urlStr: String, authKey: String, onSuccess: () -> Unit) {
// Some basic checks that the entered URL is "reasonable". The underlying
// localAPIClient will use the default server if we give it a broken URL,
// but we can make sure we can construct a URL from the input string and
// ensure it has an http/https scheme
if (authKey.isEmpty()) {
errorDialog.set(ErrorDialogType.INVALID_AUTH_KEY)
return
}
when (urlStr.startsWith("http", ignoreCase = true) &&
urlStr.contains("://") &&
urlStr.length > 7) {
Expand All @@ -44,7 +33,7 @@ class LoginWithCustomControlURLViewModel : CustomLoginViewModel() {
return
}
true -> {
loginWithCustomControlURL(urlStr) {
loginWithCustomControlURLAuthKey(urlStr, authKey) {
it.onFailure { errorDialog.set(ErrorDialogType.ADD_PROFILE_FAILED) }
it.onSuccess { onSuccess() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,15 @@ open class IpnViewModel : ViewModel() {
} ?: run { startAction() }
}

fun loginWithAuthKey(authKey: String, completionHandler: (Result<Unit>) -> Unit = {}) {
val prefs = Ipn.MaskedPrefs()
prefs.WantRunning = true
login(prefs, authKey = authKey, completionHandler)
}

fun loginWithCustomControlURL(
fun loginWithCustomControlURLAuthKey(
controlURL: String,
authKey: String,
completionHandler: (Result<Unit>) -> Unit = {}
) {
val prefs = Ipn.MaskedPrefs()
prefs.ControlURL = controlURL
login(prefs, completionHandler = completionHandler)
prefs.WantRunning = true
login(prefs, authKey = authKey, completionHandler = completionHandler)
}

fun logout(completionHandler: (Result<String>) -> Unit = {}) {
Expand Down