Browse Source

Add new AddGrantTypePermissions()/RemoveGrantTypePermissions() APIs in OpenIddictApplicationDescriptor

pull/2379/head
Kévin Chalet 5 months ago
parent
commit
f5d6a18764
  1. 42
      src/OpenIddict.Abstractions/Descriptors/OpenIddictApplicationDescriptor.cs
  2. 49
      test/OpenIddict.Abstractions.Tests/Descriptors/OpenIddictApplicationDescriptorTests.cs

42
src/OpenIddict.Abstractions/Descriptors/OpenIddictApplicationDescriptor.cs

@ -102,6 +102,27 @@ public class OpenIddictApplicationDescriptor
return this;
}
/// <summary>
/// Adds grant type permissions for all the specified <paramref name="types"/>.
/// </summary>
/// <param name="types">The grant types for which grant types permissions will be added.</param>
/// <returns>The <see cref="OpenIddictApplicationDescriptor"/> instance.</returns>
/// <exception cref="ArgumentNullException"><paramref name="types"/> is <see langword="null"/>.</exception>
public OpenIddictApplicationDescriptor AddGrantTypePermissions(params string[] types)
{
if (types is null)
{
throw new ArgumentNullException(nameof(types));
}
foreach (var type in types)
{
Permissions.Add(OpenIddictConstants.Permissions.Prefixes.GrantType + type);
}
return this;
}
/// <summary>
/// Adds resource permissions for all the specified <paramref name="resources"/>.
/// </summary>
@ -333,6 +354,27 @@ public class OpenIddictApplicationDescriptor
return this;
}
/// <summary>
/// Removes all the grant type permissions corresponding to the specified <paramref name="types"/>.
/// </summary>
/// <param name="types">The grant types for which grant types permissions will be removed.</param>
/// <returns>The <see cref="OpenIddictApplicationDescriptor"/> instance.</returns>
/// <exception cref="ArgumentNullException"><paramref name="types"/> is <see langword="null"/>.</exception>
public OpenIddictApplicationDescriptor RemoveGrantTypePermissions(params string[] types)
{
if (types is null)
{
throw new ArgumentNullException(nameof(types));
}
foreach (var type in types)
{
Permissions.Remove(OpenIddictConstants.Permissions.Prefixes.GrantType + type);
}
return this;
}
/// <summary>
/// Removes all the resource permissions corresponding to the specified <paramref name="resources"/>.
/// </summary>

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

@ -29,6 +29,30 @@ public class OpenIddictApplicationDescriptorTests
Assert.Throws<ArgumentNullException>(() => descriptor.AddAudiencePermissions(null!));
}
[Fact]
public void AddGrantTypePermissions_AddsPermissions()
{
// Arrange
var descriptor = new OpenIddictApplicationDescriptor();
// Act
descriptor.AddGrantTypePermissions(GrantTypes.Implicit, "custom_grant_type");
// Assert
Assert.Contains(OpenIddictConstants.Permissions.Prefixes.GrantType + GrantTypes.Implicit, descriptor.Permissions);
Assert.Contains(OpenIddictConstants.Permissions.Prefixes.GrantType + "custom_grant_type", descriptor.Permissions);
}
[Fact]
public void AddGrantTypePermissions_ThrowsAnExceptionForNullGrantTypes()
{
// Arrange
var descriptor = new OpenIddictApplicationDescriptor();
// Act and assert
Assert.Throws<ArgumentNullException>(() => descriptor.AddGrantTypePermissions(null!));
}
[Fact]
public void AddResourcePermissions_AddsPermissions()
{
@ -270,6 +294,31 @@ public class OpenIddictApplicationDescriptorTests
Assert.Throws<ArgumentNullException>(() => descriptor.RemoveAudiencePermissions(null!));
}
[Fact]
public void RemoveGrantTypePermissions_RemovesPermissions()
{
// Arrange
var descriptor = new OpenIddictApplicationDescriptor();
descriptor.AddGrantTypePermissions(GrantTypes.Implicit, "custom_grant_type");
// Act
descriptor.RemoveGrantTypePermissions(GrantTypes.Implicit);
// Assert
Assert.DoesNotContain(GrantTypes.Implicit, descriptor.Permissions);
Assert.Contains(OpenIddictConstants.Permissions.Prefixes.GrantType + "custom_grant_type", descriptor.Permissions);
}
[Fact]
public void RemoveGrantTypePermissions_ThrowsAnExceptionForNullGrantTypes()
{
// Arrange
var descriptor = new OpenIddictApplicationDescriptor();
// Act and assert
Assert.Throws<ArgumentNullException>(() => descriptor.RemoveGrantTypePermissions(null!));
}
[Fact]
public void RemoveResourcePermissions_RemovesPermissions()
{

Loading…
Cancel
Save