Skip to content

Commit

Permalink
Merge pull request #457 from nofrixion/feature/MOOV-3906-authenticati…
Browse files Browse the repository at this point in the history
…on-methods

Feature/moov 3906 authentication methods
  • Loading branch information
arifmatin authored Nov 14, 2024
2 parents 85c79be + 40a9ac2 commit 0d07800
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/NoFrixion.MoneyMoov/Claims/IdentityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Security.Claims;
using System.Security.Principal;
using NoFrixion.Common.Permissions;
using NoFrixion.MoneyMoov.Enums;
using NoFrixion.MoneyMoov.Extensions;
using NoFrixion.MoneyMoov.Models;

Expand Down Expand Up @@ -339,6 +340,36 @@ public static bool HasMerchantPermission(this IIdentity identity, MerchantPermis

return Enum.TryParse(claim.Value, out MerchantPermissions claimPermissions) && claimPermissions.HasFlag(permission);
}

/// <summary>
/// Gets the authentication type from the identity token.
/// </summary>
/// <param name="identity">The token identity</param>
/// <returns>The authentication type.</returns>
public static AuthenticationTypesEnum GetAuthenticationType(this IIdentity identity)
{
var claimsIdentity = identity as ClaimsIdentity;

if (claimsIdentity == null)
{
return AuthenticationTypesEnum.None;
}
else
{
var authenticationClaimType = ClaimsConstants.NOFRIXION_CLAIMS_NAMESPACE + NoFrixionClaimsEnum.approveamr;

var authenticationTypeClaimValue = claimsIdentity.Claims.FirstOrDefault(x => x.Type == authenticationClaimType)?.Value;

if (Enum.TryParse(authenticationTypeClaimValue, out AuthenticationTypesEnum authenticationType))
{
return authenticationType;
}
else
{
return AuthenticationTypesEnum.None;
}
}
}
}

#nullable enable
1 change: 1 addition & 0 deletions src/NoFrixion.MoneyMoov/Enums/AuthenticationTypesEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace NoFrixion.MoneyMoov.Enums;

[Flags]
public enum AuthenticationTypesEnum
{
None = 0,
Expand Down
34 changes: 34 additions & 0 deletions src/NoFrixion.MoneyMoov/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ public static List<T> ToList<T>(this T flags) where T : Enum
}
}

/// <summary>
/// This method converts an Enum with the Flags attribute to a list of Enums.
/// </summary>
public static List<T> ToList<T>(this T? flags) where T : struct, Enum
{
if (!typeof(T).IsDefined(typeof(FlagsAttribute), false))
{
throw new ArgumentException("The type parameter T must have the Flags attribute.", nameof(flags));
}

if (flags == null)
{
return [];
}

// Check if the enum underlying type is ulong
var underlyingType = Enum.GetUnderlyingType(typeof(T));

if (underlyingType == typeof(ulong))
{
return Enum.GetValues(typeof(T))
.Cast<T>()
.Where(value => flags.Value.HasFlag(value) && Convert.ToUInt64(value) != 0) // Exclude None or 0
.ToList();
}
else
{
return Enum.GetValues(typeof(T))
.Cast<T>()
.Where(value => flags.Value.HasFlag(value) && Convert.ToInt32(value) != 0) // Exclude None or 0
.ToList();
}
}

/// <summary>
/// This method converts list of flag enum values to a single flag enum.
/// </summary>
Expand Down
8 changes: 7 additions & 1 deletion src/NoFrixion.MoneyMoov/Models/Beneficiary/Beneficiary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using JetBrains.Annotations;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using NoFrixion.MoneyMoov.Enums;

#nullable disable

Expand Down Expand Up @@ -86,7 +87,12 @@ public class Beneficiary : IValidatableObject
/// The number of distinct authorisers that have authorised the beneficiary.
/// </summary>
public int AuthorisersCompletedCount { get; set; }


/// <summary>
/// A list of authentication types allowed to authorise the payout.
/// </summary>
[CanBeNull] public List<AuthenticationTypesEnum> AuthenticationMethods { get; set; }

public string CreatedByEmailAddress { get; set; }

public string Nonce { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions src/NoFrixion.MoneyMoov/Models/Payouts/Payout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// MIT.
// -----------------------------------------------------------------------------

using NoFrixion.MoneyMoov.Enums;
using System.ComponentModel.DataAnnotations;

namespace NoFrixion.MoneyMoov.Models;
Expand Down Expand Up @@ -345,6 +346,11 @@ public Counterparty? DestinationAccount
/// </summary>
public List<string>? AuthorisedBy { get; set; }

/// <summary>
/// A list of authentication types allowed to authorise the payout.
/// </summary>
public List<AuthenticationTypesEnum>? AuthenticationMethods { get; set; }

/// <summary>
/// If the payout destination is a beneficiary this will be the ID of it's identifier.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/NoFrixion.MoneyMoov/Models/Rules/Rule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//-----------------------------------------------------------------------------

using System.ComponentModel.DataAnnotations;
using NoFrixion.MoneyMoov.Enums;
using Quartz;
using static System.String;

Expand Down Expand Up @@ -119,6 +120,11 @@ public class Rule : IValidatableObject, IWebhookPayload

public required string Nonce { get; set; }

/// <summary>
/// A list of authentication types allowed to authorise the payout.
/// </summary>
public List<AuthenticationTypesEnum>? AuthenticationMethods { get; set; }

/// <summary>
/// The approval hash is used when approving the rule and to detect when critical
/// fields change.
Expand Down

0 comments on commit 0d07800

Please sign in to comment.