Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2015add

Browse files
committed
chore: refactor classes to reflect we are not actually doing auth
1 parent 9ab03a3 commit 2015add

File tree

8 files changed

+69
-68
lines changed

8 files changed

+69
-68
lines changed

src/main/kotlin/com/coder/toolbox/CoderRemoteProvider.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import com.coder.toolbox.util.CoderProtocolHandler
99
import com.coder.toolbox.util.DialogUi
1010
import com.coder.toolbox.util.withPath
1111
import com.coder.toolbox.views.Action
12-
import com.coder.toolbox.views.AuthWizardPage
12+
import com.coder.toolbox.views.CoderCliSetupWizardPage
1313
import com.coder.toolbox.views.CoderSettingsPage
1414
import com.coder.toolbox.views.NewEnvironmentPage
15-
import com.coder.toolbox.views.state.AuthWizardState
15+
import com.coder.toolbox.views.state.CoderCliSetupWizardState
1616
import com.coder.toolbox.views.state.WizardStep
1717
import com.jetbrains.toolbox.api.core.ui.icons.SvgIcon
1818
import com.jetbrains.toolbox.api.core.ui.icons.SvgIcon.IconType
@@ -242,7 +242,7 @@ class CoderRemoteProvider(
242242
environments.value = LoadableState.Value(emptyList())
243243
isInitialized.update { false }
244244
client = null
245-
AuthWizardState.resetSteps()
245+
CoderCliSetupWizardState.resetSteps()
246246
}
247247

248248
override val svgIcon: SvgIcon =
@@ -301,7 +301,7 @@ class CoderRemoteProvider(
301301
*/
302302
override suspend fun handleUri(uri: URI) {
303303
linkHandler.handle(
304-
uri, shouldDoAutoLogin(),
304+
uri, shouldDoAutoSetup(),
305305
{
306306
coderHeaderPage.isBusyCreatingNewEnvironment.update {
307307
true
@@ -343,17 +343,17 @@ class CoderRemoteProvider(
343343
* list.
344344
*/
345345
override fun getOverrideUiPage(): UiPage? {
346-
// Show sign in page if we have not configured the client yet.
346+
// Show the setup page if we have not configured the client yet.
347347
if (client == null) {
348348
val errorBuffer = mutableListOf<Throwable>()
349349
// When coming back to the application, initializeSession immediately.
350-
val autologin = shouldDoAutoLogin()
350+
val autoSetup = shouldDoAutoSetup()
351351
context.secrets.lastToken.let { lastToken ->
352352
context.secrets.lastDeploymentURL.let { lastDeploymentURL ->
353-
if (autologin && lastDeploymentURL.isNotBlank() && (lastToken.isNotBlank() || !settings.requireTokenAuth)) {
353+
if (autoSetup && lastDeploymentURL.isNotBlank() && (lastToken.isNotBlank() || !settings.requireTokenAuth)) {
354354
try {
355-
AuthWizardState.goToStep(WizardStep.LOGIN)
356-
return AuthWizardPage(context, settingsPage, visibilityState, true, ::onConnect)
355+
CoderCliSetupWizardState.goToStep(WizardStep.CONNECT)
356+
return CoderCliSetupWizardPage(context, settingsPage, visibilityState, true, ::onConnect)
357357
} catch (ex: Exception) {
358358
errorBuffer.add(ex)
359359
}
@@ -363,18 +363,19 @@ class CoderRemoteProvider(
363363
firstRun = false
364364

365365
// Login flow.
366-
val authWizard = AuthWizardPage(context, settingsPage, visibilityState, onConnect = ::onConnect)
366+
val setupWizardPage =
367+
CoderCliSetupWizardPage(context, settingsPage, visibilityState, onConnect = ::onConnect)
367368
// We might have navigated here due to a polling error.
368369
errorBuffer.forEach {
369-
authWizard.notify("Error encountered", it)
370+
setupWizardPage.notify("Error encountered", it)
370371
}
371372
// and now reset the errors, otherwise we show it every time on the screen
372-
return authWizard
373+
return setupWizardPage
373374
}
374375
return null
375376
}
376377

377-
private fun shouldDoAutoLogin(): Boolean = firstRun && context.secrets.rememberMe == true
378+
private fun shouldDoAutoSetup(): Boolean = firstRun && context.secrets.rememberMe == true
378379

379380
private suspend fun onConnect(client: CoderRestClient, cli: CoderCLIManager) {
380381
// Store the URL and token for use next time.

src/main/kotlin/com/coder/toolbox/views/AuthWizardPage.kt renamed to src/main/kotlin/com/coder/toolbox/views/CoderCliSetupWizardPage.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import com.coder.toolbox.cli.CoderCLIManager
55
import com.coder.toolbox.sdk.CoderRestClient
66
import com.coder.toolbox.sdk.ex.APIResponseException
77
import com.coder.toolbox.util.toURL
8-
import com.coder.toolbox.views.state.AuthContext
9-
import com.coder.toolbox.views.state.AuthWizardState
8+
import com.coder.toolbox.views.state.CoderCliSetupContext
9+
import com.coder.toolbox.views.state.CoderCliSetupWizardState
1010
import com.coder.toolbox.views.state.WizardStep
1111
import com.jetbrains.toolbox.api.remoteDev.ProviderVisibilityState
1212
import com.jetbrains.toolbox.api.ui.actions.RunnableActionDescription
@@ -16,26 +16,26 @@ import kotlinx.coroutines.flow.update
1616
import kotlinx.coroutines.launch
1717
import java.util.UUID
1818

19-
class AuthWizardPage(
19+
class CoderCliSetupWizardPage(
2020
private val context: CoderToolboxContext,
2121
private val settingsPage: CoderSettingsPage,
2222
private val visibilityState: MutableStateFlow<ProviderVisibilityState>,
23-
initialAutoLogin: Boolean = false,
23+
initialAutoSetup: Boolean = false,
2424
onConnect: suspend (
2525
client: CoderRestClient,
2626
cli: CoderCLIManager,
2727
) -> Unit,
2828
) : CoderPage(context.i18n.ptrl("Setting up Coder"), false) {
29-
private val shouldAutoLogin = MutableStateFlow(initialAutoLogin)
29+
private val shouldAutoSetup = MutableStateFlow(initialAutoSetup)
3030
private val settingsAction = Action(context.i18n.ptrl("Settings"), actionBlock = {
3131
context.ui.showUiPage(settingsPage)
3232
})
3333

34-
private val signInStep = SignInStep(context, this::notify)
34+
private val deploymentUrlStep = DeploymentUrlStep(context, this::notify)
3535
private val tokenStep = TokenStep(context)
3636
private val connectStep = ConnectStep(
3737
context,
38-
shouldAutoLogin,
38+
shouldAutoSetup,
3939
this::notify,
4040
this::displaySteps,
4141
onConnect
@@ -50,9 +50,9 @@ class AuthWizardPage(
5050
private val errorBuffer = mutableListOf<Throwable>()
5151

5252
init {
53-
if (shouldAutoLogin.value) {
54-
AuthContext.url = context.secrets.lastDeploymentURL.toURL()
55-
AuthContext.token = context.secrets.lastToken
53+
if (shouldAutoSetup.value) {
54+
CoderCliSetupContext.url = context.secrets.lastDeploymentURL.toURL()
55+
CoderCliSetupContext.token = context.secrets.lastToken
5656
}
5757
}
5858

@@ -67,22 +67,22 @@ class AuthWizardPage(
6767
}
6868

6969
private fun displaySteps() {
70-
when (AuthWizardState.currentStep()) {
70+
when (CoderCliSetupWizardState.currentStep()) {
7171
WizardStep.URL_REQUEST -> {
7272
fields.update {
73-
listOf(signInStep.panel)
73+
listOf(deploymentUrlStep.panel)
7474
}
7575
actionButtons.update {
7676
listOf(
77-
Action(context.i18n.ptrl("Sign In"), closesPage = false, actionBlock = {
78-
if (signInStep.onNext()) {
77+
Action(context.i18n.ptrl("Next"), closesPage = false, actionBlock = {
78+
if (deploymentUrlStep.onNext()) {
7979
displaySteps()
8080
}
8181
}),
8282
settingsAction
8383
)
8484
}
85-
signInStep.onVisible()
85+
deploymentUrlStep.onVisible()
8686
}
8787

8888
WizardStep.TOKEN_REQUEST -> {
@@ -106,7 +106,7 @@ class AuthWizardPage(
106106
tokenStep.onVisible()
107107
}
108108

109-
WizardStep.LOGIN -> {
109+
WizardStep.CONNECT -> {
110110
fields.update {
111111
listOf(connectStep.panel)
112112
}
@@ -115,7 +115,7 @@ class AuthWizardPage(
115115
settingsAction,
116116
Action(context.i18n.ptrl("Back"), closesPage = false, actionBlock = {
117117
connectStep.onBack()
118-
shouldAutoLogin.update {
118+
shouldAutoSetup.update {
119119
false
120120
}
121121
displaySteps()
@@ -150,7 +150,7 @@ class AuthWizardPage(
150150
context.cs.launch {
151151
context.ui.showSnackbar(
152152
UUID.randomUUID().toString(),
153-
context.i18n.ptrl("Error encountered during authentication"),
153+
context.i18n.ptrl("Error encountered while setting up Coder"),
154154
context.i18n.pnotr(textError ?: ""),
155155
context.i18n.ptrl("Dismiss")
156156
)

src/main/kotlin/com/coder/toolbox/views/ConnectStep.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import com.coder.toolbox.cli.CoderCLIManager
55
import com.coder.toolbox.cli.ensureCLI
66
import com.coder.toolbox.plugin.PluginManager
77
import com.coder.toolbox.sdk.CoderRestClient
8-
import com.coder.toolbox.views.state.AuthContext
9-
import com.coder.toolbox.views.state.AuthWizardState
8+
import com.coder.toolbox.views.state.CoderCliSetupContext
9+
import com.coder.toolbox.views.state.CoderCliSetupWizardState
1010
import com.jetbrains.toolbox.api.localization.LocalizableString
1111
import com.jetbrains.toolbox.api.ui.components.LabelField
1212
import com.jetbrains.toolbox.api.ui.components.RowGroup
@@ -50,27 +50,27 @@ class ConnectStep(
5050
context.i18n.pnotr("")
5151
}
5252

53-
if (AuthContext.isNotReadyForAuth()) {
53+
if (CoderCliSetupContext.isNotReadyForAuth()) {
5454
errorField.textState.update {
5555
context.i18n.pnotr("URL and token were not properly configured. Please go back and provide a proper URL and token!")
5656
}
5757
return
5858
}
5959

60-
statusField.textState.update { context.i18n.pnotr("Connecting to ${AuthContext.url!!.host}...") }
60+
statusField.textState.update { context.i18n.pnotr("Connecting to ${CoderCliSetupContext.url!!.host}...") }
6161
connect()
6262
}
6363

6464
/**
6565
* Try connecting to Coder with the provided URL and token.
6666
*/
6767
private fun connect() {
68-
if (!AuthContext.hasUrl()) {
68+
if (!CoderCliSetupContext.hasUrl()) {
6969
errorField.textState.update { context.i18n.ptrl("URL is required") }
7070
return
7171
}
7272

73-
if (!AuthContext.hasToken()) {
73+
if (!CoderCliSetupContext.hasToken()) {
7474
errorField.textState.update { context.i18n.ptrl("Token is required") }
7575
return
7676
}
@@ -79,8 +79,8 @@ class ConnectStep(
7979
try {
8080
val client = CoderRestClient(
8181
context,
82-
AuthContext.url!!,
83-
AuthContext.token!!,
82+
CoderCliSetupContext.url!!,
83+
CoderCliSetupContext.token!!,
8484
PluginManager.pluginInfo.version,
8585
)
8686
// allows interleaving with the back/cancel action
@@ -100,20 +100,20 @@ class ConnectStep(
100100
yield()
101101
cli.login(client.token)
102102
}
103-
statusField.textState.update { (context.i18n.ptrl("Successfully configured ${AuthContext.url!!.host}...")) }
103+
statusField.textState.update { (context.i18n.ptrl("Successfully configured ${CoderCliSetupContext.url!!.host}...")) }
104104
// allows interleaving with the back/cancel action
105105
yield()
106-
AuthContext.reset()
107-
AuthWizardState.resetSteps()
106+
CoderCliSetupContext.reset()
107+
CoderCliSetupWizardState.resetSteps()
108108
onConnect(client, cli)
109109
} catch (ex: CancellationException) {
110110
if (ex.message != USER_HIT_THE_BACK_BUTTON) {
111-
notify("Connection to ${AuthContext.url!!.host} was configured", ex)
111+
notify("Connection to ${CoderCliSetupContext.url!!.host} was configured", ex)
112112
onBack()
113113
refreshWizard()
114114
}
115115
} catch (ex: Exception) {
116-
notify("Failed to configure ${AuthContext.url!!.host}", ex)
116+
notify("Failed to configure ${CoderCliSetupContext.url!!.host}", ex)
117117
onBack()
118118
refreshWizard()
119119
}
@@ -129,11 +129,11 @@ class ConnectStep(
129129
signInJob?.cancel(CancellationException(USER_HIT_THE_BACK_BUTTON))
130130
} finally {
131131
if (shouldAutoLogin.value) {
132-
AuthContext.reset()
133-
AuthWizardState.resetSteps()
132+
CoderCliSetupContext.reset()
133+
CoderCliSetupWizardState.resetSteps()
134134
context.secrets.rememberMe = false
135135
} else {
136-
AuthWizardState.goToPreviousStep()
136+
CoderCliSetupWizardState.goToPreviousStep()
137137
}
138138
}
139139
}

src/main/kotlin/com/coder/toolbox/views/SignInStep.kt renamed to src/main/kotlin/com/coder/toolbox/views/DeploymentUrlStep.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.coder.toolbox.views
22

33
import com.coder.toolbox.CoderToolboxContext
44
import com.coder.toolbox.util.toURL
5-
import com.coder.toolbox.views.state.AuthContext
6-
import com.coder.toolbox.views.state.AuthWizardState
5+
import com.coder.toolbox.views.state.CoderCliSetupContext
6+
import com.coder.toolbox.views.state.CoderCliSetupWizardState
77
import com.jetbrains.toolbox.api.localization.LocalizableString
88
import com.jetbrains.toolbox.api.ui.components.RowGroup
99
import com.jetbrains.toolbox.api.ui.components.TextField
@@ -19,7 +19,7 @@ import java.net.URL
1919
* Populates with the provided URL, at which point the user can accept or
2020
* enter their own.
2121
*/
22-
class SignInStep(
22+
class DeploymentUrlStep(
2323
private val context: CoderToolboxContext,
2424
private val notify: (String, Throwable) -> Unit
2525
) :
@@ -32,7 +32,7 @@ class SignInStep(
3232
RowGroup.RowField(errorField)
3333
)
3434

35-
override val nextButtonTitle: LocalizableString? = context.i18n.ptrl("Sign In")
35+
override val nextButtonTitle: LocalizableString? = context.i18n.ptrl("Next")
3636

3737
override fun onVisible() {
3838
errorField.textState.update {
@@ -55,12 +55,12 @@ class SignInStep(
5555
url
5656
}
5757
try {
58-
AuthContext.url = validateRawUrl(url)
58+
CoderCliSetupContext.url = validateRawUrl(url)
5959
} catch (e: MalformedURLException) {
6060
notify("URL is invalid", e)
6161
return false
6262
}
63-
AuthWizardState.goToNextStep()
63+
CoderCliSetupWizardState.goToNextStep()
6464
return true
6565
}
6666

src/main/kotlin/com/coder/toolbox/views/TokenStep.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.coder.toolbox.views
22

33
import com.coder.toolbox.CoderToolboxContext
44
import com.coder.toolbox.util.withPath
5-
import com.coder.toolbox.views.state.AuthContext
6-
import com.coder.toolbox.views.state.AuthWizardState
5+
import com.coder.toolbox.views.state.CoderCliSetupContext
6+
import com.coder.toolbox.views.state.CoderCliSetupWizardState
77
import com.jetbrains.toolbox.api.localization.LocalizableString
88
import com.jetbrains.toolbox.api.ui.components.LinkField
99
import com.jetbrains.toolbox.api.ui.components.RowGroup
@@ -37,9 +37,9 @@ class TokenStep(
3737
errorField.textState.update {
3838
context.i18n.pnotr("")
3939
}
40-
if (AuthContext.hasUrl()) {
40+
if (CoderCliSetupContext.hasUrl()) {
4141
tokenField.textState.update {
42-
context.secrets.tokenFor(AuthContext.url!!) ?: ""
42+
context.secrets.tokenFor(CoderCliSetupContext.url!!) ?: ""
4343
}
4444
} else {
4545
errorField.textState.update {
@@ -48,7 +48,7 @@ class TokenStep(
4848
}
4949
}
5050
(linkField.urlState as MutableStateFlow).update {
51-
AuthContext.url!!.withPath("/login?redirect=%2Fcli-auth")?.toString() ?: ""
51+
CoderCliSetupContext.url!!.withPath("/login?redirect=%2Fcli-auth")?.toString() ?: ""
5252
}
5353
}
5454

@@ -59,12 +59,12 @@ class TokenStep(
5959
return false
6060
}
6161

62-
AuthContext.token = token
63-
AuthWizardState.goToNextStep()
62+
CoderCliSetupContext.token = token
63+
CoderCliSetupWizardState.goToNextStep()
6464
return true
6565
}
6666

6767
override fun onBack() {
68-
AuthWizardState.goToPreviousStep()
68+
CoderCliSetupWizardState.goToPreviousStep()
6969
}
7070
}

src/main/kotlin/com/coder/toolbox/views/state/AuthContext.kt renamed to src/main/kotlin/com/coder/toolbox/views/state/CoderCliSetupContext.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package com.coder.toolbox.views.state
33
import java.net.URL
44

55
/**
6-
* Singleton that holds authentication context (URL and token) across multiple
6+
* Singleton that holds Coder CLI setup context (URL and token) across multiple
77
* Toolbox window lifecycle events.
88
*
99
* This ensures that user input (URL and token) is not lost when the Toolbox
1010
* window is temporarily closed or recreated.
1111
*/
12-
object AuthContext {
12+
object CoderCliSetupContext {
1313
/**
1414
* The currently entered URL.
1515
*/

0 commit comments

Comments
 (0)