Skip to content

Commit

Permalink
Merge branch 'stoiveyp-PinConfirmationHelper'
Browse files Browse the repository at this point in the history
  • Loading branch information
timheuer committed Jan 29, 2022
2 parents 2fae94d + b57eab0 commit c739852
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Alexa.NET.Tests/Alexa.NET.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<None Update="Examples\NewVersionRequest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Examples\PinConfirmation.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Examples\PinConfirmationSessionResumed.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Examples\PlainTextOutputSpeech.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
13 changes: 13 additions & 0 deletions Alexa.NET.Tests/Examples/PinConfirmation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "Connections.StartConnection",
"uri": "connection://AMAZON.VerifyPerson/2",
"input": {
"requestedAuthenticationConfidenceLevel": {
"level": 400,
"customPolicy": {
"policyName": "VOICE_PIN"
}
}
},
"token": "example-token"
}
18 changes: 18 additions & 0 deletions Alexa.NET.Tests/Examples/PinConfirmationSessionResumed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "SessionResumedRequest",
"requestId": "amzn1.echo-api.request.example",
"timestamp": "2019-10-15T03:15:43Z",
"locale": "en-US",
"cause": {
"type": "ConnectionCompleted",
"token": "Some connection token data",
"status": {
"code": "200",
"message": "Successfully performed PIN confirmation"
},
"result": {
"status": "NOT_ACHIEVED",
"reason": "VERIFICATION_METHOD_NOT_SETUP"
}
}
}
17 changes: 17 additions & 0 deletions Alexa.NET.Tests/SkillConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,22 @@ public void ScheduleFoodReservationConnectionComparison()
Assert.IsType<ScheduleFoodEstablishmentReservation>(Utility.ExampleFileContent<StartConnectionDirective>("ScheduleFoodEstablishmentReservation.json").Input);
}

[Fact]
public void PinConfirmationSerializesCorrectly()
{
var task = new PinConfirmation();
Assert.True(Utility.CompareJson(task.ToConnectionDirective("example-token"), "PinConfirmation.json"));
}

[Fact]
public void ResultDeserializesCorrectly()
{
var task = new PinConfirmation();
var request = Utility.ExampleFileContent<SessionResumedRequest>("PinConfirmationSessionResumed.json");
var result = PinConfirmationConverter.ResultFromSessionResumed(request);
Assert.Equal(PinConfirmationStatus.NotAchieved,result.Status);
Assert.Equal(PinConfirmationReason.VerificationMethodNotSetup,result.Reason);
}

}
}
22 changes: 22 additions & 0 deletions Alexa.NET/ConnectionTasks/Inputs/PinConfirmation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Alexa.NET.Request;
using Newtonsoft.Json;

namespace Alexa.NET.ConnectionTasks.Inputs
{
public class PinConfirmation:IConnectionTask
{
public const string AssociatedUri = "connection://AMAZON.VerifyPerson/2";

//https://developer.amazon.com/en-US/docs/alexa/custom-skills/pin-confirmation-for-alexa-skills.html#connections-startconnection-format
[JsonIgnore]
public string ConnectionUri => AssociatedUri;

[JsonProperty("requestedAuthenticationConfidenceLevel")]
public AuthenticationConfidenceLevel RequestedAuthenticationConfidenceLevel { get; } =
new AuthenticationConfidenceLevel {
Level = 400,
Custom = new AuthenticationConfidenceLevelCustomPolicy("VOICE_PIN")

};
}
}
48 changes: 48 additions & 0 deletions Alexa.NET/ConnectionTasks/Inputs/PinConfirmationConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Linq;
using Alexa.NET.Request.Type;
using Alexa.NET.Response.Converters;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Alexa.NET.ConnectionTasks.Inputs
{
public class PinConfirmationConverter : IConnectionTaskConverter
{
private static readonly JsonSerializer Serializer = JsonSerializer.Create();

public bool CanConvert(JObject jObject)
{
return jObject.ContainsKey("uri") &&
jObject.GetValue("uri").Value<string>() == PinConfirmation.AssociatedUri;
}

public IConnectionTask Convert(JObject jObject)
{
var task = new PinConfirmation();
Serializer.Populate(jObject.CreateReader(), task);
return task;
}

public static void AddToConnectionTaskConverters()
{
if (ConnectionTaskConverter.ConnectionTaskConverters.Where(rc => rc != null)
.All(rc => rc.GetType() != typeof(PinConfirmationConverter)))
{
ConnectionTaskConverter.ConnectionTaskConverters.Add(new PinConfirmationConverter());
}
}

public static PinConfirmationResult ResultFromSessionResumed(SessionResumedRequest request)
{
if (request.Cause.Result is JObject jo)
{
var task = new PinConfirmationResult();
Serializer.Populate(jo.CreateReader(), task);
return task;
}

return null;
}
}
}
13 changes: 13 additions & 0 deletions Alexa.NET/ConnectionTasks/Inputs/PinConfirmationReason.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Runtime.Serialization;

namespace Alexa.NET.ConnectionTasks.Inputs
{
public enum PinConfirmationReason
{
[EnumMember(Value = "METHOD_LOCKOUT")] MethodLockout,

[EnumMember(Value = "VERIFICATION_METHOD_NOT_SETUP")]
VerificationMethodNotSetup,
[EnumMember(Value = "NOT_MATCH")] NotMatch
}
}
16 changes: 16 additions & 0 deletions Alexa.NET/ConnectionTasks/Inputs/PinConfirmationResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Alexa.NET.ConnectionTasks.Inputs
{
public class PinConfirmationResult
{
[JsonProperty("status",NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(StringEnumConverter))]
public PinConfirmationStatus Status { get; set; }

[JsonProperty("reason",NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(StringEnumConverter))]
public PinConfirmationReason Reason { get; set; }
}
}
11 changes: 11 additions & 0 deletions Alexa.NET/ConnectionTasks/Inputs/PinConfirmationStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Runtime.Serialization;

namespace Alexa.NET.ConnectionTasks.Inputs
{
public enum PinConfirmationStatus
{
[EnumMember(Value = "ACHIEVED")] Achieved,
[EnumMember(Value = "NOT_ACHIEVED")] NotAchieved,
[EnumMember(Value = "NOT_ENABLED")] NotEnabled
}
}
6 changes: 5 additions & 1 deletion Alexa.NET/Request/AuthenticationConfidenceLevel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Alexa.NET.Request
{
public class AuthenticationConfidenceLevel
{
[JsonProperty("level")]
public int Level { get; set; }

[JsonProperty("customPolicy",NullValueHandling = NullValueHandling.Ignore)]
public AuthenticationConfidenceLevelCustomPolicy Custom { get; set; }
}
}
17 changes: 17 additions & 0 deletions Alexa.NET/Request/AuthenticationConfidenceLevelCustomPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Newtonsoft.Json;

namespace Alexa.NET.Request
{
public class AuthenticationConfidenceLevelCustomPolicy
{
public AuthenticationConfidenceLevelCustomPolicy(){}

public AuthenticationConfidenceLevelCustomPolicy(string policyName)
{
PolicyName = policyName;
}

[JsonProperty("policyName")]
public string PolicyName { get; set; }
}
}

0 comments on commit c739852

Please sign in to comment.