Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Adds support for Authy OneTouch endpoints #26

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions Authy.php
Expand Up @@ -7,5 +7,3 @@
}

class Authy_Api extends Authy\AuthyApi {}

?>
72 changes: 62 additions & 10 deletions lib/Authy/AuthyApi.php
Expand Up @@ -41,7 +41,7 @@ class AuthyApi
public function __construct($api_key, $api_url = "https://api.authy.com")
{
$this->rest = new \GuzzleHttp\Client(array(
'base_url' => "{$api_url}/protected/json/",
'base_url' => "{$api_url}",
'defaults' => array(
'headers' => array('User-Agent' => $this->__getUserAgent() ),
'query' => array('api_key' => $api_key),
Expand All @@ -63,7 +63,7 @@ public function __construct($api_key, $api_url = "https://api.authy.com")
*/
public function registerUser($email, $cellphone, $country_code = 1)
{
$resp = $this->rest->post('users/new', array(
$resp = $this->rest->post('/protected/json/users/new', array(
'query' => array(
'user' => array(
"email" => $email,
Expand Down Expand Up @@ -97,7 +97,7 @@ public function verifyToken($authy_id, $token, $opts = array())
$authy_id = urlencode($authy_id);
$this->__validateVerify($token, $authy_id);

$resp = $this->rest->get("verify/{$token}/{$authy_id}", array(
$resp = $this->rest->get("/protected/json/verify/{$token}/{$authy_id}", array(
'query' => $params
));

Expand All @@ -115,7 +115,7 @@ public function verifyToken($authy_id, $token, $opts = array())
public function requestSms($authy_id, $opts = array())
{
$authy_id = urlencode($authy_id);
$resp = $this->rest->get("sms/{$authy_id}", array(
$resp = $this->rest->get("/protected/json/sms/{$authy_id}", array(
'query' => $opts
));

Expand All @@ -134,7 +134,7 @@ public function requestSms($authy_id, $opts = array())
public function phoneCall($authy_id, $opts = array())
{
$authy_id = urlencode($authy_id);
$resp = $this->rest->get("call/{$authy_id}", array(
$resp = $this->rest->get("/protected/json/call/{$authy_id}", array(
'query' => $opts
));

Expand All @@ -151,7 +151,7 @@ public function phoneCall($authy_id, $opts = array())
public function deleteUser($authy_id)
{
$authy_id = urlencode($authy_id);
$resp = $this->rest->post("users/delete/{$authy_id}");
$resp = $this->rest->post("/protected/json/users/delete/{$authy_id}");
return new AuthyResponse($resp);
}

Expand All @@ -165,7 +165,7 @@ public function deleteUser($authy_id)
public function userStatus($authy_id)
{
$authy_id = urlencode($authy_id);
$resp = $this->rest->get("users/{$authy_id}/status");
$resp = $this->rest->get("/protected/json/users/{$authy_id}/status");
return new AuthyResponse($resp);
}

Expand All @@ -189,7 +189,7 @@ public function phoneVerificationStart($phone_number, $country_code, $via='sms',
if ($locale != null)
$query["locale"] = $locale;

$resp = $this->rest->post("phones/verification/start", array('query' => $query));
$resp = $this->rest->post("/protected/json/phones/verification/start", array('query' => $query));

return new AuthyResponse($resp);
}
Expand All @@ -205,7 +205,7 @@ public function phoneVerificationStart($phone_number, $country_code, $via='sms',
*/
public function phoneVerificationCheck($phone_number, $country_code, $verification_code)
{
$resp = $this->rest->get("phones/verification/check", array(
$resp = $this->rest->get("/protected/json/phones/verification/check", array(
'query' => array(
"phone_number" => $phone_number,
"country_code" => $country_code,
Expand All @@ -226,7 +226,7 @@ public function phoneVerificationCheck($phone_number, $country_code, $verificati
*/
public function phoneInfo($phone_number, $country_code)
{
$resp = $this->rest->get("phones/info", array(
$resp = $this->rest->get("/protected/json/phones/info", array(
'query' => array(
"phone_number" => $phone_number,
"country_code" => $country_code
Expand All @@ -236,6 +236,46 @@ public function phoneInfo($phone_number, $country_code)
return new AuthyResponse($resp);
}

/**
* Create a new approval request for a user
*
* @param string $authy_id User's id stored in your database
* @param string $message
* @param array $opts details, hidden_details, logos, seconds_to_expire
*
* @return AuthyResponse
*
* @see http://docs.authy.com/onetouch.html#create-approvalrequest
*/
public function createApprovalRequest($authy_id, $message, $opts = array())
{
$authy_id = urlencode($authy_id);
$body = array_replace_recursive(['message' => $message], $opts);
$resp = $this->rest->post("/onetouch/json/users/{$authy_id}/approval_requests", ['body' => $body]);

return new AuthyResponse($resp);
}

/**
* Check the status of an approval request
*
* @param string $request_uuid The UUID of the approval request you want to check
*
* @return AuthyResponse
*
* @see http://docs.authy.com/onetouch.html#check-approvalrequest-status
*/
public function getApprovalRequest($request_uuid)
{
$request_uuid = urlencode($request_uuid);
$resp = $this->rest->get("/onetouch/json/approval_requests/{$request_uuid}");

return new AuthyResponse($resp);
}

/**
* @return mixed
*/
private function __getUserAgent()
{
return sprintf(
Expand All @@ -248,6 +288,12 @@ private function __getUserAgent()
);
}

/**
* @param $token
* @param $authy_id
*
* @throws AuthyFormatException
*/
private function __validateVerify($token, $authy_id)
{
$this->__validate_digit($token, "Invalid Token. Only digits accepted.");
Expand All @@ -258,6 +304,12 @@ private function __validateVerify($token, $authy_id)
}
}

/**
* @param $var
* @param $message
*
* @throws AuthyFormatException
*/
private function __validate_digit($var, $message)
{
if( !is_int($var) && !is_numeric($var) ) {
Expand Down