Skip to content

Commit 60bc136

Browse files
feature(#20): this commit fixes the authorization logic to support client authorization via the “authorization_code” flow, which previously treated realms as clients.
1 parent 43ee72a commit 60bc136

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

Applications/Backend/Source/HttpsRichardy.Federation.WebApi/Pages/Authorize.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<div class="w-full lg:w-1/2 flex items-center justify-center bg-white px-4 py-8 lg:py-0">
88
<div class="w-full max-w-md px-4 lg:px-8">
99
@if (
10-
ViewData.ModelState.ContainsKey(RealmErrors.RealmDoesNotExist.Code) ||
10+
ViewData.ModelState.ContainsKey(ClientErrors.ClientDoesNotExist.Code) ||
1111
ViewData.ModelState.ContainsKey(AuthorizationErrors.RedirectUriNotAllowed.Code)
1212
)
1313
{

Applications/Backend/Source/HttpsRichardy.Federation.WebApi/Pages/Authorize.cshtml.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public sealed class AuthorizePage : PageModel
77

88
private readonly ITokenCollection _tokenCollection;
99
private readonly IRealmCollection _realmCollection;
10+
private readonly IClientCollection _clientCollection;
1011
private readonly IRealmProvider _realmProvider;
1112

1213
#region constructors
@@ -15,13 +16,15 @@ public AuthorizePage(
1516
IUserCollection userCollection,
1617
IRealmProvider realmProvider,
1718
IRealmCollection realmCollection,
18-
ITokenCollection tokenCollection)
19+
ITokenCollection tokenCollection,
20+
IClientCollection clientCollection)
1921
{
2022
_dispatcher = dispatcher;
2123
_userCollection = userCollection;
2224
_realmCollection = realmCollection;
2325
_realmProvider = realmProvider;
2426
_tokenCollection = tokenCollection;
27+
_clientCollection = clientCollection;
2528
}
2629
#endregion
2730

@@ -33,12 +36,29 @@ public AuthorizePage(
3336

3437
public async Task<IActionResult> OnGetAsync()
3538
{
36-
var filters = RealmFilters.WithSpecifications()
39+
var filters = ClientFilters.WithSpecifications()
3740
.WithClientId(Parameters.ClientId)
3841
.Build();
3942

40-
var realms = await _realmCollection.GetRealmsAsync(filters);
41-
var realm = realms.FirstOrDefault();
43+
var clients = await _clientCollection.GetClientsAsync(filters);
44+
var client = clients.FirstOrDefault();
45+
46+
if (client is null)
47+
{
48+
ModelState.AddModelError(
49+
key: ClientErrors.ClientDoesNotExist.Code,
50+
errorMessage: ClientErrors.ClientDoesNotExist.Description
51+
);
52+
53+
return Page();
54+
}
55+
56+
var realmFilters = RealmFilters.WithSpecifications()
57+
.WithIdentifier(client.RealmId)
58+
.Build();
59+
60+
var realms = await _realmCollection.GetRealmsAsync(realmFilters);
61+
var realm = realms.First();
4262

4363
if (realm is null)
4464
{
@@ -71,7 +91,11 @@ public async Task<IActionResult> OnPostAsync()
7191
var result = await _dispatcher.DispatchAsync(Credentials);
7292
if (result.IsFailure)
7393
{
74-
ModelState.AddModelError(result.Error.Code, result.Error.Description);
94+
ModelState.AddModelError(
95+
key: result.Error.Code,
96+
errorMessage: result.Error.Description
97+
);
98+
7599
return Page();
76100
}
77101

@@ -86,7 +110,11 @@ public async Task<IActionResult> OnPostAsync()
86110

87111
if (user is null)
88112
{
89-
ModelState.AddModelError(AuthenticationErrors.UserNotFound.Code, AuthenticationErrors.UserNotFound.Description);
113+
ModelState.AddModelError(
114+
key: AuthenticationErrors.UserNotFound.Code,
115+
errorMessage: AuthenticationErrors.UserNotFound.Description
116+
);
117+
90118
return Page();
91119
}
92120

0 commit comments

Comments
 (0)