Esempio n. 1
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 default is to succesfully validate)</param>
        /// <exception cref="AuthyTokenInvalidException">Token was invalid.</exception>
        /// <exception cref="AuthyTokenReusedException">Token is already used.</exception>
        /// <exception cref="AuthyUserNotFoundException">User was not found.</exception>

        public VerifyTokenResult VerifyToken(string userId, string token, bool force = false)
        {
            if (!AuthyHelpers.TokenIsValid(token))
            {
                throw new AuthyTokenInvalidException($"Token '{token}' is invalid.");
            }

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

            var url = string.Format("{0}/protected/json/verify/{1}/{2}?api_key={3}{4}", BaseUrl, token, userId, _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;
            }));
        }
Esempio n. 2
0
        private 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      = string.Format("AuthyNet/{0} ({1})", 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();

                if (response == null)
                {
                    throw new AuthyNullResponseException(null);
                }

                string body;
                using (var reader = new StreamReader(response))
                {
                    body = reader.ReadToEnd();
                }

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

                if (result.ErrorCode != null)
                {
                    ErrorHelper.CheckErrorCodeAndThrow(result.ErrorCode, result.Message);
                }

                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. 3
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 registered as an app user. This will incrase your costs</param>
        public AuthyResult StartPhoneCall(string userId, bool force = false)
        {
            userId = AuthyHelpers.SanitizeNumber(userId);

            var url = string.Format("{0}/protected/json/call/{1}?api_key={2}{3}", this.baseUrl, userId, this.apiKey, force ? "&force=true" : string.Empty);

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

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

                return apiResponse;
            }));
        }
Esempio n. 4
0
        /// <summary>
        /// Send an SMS message to a user who isn't registered.  If the user is registered with a mobile app then no message will be sent.
        /// </summary>
        /// <param name="userId">The user ID to send the message to</param>
        /// <param name="force">Force a message to be sent even if the user is already registered as an app user. This will incrase your costs</param>
        public SendSmsResult SendSms(string userId, bool force = false)
        {
            userId = AuthyHelpers.SanitizeNumber(userId);

            var url = string.Format("{0}/protected/json/sms/{1}?api_key={2}{3}", BaseUrl, userId, _apiKey, force ? "&force=true" : string.Empty);

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

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

                return apiResponse;
            }));
        }
Esempio n. 5
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="locale">Force a specific locale. Will be auto-detected based on the phone number country if not provided</param>
        /// <param name="force">Force to the phone call to be sent even if the user is already registered as an app user. This will incrase your costs</param>
        public AuthyResult StartPhoneCall(string userId, string locale = null, bool force = false)
        {
            userId = AuthyHelpers.SanitizeNumber(userId);

            var url = string.Format("{0}/protected/json/call/{1}?api_key={2}{3}{4}",
                                    BaseUrl, userId, _apiKey, !string.IsNullOrEmpty(locale) ? "&locale=" + locale : string.Empty, 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. 6
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 default is to succesfully validate)</param>
        public VerifyTokenResult VerifyToken(string userId, string token, bool force = false)
        {
            if (!AuthyHelpers.TokenIsValid(token))
            {
                Dictionary <string, string> errors = new Dictionary <string, string>();
                errors.Add("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 = string.Format("{0}/protected/json/verify/{1}/{2}?api_key={3}{4}", this.baseUrl, token, userId, this.apiKey, force ? "&force=true" : string.Empty);

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

                VerifyTokenResult 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;
            }));
        }