Skip to content

Commit 28c44ce

Browse files
author
msftbot[bot]
authored
[Blazor] API cleanups for the authentication Package (#19601)
* Add missing OIDC provider options * Convert string to enum * Revert changes to JS files
1 parent e5cd390 commit 28c44ce

File tree

10 files changed

+109
-57
lines changed

10 files changed

+109
-57
lines changed

src/Components/WebAssembly/Authentication.Msal/src/Interop/AuthenticationService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum AuthenticationResultStatus {
2727
Redirect = "redirect",
2828
Success = "success",
2929
Failure = "failure",
30-
OperationCompleted = "operation-completed"
30+
OperationCompleted = "operationCompleted"
3131
}
3232

3333
interface AuthenticationResult {

src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export enum AuthenticationResultStatus {
4343
Redirect = 'redirect',
4444
Success = 'success',
4545
Failure = 'failure',
46-
OperationCompleted = 'operation-completed'
46+
OperationCompleted = 'operationCompleted'
4747
};
4848

4949
export interface AuthenticationResult {

src/Components/WebAssembly/WebAssembly.Authentication/src/Models/RemoteAuthenticationResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class RemoteAuthenticationResult<TState> where TState : class
1212
/// <summary>
1313
/// Gets or sets the status of the authentication operation. The status can be one of <see cref="RemoteAuthenticationStatus"/>.
1414
/// </summary>
15-
public string Status { get; set; }
15+
public RemoteAuthenticationStatus Status { get; set; }
1616

1717
/// <summary>
1818
/// Gets or sets the error message of a failed authentication operation.

src/Components/WebAssembly/WebAssembly.Authentication/src/Models/RemoteAuthenticationStatus.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
66
/// <summary>
77
/// Represents the status of an authentication operation.
88
/// </summary>
9-
public class RemoteAuthenticationStatus
9+
public enum RemoteAuthenticationStatus
1010
{
1111
/// <summary>
1212
/// The application is going to be redirected.
1313
/// </summary>
14-
public const string Redirect = "redirect";
14+
Redirect,
1515

1616
/// <summary>
1717
/// The authentication operation completed successfully.
1818
/// </summary>
19-
public const string Success = "success";
19+
Success,
2020

2121
/// <summary>
2222
/// There was an error performing the authentication operation.
2323
/// </summary>
24-
public const string Failure = "failure";
24+
Failure,
2525

2626
/// <summary>
2727
/// The operation in the current navigation context has completed. This signals that the application running on the
2828
/// current browser context is about to be shut down and no other work is required.
2929
/// </summary>
30-
public const string OperationCompleted = "operation-completed";
30+
OperationCompleted,
3131
}
3232
}

src/Components/WebAssembly/WebAssembly.Authentication/src/Options/OidcProviderOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,17 @@ public class OidcProviderOptions
4040
/// </summary>
4141
[JsonPropertyName("post_logout_redirect_uri")]
4242
public string PostLogoutRedirectUri { get; set; }
43+
44+
/// <summary>
45+
/// Gets or sets the response type to use on the authorization flow. The valid values are specified by the identity provider metadata.
46+
/// </summary>
47+
[JsonPropertyName("response_type")]
48+
public string ResponseType { get; set; }
49+
50+
/// <summary>
51+
/// Gets or sets the response mode to use in the authorization flow.
52+
/// </summary>
53+
[JsonPropertyName("response_mode")]
54+
public string ResponseMode { get; set; }
4355
}
4456
}

src/Components/WebAssembly/WebAssembly.Authentication/src/RemoteAuthenticatorViewCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private async Task ProcessLogOut(string returnUrl)
300300
await NavigateToReturnUrl(uri);
301301
break;
302302
default:
303-
throw new InvalidOperationException($"Invalid authentication result status '{result.Status ?? "(null)"}'.");
303+
throw new InvalidOperationException($"Invalid authentication result status.");
304304
}
305305
}
306306
else
@@ -332,7 +332,7 @@ private async Task ProcessLogOutCallback()
332332
await NavigateToReturnUrl(uri);
333333
break;
334334
default:
335-
throw new InvalidOperationException($"Invalid authentication result status '{result.Status ?? "(null)"}'.");
335+
throw new InvalidOperationException($"Invalid authentication result status.");
336336
}
337337
}
338338

src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AccessTokenResult.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class AccessTokenResult
1818
/// <param name="status">The status of the result.</param>
1919
/// <param name="token">The <see cref="AccessToken"/> in case it was successful.</param>
2020
/// <param name="redirectUrl">The redirect uri to go to for provisioning the token.</param>
21-
public AccessTokenResult(string status, AccessToken token, string redirectUrl)
21+
public AccessTokenResult(AccessTokenResultStatus status, AccessToken token, string redirectUrl)
2222
{
2323
Status = status;
2424
_token = token;
@@ -28,7 +28,7 @@ public AccessTokenResult(string status, AccessToken token, string redirectUrl)
2828
/// <summary>
2929
/// Gets or sets the status of the current operation. See <see cref="AccessTokenResultStatus"/> for a list of statuses.
3030
/// </summary>
31-
public string Status { get; set; }
31+
public AccessTokenResultStatus Status { get; set; }
3232

3333
/// <summary>
3434
/// Gets or sets the URL to redirect to if <see cref="Status"/> is <see cref="AccessTokenResultStatus.RequiresRedirect"/>.
@@ -42,7 +42,7 @@ public AccessTokenResult(string status, AccessToken token, string redirectUrl)
4242
/// <returns><c>true</c> when the token request is successful; <c>false</c> otherwise.</returns>
4343
public bool TryGetToken(out AccessToken accessToken)
4444
{
45-
if (string.Equals(Status, AccessTokenResultStatus.Success, StringComparison.OrdinalIgnoreCase))
45+
if (Status == AccessTokenResultStatus.Success)
4646
{
4747
accessToken = _token;
4848
return true;

src/Components/WebAssembly/WebAssembly.Authentication/src/Services/AccessTokenResultStatus.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication
66
/// <summary>
77
/// Represents the possible results from trying to acquire an access token.
88
/// </summary>
9-
public class AccessTokenResultStatus
9+
public enum AccessTokenResultStatus
1010
{
1111
/// <summary>
1212
/// The token was successfully acquired.
1313
/// </summary>
14-
public const string Success = "success";
14+
Success,
1515

1616
/// <summary>
1717
/// A redirect is needed in order to provision the token.
1818
/// </summary>
19-
public const string RequiresRedirect = "requiesRedirect";
19+
RequiresRedirect,
2020
}
2121
}

src/Components/WebAssembly/WebAssembly.Authentication/src/Services/RemoteAuthenticationService.cs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public virtual async Task<RemoteAuthenticationResult<TRemoteAuthenticationState>
7070
RemoteAuthenticationContext<TRemoteAuthenticationState> context)
7171
{
7272
await EnsureAuthService();
73-
var result = await _jsRuntime.InvokeAsync<RemoteAuthenticationResult<TRemoteAuthenticationState>>("AuthenticationService.signIn", context.State);
73+
var internalResult = await _jsRuntime.InvokeAsync<InternalRemoteAuthenticationResult<TRemoteAuthenticationState>>("AuthenticationService.signIn", context.State);
74+
var result = internalResult.Convert();
7475
if (result.Status == RemoteAuthenticationStatus.Success)
7576
{
7677
UpdateUser(GetUser());
@@ -84,7 +85,8 @@ public virtual async Task<RemoteAuthenticationResult<TRemoteAuthenticationState>
8485
RemoteAuthenticationContext<TRemoteAuthenticationState> context)
8586
{
8687
await EnsureAuthService();
87-
var result = await _jsRuntime.InvokeAsync<RemoteAuthenticationResult<TRemoteAuthenticationState>>("AuthenticationService.completeSignIn", context.Url);
88+
var internalResult = await _jsRuntime.InvokeAsync<InternalRemoteAuthenticationResult<TRemoteAuthenticationState>>("AuthenticationService.completeSignIn", context.Url);
89+
var result = internalResult.Convert();
8890
if (result.Status == RemoteAuthenticationStatus.Success)
8991
{
9092
UpdateUser(GetUser());
@@ -98,7 +100,8 @@ public virtual async Task<RemoteAuthenticationResult<TRemoteAuthenticationState>
98100
RemoteAuthenticationContext<TRemoteAuthenticationState> context)
99101
{
100102
await EnsureAuthService();
101-
var result = await _jsRuntime.InvokeAsync<RemoteAuthenticationResult<TRemoteAuthenticationState>>("AuthenticationService.signOut", context.State);
103+
var internalResult = await _jsRuntime.InvokeAsync<InternalRemoteAuthenticationResult<TRemoteAuthenticationState>>("AuthenticationService.signOut", context.State);
104+
var result = internalResult.Convert();
102105
if (result.Status == RemoteAuthenticationStatus.Success)
103106
{
104107
UpdateUser(GetUser());
@@ -112,7 +115,8 @@ public virtual async Task<RemoteAuthenticationResult<TRemoteAuthenticationState>
112115
RemoteAuthenticationContext<TRemoteAuthenticationState> context)
113116
{
114117
await EnsureAuthService();
115-
var result = await _jsRuntime.InvokeAsync<RemoteAuthenticationResult<TRemoteAuthenticationState>>("AuthenticationService.completeSignOut", context.Url);
118+
var internalResult = await _jsRuntime.InvokeAsync<InternalRemoteAuthenticationResult<TRemoteAuthenticationState>>("AuthenticationService.completeSignOut", context.Url);
119+
var result = internalResult.Convert();
116120
if (result.Status == RemoteAuthenticationStatus.Success)
117121
{
118122
UpdateUser(GetUser());
@@ -127,13 +131,18 @@ public virtual async ValueTask<AccessTokenResult> RequestAccessToken()
127131
await EnsureAuthService();
128132
var result = await _jsRuntime.InvokeAsync<InternalAccessTokenResult>("AuthenticationService.getAccessToken");
129133

130-
if (string.Equals(result.Status, AccessTokenResultStatus.RequiresRedirect, StringComparison.OrdinalIgnoreCase))
134+
if (!Enum.TryParse<AccessTokenResultStatus>(result.Status, ignoreCase: true, out var parsedStatus))
135+
{
136+
throw new InvalidOperationException($"Invalid access token result status '{result.Status ?? "(null)"}'");
137+
}
138+
139+
if (parsedStatus == AccessTokenResultStatus.RequiresRedirect)
131140
{
132141
var redirectUrl = GetRedirectUrl(null);
133142
result.RedirectUrl = redirectUrl.ToString();
134143
}
135144

136-
return new AccessTokenResult(result.Status, result.Token, result.RedirectUrl);
145+
return new AccessTokenResult(parsedStatus, result.Token, result.RedirectUrl);
137146
}
138147

139148
/// <inheritdoc />
@@ -147,13 +156,18 @@ public virtual async ValueTask<AccessTokenResult> RequestAccessToken(AccessToken
147156
await EnsureAuthService();
148157
var result = await _jsRuntime.InvokeAsync<InternalAccessTokenResult>("AuthenticationService.getAccessToken", options);
149158

150-
if (string.Equals(result.Status, AccessTokenResultStatus.RequiresRedirect, StringComparison.OrdinalIgnoreCase))
159+
if (!Enum.TryParse<AccessTokenResultStatus>(result.Status, ignoreCase: true, out var parsedStatus))
160+
{
161+
throw new InvalidOperationException($"Invalid access token result status '{result.Status ?? "(null)"}'");
162+
}
163+
164+
if (parsedStatus == AccessTokenResultStatus.RequiresRedirect)
151165
{
152166
var redirectUrl = GetRedirectUrl(options.ReturnUrl);
153167
result.RedirectUrl = redirectUrl.ToString();
154168
}
155169

156-
return new AccessTokenResult(result.Status, result.Token, result.RedirectUrl);
170+
return new AccessTokenResult(parsedStatus, result.Token, result.RedirectUrl);
157171
}
158172

159173
private Uri GetRedirectUrl(string customReturnUrl)
@@ -242,4 +256,32 @@ internal struct InternalAccessTokenResult
242256
public AccessToken Token { get; set; }
243257
public string RedirectUrl { get; set; }
244258
}
259+
260+
// Internal for testing purposes
261+
internal struct InternalRemoteAuthenticationResult<TRemoteAuthenticationState> where TRemoteAuthenticationState : RemoteAuthenticationState
262+
{
263+
public string Status { get; set; }
264+
265+
public string ErrorMessage { get; set; }
266+
267+
public TRemoteAuthenticationState State { get; set; }
268+
269+
public RemoteAuthenticationResult<TRemoteAuthenticationState> Convert()
270+
{
271+
var result = new RemoteAuthenticationResult<TRemoteAuthenticationState>();
272+
result.ErrorMessage = ErrorMessage;
273+
result.State = State;
274+
275+
if (Status != null && Enum.TryParse<RemoteAuthenticationStatus>(Status, ignoreCase: true, out var status))
276+
{
277+
result.Status = status;
278+
}
279+
else
280+
{
281+
throw new InvalidOperationException($"Can't convert status '${Status ?? "(null)"}'.");
282+
}
283+
284+
return result;
285+
}
286+
}
245287
}

0 commit comments

Comments
 (0)