Skip to content

Commit

Permalink
converters & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 committed Dec 21, 2024
1 parent 0fc35df commit c145098
Show file tree
Hide file tree
Showing 47 changed files with 714 additions and 400 deletions.
32 changes: 16 additions & 16 deletions lib/Logitar.Identity.Core/ApiKeys/ApiKeyId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,34 @@ public readonly struct ApiKeyId
public EntityId EntityId { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ApiKeyId"/> struct.
/// Initializes a new instance of the <see cref="ApiKeyId"/> class.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <param name="entityId">The entity identifier.</param>
public ApiKeyId(TenantId? tenantId, Guid entityId) : this(tenantId, Convert.ToBase64String(entityId.ToByteArray()).ToUriSafeBase64())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ApiKeyId"/> struct.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <param name="entityId">The entity identifier.</param>
public ApiKeyId(TenantId? tenantId, string entityId)
public ApiKeyId(TenantId? tenantId, EntityId entityId)
{
StreamId = new(tenantId == null ? entityId.Value : string.Join(Separator, tenantId, entityId));
TenantId = tenantId;
EntityId = new(entityId);
StreamId = new(tenantId.HasValue ? string.Join(Separator, tenantId, entityId) : entityId);
EntityId = entityId;
}

/// <summary>
/// Initializes a new instance of the <see cref="ApiKeyId"/> struct.
/// Initializes a new instance of the <see cref="ApiKeyId"/> class.
/// </summary>
/// <param name="streamId">A stream identifier.</param>
/// <param name="streamId">The identifier of the event stream.</param>
public ApiKeyId(StreamId streamId)
{
StreamId = streamId;

string[] values = streamId.Value.Split(Separator);
TenantId = values.Length == 2 ? new(values[0]) : null;
if (values.Length > 2)
{
throw new ArgumentException($"The value '{streamId}' is not a valid API key ID.", nameof(streamId));
}
else if (values.Length == 2)
{
TenantId = new(values.First());
}
EntityId = new(values.Last());
}

Expand All @@ -67,7 +67,7 @@ public ApiKeyId(StreamId streamId)
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <returns>The generated identifier.</returns>
public static ApiKeyId NewId(TenantId? tenantId = null) => new(tenantId, Guid.NewGuid());
public static ApiKeyId NewId(TenantId? tenantId = null) => new(tenantId, EntityId.NewId());

/// <summary>
/// Returns a value indicating whether or not the specified identifiers are equal.
Expand Down
3 changes: 1 addition & 2 deletions lib/Logitar.Identity.Core/Passwords/OneTimePassword.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Logitar.EventSourcing;
using Logitar.Identity.Core.ApiKeys;
using Logitar.Identity.Core.Passwords.Events;

namespace Logitar.Identity.Core.Passwords;
Expand All @@ -23,7 +22,7 @@ public class OneTimePassword : AggregateRoot
/// <summary>
/// Gets the identifier of the One-Time Password (OTP).
/// </summary>
public new ApiKeyId Id => new(base.Id);
public new OneTimePasswordId Id => new(base.Id);
/// <summary>
/// Gets the tenant identifier of the One-Time Password (OTP).
/// </summary>
Expand Down
41 changes: 25 additions & 16 deletions lib/Logitar.Identity.Core/Passwords/OneTimePasswordId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace Logitar.Identity.Core.Passwords;
/// </summary>
public readonly struct OneTimePasswordId
{
/// <summary>
/// The separator between the tenant ID and the entity ID.
/// </summary>
private const char Separator = ':';

/// <summary>
/// Gets the identifier of the event stream.
/// </summary>
Expand All @@ -26,39 +31,43 @@ public readonly struct OneTimePasswordId
public EntityId EntityId { get; }

/// <summary>
/// Initializes a new instance of the <see cref="OneTimePasswordId"/> struct.
/// Initializes a new instance of the <see cref="OneTimePasswordId"/> class.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <param name="entityId">The entity identifier.</param>
public OneTimePasswordId(TenantId? tenantId, Guid entityId) : this(tenantId, Convert.ToBase64String(entityId.ToByteArray()).ToUriSafeBase64())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="OneTimePasswordId"/> struct.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <param name="entityId">The entity identifier.</param>
public OneTimePasswordId(TenantId? tenantId, string entityId)
public OneTimePasswordId(TenantId? tenantId, EntityId entityId)
{
StreamId = new(tenantId == null ? entityId.Value : string.Join(Separator, tenantId, entityId));
TenantId = tenantId;
EntityId = new(entityId);
StreamId = new(tenantId.HasValue ? $"{tenantId}:{entityId}" : entityId);
EntityId = entityId;
}

/// <summary>
/// Initializes a new instance of the <see cref="OneTimePasswordId"/> struct.
/// Initializes a new instance of the <see cref="OneTimePasswordId"/> class.
/// </summary>
/// <param name="streamId">A stream identifier.</param>
/// <param name="streamId">The identifier of the event stream.</param>
public OneTimePasswordId(StreamId streamId)
{
StreamId = streamId;

string[] values = streamId.Value.Split(Separator);
if (values.Length > 2)
{
throw new ArgumentException($"The value '{streamId}' is not a valid session ID.", nameof(streamId));
}
else if (values.Length == 2)
{
TenantId = new(values.First());
}
EntityId = new(values.Last());
}

/// <summary>
/// Randomly generates a new One-Time Password (OTP) identifier.
/// Randomly generates a new session identifier.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <returns>The generated identifier.</returns>
public static OneTimePasswordId NewId(TenantId? tenantId = null) => new(tenantId, Guid.NewGuid());
public static OneTimePasswordId NewId(TenantId? tenantId = null) => new(tenantId, EntityId.NewId());

/// <summary>
/// Returns a value indicating whether or not the specified identifiers are equal.
Expand Down
32 changes: 16 additions & 16 deletions lib/Logitar.Identity.Core/Roles/RoleId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,34 @@ public readonly struct RoleId
public EntityId EntityId { get; }

/// <summary>
/// Initializes a new instance of the <see cref="RoleId"/> struct.
/// Initializes a new instance of the <see cref="RoleId"/> class.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <param name="entityId">The entity identifier.</param>
public RoleId(TenantId? tenantId, Guid entityId) : this(tenantId, Convert.ToBase64String(entityId.ToByteArray()).ToUriSafeBase64())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RoleId"/> struct.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <param name="entityId">The entity identifier.</param>
public RoleId(TenantId? tenantId, string entityId)
public RoleId(TenantId? tenantId, EntityId entityId)
{
StreamId = new(tenantId == null ? entityId.Value : string.Join(Separator, tenantId, entityId));
TenantId = tenantId;
EntityId = new(entityId);
StreamId = new(tenantId.HasValue ? string.Join(Separator, tenantId, entityId) : entityId);
EntityId = entityId;
}

/// <summary>
/// Initializes a new instance of the <see cref="RoleId"/> struct.
/// Initializes a new instance of the <see cref="RoleId"/> class.
/// </summary>
/// <param name="streamId">A stream identifier.</param>
/// <param name="streamId">The identifier of the event stream.</param>
public RoleId(StreamId streamId)
{
StreamId = streamId;

string[] values = streamId.Value.Split(Separator);
TenantId = values.Length == 2 ? new(values[0]) : null;
if (values.Length > 2)
{
throw new ArgumentException($"The value '{streamId}' is not a valid role ID.", nameof(streamId));
}
else if (values.Length == 2)
{
TenantId = new(values.First());
}
EntityId = new(values.Last());
}

Expand All @@ -67,7 +67,7 @@ public RoleId(StreamId streamId)
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <returns>The generated identifier.</returns>
public static RoleId NewId(TenantId? tenantId = null) => new(tenantId, Guid.NewGuid());
public static RoleId NewId(TenantId? tenantId = null) => new(tenantId, EntityId.NewId());

/// <summary>
/// Returns a value indicating whether or not the specified identifiers are equal.
Expand Down
39 changes: 24 additions & 15 deletions lib/Logitar.Identity.Core/Sessions/SessionId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace Logitar.Identity.Core.Sessions;
/// </summary>
public readonly struct SessionId
{
/// <summary>
/// The separator between the tenant ID and the entity ID.
/// </summary>
private const char Separator = ':';

/// <summary>
/// Gets the identifier of the event stream.
/// </summary>
Expand All @@ -26,39 +31,43 @@ public readonly struct SessionId
public EntityId EntityId { get; }

/// <summary>
/// Initializes a new instance of the <see cref="SessionId"/> struct.
/// Initializes a new instance of the <see cref="SessionId"/> class.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <param name="entityId">The entity identifier.</param>
public SessionId(TenantId? tenantId, Guid entityId) : this(tenantId, Convert.ToBase64String(entityId.ToByteArray()).ToUriSafeBase64())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SessionId"/> struct.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <param name="entityId">The entity identifier.</param>
public SessionId(TenantId? tenantId, string entityId)
public SessionId(TenantId? tenantId, EntityId entityId)
{
StreamId = new(tenantId == null ? entityId.Value : string.Join(Separator, tenantId, entityId));
TenantId = tenantId;
EntityId = new(entityId);
StreamId = new(tenantId.HasValue ? $"{tenantId}:{entityId}" : entityId);
EntityId = entityId;
}

/// <summary>
/// Initializes a new instance of the <see cref="SessionId"/> struct.
/// Initializes a new instance of the <see cref="SessionId"/> class.
/// </summary>
/// <param name="streamId">A stream identifier.</param>
/// <param name="streamId">The identifier of the event stream.</param>
public SessionId(StreamId streamId)
{
StreamId = streamId;

string[] values = streamId.Value.Split(Separator);
if (values.Length > 2)
{
throw new ArgumentException($"The value '{streamId}' is not a valid session ID.", nameof(streamId));
}
else if (values.Length == 2)
{
TenantId = new(values.First());
}
EntityId = new(values.Last());
}

/// <summary>
/// Randomly generates a new session identifier.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <returns>The generated identifier.</returns>
public static SessionId NewId(TenantId? tenantId = null) => new(tenantId, Guid.NewGuid());
public static SessionId NewId(TenantId? tenantId = null) => new(tenantId, EntityId.NewId());

/// <summary>
/// Returns a value indicating whether or not the specified identifiers are equal.
Expand Down
34 changes: 17 additions & 17 deletions lib/Logitar.Identity.Core/Users/UserId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Logitar.Identity.Core.Users;

/// <summary>
/// Represents the identifier of an user.
/// Represents the identifier of a user.
/// </summary>
public readonly struct UserId
{
Expand Down Expand Up @@ -31,34 +31,34 @@ public readonly struct UserId
public EntityId EntityId { get; }

/// <summary>
/// Initializes a new instance of the <see cref="UserId"/> struct.
/// Initializes a new instance of the <see cref="UserId"/> class.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <param name="entityId">The entity identifier.</param>
public UserId(TenantId? tenantId, Guid entityId) : this(tenantId, Convert.ToBase64String(entityId.ToByteArray()).ToUriSafeBase64())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="UserId"/> struct.
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <param name="entityId">The entity identifier.</param>
public UserId(TenantId? tenantId, string entityId)
public UserId(TenantId? tenantId, EntityId entityId)
{
StreamId = new(tenantId == null ? entityId.Value : string.Join(Separator, tenantId, entityId));
TenantId = tenantId;
EntityId = new(entityId);
StreamId = new(tenantId.HasValue ? string.Join(Separator, tenantId, entityId) : entityId);
EntityId = entityId;
}

/// <summary>
/// Initializes a new instance of the <see cref="UserId"/> struct.
/// Initializes a new instance of the <see cref="UserId"/> class.
/// </summary>
/// <param name="streamId">A stream identifier.</param>
/// <param name="streamId">The identifier of the event stream.</param>
public UserId(StreamId streamId)
{
StreamId = streamId;

string[] values = streamId.Value.Split(Separator);
TenantId = values.Length == 2 ? new(values[0]) : null;
if (values.Length > 2)
{
throw new ArgumentException($"The value '{streamId}' is not a valid user ID.", nameof(streamId));
}
else if (values.Length == 2)
{
TenantId = new(values.First());
}
EntityId = new(values.Last());
}

Expand All @@ -67,7 +67,7 @@ public UserId(StreamId streamId)
/// </summary>
/// <param name="tenantId">The tenant identifier.</param>
/// <returns>The generated identifier.</returns>
public static UserId NewId(TenantId? tenantId = null) => new(tenantId, Guid.NewGuid());
public static UserId NewId(TenantId? tenantId = null) => new(tenantId, EntityId.NewId());

/// <summary>
/// Returns a value indicating whether or not the specified identifiers are equal.
Expand Down
3 changes: 2 additions & 1 deletion lib/Logitar.Identity.Core/Users/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public virtual async Task<FoundUsers> FindAsync(string? tenantIdValue, string id
UserId? userId = null;
try
{
userId = new(tenantId, id);
EntityId entityId = new(id);
userId = new(tenantId, entityId);
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Logitar.EventSourcing;
using Logitar.Identity.Core.ApiKeys;

namespace Logitar.Identity.Infrastructure.Converters;

public class ApiKeyIdConverter : JsonConverter<ApiKeyId>
{
public override ApiKeyId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? value = reader.GetString();
if (string.IsNullOrWhiteSpace(value))
{
return new ApiKeyId();
}
StreamId streamId = new(value);
return new(streamId);
}

public override void Write(Utf8JsonWriter writer, ApiKeyId apiKeyId, JsonSerializerOptions options)
{
writer.WriteStringValue(apiKeyId.Value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Logitar.Identity.Core;

namespace Logitar.Identity.Infrastructure.Converters;

public class CustomIdentifierConverter : JsonConverter<CustomIdentifier>
{
public override CustomIdentifier? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return CustomIdentifier.TryCreate(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, CustomIdentifier customIdentifier, JsonSerializerOptions options)
{
writer.WriteStringValue(customIdentifier.Value);
}
}
Loading

0 comments on commit c145098

Please sign in to comment.