Skip to content

Commit

Permalink
feat: redirect url on silent auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Nov 22, 2023
1 parent 0e38b86 commit d87b602
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"locale": "en-us",
"channel_timeout": 300,
"code_length": 4,
"brand": "ACME, Inc",
"workflow": [
{
"channel": "silent_auth",
"to": "447700900000",
"redirect_url": "https://acme-app.com/sa/redirect"
}
]
}
46 changes: 35 additions & 11 deletions Vonage.Test.Unit/VerifyV2/StartVerification/RequestBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,6 @@ public void Create_ShouldReturnFailure_GivenFromIsProvidedButEmpty(string value)
.Should()
.BeFailure(ResultFailure.FromErrorMessage("Email is invalid."));

[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void Create_ShouldReturnFailure_GivenSilentAuthWorkflowToIsNullOrWhitespace(string value) =>
BuildBaseRequest()
.WithWorkflow(SilentAuthWorkflow.Parse(value))
.Create()
.Should()
.BeFailure(ResultFailure.FromErrorMessage("Number cannot be null or whitespace."));

[Theory]
[InlineData("")]
[InlineData(" ")]
Expand Down Expand Up @@ -331,6 +320,41 @@ public void Create_ShouldSetSilentAuthWorkflow() =>
workflow.Channel.Should().Be("silent_auth");
workflow.To.Number.Should().Be("123456789");
});

[Fact]
public void Create_ShouldSetSilentAuthWorkflowWithRedirect() =>
BuildBaseRequest()
.WithWorkflow(SilentAuthWorkflow.Parse("123456789", "https://example.com"))
.Create()
.Map(request => request.Workflows)
.Should()
.BeSuccess(workflows =>
{
workflows.Should().HaveCount(1);
var workflow = workflows[0] as SilentAuthWorkflow? ?? default;
workflow.Channel.Should().Be("silent_auth");
workflow.To.Number.Should().Be("123456789");
workflow.RedirectUrl.Should().BeSome("https://example.com");
});

[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void Create_ShouldSetSilentAuthWorkflowWithoutRedirect_GivenRedirectIsNullOrWhitespace(string value) =>
BuildBaseRequest()
.WithWorkflow(SilentAuthWorkflow.Parse("123456789", value))
.Create()
.Map(request => request.Workflows)
.Should()
.BeSuccess(workflows =>
{
workflows.Should().HaveCount(1);
var workflow = workflows[0] as SilentAuthWorkflow? ?? default;
workflow.Channel.Should().Be("silent_auth");
workflow.To.Number.Should().Be("123456789");
workflow.RedirectUrl.Should().BeNone();
});

[Fact]
public void Create_ShouldSetSmsWorkflow() =>
Expand Down
4 changes: 1 addition & 3 deletions Vonage.Test.Unit/VerifyV2/StartVerification/RequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ namespace Vonage.Test.Unit.VerifyV2.StartVerification
{
public class RequestTest
{
private readonly Fixture fixture;

public RequestTest() => this.fixture = new Fixture();
private readonly Fixture fixture = new Fixture();

[Fact]
public void GetEndpointPath_ShouldReturnApiEndpoint() =>
Expand Down
10 changes: 10 additions & 0 deletions Vonage.Test.Unit/VerifyV2/StartVerification/SerializationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public void ShouldSerializeSilentAuthWorkflow() =>
.GetStringContent()
.Should()
.BeSuccess(this.helper.GetRequestJson());

[Fact]
public void ShouldSerializeSilentAuthWorkflowWithRedirectUrl() =>
StartVerificationRequest.Build()
.WithBrand("ACME, Inc")
.WithWorkflow(SilentAuthWorkflow.Parse("447700900000", "https://acme-app.com/sa/redirect"))
.Create()
.GetStringContent()
.Should()
.BeSuccess(this.helper.GetRequestJson());

[Fact]
public void ShouldSerializeSmsWorkflow() =>
Expand Down
3 changes: 3 additions & 0 deletions Vonage.Test.Unit/Vonage.Test.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,9 @@
<None Update="Data\ApplicationTests\CreateApplicationAsyncWithPrivacySettings-response.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="VerifyV2\StartVerification\Data\ShouldSerializeSilentAuthWorkflowWithRedirectUrl-request.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="node ../.scripts/init.js"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Vonage.Common;
using Vonage.Common.Monads;
using Vonage.Common.Serialization;
using Vonage.Common.Validation;

namespace Vonage.VerifyV2.StartVerification.SilentAuth;

Expand All @@ -10,7 +11,11 @@ namespace Vonage.VerifyV2.StartVerification.SilentAuth;
/// </summary>
public readonly struct SilentAuthWorkflow : IVerificationWorkflow
{
private SilentAuthWorkflow(PhoneNumber to) => this.To = to;
private SilentAuthWorkflow(PhoneNumber to, string redirectUrl = null)
{
this.To = to;
this.RedirectUrl = string.IsNullOrWhiteSpace(redirectUrl) ? Maybe<string>.None : redirectUrl;
}

/// <inheritdoc />
[JsonPropertyOrder(0)]
Expand All @@ -24,6 +29,14 @@ namespace Vonage.VerifyV2.StartVerification.SilentAuth;
[JsonConverter(typeof(PhoneNumberJsonConverter))]
public PhoneNumber To { get; }

/// <summary>
/// Final redirect added at the end of the check_url request/response lifecycle. See the documentation for integrations. Will contain the request_id and code as a url fragment after the URL.
/// </summary>
[JsonPropertyOrder(2)]
[JsonConverter(typeof(MaybeJsonConverter<string>))]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public Maybe<string> RedirectUrl { get; }

/// <summary>
/// Parses the input into a SilentAuthWorkflow.
/// </summary>
Expand All @@ -32,6 +45,15 @@ namespace Vonage.VerifyV2.StartVerification.SilentAuth;
public static Result<SilentAuthWorkflow> Parse(string to) =>
PhoneNumber.Parse(to).Map(phoneNumber => new SilentAuthWorkflow(phoneNumber));

/// <summary>
/// Parses the input into a SilentAuthWorkflow.
/// </summary>
/// <param name="to">The phone number to use for authentication.</param>
/// <param name="redirectUrl">The final redirect added at the end of the check_url request/response lifecycle</param>
/// <returns>Success or failure.</returns>
public static Result<SilentAuthWorkflow> Parse(string to, string redirectUrl) =>
PhoneNumber.Parse(to).Map(phoneNumber => new SilentAuthWorkflow(phoneNumber, redirectUrl));

/// <inheritdoc />
public string Serialize(IJsonSerializer serializer) => serializer.SerializeObject(this);
}

0 comments on commit d87b602

Please sign in to comment.