Skip to content

Commit 675962f

Browse files
committed
fix(auth): avoid disabled UI OAuth redirects
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
1 parent 1b4d116 commit 675962f

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

src/EventStore.ClusterNode/Components/Pages/SignIn.razor

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
private SecurityAuthenticationInfo Authentication { get; set; } = SecurityAuthenticationInfo.Unavailable();
111111
private bool AuthenticationAvailable { get; set; } = true;
112112
private string Message { get; set; } = "";
113+
private string DisplayedOAuthError { get; set; } = "";
113114
private string Destination => SecurityBrowserService.NormalizeReturnUrl(ReturnUrl);
114115

115116
protected override void OnParametersSet() {
@@ -118,12 +119,19 @@
118119
try {
119120
Authentication = Security.AuthenticationInfo();
120121
AuthenticationAvailable = true;
121-
Message = string.IsNullOrWhiteSpace(OAuthError)
122-
? ""
123-
: OAuthErrorMessage(OAuthError);
122+
if (string.IsNullOrWhiteSpace(OAuthError)) {
123+
if (!string.IsNullOrWhiteSpace(DisplayedOAuthError)) {
124+
Message = "";
125+
DisplayedOAuthError = "";
126+
}
127+
} else {
128+
Message = OAuthErrorMessage(OAuthError);
129+
DisplayedOAuthError = OAuthError;
130+
}
124131
} catch (Exception ex) {
125132
Authentication = SecurityAuthenticationInfo.Unavailable();
126133
AuthenticationAvailable = false;
134+
DisplayedOAuthError = "";
127135
Message = $"Unable to load authentication information: {UserInterfaceMessages.Friendly(ex)}";
128136
}
129137

@@ -153,22 +161,26 @@
153161
try {
154162
var result = await Security.Validate(Input.Username, Input.Password);
155163
if (!result.Success) {
164+
DisplayedOAuthError = "";
156165
Message = result.Message;
157166
return;
158167
}
159168

160169
var context = HttpContextAccessor.HttpContext;
161170
if (context is null) {
171+
DisplayedOAuthError = "";
162172
Message = "The sign-in response is no longer available.";
163173
return;
164174
}
165175

166176
UiCredentialCookie.DeleteOAuthToken(context.Response);
167177
UiCredentialCookie.AppendBasic(context.Response, new UiCredentials(Input.Username.Trim(), Input.Password));
168178
} catch (OperationCanceledException) {
179+
DisplayedOAuthError = "";
169180
Message = "Sign-in was canceled.";
170181
return;
171182
} catch (Exception ex) {
183+
DisplayedOAuthError = "";
172184
Message = $"Failed to sign in: {UserInterfaceMessages.Friendly(ex)}";
173185
return;
174186
}

src/EventStore.ClusterNode/Components/Services/OAuthBrowserFlowEndpoints.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ private static string SignInLocation(string returnUrl) =>
239239
: $"/ui/signin?returnUrl={Uri.EscapeDataString(returnUrl)}";
240240

241241
private static string DirectReturnLocation(string returnUrl) =>
242-
string.IsNullOrWhiteSpace(returnUrl) || returnUrl == "/ui"
242+
string.IsNullOrWhiteSpace(returnUrl) ||
243+
returnUrl.Equals("/ui", StringComparison.OrdinalIgnoreCase) ||
244+
returnUrl.StartsWith("/ui/", StringComparison.OrdinalIgnoreCase)
243245
? "/"
244246
: returnUrl;
245247

src/EventStore.Core.Tests/Authentication/OAuthBrowserFlowServiceTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public async Task callback_redirects_to_return_url_when_admin_ui_is_disabled()
8484
await result.ExecuteAsync(context);
8585

8686
Assert.AreEqual(HttpStatusCode.Redirect, (HttpStatusCode)context.Response.StatusCode);
87-
Assert.That(context.Response.Headers.Location.ToString(), Is.EqualTo("/ui/streams"));
87+
Assert.That(context.Response.Headers.Location.ToString(), Is.EqualTo("/"));
8888
Assert.That(context.Response.Headers.SetCookie.ToString(), Does.Contain($"{UiCredentialCookie.OAuthCookieName}=access-token"));
8989
}
9090

@@ -152,7 +152,7 @@ public async Task callback_with_provider_error_redirects_to_return_url_when_admi
152152
await result.ExecuteAsync(context);
153153

154154
Assert.AreEqual(HttpStatusCode.Redirect, (HttpStatusCode)context.Response.StatusCode);
155-
Assert.That(context.Response.Headers.Location.ToString(), Is.EqualTo("/ui/streams?oauth_error=provider_error"));
155+
Assert.That(context.Response.Headers.Location.ToString(), Is.EqualTo("/?oauth_error=provider_error"));
156156
Assert.That(context.Response.Headers.SetCookie.ToString(), Does.Not.Contain($"{UiCredentialCookie.OAuthCookieName}=access-token"));
157157
Assert.That(handler.Body, Is.Empty);
158158
}

0 commit comments

Comments
 (0)