Skip to content

Commit f5d6a18

Browse files
committed
Add new AddGrantTypePermissions()/RemoveGrantTypePermissions() APIs in OpenIddictApplicationDescriptor
1 parent 5cf7aad commit f5d6a18

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/OpenIddict.Abstractions/Descriptors/OpenIddictApplicationDescriptor.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@ public OpenIddictApplicationDescriptor AddAudiencePermissions(params string[] au
102102
return this;
103103
}
104104

105+
/// <summary>
106+
/// Adds grant type permissions for all the specified <paramref name="types"/>.
107+
/// </summary>
108+
/// <param name="types">The grant types for which grant types permissions will be added.</param>
109+
/// <returns>The <see cref="OpenIddictApplicationDescriptor"/> instance.</returns>
110+
/// <exception cref="ArgumentNullException"><paramref name="types"/> is <see langword="null"/>.</exception>
111+
public OpenIddictApplicationDescriptor AddGrantTypePermissions(params string[] types)
112+
{
113+
if (types is null)
114+
{
115+
throw new ArgumentNullException(nameof(types));
116+
}
117+
118+
foreach (var type in types)
119+
{
120+
Permissions.Add(OpenIddictConstants.Permissions.Prefixes.GrantType + type);
121+
}
122+
123+
return this;
124+
}
125+
105126
/// <summary>
106127
/// Adds resource permissions for all the specified <paramref name="resources"/>.
107128
/// </summary>
@@ -333,6 +354,27 @@ public OpenIddictApplicationDescriptor RemoveAudiencePermissions(params string[]
333354
return this;
334355
}
335356

357+
/// <summary>
358+
/// Removes all the grant type permissions corresponding to the specified <paramref name="types"/>.
359+
/// </summary>
360+
/// <param name="types">The grant types for which grant types permissions will be removed.</param>
361+
/// <returns>The <see cref="OpenIddictApplicationDescriptor"/> instance.</returns>
362+
/// <exception cref="ArgumentNullException"><paramref name="types"/> is <see langword="null"/>.</exception>
363+
public OpenIddictApplicationDescriptor RemoveGrantTypePermissions(params string[] types)
364+
{
365+
if (types is null)
366+
{
367+
throw new ArgumentNullException(nameof(types));
368+
}
369+
370+
foreach (var type in types)
371+
{
372+
Permissions.Remove(OpenIddictConstants.Permissions.Prefixes.GrantType + type);
373+
}
374+
375+
return this;
376+
}
377+
336378
/// <summary>
337379
/// Removes all the resource permissions corresponding to the specified <paramref name="resources"/>.
338380
/// </summary>

test/OpenIddict.Abstractions.Tests/Descriptors/OpenIddictApplicationDescriptorTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ public void AddAudiencePermissions_ThrowsAnExceptionForNullAudiences()
2929
Assert.Throws<ArgumentNullException>(() => descriptor.AddAudiencePermissions(null!));
3030
}
3131

32+
[Fact]
33+
public void AddGrantTypePermissions_AddsPermissions()
34+
{
35+
// Arrange
36+
var descriptor = new OpenIddictApplicationDescriptor();
37+
38+
// Act
39+
descriptor.AddGrantTypePermissions(GrantTypes.Implicit, "custom_grant_type");
40+
41+
// Assert
42+
Assert.Contains(OpenIddictConstants.Permissions.Prefixes.GrantType + GrantTypes.Implicit, descriptor.Permissions);
43+
Assert.Contains(OpenIddictConstants.Permissions.Prefixes.GrantType + "custom_grant_type", descriptor.Permissions);
44+
}
45+
46+
[Fact]
47+
public void AddGrantTypePermissions_ThrowsAnExceptionForNullGrantTypes()
48+
{
49+
// Arrange
50+
var descriptor = new OpenIddictApplicationDescriptor();
51+
52+
// Act and assert
53+
Assert.Throws<ArgumentNullException>(() => descriptor.AddGrantTypePermissions(null!));
54+
}
55+
3256
[Fact]
3357
public void AddResourcePermissions_AddsPermissions()
3458
{
@@ -270,6 +294,31 @@ public void RemoveAudiencePermissions_ThrowsAnExceptionForNullAudiences()
270294
Assert.Throws<ArgumentNullException>(() => descriptor.RemoveAudiencePermissions(null!));
271295
}
272296

297+
[Fact]
298+
public void RemoveGrantTypePermissions_RemovesPermissions()
299+
{
300+
// Arrange
301+
var descriptor = new OpenIddictApplicationDescriptor();
302+
descriptor.AddGrantTypePermissions(GrantTypes.Implicit, "custom_grant_type");
303+
304+
// Act
305+
descriptor.RemoveGrantTypePermissions(GrantTypes.Implicit);
306+
307+
// Assert
308+
Assert.DoesNotContain(GrantTypes.Implicit, descriptor.Permissions);
309+
Assert.Contains(OpenIddictConstants.Permissions.Prefixes.GrantType + "custom_grant_type", descriptor.Permissions);
310+
}
311+
312+
[Fact]
313+
public void RemoveGrantTypePermissions_ThrowsAnExceptionForNullGrantTypes()
314+
{
315+
// Arrange
316+
var descriptor = new OpenIddictApplicationDescriptor();
317+
318+
// Act and assert
319+
Assert.Throws<ArgumentNullException>(() => descriptor.RemoveGrantTypePermissions(null!));
320+
}
321+
273322
[Fact]
274323
public void RemoveResourcePermissions_RemovesPermissions()
275324
{

0 commit comments

Comments
 (0)