Esempio n. 1
0
        private static TResult Execute <TResult>(Func <WebClient, TResult> execute)
            where TResult : AuthyResult, new()
        {
            var client         = new WebClient();
            var libraryVersion = AuthyHelpers.GetVersion();
            var runtimeVersion = AuthyHelpers.GetSystemInfo();
            var userAgent      = $"AuthyNet/{libraryVersion} ({runtimeVersion})";

            // Set a custom user agent
            client.Headers.Add("user-agent", userAgent);

            try
            {
                return(execute(client));
            }
            catch (WebException webex)
            {
                var response = webex.Response.GetResponseStream();

                string body;
                using (var reader = new StreamReader(response ?? throw new Exception("Error streaming response")))
                {
                    body = reader.ReadToEnd();
                }

                var result = JsonConvert.DeserializeObject <TResult>(body);

                switch (((HttpWebResponse)webex.Response).StatusCode)
                {
                case HttpStatusCode.ServiceUnavailable:
                    result.Status = AuthyStatus.ServiceUnavailable;
                    break;

                case HttpStatusCode.Unauthorized:
                    result.Status = AuthyStatus.Unauthorized;
                    break;

                default:
                case HttpStatusCode.BadRequest:
                    result.Status = AuthyStatus.BadRequest;
                    break;
                }

                return(result);
            }
            finally
            {
                client.Dispose();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Send the token via phone call to a user who isn't registered.  If the user is registered with a mobile app then the phone call will be ignored.
        /// </summary>
        /// <param name="userId">The user ID to send the phone call to</param>
        /// <param name="force">Force to the phone call to be sent even if the user is already reigistered as an app user.  This will incrase your costs</param>
        public AuthyResult StartPhoneCall(string userId, bool force = false)
        {
            userId = AuthyHelpers.SanitizeNumber(userId);

            var url =
                $"{BaseUrl}/protected/json/call/{userId}?api_key={_apiKey}{(force ? "&force=true" : string.Empty)}";

            return(Execute(client =>
            {
                var response = client.DownloadString(url);

                var apiResponse = JsonConvert.DeserializeObject <AuthyResult>(response);
                apiResponse.Status = AuthyStatus.Success;
                apiResponse.RawResponse = response;

                return apiResponse;
            }));
        }
Esempio n. 3
0
        /// <summary>
        /// Verify a token with authy
        /// </summary>
        /// <param name="userId">The Authy user id</param>
        /// <param name="token">The token to verify</param>
        /// <param name="force">Force verification to occur even if the user isn't registered (if the user hasn't finished registering the deefault is to succesfully validate)</param>
        public VerifyTokenResult VerifyToken(string userId, string token, bool force = false)
        {
            if (!AuthyHelpers.TokenIsValid(token))
            {
                var errors = new Dictionary <string, string> {
                    { "token", "is invalid" }
                };

                return(new VerifyTokenResult()
                {
                    Status = AuthyStatus.BadRequest,
                    Success = false,
                    Message = "Token is invalid.",
                    Errors = errors
                });
            }

            token  = AuthyHelpers.SanitizeNumber(token);
            userId = AuthyHelpers.SanitizeNumber(userId);

            var url =
                $"{BaseUrl}/protected/json/verify/{token}/{userId}?api_key={_apiKey}{(force ? "&force=true" : string.Empty)}";

            return(Execute(client =>
            {
                var response = client.DownloadString(url);

                var apiResponse = JsonConvert.DeserializeObject <VerifyTokenResult>(response);

                if (apiResponse.Token == "is valid")
                {
                    apiResponse.Status = AuthyStatus.Success;
                }
                else
                {
                    apiResponse.Success = false;
                    apiResponse.Status = AuthyStatus.Unauthorized;
                }

                apiResponse.RawResponse = response;

                return apiResponse;
            }));
        }