Exemplo 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;
            }));
        }
Exemplo 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 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;
            }));
        }
Exemplo n.º 3
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;
            }));
        }
Exemplo n.º 4
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;
            }));
        }
Exemplo n.º 5
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;
            }));
        }