Skip to content

Commit ac91ba5

Browse files
committed
Show actual error in token dialog
Technically we only retried on auth failure so the hardcoded error message should be correct, but seems better to relay the actual error message.
1 parent 4f4a1f6 commit ac91ba5

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

Diff for: src/main/kotlin/com/coder/gateway/util/Dialogs.kt

+12-15
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ class DialogUi(
171171
* Open a dialog for providing the token. Show any existing token so
172172
* the user can validate it if a previous connection failed.
173173
*
174-
* If we are not retrying and the user has not checked the existing
175-
* token box then also open a browser to the auth page.
174+
* If we have not already tried once (no error) and the user has not checked
175+
* the existing token box then also open a browser to the auth page.
176176
*
177177
* If the user has checked the existing token box then return the token
178178
* on disk immediately and skip the dialog (this will overwrite any
@@ -182,16 +182,16 @@ class DialogUi(
182182
fun askToken(
183183
url: URL,
184184
token: Pair<String, Source>?,
185-
isRetry: Boolean,
186185
useExisting: Boolean,
186+
error: String?,
187187
): Pair<String, Source>? {
188188
val getTokenUrl = url.withPath("/login?redirect=%2Fcli-auth")
189189

190-
// On the first run either open a browser to generate a new token
191-
// or, if using an existing token, use the token on disk if it
192-
// exists otherwise assume the user already copied an existing
193-
// token and they will paste in.
194-
if (!isRetry) {
190+
// On the first run (no error) either open a browser to generate a new
191+
// token or, if using an existing token, use the token on disk if it
192+
// exists otherwise assume the user already copied an existing token and
193+
// they will paste in.
194+
if (error == null) {
195195
if (!useExisting) {
196196
openUrl(getTokenUrl)
197197
} else {
@@ -209,15 +209,12 @@ class DialogUi(
209209
val tokenFromUser =
210210
ask(
211211
title = "Session Token",
212-
description = if (isRetry) {
213-
"This token was rejected by ${url.host}."
214-
} else {
215-
token?.second?.description("token", url)
216-
?: "No existing token for ${url.host} found."
217-
},
212+
description = error
213+
?: token?.second?.description("token", url)
214+
?: "No existing token for ${url.host} found.",
218215
placeholder = token?.first,
219216
link = Pair("Session Token:", getTokenUrl.toString()),
220-
isError = isRetry,
217+
isError = error != null,
221218
)
222219
if (tokenFromUser.isNullOrBlank()) {
223220
return null

Diff for: src/main/kotlin/com/coder/gateway/util/LinkHandler.kt

+9-8
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,20 @@ open class LinkHandler(
146146
private fun authenticate(
147147
deploymentURL: String,
148148
tryToken: Pair<String, Source>?,
149-
lastToken: Pair<String, Source>? = null,
149+
error: String? = null,
150150
): CoderRestClient {
151151
val token =
152152
if (settings.requireTokenAuth) {
153-
// Try the provided token, unless we already did.
154-
val isRetry = lastToken != null
155-
if (tryToken != null && !isRetry) {
153+
// Try the provided token immediately on the first attempt.
154+
if (tryToken != null && error == null) {
156155
tryToken
157156
} else {
157+
// Otherwise ask for a new token, showing the previous token.
158158
dialogUi.askToken(
159159
deploymentURL.toURL(),
160-
lastToken,
161-
isRetry,
162-
true,
160+
tryToken,
161+
useExisting = true,
162+
error,
163163
)
164164
}
165165
} else {
@@ -175,7 +175,8 @@ open class LinkHandler(
175175
} catch (ex: APIResponseException) {
176176
// If doing token auth we can ask and try again.
177177
if (settings.requireTokenAuth && ex.isUnauthorized) {
178-
authenticate(deploymentURL, tryToken, token)
178+
val msg = humanizeConnectionError(client.url, true, ex)
179+
authenticate(deploymentURL, token, msg)
179180
} else {
180181
throw ex
181182
}

Diff for: src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,10 @@ class CoderWorkspacesStepView :
508508
* Ask for a new token if token auth is required (regardless of whether we
509509
* already have a token), place it in the local fields model, then connect.
510510
*
511-
* If the token is invalid abort and start over from askTokenAndConnect()
512-
* unless retry is false.
511+
* If the token is invalid try again until the user aborts or we get a valid
512+
* token. Any other error will not be retried.
513513
*/
514-
private fun maybeAskTokenThenConnect(isRetry: Boolean = false) {
514+
private fun maybeAskTokenThenConnect(error: String? = null) {
515515
val oldURL = fields.coderURL
516516
component.apply() // Force bindings to be filled.
517517
val newURL = fields.coderURL.toURL()
@@ -522,12 +522,12 @@ class CoderWorkspacesStepView :
522522
// If this is a new URL there is no point in trying to use the same
523523
// token.
524524
if (oldURL == newURL.toString()) fields.token else null,
525-
isRetry,
526525
fields.useExistingToken,
526+
error,
527527
) ?: return // User aborted.
528528
fields.token = pastedToken
529529
connect(newURL, pastedToken.first) {
530-
maybeAskTokenThenConnect(true)
530+
maybeAskTokenThenConnect(it)
531531
}
532532
} else {
533533
connect(newURL, null)
@@ -551,7 +551,7 @@ class CoderWorkspacesStepView :
551551
private fun connect(
552552
deploymentURL: URL,
553553
token: String?,
554-
onAuthFailure: (() -> Unit)? = null,
554+
onAuthFailure: ((error: String) -> Unit)? = null,
555555
): Job {
556556
tfUrlComment?.foreground = UIUtil.getContextHelpForeground()
557557
tfUrlComment?.text =
@@ -640,7 +640,7 @@ class CoderWorkspacesStepView :
640640
logger.error(msg, e)
641641

642642
if (e is APIResponseException && e.isUnauthorized && onAuthFailure != null) {
643-
onAuthFailure.invoke()
643+
onAuthFailure.invoke(msg)
644644
}
645645
}
646646
}

0 commit comments

Comments
 (0)