Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial php library version

Signed-off-by: David A. Cuadrado <krawek@gmail.com>
  • Loading branch information...
commit e9b61365e265f256dc51dbc1bbcb3c90deed6438 0 parents
David Cuadrado dcu authored
1  .gitignore
@@ -0,0 +1 @@
+vendor
16 composer.json
@@ -0,0 +1,16 @@
+{
+ "name": "authy/php",
+ "description": "PHP client for Authy",
+ "require": {
+ "resty/resty": "0.3.8"
+ },
+ "require-dev": {
+ "EHER/PHPUnit": ">= 1.6"
+ },
+ "authors": [
+ {
+ "name": "David Cuadrado",
+ "email": "david@authy.com"
+ }
+ ]
+}
17 composer.lock
@@ -0,0 +1,17 @@
+{
+ "hash": "57e866923f25a483462d84fb99649f8a",
+ "packages": [
+ {
+ "package": "resty/resty",
+ "version": "0.3.8"
+ }
+ ],
+ "packages-dev": null,
+ "aliases": [
+
+ ],
+ "minimum-stability": "dev",
+ "stability-flags": [
+
+ ]
+}
51 lib/Authy/Api.php
@@ -0,0 +1,51 @@
+<?php
+
+class AuthyApi
+{
+ const VERSION = '0.0.1';
+ protected $rest;
+ protected $api_key;
+ protected $api_url;
+
+ public function __construct($api_key, $api_url = "http://sandbox-api.authy.com")
+ {
+ $this->rest = new Resty();
+ $this->rest->setBaseURL($api_url);
+ $this->rest->setUserAgent("authy-php v".AuthyApi::VERSION);
+
+ $this->api_key = $api_key;
+ $this->api_url = $api_url;
+ }
+
+ public function register_user($email, $cellphone, $country_code) {
+ $params = $this->default_params();
+ $params['user'] = array(
+ "email" => $email,
+ "country_code" => $country_code,
+ "cellphone" => $cellphone
+ );
+
+ $resp = $this->rest->post('/protected/json/users/new', $params);
+
+ return new AuthyUser($resp);
+ }
+
+ public function verify_token($authy_id, $token, $opts = array()) {
+ $params = array_merge($this->default_params(), $opts);
+ $resp = $this->rest->get('/protected/json/verify/'. urlencode($token) .'/'. urlencode($authy_id), $params);
+
+ return new AuthyResponse($resp);
+ }
+
+ public function request_sms($authy_id, $opts = array()) {
+ $params = array_merge($this->default_params(), $opts);
+
+ $resp = $this->rest->get('/protected/json/sms/'.urlencode($authy_id), $params);
+
+ return new AuthyResponse($resp);
+ }
+
+ protected function default_params() {
+ return array("api_key" => $this->api_key);
+ }
+};
37 lib/Authy/Response.php
@@ -0,0 +1,37 @@
+<?php
+
+class AuthyResponse {
+ protected $raw_response;
+ protected $body;
+ protected $errors;
+
+ public function __construct($raw_response) {
+ $this->raw_response = $raw_response;
+ $this->body = $raw_response['body'];
+ $this->errors = new stdClass();
+
+ // Handle errors
+ if(isset($this->body->errors)) {
+ $this->errors = $this->body->errors; // when response is {errors: {}}
+ unset($this->body->errors);
+ } else if($raw_response['status'] == 400) {
+ $this->errors = $this->body; // body here is a stdClass
+ $this->body = new stdClass();
+ } else if($raw_response['status'] != 200 && gettype($this->body) == 'string') {
+ $this->errors = (object) array("error" => $this->body); // the response was an error so put the body as an error
+ $this->body = new stdClass();
+ }
+ }
+
+ public function ok() {
+ return $this->raw_response['status'] == 200;
+ }
+
+ public function id() {
+ return isset($this->body->id) ? $this->body->id : null;
+ }
+
+ public function errors() {
+ return $this->errors;
+ }
+}
11 lib/Authy/User.php
@@ -0,0 +1,11 @@
+<?php
+
+class AuthyUser extends \AuthyResponse {
+ public function __construct($raw_response) {
+ if(isset($raw_response['body']->user)) {
+ $raw_response['body'] = $raw_response['body']->user; // response is {user: {id: id}}
+ }
+
+ parent::__construct($raw_response);
+ }
+}
15 phpunit.xml.dist
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<phpunit bootstrap="tests/bootstrap.php" colors="true">
+ <testsuites>
+ <testsuite name="Authy API Test Suite">
+ <directory>tests/Authy/</directory>
+ </testsuite>
+ </testsuites>
+
+ <filter>
+ <whitelist>
+ <directory suffix=".php">src/Authy/</directory>
+ </whitelist>
+ </filter>
+</phpunit>
63 tests/Authy/ApiTest.php
@@ -0,0 +1,63 @@
+<?php
+
+require 'vendor/resty/resty/Resty.php';
+
+class ApiTest extends \PHPUnit_Framework_TestCase
+{
+ public function setUp() {
+ $this->client = new AuthyApi('bf12974d70818a08199d17d5e2bae630', 'http://sandbox-api.authy.com');
+ $this->invalid_token = '1234567';
+ $this->valid_token = '0000000';
+ }
+
+ public function testCreateUserWithValidData() {
+ $user = $this->client->register_user('user@example.com', '305-456-2345', 1);
+
+ $this->assertEquals("integer", gettype($user->id()));
+ $this->assertEmpty((array) $user->errors());
+ }
+
+ public function testCreateUserWithInvalidData() {
+ $user = $this->client->register_user('user@example.com', '', 1);
+
+ $this->assertEquals("NULL", gettype($user->id()));
+ $this->assertNotEmpty((array) $user->errors());
+ }
+
+ public function testVerifyTokenWithValidUser() {
+ $user = $this->client->register_user('user@example.com', '305-456-2345', 1);
+ $token = $this->client->verify_token($user->id(), $this->invalid_token);
+
+ $this->assertEquals(false, $token->ok());
+ }
+
+ public function testVerifyTokenWithInvalidUser() {
+ $token = $this->client->verify_token(0, $this->invalid_token);
+
+ $this->assertEquals(false, $token->ok());
+ $this->assertNotEmpty((array) $token->errors());
+ $this->assertEquals("user has not configured this application", $token->errors()->error);
+ }
+
+ public function testVerifyTokenWithInvalidToken() {
+ $user = $this->client->register_user('user@example.com', '305-456-2345', 1);
+ $token = $this->client->verify_token($user->id(), $this->valid_token);
+ $this->assertEquals(true, $token->ok());
+ }
+
+ public function testRequestSmsWithInvalidUser() {
+ $sms = $this->client->request_sms(0, array("force" => true));
+
+ $this->assertEquals(false, $sms->ok());
+ }
+
+ public function testRequestSmsWithValidUser() {
+ $user = $this->client->register_user('user@example.com', '305-456-2345', 1);
+ $sms = $this->client->request_sms($user->id(), array("force" => true));
+
+ $this->assertEquals(false, $sms->ok());
+ $this->assertEquals("is not activated for this account", $sms->errors()->enable_sms);
+ }
+}
+
+
5 tests/Authy/TestCase.php
@@ -0,0 +1,5 @@
+<?php
+
+class TestCase extends \PHPUnit_Framework_TestCase
+{
+}
14 tests/bootstrap.php
@@ -0,0 +1,14 @@
+<?php
+
+require_once __DIR__.'/Authy/TestCase.php';
+
+spl_autoload_register(function($class)
+{
+ $file = __DIR__.'/../lib/Authy/'.str_replace('Authy', '', $class).'.php';
+
+ if (file_exists($file)) {
+ require $file;
+ return true;
+ }
+});
+
Please sign in to comment.
Something went wrong with that request. Please try again.