Commits

Devin Martin  committed 423b025

Update the client library to primarily rely on the HTTP status. Any inferred information beyond that is nice, but not required

  • Participants
  • Parent commits 4092d0d

Comments (0)

Files changed (5)

File Authy.Net.Tests/ClientTests.cs

         {
             var client = new AuthyClient(badApiKey, true);
             var result = client.RegisterUser("test@test.com", "123-456-7890");
-            Assert.AreEqual(AuthyStatus.InvalidApiKey, result.Status);
+            Assert.AreEqual(AuthyStatus.Unauthorized, result.Status);
+            Assert.IsTrue((result.ErrorFields & AuthyErrorFields.ApiKey) == AuthyErrorFields.ApiKey);
         }
 
         [TestMethod]
 
             // now for the actual bad token test
             var verifyResult = client.VerifyToken(registrationResult.UserId, "1234567");
-            Assert.AreEqual(AuthyStatus.InvalidToken, verifyResult.Status);
+            Assert.AreEqual(AuthyStatus.Unauthorized, verifyResult.Status);
+            Assert.IsTrue((verifyResult.ErrorFields & AuthyErrorFields.Token) == AuthyErrorFields.Token);
         }
 
         [TestMethod]
         {
             var client = new AuthyClient(badApiKey, true);
             var result = client.VerifyToken("1", "0000000");
-            Assert.AreEqual(AuthyStatus.InvalidApiKey, result.Status);
+
+            Assert.AreEqual(AuthyStatus.Unauthorized, result.Status);
+            Assert.IsTrue((result.ErrorFields & AuthyErrorFields.ApiKey) == AuthyErrorFields.ApiKey);
         }
 
         [TestMethod]
         {
             var client = this.GoodApiKeyClient;
             var result = client.VerifyToken("99999", "1111111");
-            Assert.AreEqual(AuthyStatus.InvalidUser, result.Status);
+            Assert.AreEqual(AuthyStatus.Unauthorized, result.Status);
+            Assert.IsTrue((result.ErrorFields & AuthyErrorFields.User) == AuthyErrorFields.User);
         }
 
         private AuthyClient GoodApiKeyClient

File Authy.Net/AuthyClient.cs

                         result.Status = AuthyStatus.ServiceUnavailable;
                         break;
                     case HttpStatusCode.Unauthorized:
+                        result.Status = AuthyStatus.Unauthorized;
+
+                        // try to infer a more specific reason that an unauthorized error occured
                         if (body.Contains("user has not configured this application") || body.Contains("\"user\":\"user doesn\'t exist in this application\""))
-                            result.Status = AuthyStatus.InvalidUser;
+                            result.ErrorFields = result.ErrorFields | AuthyErrorFields.User;
                         else if (body.Contains("Invalid API key"))
-                            result.Status = AuthyStatus.InvalidApiKey;
+                            result.ErrorFields = result.ErrorFields | AuthyErrorFields.ApiKey;
                         else if (body.Contains("\"token\":\"is invalid"))
-                            result.Status = AuthyStatus.InvalidToken;
-                        else
-                            throw new ApplicationException("An unknown error has occured");
+                            result.ErrorFields = result.ErrorFields | AuthyErrorFields.Token;
+
                         break;
                     default:
                     case HttpStatusCode.BadRequest:
+                        result.Status = AuthyStatus.BadRequest;
+
                         var invalidEmail = body.Contains("\"email\":\"is invalid\"");
                         var invalidCellphone = body.Contains("must be a valid cellphone number.");
+
                         if (invalidCellphone || invalidEmail)
                         {
-                            result.Status = AuthyStatus.BadRequest;
                             if (invalidEmail)
                                 result.ErrorFields = result.ErrorFields | AuthyErrorFields.Email;
                             if (invalidCellphone)
                                 result.ErrorFields = result.ErrorFields | AuthyErrorFields.Cellphone;
                         }
-                        else
-                            throw new ApplicationException("An unknown error has occured");
+
                         break;
                 }
                 return result;

File Authy.Net/AuthyErrorFields.cs

     public enum AuthyErrorFields
     {
         None = 0,
+        /// <summary>
+        /// The provided email is malformatted
+        /// </summary>
         Email = 1,
-        Cellphone = 2
+        /// <summary>
+        /// The cellphone number provided is malformatted
+        /// </summary>
+        Cellphone = 2,
+        /// <summary>
+        /// The provide API key is invalid
+        /// </summary>
+        ApiKey = 4,
+        /// <summary>
+        /// The token for the user is invalid
+        /// </summary>
+        Token = 8,
+        /// <summary>
+        /// The user ID provided doesn't exist
+        /// </summary>
+        User = 16,
     }
 }

File Authy.Net/AuthyResult.cs

         public AuthyStatus Status { get; set; }
 
         /// <summary>
-        /// Fields that have problems
+        /// Fields that have problems.  This value may not be correct.  If possible, it might be best not to rely on them.
         /// </summary>
         public AuthyErrorFields ErrorFields { get; set; }
 

File Authy.Net/AuthyStatus.cs

         /// </summary>
         BadRequest,
         /// <summary>
-        /// The provide API key is invalid
+        /// The request was unauthorized.
+        /// 
+        /// This could mean that an API key is wrong or it could mean that a token is incorrect.
         /// </summary>
-        InvalidApiKey,
-        /// <summary>
-        /// The token for the user is invalid
-        /// </summary>
-        InvalidToken,
-        /// <summary>
-        /// The user ID provided doesn't exist
-        /// </summary>
-        InvalidUser,
+        Unauthorized,
         /// <summary>
         /// The service is unavailable.  This usually means that the API call limit has been exceded
         /// </summary>