Skip to content

Commit 92de02f

Browse files
Copilotmarcominerva
andcommitted
Address PR feedback: add Roles to single credentials, rename Admin to Administrator, use configured RoleClaimType
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
1 parent 1f7daa2 commit 92de02f

6 files changed

Lines changed: 30 additions & 12 deletions

File tree

samples/MinimalApis/ApiKeySample/Program.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Security.Claims;
22
using ApiKeySample.Authentication;
33
using Microsoft.AspNetCore.Authentication;
4+
using Microsoft.Extensions.Options;
45
using SimpleAuthentication;
56
using SimpleAuthentication.ApiKey;
67

@@ -55,16 +56,16 @@
5556
app.UseAuthentication();
5657
app.UseAuthorization();
5758

58-
app.MapGet("api/me", (ClaimsPrincipal user) =>
59+
app.MapGet("api/me", (ClaimsPrincipal user, IOptions<ApiKeySettings> options) =>
5960
{
60-
var roles = user.FindAll(ClaimTypes.Role).Select(c => c.Value);
61+
var roles = user.FindAll(options.Value.RoleClaimType).Select(c => c.Value);
6162
return TypedResults.Ok(new User(user.Identity!.Name, roles));
6263
})
6364
.RequireAuthorization()
6465
.WithOpenApi();
6566

66-
app.MapGet("api/admin", () => "Admin access granted")
67-
.RequireAuthorization(policy => policy.RequireRole("Admin"))
67+
app.MapGet("api/admin", () => "Administrator access granted")
68+
.RequireAuthorization(policy => policy.RequireRole("Administrator"))
6869
.WithOpenApi();
6970

7071
app.MapGet("api/user", () => "User access granted")

samples/MinimalApis/ApiKeySample/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
// You can set a fixed API Key for authentication. If you have a single value, you can just use the plain property:
1212
"ApiKeyValue": "f1I7S5GXa4wQDgLQWgz0",
1313
"UserName": "ApiUser", // Required if ApiKeyValue is used
14+
"Roles": [ "Administrator" ],
1415
// Otherwise, you can create an array of ApiKeys:
1516
"ApiKeys": [
1617
{
1718
"Value": "ArAilHVOoL3upX78Cohq",
1819
"UserName": "alice",
19-
"Roles": [ "Admin", "User" ]
20+
"Roles": [ "Administrator", "User" ]
2021
},
2122
{
2223
"Value": "DiUU5EqImTYkxPDAxBVS",

samples/MinimalApis/BasicAuthenticationSample/Program.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Security.Claims;
22
using BasicAuthenticationSample.Authentication;
33
using Microsoft.AspNetCore.Authentication;
4+
using Microsoft.Extensions.Options;
45
using SimpleAuthentication;
56
using SimpleAuthentication.BasicAuthentication;
67

@@ -55,16 +56,16 @@
5556
app.UseAuthentication();
5657
app.UseAuthorization();
5758

58-
app.MapGet("api/me", (ClaimsPrincipal user) =>
59+
app.MapGet("api/me", (ClaimsPrincipal user, IOptions<BasicAuthenticationSettings> options) =>
5960
{
60-
var roles = user.FindAll(ClaimTypes.Role).Select(c => c.Value);
61+
var roles = user.FindAll(options.Value.RoleClaimType).Select(c => c.Value);
6162
return TypedResults.Ok(new User(user.Identity!.Name, roles));
6263
})
6364
.RequireAuthorization()
6465
.WithOpenApi();
6566

66-
app.MapGet("api/admin", () => "Admin access granted")
67-
.RequireAuthorization(policy => policy.RequireRole("Admin"))
67+
app.MapGet("api/admin", () => "Administrator access granted")
68+
.RequireAuthorization(policy => policy.RequireRole("Administrator"))
6869
.WithOpenApi();
6970

7071
app.MapGet("api/user", () => "User access granted")

samples/MinimalApis/BasicAuthenticationSample/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
//"RoleClaimType": "user_role", // Default: http://schemas.microsoft.com/ws/2008/06/identity/claims/role
99
"UserName": "marco",
1010
"Password": "P@$$w0rd",
11+
"Roles": [ "Administrator" ],
1112
// Otherwise, you can create an array of Credentials:
1213
"Credentials": [
1314
{
1415
"UserName": "alice",
1516
"Password": "Password1",
16-
"Roles": [ "Admin", "User" ]
17+
"Roles": [ "Administrator", "User" ]
1718
},
1819
{
1920
"UserName": "bob",

src/SimpleAuthentication.Abstractions/ApiKey/ApiKeySettings.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public class ApiKeySettings : AuthenticationSchemeOptions
4141
/// <seealso cref="ApiKeyValue"/>
4242
public string? UserName { get; set; }
4343

44+
/// <summary>
45+
/// Gets or sets the optional list of roles to assign to the user when using <see cref="ApiKeyValue"/> and <see cref="UserName"/>.
46+
/// </summary>
47+
/// <seealso cref="ApiKeyValue"/>
48+
/// <seealso cref="UserName"/>
49+
public string[]? Roles { get; set; }
50+
4451
private ICollection<ApiKey> apiKeys = [];
4552
/// <summary>
4653
/// The collection of valid API keys.
@@ -53,7 +60,7 @@ public ICollection<ApiKey> ApiKeys
5360
if (!string.IsNullOrWhiteSpace(ApiKeyValue) && !string.IsNullOrWhiteSpace(UserName))
5461
{
5562
// If necessary, add the API Key from the base properties.
56-
apiKeys.Add(new(ApiKeyValue, UserName));
63+
apiKeys.Add(new(ApiKeyValue, UserName, Roles));
5764
}
5865

5966
return apiKeys;

src/SimpleAuthentication.Abstractions/BasicAuthentication/BasicAuthenticationSettings.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public class BasicAuthenticationSettings : AuthenticationSchemeOptions
3030
/// <seealso cref="IBasicAuthenticationValidator"/>
3131
public string? Password { get; set; }
3232

33+
/// <summary>
34+
/// Gets or sets the optional list of roles to assign to the user when using <see cref="UserName"/> and <see cref="Password"/>.
35+
/// </summary>
36+
/// <seealso cref="UserName"/>
37+
/// <seealso cref="Password"/>
38+
public string[]? Roles { get; set; }
39+
3340
private ICollection<Credential> credentials = [];
3441
/// <summary>
3542
/// The collection of authorization credentials.
@@ -42,7 +49,7 @@ public ICollection<Credential> Credentials
4249
if (!string.IsNullOrWhiteSpace(UserName) && !string.IsNullOrWhiteSpace(Password))
4350
{
4451
// If necessary, add the credentials from the base properties.
45-
credentials.Add(new Credential(UserName, Password));
52+
credentials.Add(new Credential(UserName, Password, Roles));
4653
}
4754

4855
return credentials;

0 commit comments

Comments
 (0)