Skip to content

Commit

Permalink
feat: extend support for VideoTokenGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Dec 13, 2023
1 parent 8af1d00 commit 8716a7a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
8 changes: 8 additions & 0 deletions Vonage.Common/Exceptions/VonageAuthenticationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public static VonageAuthenticationException FromMissingApiKeyOrSecret() =>
/// <returns>The exception.</returns>
public static VonageAuthenticationException FromMissingApplicationIdOrPrivateKey() =>
new("AppId or Private Key Path missing.");

/// <summary>
/// Creates an exception indicating the ApplicationId or PrivateKeyPath are missing.
/// </summary>
/// <param name="failure">The failure.</param>
/// <returns>The exception.</returns>
public static VonageAuthenticationException FromError(string failure) =>
new(failure);
}
8 changes: 6 additions & 2 deletions Vonage.Common/Failures/AuthenticationFailure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
namespace Vonage.Common.Failures;

/// <inheritdoc />
public struct AuthenticationFailure : IResultFailure
public readonly struct AuthenticationFailure : IResultFailure
{
private readonly string failure;

public AuthenticationFailure(string failure) => this.failure = failure;

/// <inheritdoc />
public Type Type => typeof(AuthenticationFailure);

/// <inheritdoc />
public string GetFailureMessage() => VonageAuthenticationException.FromMissingApplicationIdOrPrivateKey().Message;
public string GetFailureMessage() => this.failure;

/// <inheritdoc />
public Exception ToException() => VonageAuthenticationException.FromMissingApplicationIdOrPrivateKey();
Expand Down
3 changes: 2 additions & 1 deletion Vonage.Test.Unit/JwtTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class JwtTest
[InlineData(" ")]
[InlineData(null)]
public void GenerateToken_ShouldReturnAuthenticationFailure_GivenPrivateKeyIsNullOrWhitespace(string key) =>
new Jwt().GenerateToken(ApplicationId, key).Should().BeFailure(new AuthenticationFailure());
new Jwt().GenerateToken(ApplicationId, key).Should()
.BeFailure(new AuthenticationFailure("AppId or Private Key Path missing."));

[Fact]
public void GenerateToken_ShouldReturnSuccess_GivenCredentialsAreProvided() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ public class VideoTokenGeneratorTest

[Fact]
public void GenerateToken_ShouldReturnFailure_GivenTokenGenerationFails() =>
CreateClaims()
.Bind(claims =>
new VideoTokenGenerator().GenerateToken(Credentials.FromAppIdAndPrivateKey("123", "random"),
claims))
new VideoTokenGenerator().GenerateToken(Credentials.FromAppIdAndPrivateKey("123", "random"), CreateClaims())
.Should()
.BeFailure(failure =>
failure.GetFailureMessage().Should()
.Be("RsaUsingSha alg expects key to be of RSA type or Jwk type with kty='RSA'"));

[Fact]
public void GenerateToken_ShouldReturnSuccess_GivenCredentialsAreProvided() =>
CreateClaims()
.Bind(claims => new VideoTokenGenerator().GenerateToken(this.GetCredentials(), claims))
new VideoTokenGenerator().GenerateToken(this.GetCredentials(), CreateClaims())
.Should()
.BeSuccess(token =>
{
Expand Down
4 changes: 2 additions & 2 deletions Vonage/Jwt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public Result<string> GenerateToken(string applicationId, string privateKey,
{
return CreateToken(applicationId, privateKey);
}
catch (Exception)
catch (Exception exception)
{
return Result<string>.FromFailure(new AuthenticationFailure());
return Result<string>.FromFailure(new AuthenticationFailure(exception.Message));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Vonage/Video/Authentication/IVideoTokenGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public interface IVideoTokenGenerator
/// <param name="credentials">The application credentials.</param>
/// <param name="claims">Additional claims for the token.</param>
/// <returns>A success state with the token if the parsing succeeded. A failure state with an error if it failed.</returns>
Result<VideoToken> GenerateToken(Credentials credentials, TokenAdditionalClaims claims);
Result<VideoToken> GenerateToken(Credentials credentials, Result<TokenAdditionalClaims> claims);
}
23 changes: 7 additions & 16 deletions Vonage/Video/Authentication/VideoTokenGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using Vonage.Common.Failures;
using Vonage.Common.Monads;
using Vonage.Common.Monads;
using Vonage.Request;

namespace Vonage.Video.Authentication;
Expand All @@ -9,17 +7,10 @@ namespace Vonage.Video.Authentication;
public class VideoTokenGenerator : Jwt, IVideoTokenGenerator
{
/// <inheritdoc />
public Result<VideoToken> GenerateToken(Credentials credentials, TokenAdditionalClaims claims)
{
try
{
var tokenValue = CreateTokenWithClaims(credentials.ApplicationId, credentials.ApplicationKey,
claims.ToDataDictionary());
return Result<VideoToken>.FromSuccess(new VideoToken(claims.SessionId, tokenValue));
}
catch (Exception exception)
{
return Result<VideoToken>.FromFailure(ResultFailure.FromErrorMessage(exception.Message));
}
}
public Result<VideoToken> GenerateToken(Credentials credentials, Result<TokenAdditionalClaims> claims) =>
claims.Bind(validClaim => this.BuildVideoToken(credentials, validClaim));

private Result<VideoToken> BuildVideoToken(Credentials credentials, TokenAdditionalClaims validClaim) =>
this.GenerateToken(credentials, validClaim.ToDataDictionary())
.Map(token => new VideoToken(validClaim.SessionId, token));
}

0 comments on commit 8716a7a

Please sign in to comment.