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

Added Prompt Client to Management API SDK #522

Merged
merged 6 commits into from
Sep 8, 2021
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
54 changes: 54 additions & 0 deletions src/Auth0.ManagementApi/Clients/PromptsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Auth0.ManagementApi.Models.Prompts;

namespace Auth0.ManagementApi.Clients
{
/// <summary>
/// Contains methods to access the /prompts endpoints.
/// </summary>
public class PromptsClient : BaseClient
{
private const string PromptsBasePath = "prompts";
/// <summary>
/// Initializes a new instance on <see cref="PromptsClient"/>
/// </summary>
/// <param name="connection"><see cref="IManagementConnection"/> used to make all API calls.</param>
/// <param name="baseUri"><see cref="Uri"/> of the endpoint to use in making API calls.</param>
/// <param name="defaultHeaders">Dictionary containing default headers included with every request this client makes.</param>
public PromptsClient(IManagementConnection connection, Uri baseUri, IDictionary<string, string> defaultHeaders)
: base(connection, baseUri, defaultHeaders)
{
}

/// <summary>
/// Get prompts settings
/// </summary>
/// <remarks>
/// Get prompts settings
/// </remarks>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>A <see cref="Prompt"/> instance containing the information about the prompt settings.</returns>
public Task<Prompt> GetAsync(CancellationToken cancellationToken = default)
{
return Connection.GetAsync<Prompt>(BuildUri($"{PromptsBasePath}"), DefaultHeaders, cancellationToken: cancellationToken);
}

/// <summary>
/// Update prompts settings.
/// </summary>
/// <remarks>
/// Update prompts settings.
/// </remarks>
/// <param name="request">Specifies prompt setting values that are to be updated.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>The <see cref="Prompt"/> that was updated.</returns>
public Task<Prompt> UpdateAsync(PromptUpdateRequest request, CancellationToken cancellationToken = default)
{
return Connection.SendAsync<Prompt>(new HttpMethod("PATCH"), BuildUri($"{PromptsBasePath}"), request, DefaultHeaders, cancellationToken: cancellationToken);
}
}
}
6 changes: 6 additions & 0 deletions src/Auth0.ManagementApi/ManagementApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public class ManagementApiClient : IDisposable
/// </summary>
public OrganizationsClient Organizations { get; }

/// <summary>
/// Contains all the methods to call the /prompts endpoints.
/// </summary>
public PromptsClient Prompts { get; }

/// <summary>
/// Contains all the methods to call the /resource-servers endpoints.
/// </summary>
Expand Down Expand Up @@ -171,6 +176,7 @@ public ManagementApiClient(string token, Uri baseUri, IManagementConnection mana
Jobs = new JobsClient(managementConnection, baseUri, defaultHeaders);
Logs = new LogsClient(managementConnection, baseUri, defaultHeaders);
LogStreams = new LogStreamsClient(managementConnection, baseUri, defaultHeaders);
Prompts = new PromptsClient(managementConnection, baseUri, defaultHeaders);
Organizations = new OrganizationsClient(managementConnection, baseUri, defaultHeaders);
ResourceServers = new ResourceServersClient(managementConnection, baseUri, defaultHeaders);
Roles = new RolesClient(managementConnection, baseUri, defaultHeaders);
Expand Down
28 changes: 28 additions & 0 deletions src/Auth0.ManagementApi/Models/Prompts/Prompt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.Prompts
{
/// <summary>
/// Represents Prompt Settings.
/// </summary>
public class Prompt
{
/// <summary>
/// Which login experience to use. Can be new or classic
/// </summary>
[JsonProperty("universal_login_experience")]
public string UniversalLoginExperience { get; set; }

/// <summary>
/// Whether identifier first is enabled or not.
/// </summary>
[JsonProperty("identifier_first")]
public bool IdentifierFirst { get; set; }

/// <summary>
/// Use WebAuthn with Device Biometrics as the first authentication factor
/// </summary>
[JsonProperty("webauthn_platform_first_factor")]
public bool WebAuthnPlatformFirstFactor { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Auth0.ManagementApi.Models.Prompts
{
/// <summary>
/// Request configuration for updating prompt settings.
/// </summary>
public class PromptUpdateRequest : Prompt
{
}
}
47 changes: 47 additions & 0 deletions tests/Auth0.ManagementApi.IntegrationTests/PromptsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Auth0.Tests.Shared;
using FluentAssertions;
using System.Threading.Tasks;
using Auth0.ManagementApi.Models.Prompts;
using Xunit;

namespace Auth0.ManagementApi.IntegrationTests
{
public class PromptsTests : TestBase, IAsyncLifetime
{
private ManagementApiClient _apiClient;
public async Task InitializeAsync()
{
string token = await GenerateManagementApiToken();

_apiClient = new ManagementApiClient(token, GetVariable("AUTH0_MANAGEMENT_API_URL"), new HttpClientManagementConnection(options: new HttpClientManagementConnectionOptions { NumberOfHttpRetries = 9 }));
}

public Task DisposeAsync()
{
_apiClient.Dispose();
return Task.CompletedTask;
}

[Fact]
public async Task Test_get_and_update_prompts()
{
var prompts = await _apiClient.Prompts.GetAsync();
prompts.Should().NotBeNull();

var originalExperience = prompts.UniversalLoginExperience;
var newExperience = originalExperience == "classic" ? "new" : "classic";

await _apiClient.Prompts.UpdateAsync(new PromptUpdateRequest {UniversalLoginExperience = newExperience });

prompts = await _apiClient.Prompts.GetAsync();
prompts.Should().NotBeNull();
prompts.UniversalLoginExperience.Should().Be(newExperience);

await _apiClient.Prompts.UpdateAsync(new PromptUpdateRequest { UniversalLoginExperience = originalExperience });

prompts = await _apiClient.Prompts.GetAsync();
prompts.Should().NotBeNull();
prompts.UniversalLoginExperience.Should().Be(originalExperience);
}
}
}