Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Grants endpoint #633

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Auth0.ManagementApi/Auth0.ManagementApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
</ItemGroup>
<ItemGroup>
<None Remove="Models\Users\" />
<None Remove="Models\Grants\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\Users\" />
<Folder Include="Models\Grants\" />
</ItemGroup>
</Project>
63 changes: 63 additions & 0 deletions src/Auth0.ManagementApi/Clients/GrantsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Auth0.ManagementApi.Models.Grants;
using Auth0.ManagementApi.Paging;
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Clients
{

public class GrantsClient : BaseClient, IGrantsClient
{

readonly JsonConverter[] converters = new JsonConverter[] { new PagedListConverter<Grant>("grants") };

public GrantsClient(IManagementConnection connection, Uri baseUri, IDictionary<string, string> defaultHeaders) : base(connection, baseUri, defaultHeaders)
{
}

/// <inheritdoc/>
public Task DeleteAllAsync(string userId, CancellationToken cancellationToken = default)
{
var queryStrings = new Dictionary<string, string>
{
{"user_id", userId}
};

return Connection.SendAsync<object>(HttpMethod.Delete, BuildUri($"grants", queryStrings), null, DefaultHeaders, cancellationToken: cancellationToken);
}

/// <inheritdoc/>
public Task DeleteAsync(string id, CancellationToken cancellationToken = default)
{

return Connection.SendAsync<object>(HttpMethod.Delete, BuildUri($"grants/{EncodePath(id)}"), null, DefaultHeaders, cancellationToken: cancellationToken);
}

/// <inheritdoc/>
public Task<IPagedList<Grant>> GetAllAsync(GetGrantsRequest request, PaginationInfo pagination = null, CancellationToken cancellationToken = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));

var queryStrings = new Dictionary<string, string>
{
{"user_id", request.UserId},
{"client_id", request.ClientId},
{"audience", request.Audience},
};

if (pagination != null)
{
queryStrings.Add("page", pagination.PageNo.ToString());
queryStrings.Add("per_page", pagination.PerPage.ToString());
queryStrings.Add("include_totals", pagination.IncludeTotals.ToString().ToLower());
}

return Connection.GetAsync<IPagedList<Grant>>(BuildUri("grants", queryStrings), DefaultHeaders, converters, cancellationToken);
}
}
}
44 changes: 44 additions & 0 deletions src/Auth0.ManagementApi/Clients/IGrantsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Threading;
using System.Threading.Tasks;
using Auth0.ManagementApi.Models.Grants;
using Auth0.ManagementApi.Paging;

namespace Auth0.ManagementApi.Clients
{
public interface IGrantsClient
{
/// <summary>
/// Retrieve all Grants.
/// </summary>
/// <remarks>
/// A token with scope read:grants is needed
/// </remarks>
/// <param name="request">Specifies criteria to use when querying grants.</param>
/// <param name="pagination">Specifies pagination info to use.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>An <see cref="IPagedList{Grant}"/> containing the grants.</returns>
Task<IPagedList<Grant>> GetAllAsync(GetGrantsRequest request, PaginationInfo pagination = null, CancellationToken cancellationToken = default);

/// <summary>
/// Delete an existing Grant
/// </summary>
/// <remarks>
/// A token with scope delete:grants is needed.
/// </remarks>
/// <param name="id">The ID of the grant to delete.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A <see cref="Task"/> that represents the asynchronous delete operation.</returns>
Task DeleteAsync(string id, CancellationToken cancellationToken = default);

/// <summary>
/// Deletes all Grants of a given user.
/// </summary>
/// <remarks>
/// A token with scope delete:grants is needed.
/// </remarks>
/// <param name="id">The ID of the user.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A <see cref="Task"/> that represents the asynchronous delete operation.</returns>
Task DeleteAllAsync(string userId, CancellationToken cancellationToken = default);
}
}
6 changes: 6 additions & 0 deletions src/Auth0.ManagementApi/ManagementApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public class ManagementApiClient : IManagementApiClient
/// </summary>
public IEmailTemplatesClient EmailTemplates { get; }

/// <summary>
/// Contains all the methods to call the /grants endpoints.
/// </summary>
public IGrantsClient Grants { get; }

/// <summary>
/// Contains all the methods to call the /guardian endpoints.
/// </summary>
Expand Down Expand Up @@ -190,6 +195,7 @@ public ManagementApiClient(string token, Uri baseUri, IManagementConnection mana
DeviceCredentials = new DeviceCredentialsClient(managementConnection, baseUri, DefaultHeaders);
EmailProvider = new EmailProviderClient(managementConnection, baseUri, DefaultHeaders);
EmailTemplates = new EmailTemplatesClient(managementConnection, baseUri, DefaultHeaders);
Grants = new GrantsClient(managementConnection, baseUri, DefaultHeaders);
Guardian = new GuardianClient(managementConnection, baseUri, DefaultHeaders);
Hooks = new HooksClient(managementConnection, baseUri, DefaultHeaders);
Jobs = new JobsClient(managementConnection, baseUri, DefaultHeaders);
Expand Down
20 changes: 20 additions & 0 deletions src/Auth0.ManagementApi/Models/Grants/GetGrantsRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Auth0.ManagementApi.Models.Grants
{
public class GetGrantsRequest
{
/// <summary>
/// Id of the user of the grants to retrieve.
/// </summary>
public string UserId { get; set; }

/// <summary>
/// Id of the client of the grants to retrieve.
/// </summary>
public string ClientId { get; set; }

/// <summary>
/// Audience of the grants to retrieve.
/// </summary>
public string Audience { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/Auth0.ManagementApi/Models/Grants/Grant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.Grants
{
public class Grant
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("clientID")]
public string ClientId { get; set; }
[JsonProperty("user_id")]
public string UserId { get; set; }
[JsonProperty("audience")]
public string Audience { get; set; }
[JsonProperty("scope")]
public IList<string> Scope { get; set; }

}
}
43 changes: 43 additions & 0 deletions tests/Auth0.ManagementApi.IntegrationTests/GrantsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Auth0.IntegrationTests.Shared.CleanUp;
using Auth0.ManagementApi.IntegrationTests.Testing;
using Auth0.ManagementApi.Models.Grants;
using Auth0.ManagementApi.Paging;
using Xunit;


namespace Auth0.ManagementApi.IntegrationTests
{
public class GrantsTestsFixture : TestBaseFixture
{
public override async Task DisposeAsync()
{
foreach (KeyValuePair<CleanUpType, IList<string>> entry in identifiers)
{
await ManagementTestBaseUtils.CleanupAsync(ApiClient, entry.Key, entry.Value);
}

ApiClient.Dispose();
}
}

public class GrantsTests : IClassFixture<GrantsTestsFixture>
{
GrantsTestsFixture fixture;

public GrantsTests(GrantsTestsFixture fixture)
{
this.fixture = fixture;
}



[Fact]
public async Task Test_GetAll()
{
var grants = await fixture.ApiClient.Grants.GetAllAsync(new GetGrantsRequest(), new PaginationInfo(0, 50, true));
}

}
}