11package com.coder.toolbox.views
22
33import com.coder.toolbox.CoderToolboxContext
4+ import com.coder.toolbox.util.toURL
45import com.coder.toolbox.views.state.AuthWizardState
56import com.jetbrains.toolbox.api.localization.LocalizableString
67import com.jetbrains.toolbox.api.ui.components.LabelField
@@ -9,14 +10,16 @@ import com.jetbrains.toolbox.api.ui.components.TextField
910import com.jetbrains.toolbox.api.ui.components.TextType
1011import com.jetbrains.toolbox.api.ui.components.ValidationErrorField
1112import kotlinx.coroutines.flow.update
13+ import java.net.MalformedURLException
1214
1315/* *
1416 * A page with a field for providing the Coder deployment URL.
1517 *
1618 * Populates with the provided URL, at which point the user can accept or
1719 * enter their own.
1820 */
19- class SignInStep (private val context : CoderToolboxContext ) : WizardStep {
21+ class SignInStep (private val context : CoderToolboxContext , private val notify : (String , Throwable ) -> Unit ) :
22+ WizardStep {
2023 private val urlField = TextField (context.i18n.ptrl(" Deployment URL" ), " " , TextType .General )
2124 private val descriptionField = LabelField (context.i18n.pnotr(" " ))
2225 private val errorField = ValidationErrorField (context.i18n.pnotr(" " ))
@@ -53,12 +56,28 @@ class SignInStep(private val context: CoderToolboxContext) : WizardStep {
5356 } else {
5457 url
5558 }
56-
59+ try {
60+ validateRawUrl(url)
61+ } catch (e: MalformedURLException ) {
62+ notify(" URL is invalid" , e)
63+ return false
64+ }
5765 context.secrets.lastDeploymentURL = url
5866 AuthWizardState .goToNextStep()
5967 return true
6068 }
6169
70+ /* *
71+ * Throws [MalformedURLException] if the given string violates RFC-2396
72+ */
73+ private fun validateRawUrl (url : String ) {
74+ try {
75+ url.toURL()
76+ } catch (e: Exception ) {
77+ throw MalformedURLException (e.message)
78+ }
79+ }
80+
6281 override fun onBack () {
6382 // it's the first step. Can't go anywhere back from here
6483 }
0 commit comments