Skip to content

Commit

Permalink
Adds support for sessions and refresh-tokens end-points
Browse files Browse the repository at this point in the history
  • Loading branch information
kailash-b committed Oct 22, 2024
1 parent 2618df5 commit 6a7a0d1
Show file tree
Hide file tree
Showing 23 changed files with 749 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Auth0.ManagementApi/Clients/IRefreshTokenClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Threading;
using System.Threading.Tasks;

using Auth0.ManagementApi.Models.RefreshTokens;

namespace Auth0.ManagementApi.Clients
{
public interface IRefreshTokenClient
{
/// <summary>
/// Retrieve refresh token information.
/// </summary>
/// <param name="request"><see cref="RefreshTokenGetRequest"/></param>
/// <param name="cancellationToken"> <see cref="CancellationToken"/></param>
/// <returns></returns>
Task<RefreshTokenInformation> GetAsync(RefreshTokenGetRequest request, CancellationToken cancellationToken = default);

/// <summary>
/// Delete a refresh token by its Id.
/// </summary>
/// <param name="id">Id of the refresh token to delete.</param>
/// <param name="cancellationToken"> <see cref="CancellationToken"/></param>
/// <returns></returns>
Task DeleteAsync(string id, CancellationToken cancellationToken = default);
}
}
33 changes: 33 additions & 0 deletions src/Auth0.ManagementApi/Clients/ISessionsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Threading;
using System.Threading.Tasks;
using Auth0.ManagementApi.Models.Sessions;

namespace Auth0.ManagementApi.Clients
{
public interface ISessionsClient
{
/// <summary>
/// Retrieve session information.
/// </summary>
/// <param name="request"><see cref="SessionsGetRequest"/></param>
/// <param name="cancellationToken"> <see cref="CancellationToken"/></param>
/// <returns></returns>
Task<Sessions> GetAsync(SessionsGetRequest request, CancellationToken cancellationToken = default);

/// <summary>
/// Delete a session by Id.
/// </summary>
/// <param name="id">Id of the session to delete.</param>
/// <param name="cancellationToken"> <see cref="CancellationToken"/></param>
/// <returns></returns>
Task DeleteAsync(string id, CancellationToken cancellationToken = default);

/// <summary>
/// Revokes a session by Id and all associated refresh tokens.
/// </summary>
/// <param name="id">Id of the session to revoke</param>
/// <param name="cancellationToken"> <see cref="CancellationToken"/></param>
/// <returns></returns>
Task RevokeAsync(string id, CancellationToken cancellationToken = default);
}
}
50 changes: 50 additions & 0 deletions src/Auth0.ManagementApi/Clients/RefreshTokenClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Auth0.ManagementApi.Models.RefreshTokens;

namespace Auth0.ManagementApi.Clients
{

/// <inheritdoc cref="Auth0.ManagementApi.Clients.IRefreshTokenClient"/>
public class RefreshTokenClient : BaseClient, IRefreshTokenClient
{
private const string RefreshTokensBasePath = "refresh-tokens";

/// <summary>
/// Initializes a new instance on <see cref="RefreshTokenClient"/>
/// </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 RefreshTokenClient(IManagementConnection connection, Uri baseUri, IDictionary<string, string> defaultHeaders)
: base(connection, baseUri, defaultHeaders)
{
}

/// <inheritdoc cref="Auth0.ManagementApi.Clients.IRefreshTokenClient.GetAsync"/>
public Task<RefreshTokenInformation> GetAsync(RefreshTokenGetRequest request, CancellationToken cancellationToken = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));

if(string.IsNullOrEmpty(request.Id))
throw new ArgumentException("Value cannot be null or empty.", nameof(request.Id));

return Connection.GetAsync<RefreshTokenInformation>(
BuildUri($"{RefreshTokensBasePath}/{EncodePath(request.Id)}"),
DefaultHeaders, cancellationToken: cancellationToken);
}

/// <inheritdoc cref="Auth0.ManagementApi.Clients.IRefreshTokenClient.DeleteAsync"/>
public Task DeleteAsync(string id, CancellationToken cancellationToken = default)
{
return Connection.SendAsync<object>(
HttpMethod.Delete,
BuildUri($"{RefreshTokensBasePath}/{EncodePath(id)}"),
null, DefaultHeaders, cancellationToken: cancellationToken);
}
}
}
59 changes: 59 additions & 0 deletions src/Auth0.ManagementApi/Clients/SessionsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Auth0.ManagementApi.Models.Sessions;

namespace Auth0.ManagementApi.Clients
{

/// <inheritdoc cref="Auth0.ManagementApi.Clients.ISessionsClient"/>
public class SessionsClient : BaseClient, ISessionsClient
{
private const string SessionsBasePath = "sessions";

/// <summary>
/// Initializes a new instance of <see cref="SessionsClient"/>.
/// </summary>
/// <param name="connection"></param>
/// <param name="baseUri"></param>
/// <param name="defaultHeaders"></param>
public SessionsClient(IManagementConnection connection, Uri baseUri, IDictionary<string, string> defaultHeaders)
: base(connection, baseUri, defaultHeaders)
{
}

/// <inheritdoc cref="Auth0.ManagementApi.Clients.ISessionsClient.GetAsync"/>
public Task<Sessions> GetAsync(SessionsGetRequest request, CancellationToken cancellationToken = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));

if(string.IsNullOrEmpty(request.Id))
throw new ArgumentException("Value cannot be null or empty.", nameof(request.Id));

return Connection.GetAsync<Sessions>(
BuildUri($"{SessionsBasePath}/{EncodePath(request.Id)}"),
DefaultHeaders, cancellationToken: cancellationToken);
}

/// <inheritdoc cref="Auth0.ManagementApi.Clients.ISessionsClient.DeleteAsync"/>
public Task DeleteAsync(string id, CancellationToken cancellationToken = default)
{
return Connection.SendAsync<object>(
HttpMethod.Delete,
BuildUri($"{SessionsBasePath}/{EncodePath(id)}"),
null, DefaultHeaders, cancellationToken: cancellationToken);
}

/// <inheritdoc cref="Auth0.ManagementApi.Clients.ISessionsClient.RevokeAsync"/>
public Task RevokeAsync(string id, CancellationToken cancellationToken = default)
{
return Connection.SendAsync<Task>(HttpMethod.Post,
BuildUri($"{SessionsBasePath}/{EncodePath(id)}/revoke"),
null,
DefaultHeaders, cancellationToken: cancellationToken);
}
}
}
10 changes: 10 additions & 0 deletions src/Auth0.ManagementApi/IManagementApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ public interface IManagementApiClient : IDisposable
/// Contains all the methods to call the /users endpoints.
/// </summary>
IUsersClient Users { get; }

/// <summary>
/// Contains all the methods to call the /refresh-tokens endpoints.
/// </summary>
IRefreshTokenClient RefreshTokens { get; }

/// <summary>
/// Contains all the methods to call the /sessions endpoints.
/// </summary>
ISessionsClient Sessions { get; }

/// <summary>
/// Update the Access Token used with every request.
Expand Down
8 changes: 8 additions & 0 deletions src/Auth0.ManagementApi/ManagementApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ public class ManagementApiClient : IManagementApiClient
/// Contains all the methods to call the /users endpoints.
/// </summary>
public IUsersClient Users { get; }

/// <inheritdoc cref="Auth0.ManagementApi.IManagementApiClient.RefreshTokens"/>
public IRefreshTokenClient RefreshTokens { get; }

/// <inheritdoc cref="Auth0.ManagementApi.IManagementApiClient.Sessions"/>
public ISessionsClient Sessions { get; }

private Dictionary<string, string> DefaultHeaders { get; set; }

Expand Down Expand Up @@ -213,6 +219,8 @@ public ManagementApiClient(string token, Uri baseUri, IManagementConnection mana
Tickets = new TicketsClient(managementConnection, baseUri, DefaultHeaders);
UserBlocks = new UserBlocksClient(managementConnection, baseUri, DefaultHeaders);
Users = new UsersClient(managementConnection, baseUri, DefaultHeaders);
RefreshTokens = new RefreshTokenClient(managementConnection, baseUri, DefaultHeaders);
Sessions = new SessionsClient(managementConnection, baseUri, DefaultHeaders);
}

/// <summary>
Expand Down
46 changes: 46 additions & 0 deletions src/Auth0.ManagementApi/Models/RefreshTokens/Device.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.RefreshTokens
{
/// <summary>
/// Device used while issuing/exchanging the refresh token/Session
/// </summary>
public class Device
{
/// <summary>
/// First IP address associated with the refresh token/Session
/// </summary>
[JsonProperty("initial_ip")]
public string InitialIp { get; set; }

/// <summary>
/// First autonomous system number associated with the refresh token/Session
/// </summary>
[JsonProperty("initial_asn")]
public string InitialAsn { get; set; }

/// <summary>
/// First user agent associated with the refresh token/Session
/// </summary>
[JsonProperty("initial_user_agent")]
public string InitialUserAgent { get; set; }

/// <summary>
/// Last IP address associated with the refresh token/Session
/// </summary>
[JsonProperty("last_ip")]
public string LastIp { get; set; }

/// <summary>
/// Last autonomous system number associated with the refresh token/Session
/// </summary>
[JsonProperty("last_asn")]
public string LastAsn { get; set; }

/// <summary>
/// Last user agent associated with the refresh token/Session
/// </summary>
[JsonProperty("last_user_agent")]
public string LastUserAgent { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Auth0.ManagementApi.Models.RefreshTokens
{
/// <summary>
/// Represents the information required to Get <see cref="RefreshTokenInformation"/>
/// </summary>
public class RefreshTokenGetRequest
{
/// <summary>
/// ID of the refresh token to retrieve
/// </summary>
public string Id { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.RefreshTokens
{
public class RefreshTokenInformation : SessionsBase
{
/// <summary>
/// ID of the client application granted with this refresh token
/// </summary>
[JsonProperty("client_id")]
public string ClientId { get; set; }

/// <summary>
/// ID of the authenticated session used to obtain this refresh-token
/// </summary>
[JsonProperty("session_id")]
public string SessionId { get; set; }

/// <summary>
/// True if the token is a rotating refresh token
/// </summary>
[JsonProperty("rotating")]
public bool? Rotating { get; set; }

/// <summary>
/// <inheritdoc cref="Auth0.ManagementApi.Models.RefreshTokens.ResourceServer"/>
/// </summary>
[JsonProperty("resource_servers")]
public IList<ResourceServer> ResourceServers { get; set; }

/// <summary>
/// The date and time when the refresh token was last exchanged
/// </summary>
[JsonProperty("last_exchanged_at")]
public DateTime? LastExchangedAt { get; set; }
}
}
18 changes: 18 additions & 0 deletions src/Auth0.ManagementApi/Models/RefreshTokens/ResourceServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Auth0.ManagementApi.Models.RefreshTokens
{
/// <summary>
/// A list of the resource server IDs associated to this refresh-token and their granted scopes
/// </summary>
public class ResourceServer
{
/// <summary>
/// Resource server ID
/// </summary>
public string Audience { get; set; }

/// <summary>
/// List of scopes for the refresh token
/// </summary>
public string Scopes { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/Auth0.ManagementApi/Models/Sessions/Authentication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.Sessions
{
public class Authentication
{
/// <summary>
/// Contains the authentication methods a user has completed during their session
/// </summary>
[JsonProperty("methods")]
public IList<AuthenticationMethods> Methods { get; set; }
}
}
29 changes: 29 additions & 0 deletions src/Auth0.ManagementApi/Models/Sessions/AuthenticationMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Newtonsoft.Json;

namespace Auth0.ManagementApi.Models.Sessions
{
/// <summary>
/// Authentication signal details
/// </summary>
public class AuthenticationMethods
{
/// <summary>
/// One of: "federated", "passkey", "pwd", "sms", "email", "mfa", "mock" or a custom method denoted by a URL
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }

/// <summary>
/// Timestamp of when the signal was received
/// </summary>
[JsonProperty("timestamp")]
public DateTime? Timestamp { get; set; }

/// <summary>
/// A specific MFA factor. Only present when "name" is set to "mfa"
/// </summary>
[JsonProperty("^type$")]
public string Type { get; set; }
}
}
Loading

0 comments on commit 6a7a0d1

Please sign in to comment.