Skip to content

Commit

Permalink
feat: add optional claims when generating a token (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d authored Apr 25, 2023
1 parent c437f72 commit 9dc1f4a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
27 changes: 21 additions & 6 deletions Vonage/Jwt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ namespace Vonage;
/// <inheritdoc />
public class Jwt : ITokenGenerator
{
public static string CreateToken(string appId, string privateKey) =>
CreateTokenWithClaims(appId, privateKey);
/// <summary>
/// Creates a token from application id and private key.
/// </summary>
/// <param name="appId">The application id.</param>
/// <param name="privateKey">The private key.</param>
/// <param name="claims">The additional claims.</param>
/// <returns>The token.</returns>
public static string CreateToken(string appId, string privateKey, Dictionary<string, object> claims = null) =>
CreateTokenWithClaims(appId, privateKey, claims);

/// <inheritdoc />
public Result<string> GenerateToken(string applicationId, string privateKey)
public Result<string> GenerateToken(string applicationId, string privateKey,
Dictionary<string, object> claims = null)
{
try
{
Expand All @@ -31,11 +39,18 @@ public Result<string> GenerateToken(string applicationId, string privateKey)
}

/// <inheritdoc />
public Result<string> GenerateToken(Credentials credentials) =>
public Result<string> GenerateToken(Credentials credentials, Dictionary<string, object> claims = null) =>
this.GenerateToken(credentials.ApplicationId, credentials.ApplicationKey);

protected static string CreateTokenWithClaims(string appId, string privateKey,
Dictionary<string, object> claims = null)
/// <summary>
/// Creates a token with custom claims.
/// </summary>
/// <param name="appId">The application Id.</param>
/// <param name="privateKey">The private key.</param>
/// <param name="claims">The custom claims.</param>
/// <returns>The token.</returns>
/// <exception cref="VonageAuthenticationException">When the private key is null or whitespace.</exception>
protected static string CreateTokenWithClaims(string appId, string privateKey, Dictionary<string, object> claims)
{
if (string.IsNullOrWhiteSpace(privateKey))
{
Expand Down
9 changes: 6 additions & 3 deletions Vonage/Voice/ITokenGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Vonage.Common.Monads;
using System.Collections.Generic;
using Vonage.Common.Monads;
using Vonage.Request;

namespace Vonage.Voice;
Expand All @@ -13,13 +14,15 @@ public interface ITokenGenerator
/// </summary>
/// <param name="applicationId">The application Id.</param>
/// <param name="privateKey">The application private key.</param>
/// <param name="claims">The additional claims.</param>
/// <returns>The token.</returns>
Result<string> GenerateToken(string applicationId, string privateKey);
Result<string> GenerateToken(string applicationId, string privateKey, Dictionary<string, object> claims = null);

/// <summary>
/// Generates a token.
/// </summary>
/// <param name="credentials">The application credentials.</param>
/// <param name="claims">The additional claims.</param>
/// <returns>The token.</returns>
Result<string> GenerateToken(Credentials credentials);
Result<string> GenerateToken(Credentials credentials, Dictionary<string, object> claims = null);
}

0 comments on commit 9dc1f4a

Please sign in to comment.