Api.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * ApiClient
  4. *
  5. * PHP version 5
  6. *
  7. * @category Services
  8. * @package Authy
  9. * @author David Cuadrado <[email protected]>
  10. * @license http://creativecommons.org/licenses/MIT/ MIT
  11. * @link http://authy.github.com/pear
  12. */
  13. /**
  14. * Authy API interface.
  15. *
  16. * @category Services
  17. * @package Authy
  18. * @author David Cuadrado <[email protected]>
  19. * @license http://creativecommons.org/licenses/MIT/ MIT
  20. * @link http://authy.github.com/pear
  21. */
  22. class Authy_Api
  23. {
  24. const VERSION = '1.3.0';
  25. protected $rest;
  26. protected $api_key;
  27. protected $api_url;
  28. /**
  29. * Constructor.
  30. *
  31. * @param string $api_key Api Key
  32. * @param string $api_url Optional api url
  33. */
  34. public function __construct($api_key, $api_url = "https://api.authy.com")
  35. {
  36. $this->rest = new Resty();
  37. $this->rest->setBaseURL($api_url);
  38. $this->rest->setUserAgent("authy-php v".Authy_Api::VERSION);
  39. $this->api_key = $api_key;
  40. $this->api_url = $api_url;
  41. }
  42. /**
  43. * Register a user.
  44. *
  45. * @param string $email New user's email
  46. * @param string $cellphone New user's cellphone
  47. * @param string $country_code New user's country code. defaults to USA(1)
  48. *
  49. * @return Authy_User the new registered user
  50. */
  51. public function registerUser($email, $cellphone, $country_code = 1)
  52. {
  53. $params = $this->defaultParams();
  54. $params['user'] = array(
  55. "email" => $email,
  56. "country_code" => $country_code,
  57. "cellphone" => $cellphone
  58. );
  59. $resp = $this->rest->post('/protected/json/users/new', $params);
  60. return new Authy_User($resp);
  61. }
  62. /**
  63. * Verify a given token.
  64. *
  65. * @param string $authy_id User's id stored in your database
  66. * @param string $token The token entered by the user
  67. * @param string $opts Array of options, for example:
  68. * array("force" => "true")
  69. *
  70. * @return Authy_Response the server response
  71. */
  72. public function verifyToken($authy_id, $token, $opts = array())
  73. {
  74. $params = array_merge($this->defaultParams(), $opts);
  75. if (!array_key_exists("force", $params)) {
  76. $params["force"] = "true";
  77. }
  78. $url = '/protected/json/verify/'. urlencode($token)
  79. .'/'. urlencode($authy_id);
  80. $resp = $this->rest->get($url, $params);
  81. return new Authy_Response($resp);
  82. }
  83. /**
  84. * Request a valid token via SMS.
  85. *
  86. * @param string $authy_id User's id stored in your database
  87. * @param string $opts Array of options, for example:
  88. * array("force" => "true")
  89. *
  90. * @return Authy_Response the server response
  91. */
  92. public function requestSms($authy_id, $opts = array())
  93. {
  94. $params = array_merge($this->defaultParams(), $opts);
  95. $url = '/protected/json/sms/'.urlencode($authy_id);
  96. $resp = $this->rest->get($url, $params);
  97. return new Authy_Response($resp);
  98. }
  99. /**
  100. * Cellphone call, usually used with SMS Token issues or if no smartphone is available.
  101. * This function needs the app to be on Starter Plan (free) or higher.
  102. *
  103. * @param string $authy_id User's id stored in your database
  104. * @param string $opts Array of options, for example:
  105. * array("force" => "true")
  106. *
  107. * @return Authy_Response the server response
  108. */
  109. public function phoneCall($authy_id, $opts = array())
  110. {
  111. $params = array_merge($this->defaultParams(), $opts);
  112. $url = '/protected/json/call/'.urlencode($authy_id);
  113. $resp = $this->rest->get($url, $params);
  114. return new Authy_Response($resp);
  115. }
  116. /**
  117. * Deletes an user.
  118. *
  119. * @param string $authy_id User's id stored in your database
  120. *
  121. * @return Authy_Response the server response
  122. */
  123. public function deleteUser($authy_id)
  124. {
  125. $params = array_merge($this->defaultParams());
  126. $url = '/protected/json/users/delete/'.urlencode($authy_id);
  127. $resp = $this->rest->post($url, $params);
  128. return new Authy_Response($resp);
  129. }
  130. /**
  131. * Return the default parameters.
  132. *
  133. * @return array array with the default parameters
  134. */
  135. protected function defaultParams()
  136. {
  137. return array("api_key" => $this->api_key);
  138. }
  139. };