-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement authentication mechanism for NumberVerification API
- Loading branch information
Showing
22 changed files
with
477 additions
and
38 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
Vonage.Test/NumberVerification/Authenticate/AuthenticateRequestTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using FluentAssertions; | ||
using Vonage.Common.Failures; | ||
using Vonage.NumberVerification.Authenticate; | ||
using Vonage.Test.Common.Extensions; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.NumberVerification.Authenticate; | ||
|
||
[Trait("Category", "Request")] | ||
public class AuthenticateRequestTest | ||
{ | ||
private const string ValidScope = "scope=test"; | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
[InlineData(null)] | ||
public void ParseFromPhoneNumber_ShouldReturnFailure_GivenNumberIsNullOrWhitespace(string value) => | ||
AuthenticateRequest.Parse(value, ValidScope).Should() | ||
.BeFailure(ResultFailure.FromErrorMessage("Number cannot be null or whitespace.")); | ||
|
||
[Theory] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
[InlineData(null)] | ||
public void ParseFromPhoneNumber_ShouldReturnFailure_GivenScopeIsNullOrWhitespace(string value) => | ||
AuthenticateRequest.Parse("1234567", value).Should() | ||
.BeParsingFailure("Scope cannot be null or whitespace."); | ||
|
||
[Fact] | ||
public void ParseFromPhoneNumber_ShouldReturnFailure_GivenNumberContainsNonDigits() => | ||
AuthenticateRequest.Parse("1234567abc123", ValidScope).Should() | ||
.BeFailure(ResultFailure.FromErrorMessage("Number can only contain digits.")); | ||
|
||
[Fact] | ||
public void ParseFromPhoneNumber_ShouldReturnFailure_GivenNumberLengthIsLowerThan7() => | ||
AuthenticateRequest.Parse("123456", ValidScope).Should() | ||
.BeFailure(ResultFailure.FromErrorMessage("Number length cannot be lower than 7.")); | ||
|
||
[Fact] | ||
public void ParseFromPhoneNumber_ShouldReturnFailure_GivenNumberLengthIsHigherThan15() => | ||
AuthenticateRequest.Parse("1234567890123456", ValidScope).Should() | ||
.BeFailure(ResultFailure.FromErrorMessage("Number length cannot be higher than 15.")); | ||
|
||
[Theory] | ||
[InlineData("1234567", "1234567")] | ||
[InlineData("123456789012345", "123456789012345")] | ||
[InlineData("+1234567890", "1234567890")] | ||
[InlineData("+123456789012345", "123456789012345")] | ||
[InlineData("+++1234567890", "1234567890")] | ||
public void ParseFromPhoneNumber_ShouldSetNumber(string value, string expected) => | ||
AuthenticateRequest.Parse(value, ValidScope) | ||
.Map(request => request.PhoneNumber.Number) | ||
.Should() | ||
.BeSuccess(expected); | ||
|
||
[Fact] | ||
public void ParseFromPhoneNumber_ShouldSetScope() => | ||
AuthenticateRequest.Parse("1234567", ValidScope) | ||
.Map(request => request.Scope) | ||
.Should() | ||
.BeSuccess(ValidScope); | ||
|
||
[Fact] | ||
public void BuildAuthorizeRequest() | ||
{ | ||
var request = AuthenticateRequest.Parse("123456789", ValidScope).GetSuccessUnsafe(); | ||
request.BuildAuthorizeRequest().Number.Should().Be(request.PhoneNumber); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Vonage.Test/NumberVerification/Authenticate/AuthenticateResponseTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Net.Http.Headers; | ||
using FluentAssertions; | ||
using Vonage.NumberVerification.Authenticate; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.NumberVerification.Authenticate; | ||
|
||
[Trait("Category", "Request")] | ||
public class AuthenticateResponseTest | ||
{ | ||
[Fact] | ||
public void BuildAuthenticationHeader_ShouldReturnBearerAuth() => | ||
new AuthenticateResponse("123456789") | ||
.BuildAuthenticationHeader() | ||
.Should() | ||
.Be(new AuthenticationHeaderValue("Bearer", "123456789")); | ||
} |
16 changes: 16 additions & 0 deletions
16
Vonage.Test/NumberVerification/Authenticate/AuthorizeRequestTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Vonage.NumberVerification.Authenticate; | ||
using Vonage.Test.Common.Extensions; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.NumberVerification.Authenticate; | ||
|
||
[Trait("Category", "Request")] | ||
public class AuthorizeRequestTest | ||
{ | ||
[Fact] | ||
public void GetEndpointPath_ShouldReturnApiEndpoint() => | ||
AuthenticateRequest.Parse("123456789", "scope") | ||
.Map(request => request.BuildAuthorizeRequest()) | ||
.Map(r => r.GetEndpointPath()) | ||
.Should().BeSuccess("oauth2/auth"); | ||
} |
15 changes: 15 additions & 0 deletions
15
Vonage.Test/NumberVerification/Authenticate/AuthorizeResponseTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using FluentAssertions; | ||
using Vonage.NumberVerification.Authenticate; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.NumberVerification.Authenticate; | ||
|
||
[Trait("Category", "Request")] | ||
public class AuthorizeResponseTest | ||
{ | ||
[Fact] | ||
public void BuildGetTokenRequest() => | ||
new AuthorizeResponse("123456", 0, 0) | ||
.BuildGetTokenRequest() | ||
.Should().Be(new GetTokenRequest("123456")); | ||
} |
5 changes: 5 additions & 0 deletions
5
Vonage.Test/NumberVerification/Authenticate/Data/ShouldDeserializeAccessToken-response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"access_token": "ABCDEFG", | ||
"token_type": "Bearer", | ||
"expires_in": 3600 | ||
} |
5 changes: 5 additions & 0 deletions
5
Vonage.Test/NumberVerification/Authenticate/Data/ShouldDeserializeAuthorize-response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"auth_req_id": "123456789", | ||
"expires_in": 120, | ||
"interval": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Vonage.NumberVerification.Authenticate; | ||
using Vonage.Test.Common.Extensions; | ||
using WireMock.ResponseBuilders; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.NumberVerification.Authenticate; | ||
|
||
[Trait("Category", "E2E")] | ||
public class E2ETest : SimSwap.E2EBase | ||
{ | ||
public E2ETest() : base(typeof(E2ETest).Namespace) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task Authenticate() | ||
{ | ||
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create() | ||
.WithPath("/oauth2/auth") | ||
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue) | ||
.WithBody( | ||
"login_hint=tel:%2B447700900000&scope=openid+dpv%3AFraudPreventionAndDetection%23check-sim-swap") | ||
.UsingPost()) | ||
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK) | ||
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserializeAuthorize)))); | ||
this.Helper.Server.Given(WireMock.RequestBuilders.Request.Create() | ||
.WithPath("/oauth2/token") | ||
.WithHeader("Authorization", this.Helper.ExpectedAuthorizationHeaderValue) | ||
.WithBody("auth_req_id=123456789&grant_type=urn:openid:params:grant-type:ciba") | ||
.UsingPost()) | ||
.RespondWith(Response.Create().WithStatusCode(HttpStatusCode.OK) | ||
.WithBody(this.Serialization.GetResponseJson(nameof(SerializationTest.ShouldDeserializeAccessToken)))); | ||
await this.Helper.VonageClient.NumberVerificationClient | ||
.AuthenticateAsync(AuthenticateRequest.Parse("447700900000", | ||
"dpv:FraudPreventionAndDetection#check-sim-swap")) | ||
.Should() | ||
.BeSuccessAsync(new AuthenticateResponse("ABCDEFG")); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Vonage.Test/NumberVerification/Authenticate/GetTokenRequestTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using FluentAssertions; | ||
using Vonage.NumberVerification.Authenticate; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.NumberVerification.Authenticate; | ||
|
||
[Trait("Category", "Request")] | ||
public class GetTokenRequestTest | ||
{ | ||
[Fact] | ||
public void GetEndpointPath_ShouldReturnApiEndpoint() => | ||
new GetTokenRequest("123456").GetEndpointPath().Should().Be("oauth2/token"); | ||
} |
31 changes: 31 additions & 0 deletions
31
Vonage.Test/NumberVerification/Authenticate/SerializationTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Vonage.NumberVerification.Authenticate; | ||
using Vonage.Serialization; | ||
using Vonage.Test.Common; | ||
using Vonage.Test.Common.Extensions; | ||
using Xunit; | ||
|
||
namespace Vonage.Test.NumberVerification.Authenticate; | ||
|
||
[Trait("Category", "Serialization")] | ||
public class SerializationTest | ||
{ | ||
private readonly SerializationTestHelper helper = new SerializationTestHelper( | ||
typeof(SerializationTest).Namespace, | ||
JsonSerializerBuilder.BuildWithSnakeCase()); | ||
|
||
[Fact] | ||
public void ShouldDeserializeAuthorize() => this.helper.Serializer | ||
.DeserializeObject<AuthorizeResponse>(this.helper.GetResponseJson()) | ||
.Should() | ||
.BeSuccess(GetExpectedAuthorizeResponse()); | ||
|
||
[Fact] | ||
public void ShouldDeserializeAccessToken() => this.helper.Serializer | ||
.DeserializeObject<GetTokenResponse>(this.helper.GetResponseJson()) | ||
.Should() | ||
.BeSuccess(GetExpectedTokenResponse()); | ||
|
||
internal static AuthorizeResponse GetExpectedAuthorizeResponse() => new AuthorizeResponse("123456789", 120, 2); | ||
|
||
internal static GetTokenResponse GetExpectedTokenResponse() => new GetTokenResponse("ABCDEFG", "Bearer", 3600); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Vonage.Serialization; | ||
using Vonage.Test.Common; | ||
using Vonage.Test.TestHelpers; | ||
|
||
namespace Vonage.Test.NumberVerification; | ||
|
||
public class E2EBase | ||
{ | ||
internal readonly TestingContext Helper; | ||
internal readonly SerializationTestHelper Serialization; | ||
|
||
protected E2EBase(string serializationNamespace) : this() => this.Serialization = | ||
new SerializationTestHelper(serializationNamespace, JsonSerializerBuilder.BuildWithSnakeCase()); | ||
|
||
protected E2EBase() => this.Helper = TestingContext.WithBearerCredentials("Url.Api.EMEA"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Vonage/NumberVerification/Authenticate/AuthenticateRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Vonage.Common; | ||
using Vonage.Common.Monads; | ||
using Vonage.Common.Validation; | ||
|
||
namespace Vonage.NumberVerification.Authenticate; | ||
|
||
/// <summary> | ||
/// Represents a request to authenticate towards NumberVerification API. | ||
/// </summary> | ||
public readonly struct AuthenticateRequest | ||
{ | ||
/// <summary> | ||
/// Parses the input into an AuthenticateRequest. | ||
/// </summary> | ||
/// <param name="number">The phone number.</param> | ||
/// <param name="tokenScope">The authorization scope for the token.</param> | ||
/// <returns>Success if the input matches all requirements. Failure otherwise.</returns> | ||
public static Result<AuthenticateRequest> Parse(string number, string tokenScope) => | ||
PhoneNumber.Parse(number).Map(phoneNumber => new AuthenticateRequest | ||
{ | ||
PhoneNumber = phoneNumber, | ||
Scope = tokenScope, | ||
}) | ||
.Map(InputEvaluation<AuthenticateRequest>.Evaluate) | ||
.Bind(evaluation => evaluation.WithRules(VerifyScope)); | ||
|
||
/// <summary> | ||
/// Subscriber number in E.164 format (starting with country code). Optionally prefixed with '+'. | ||
/// </summary> | ||
public PhoneNumber PhoneNumber { get; private init; } | ||
|
||
/// <summary> | ||
/// The authorization scope for the token. | ||
/// </summary> | ||
public string Scope { get; private init; } | ||
|
||
private static Result<AuthenticateRequest> VerifyScope(AuthenticateRequest request) => | ||
InputValidation.VerifyNotEmpty(request, request.Scope, nameof(request.Scope)); | ||
|
||
internal AuthorizeRequest BuildAuthorizeRequest() => new AuthorizeRequest(this.PhoneNumber, this.Scope); | ||
} |
16 changes: 16 additions & 0 deletions
16
Vonage/NumberVerification/Authenticate/AuthenticateResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Net.Http.Headers; | ||
|
||
namespace Vonage.NumberVerification.Authenticate; | ||
|
||
/// <summary> | ||
/// Represents an authentication response. | ||
/// </summary> | ||
/// <param name="AccessToken">The access token.</param> | ||
public record AuthenticateResponse(string AccessToken) | ||
{ | ||
/// <summary> | ||
/// </summary> | ||
/// <returns></returns> | ||
public AuthenticationHeaderValue BuildAuthenticationHeader() => | ||
new AuthenticationHeaderValue("Bearer", this.AccessToken); | ||
} |
Oops, something went wrong.