1
1
package com.coder.toolbox.views
2
2
3
3
import com.coder.toolbox.CoderToolboxContext
4
+ import com.coder.toolbox.util.toURL
4
5
import com.coder.toolbox.views.state.AuthWizardState
5
6
import com.jetbrains.toolbox.api.localization.LocalizableString
6
7
import com.jetbrains.toolbox.api.ui.components.LabelField
@@ -9,14 +10,16 @@ import com.jetbrains.toolbox.api.ui.components.TextField
9
10
import com.jetbrains.toolbox.api.ui.components.TextType
10
11
import com.jetbrains.toolbox.api.ui.components.ValidationErrorField
11
12
import kotlinx.coroutines.flow.update
13
+ import java.net.MalformedURLException
12
14
13
15
/* *
14
16
* A page with a field for providing the Coder deployment URL.
15
17
*
16
18
* Populates with the provided URL, at which point the user can accept or
17
19
* enter their own.
18
20
*/
19
- class SignInStep (private val context : CoderToolboxContext ) : WizardStep {
21
+ class SignInStep (private val context : CoderToolboxContext , private val notify : (String , Throwable ) -> Unit ) :
22
+ WizardStep {
20
23
private val urlField = TextField (context.i18n.ptrl(" Deployment URL" ), " " , TextType .General )
21
24
private val descriptionField = LabelField (context.i18n.pnotr(" " ))
22
25
private val errorField = ValidationErrorField (context.i18n.pnotr(" " ))
@@ -53,12 +56,28 @@ class SignInStep(private val context: CoderToolboxContext) : WizardStep {
53
56
} else {
54
57
url
55
58
}
56
-
59
+ try {
60
+ validateRawUrl(url)
61
+ } catch (e: MalformedURLException ) {
62
+ notify(" URL is invalid" , e)
63
+ return false
64
+ }
57
65
context.secrets.lastDeploymentURL = url
58
66
AuthWizardState .goToNextStep()
59
67
return true
60
68
}
61
69
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
+
62
81
override fun onBack () {
63
82
// it's the first step. Can't go anywhere back from here
64
83
}
0 commit comments