Skip to content

Commit 10c4390

Browse files
feat: split scope groups and role claims (#529)
1 parent 90987de commit 10c4390

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

src/oidc-guard/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ public static void Main(string[] args)
249249

250250
builder.Services.Configure<ForwardedHeadersOptions>(options => options.ForwardedHeaders = ForwardedHeaders.All);
251251

252+
builder.Services.AddTransient<IClaimsTransformation, ClaimSplitter>();
253+
252254
builder.Services.AddHostedService<HostedService>();
253255

254256
var app = builder.Build();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using System.Security.Claims;
3+
4+
namespace oidc_guard.Services;
5+
6+
public class ClaimSplitter : IClaimsTransformation
7+
{
8+
private static readonly string[] SplitClaimTypes = ["scope", "groups", "role"];
9+
10+
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
11+
{
12+
var identities = new List<ClaimsIdentity>();
13+
14+
foreach (var id in principal.Identities)
15+
{
16+
var identity = new ClaimsIdentity(id.AuthenticationType, id.NameClaimType, id.RoleClaimType);
17+
18+
foreach (var claim in id.Claims)
19+
{
20+
if (SplitClaimTypes.Contains(claim.Type) && claim.Value.Contains(' '))
21+
{
22+
var values = claim.Value.Split(' ', StringSplitOptions.RemoveEmptyEntries);
23+
24+
foreach (var value in values)
25+
{
26+
identity.AddClaim(new Claim(claim.Type, value, claim.ValueType, claim.Issuer));
27+
}
28+
29+
continue;
30+
}
31+
32+
identity.AddClaim(claim);
33+
}
34+
35+
identities.Add(identity);
36+
}
37+
38+
return Task.FromResult(new ClaimsPrincipal(identities));
39+
}
40+
}

tests/oidc-guard-tests/AuthTests.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,90 @@ public static IEnumerable<object[]> GetArrayTests()
182182
},
183183
HttpStatusCode.Forbidden
184184
},
185+
186+
new object[]
187+
{
188+
"?scope=admin",
189+
new List<Claim>
190+
{
191+
new Claim("scope", "admin editor viewer")
192+
},
193+
HttpStatusCode.OK
194+
},
195+
new object[]
196+
{
197+
"?scope=admin&scope=editor",
198+
new List<Claim>
199+
{
200+
new Claim("scope", "admin editor viewer")
201+
},
202+
HttpStatusCode.OK
203+
},
204+
new object[]
205+
{
206+
"?scope=admin&scope=editor&scope=viewer",
207+
new List<Claim>
208+
{
209+
new Claim("scope", "admin editor viewer")
210+
},
211+
HttpStatusCode.OK
212+
},
213+
214+
new object[]
215+
{
216+
"?groups=admin",
217+
new List<Claim>
218+
{
219+
new Claim("groups", "admin editor viewer")
220+
},
221+
HttpStatusCode.OK
222+
},
223+
new object[]
224+
{
225+
"?groups=admin&groups=editor",
226+
new List<Claim>
227+
{
228+
new Claim("groups", "admin editor viewer")
229+
},
230+
HttpStatusCode.OK
231+
},
232+
new object[]
233+
{
234+
"?groups=admin&groups=editor&groups=viewer",
235+
new List<Claim>
236+
{
237+
new Claim("groups", "admin editor viewer")
238+
},
239+
HttpStatusCode.OK
240+
},
241+
242+
new object[]
243+
{
244+
"?role=admin",
245+
new List<Claim>
246+
{
247+
new Claim("role", "admin editor viewer")
248+
},
249+
HttpStatusCode.OK
250+
},
251+
new object[]
252+
{
253+
"?role=admin&role=editor",
254+
new List<Claim>
255+
{
256+
new Claim("role", "admin editor viewer")
257+
},
258+
HttpStatusCode.OK
259+
},
260+
new object[]
261+
{
262+
"?role=admin&role=editor&role=viewer",
263+
new List<Claim>
264+
{
265+
new Claim("role", "admin editor viewer")
266+
},
267+
HttpStatusCode.OK
268+
},
185269
];
186270
}
187271

@@ -267,6 +351,22 @@ public static IEnumerable<object[]> GetInjectClaimsTests()
267351
}
268352
},
269353

354+
new object[]
355+
{
356+
"?tid=11111111-1111-1111-1111-111111111111&inject-claim=groups",
357+
new List<Claim>
358+
{
359+
new("tid", "11111111-1111-1111-1111-111111111111"),
360+
new("aud", "22222222-2222-2222-2222-222222222222"),
361+
new("groups", "admin editor viewer"),
362+
},
363+
HttpStatusCode.OK,
364+
new List<KeyValuePair<string,string>>
365+
{
366+
new("groups", "admin, editor, viewer"),
367+
}
368+
},
369+
270370
new object[]
271371
{
272372
"?tid=11111111-1111-1111-1111-111111111111&inject-claim=group",

0 commit comments

Comments
 (0)