Skip to content

Commit

Permalink
Merge pull request #8 from illiminable/master
Browse files Browse the repository at this point in the history
Two-way support and remove limit on leading zeroes.
  • Loading branch information
illiminable committed Jan 14, 2014
2 parents 663cb8d + 4596e0f commit 53b24ed
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 31 deletions.
36 changes: 36 additions & 0 deletions docs/Web_Services.aml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,42 @@
</para>
</entry>
</row>
<row>
<entry>
<para>
<legacyBold>Push</legacyBold>
</para>
</entry>
<entry>
<para>
Delivery authorization requests to your users via Push Notification, and then by receiving their permission responses via their mobile device’s wireless Internet connection.
</para>
</entry>
</row>
<row>
<entry>
<para>
<legacyBold>SoftToken</legacyBold>
</para>
</entry>
<entry>
<para>
Authenticate your end users when they use the TeleSign AuthID application on their mobile device to generate a Time-based One-time Password (TOTP) verification code
</para>
</entry>
</row>
<row>
<entry>
<para>
<legacyBold>TwoWaySms</legacyBold>
</para>
</entry>
<entry>
<para>
Authenticate your users and verify user transactions via two-way Short Message Service (SMS) wireless communication
</para>
</entry>
</row>
</table>
</entry>
</row>
Expand Down
5 changes: 0 additions & 5 deletions src/TeleSign.Services.PhoneId/RawPhoneIdService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ public class RawPhoneIdService : TeleSignService
/// </summary>
private const string LiveResourceFormatString = "/v1/phoneid/live/{0}";

/// <summary>
/// The default value for a use case id if not specified.
/// </summary>
private const string DefaultUseCaseId = "othr";

/// <summary>
/// Initializes a new instance of the RawPhoneIdService class with a supplied credential and uri.
/// </summary>
Expand Down
12 changes: 6 additions & 6 deletions src/TeleSign.Services.UnitTests/BasePhoneIdResponseParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void TestValidPhoneIdScoreParsing()

Assert.AreEqual(response.Errors.Count, 0);
Assert.AreEqual(response.ReferenceId, "013890676FB7010BE1D4494A0000000E");
Assert.AreEqual(response.ResourceUri, null);
Assert.AreEqual(response.ResourceUri, string.Empty);
Assert.AreNotEqual(response.RawResponse, null);
Assert.AreEqual(response.SubResource, "score");

Expand Down Expand Up @@ -62,7 +62,7 @@ public void TestValidPhoneIdStandardParsing()

Assert.AreEqual(response.Errors.Count, 0);
Assert.AreEqual(response.ReferenceId, "01389064DBCE010BE1D449490000000E");
Assert.AreEqual(response.ResourceUri, null);
Assert.AreEqual(response.ResourceUri, string.Empty);
Assert.AreNotEqual(response.RawResponse, null);
Assert.AreEqual(response.SubResource, "standard");

Expand Down Expand Up @@ -115,7 +115,7 @@ public void TestValidPhoneIdContactNoMatchParsing()
PhoneIdContactResponse response = this.CreateParser().ParsePhoneIdContactResponse(json);

Assert.AreEqual(response.ReferenceId, "013890689D26010BE1D4494700000010");
Assert.AreEqual(response.ResourceUri, null);
Assert.AreEqual(response.ResourceUri, string.Empty);
Assert.AreNotEqual(response.RawResponse, null);
Assert.AreEqual(response.SubResource, "contact");

Expand Down Expand Up @@ -173,7 +173,7 @@ public void TestValidPhoneIdContactMatchParsing()

Assert.AreEqual(response.Errors.Count, 0);
Assert.AreEqual(response.ReferenceId, "0138906A9011010BE1D4494B0000000E");
Assert.AreEqual(response.ResourceUri, null);
Assert.AreEqual(response.ResourceUri, string.Empty);
Assert.AreNotEqual(response.RawResponse, null);
Assert.AreEqual(response.SubResource, "contact");

Expand Down Expand Up @@ -236,7 +236,7 @@ public void TestValidPhoneIdLiveNoMatchParsing()
PhoneIdLiveResponse response = this.CreateParser().ParsePhoneIdLiveResponse(json);

Assert.AreEqual(response.ReferenceId, "013B23ED8EC2010BE4D40E410000006B");
Assert.AreEqual(response.ResourceUri, null);
Assert.AreEqual(response.ResourceUri, string.Empty);
Assert.AreNotEqual(response.RawResponse, null);
Assert.AreEqual(response.SubResource, "live");

Expand Down Expand Up @@ -293,7 +293,7 @@ public void TestValidPhoneIdLiveMatchParsing()
PhoneIdLiveResponse response = this.CreateParser().ParsePhoneIdLiveResponse(json);

Assert.AreEqual(response.ReferenceId, "013B23B1FCCB010BE4D40D4D0000005F");
Assert.AreEqual(response.ResourceUri, null);
Assert.AreEqual(response.ResourceUri, string.Empty);
Assert.AreNotEqual(response.RawResponse, null);
Assert.AreEqual(response.SubResource, "live");

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"reference_id": "013890676FB7010BE1D4494A0000000E", "resource_uri": null, "sub_resource": "score", "status": {"updated_on": "2012-07-16T15:28:14.349203Z", "code": 300, "description": "Transaction successfully completed"}, "errors": [], "numbering": {"original": {"complete_phone_number": "13107409700", "country_code":"1", "phone_number": "3107409700"}, "cleansing": {"call": {"country_code": "1","phone_number": "3107409700", "cleansed_code": 100, "max_length": 10, "min_length": 10}, "sms": {"country_code": "1", "phone_number": "3107409700", "cleansed_code": 100, "max_length": 10, "min_length": 10}}}, "risk": {"level": "low", "recommendation": "allow", "score": 0}}
{"reference_id": "013890676FB7010BE1D4494A0000000E", "resource_uri": null, "sub_resource": "score", "status": {"updated_on": "2012-07-16T15:28:14.349203Z", "code": 300, "description": "Transaction successfully completed"}, "errors": [], "location": {"city": "Los Angeles", "state": "CA", "zip": "90066", "metro_code": "4480", "county": "Los Angeles", "country": {"name": "United States", "iso2": "US", "iso3": "USA"}, "coordinates": {"latitude": 33.99791, "longitude": -118.42302}, "time_zone": {"name": "America/Los_Angeles", "utc_offset_min": "-8", "utc_offset_max": "-8"}}, "numbering": {"original": {"complete_phone_number": "13107409700", "country_code":"1", "phone_number": "3107409700"}, "cleansing": {"call": {"country_code": "1","phone_number": "3107409700", "cleansed_code": 100, "max_length": 10, "min_length": 10}, "sms": {"country_code": "1", "phone_number": "3107409700", "cleansed_code": 100, "max_length": 10, "min_length": 10}}}, "risk": {"level": "low", "recommendation": "allow", "score": 0}, "phone_type": {"code": "1", "description": "FIXED_LINE"}}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ private CodeState ParseCodeState(string codeStateString)

if (!Enum.TryParse<CodeState>(codeStateString, true, out codeState))
{
codeState = CodeState.Other;
if (codeStateString == "VALID_YES")
{
codeState = CodeState.Valid;
}
else
{
codeState = CodeState.Other;
}
}

return codeState;
Expand Down
116 changes: 114 additions & 2 deletions src/TeleSign.Services.Verify/RawVerifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,98 @@ public string CallRaw(
null,
language);
}

/////// <summary>
/////// The TeleSign Verify Soft Token web service is a server-side component of the TeleSign AuthID application, and it allows you to authenticate your end users when they use the TeleSign AuthID application on their mobile device to generate a Time-based One-time Password (TOTP) verification code
/////// </summary>
/////// <param name="phoneNumber">The phone number for the Verify Soft Token request, including country code</param>
/////// <param name="softTokenId">
/////// The alphanumeric string that uniquely identifies your TeleSign soft token subscription
/////// </param>
/////// <param name="verifyCode">
/////// The verification code received from the end user
/////// </param>
/////// <returns>The raw JSON response from the REST API.</returns>
////public string SoftTokenRaw(
//// string phoneNumber,
//// string softTokenId = null,
//// string verifyCode = null)
////{
//// phoneNumber = this.CleanupPhoneNumber(phoneNumber);

//// if (softTokenId == null)
//// {
//// softTokenId = string.Empty;
//// }

//// if (verifyCode == null)
//// {
//// verifyCode = string.Empty;
//// }

//// Dictionary<string, string> args = ConstructVerifyArgs(
//// VerificationMethod.SoftToken,
//// phoneNumber,
//// softTokenId,
//// verifyCode);

//// string resourceName = string.Format(
//// RawVerifyService.VerifyResourceFormatString,
//// VerificationMethod.SoftToken.ToString().ToLowerInvariant());

//// WebRequest request = this.ConstructWebMobileRequest(
//// resourceName,
//// "POST",
//// args);

//// return this.WebRequester.ReadResponseAsString(request);
////}

/// <summary>
/// The TeleSign Verify 2-Way SMS web service allows you to authenticate your users and verify user transactions via two-way Short Message Service (SMS) wireless communication. Verification requests are sent to user’s in a text message, and users return their verification responses by replying to the text message.
/// </summary>
/// <param name="phoneNumber">The phone number for the Verify Soft Token request, including country code</param>
/// <param name="ucid">
/// A string specifying one of the Use Case Codes
/// </param>
/// <param name="message">
/// The text to display in the body of the text message. You must include the $$CODE$$ placeholder for the verification code somewhere in your message text. TeleSign automatically replaces it with a randomly-generated verification code
/// </param>
/// <param name="validityPeriod">
/// This parameter allows you to place a time-limit on the verification. This provides an extra level of security by restricting the amount of time your end user has to respond (after which, TeleSign automatically rejects their response). Values are expressed as a natural number followed by a lower-case letter that represents the unit of measure. You can use 's' for seconds, 'm' for minutes, 'h' for hours, and 'd' for days
/// </param>
/// <returns>The raw JSON response from the REST API.</returns>
public string TwoWaySmsRaw(
string phoneNumber,
string message,
string validityPeriod = "5m",
string useCaseId = RawVerifyService.DefaultUseCaseId)
{
CheckArgument.NotEmpty(message, "message");
CheckArgument.NotNullOrEmpty(validityPeriod, "validityPeriod");

phoneNumber = this.CleanupPhoneNumber(phoneNumber);

Dictionary<string, string> args = ConstructVerifyArgs(
VerificationMethod.TwoWaySms,
phoneNumber,
null,
message,
null,
validityPeriod,
useCaseId);

string resourceName = string.Format(
RawVerifyService.VerifyResourceFormatString,
"two_way_sms");

WebRequest request = this.ConstructWebRequest(
resourceName,
"POST",
args);

return this.WebRequester.ReadResponseAsString(request);
}

/// <summary>
/// Initiates a PhoneId Push Mobile transaction returning the raw JSON response from
Expand Down Expand Up @@ -256,14 +348,23 @@ private static Dictionary<string, string> ConstructVerifyArgs(
string phoneNumber,
string verifyCode,
string messageTemplate,
string language)
string language,
string validityPeriod = null,
string useCaseId = RawVerifyService.DefaultUseCaseId)
{
// TODO: Review code generation rules.
if (verifyCode == null)
{
Random r = new Random();
verifyCode = r.Next(100, 99999).ToString();
}
else
{
if (verificationMethod == VerificationMethod.TwoWaySms)
{
throw new ArgumentException("Verify Code cannot be specified for Two-Way SMS", "verifyCode");
}
}

// TODO: Check code validity here?

Expand All @@ -277,17 +378,28 @@ private static Dictionary<string, string> ConstructVerifyArgs(
args.Add("notification_value", verifyCode.ToString());
}
}
else if (verificationMethod == VerificationMethod.TwoWaySms)
{
// Two way sms doesn't take a verify code. So nothing here.
}
else
{
args.Add("verify_code", verifyCode.ToString());
}

args.Add("language", language);

if (verificationMethod == VerificationMethod.Sms || verificationMethod == VerificationMethod.Push)
if (verificationMethod == VerificationMethod.Sms || verificationMethod == VerificationMethod.Push || verificationMethod == VerificationMethod.TwoWaySms)
{
args.Add("template", messageTemplate);
}

if (verificationMethod == VerificationMethod.TwoWaySms)
{
args.Add("validity_period", validityPeriod);
args.Add("ucid", useCaseId);
}

return args;
}
}
Expand Down
86 changes: 75 additions & 11 deletions src/TeleSign.Services.Verify/VerifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,81 @@ public VerifyResponse SendSms(
x);
}
}

/////// <summary>
/////// The TeleSign Verify Soft Token web service is a server-side component of the TeleSign AuthID application, and it allows you to authenticate your end users when they use the TeleSign AuthID application on their mobile device to generate a Time-based One-time Password (TOTP) verification code
/////// </summary>
/////// <param name="phoneNumber">The phone number for the Verify Soft Token request, including country code</param>
/////// <param name="softTokenId">
/////// The alphanumeric string that uniquely identifies your TeleSign soft token subscription
/////// </param>
/////// <param name="verifyCode">
/////// The verification code received from the end user
/////// </param>
/////// <returns>The raw JSON response from the REST API.</returns>
////public VerifyResponse SendSoftToken(
//// string phoneNumber,
//// string softTokenId = null,
//// string verifyCode = null)
////{
//// phoneNumber = this.CleanupPhoneNumber(phoneNumber);

//// string rawResponse = this.SoftTokenRaw(
//// phoneNumber,
//// softTokenId,
//// verifyCode);

//// try
//// {
//// return this.parser.ParseVerifyResponse(rawResponse);
//// }
//// catch (Exception x)
//// {
//// throw new ResponseParseException(
//// "Error parsing Verify SoftToken response",
//// rawResponse,
//// x);
//// }
////}

/// <summary>
/// The TeleSign Verify 2-Way SMS web service allows you to authenticate your users and verify user transactions via two-way Short Message Service (SMS) wireless communication. Verification requests are sent to user’s in a text message, and users return their verification responses by replying to the text message.
/// </summary>
/// <param name="phoneNumber">The phone number for the Verify Soft Token request, including country code</param>
/// <param name="ucid">
/// A string specifying one of the Use Case Codes
/// </param>
/// <param name="message">
/// The text to display in the body of the text message. You must include the $$CODE$$ placeholder for the verification code somewhere in your message text. TeleSign automatically replaces it with a randomly-generated verification code
/// </param>
/// <param name="validityPeriod">
/// This parameter allows you to place a time-limit on the verification. This provides an extra level of security by restricting the amount of time your end user has to respond (after which, TeleSign automatically rejects their response). Values are expressed as a natural number followed by a lower-case letter that represents the unit of measure. You can use 's' for seconds, 'm' for minutes, 'h' for hours, and 'd' for days
/// </param>
/// <returns>The raw JSON response from the REST API.</returns>
public VerifyResponse SendTwoWaySms(
string phoneNumber,
string message = null,
string validityPeriod = "5m")
{
phoneNumber = this.CleanupPhoneNumber(phoneNumber);

string rawResponse = this.TwoWaySmsRaw(
phoneNumber,
message,
validityPeriod);

try
{
return this.parser.ParseVerifyResponse(rawResponse);
}
catch (Exception x)
{
throw new ResponseParseException(
"Error parsing Verify TwoWaySms response",
rawResponse,
x);
}
}

/// <summary>
/// Initiates a TeleSign Verify transaction via a voice call.
Expand Down Expand Up @@ -222,7 +297,6 @@ public VerifyResponse InitiateCall(
/// <param name="language">
/// The language that the message should be in. This parameter is ignored if
/// you supplied a message template.
/// TODO: Details about language string format.
/// </param>
/// <returns>
/// A VerifyResponse object with the status and returned information
Expand Down Expand Up @@ -334,16 +408,6 @@ public virtual void ValidateCodeFormat(string verifyCode)
// Empty code is never valid
CheckArgument.NotEmpty(verifyCode, "verifyCode");

// Leading zeros are not allowed.
if (verifyCode[0] == '0')
{
string message = string.Format(
"Verify code '{0}' must not have leading zeroes.",
verifyCode);

throw new ArgumentException(message);
}

foreach (char c in verifyCode)
{
// Only decimal digits are allowed 0-9.
Expand Down
1 change: 1 addition & 0 deletions src/TeleSign.Services/TeleSign.config.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<TeleSignConfig>
<ServiceUri>https://rest.telesign.com</ServiceUri>
<ServiceMobileUri>https://rest-mobile.telesign.com</ServiceMobileUri>
<Accounts>
<Account name="default">
<!-- Enter your customer id and secret key here. -->
Expand Down
Loading

0 comments on commit 53b24ed

Please sign in to comment.