diff --git a/.gitignore b/.gitignore index 85ba71a..7f6bff1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .* test.php +/vendor/ diff --git a/README.md b/README.md index 7a45999..8967345 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,29 @@ GAuthify-PHP =============== -[Direct link to library](https://github.com/GAuthify/GAuthify-PHP). +[Direct link to library](https://github.com/jeichorn/GAuthify-PHP). This is the PHP API Client for [GAuthify](https://www.gauthify.com). The GAuthify REST api helps websites quickly add multi-factor authentication through Google Authenticator, SMS, and Email. This package is a simple wrapper around that api. +It has been forked by Joshua Eichorn @Pagely(https://pagely.com) to be namespaced and installable with composer Installation -------------- -This library requires and automatically installs requests. -To install simply add gauthify.php into your project directory. +Install using composer + + composer require jeichorn/g-authify-php + +Include vendor/autoload.php, and use the class + + include 'vendor/autoload.php'; + use GAuthify\GAuthify; To run a quick test to make sure everything works fine run: - require_once("gauthify.php"); - $gauthify = new GAuthify() - $gauthify->quick_test((optional), (optional)) + include 'vendor/autoload.php'; + use GAuthify\GAuthify; + $gauthify = new GAuthify(); + $gauthify->quick_test((optional), (optional)); Usage -------------- @@ -24,7 +32,8 @@ First get an API key by signing up for an account [here](http://www.gauthify.com First instantiate a GAuthify object: - require_once("gauthify.php"); + include 'vendor/autoload.php'; + use GAuthify\GAuthify; $auth_instance = new GAuthify(); @@ -125,7 +134,7 @@ They should rarely change and will be backward compatible. The primary error class is GAuthifyError, it can be used as follows: - require('gauthify.php') + use GAuthify\GAuthifyError; try{ diff --git a/bin/run-tests b/bin/run-tests new file mode 100755 index 0000000..c5fe1cb --- /dev/null +++ b/bin/run-tests @@ -0,0 +1,14 @@ +#!/usr/bin/env php +quick_test(); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..fc2f862 --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "jeichorn/g-authify-php", + "description": "GAuthify PHP Client Library", + "license": "MIT", + "authors": [ + { + "name": "Joshua Eichorn", + "email": "joshua.eichorn@pagely.com" + }, + { + "name": "Parham Negahdar", + "email": "pnegahdar@example.com" + } + ], + "autoload": { + "psr-4": { + "GAuthify\\": "src/" + }, + "classmap": [ + "src/exceptions.php" + ] + }, + "require": {} +} diff --git a/gauthify.php b/src/GAuthify.php similarity index 58% rename from gauthify.php rename to src/GAuthify.php index 19aeb1c..9704f2c 100644 --- a/gauthify.php +++ b/src/GAuthify.php @@ -1,61 +1,6 @@ msg = $msg; - $this->http_status = $http_status; - $this->error_code = $error_code; - $this->response_body = $response_body; - parent::__construct($msg, $http_status); - } - -} - -class ApiKeyError extends GAuthifyError -{ - /* - * Raised when API Key is incorrect - */ -} - -class ParameterError extends GAuthifyError -{ - /* - * Raised when submitting bad parameters or missing parameters - */ -} - -class ConflictError extends GAuthifyError -{ - /* - * Raised when a conflicting result exists (e.g post an existing user) - */ -} - -class NotFoundError extends GAuthifyError -{ - /* - * Raised when a result isn't found for the parameters provided. - */ -} - -class ServerError extends GAuthifyError -{ - /* - * Raised for any other error that the server can give, mainly a 500 - */ -} - -class RateLimitError extends GAuthifyError -{ - /* - * Raised when API limit reached either by lack of payment or membership limit - */ -} +namespace GAuthify; +use Exception; class GAuthify { @@ -73,10 +18,8 @@ public function __construct($api_key) 'https://alpha.gauthify.com/v1/', 'https://beta.gauthify.com/v1/' ); - } - private function request_handler($type, $url_addon = '', $params = array()) { /* @@ -95,7 +38,7 @@ private function request_handler($type, $url_addon = '', $params = array()) curl_setopt($req, CURLOPT_TIMEOUT, 5); $resp = curl_exec($req); if (!$resp) { - throw new Exception('Execution Error', 100); + throw new GAuthifyException('Execution Error', 100); } $status_code = curl_getinfo($req, CURLINFO_HTTP_CODE); $json_resp = json_decode($resp, true); @@ -112,7 +55,7 @@ private function request_handler($type, $url_addon = '', $params = array()) throw new ConflictError($json_resp['error_message'], $status_code, $json_resp['error_code'], $resp); } if (!$json_resp) { - throw new Exception("JSON parse error. Likely header size issue.", 100); + throw new GAuthifyException("JSON parse error. Likely header size issue.", 100); } break; } catch (Exception $e) { @@ -284,113 +227,4 @@ public function api_errors() return $this->request_handler('GET', $url_addon); } - - public function quick_test($test_email = false, $test_sms_number = false, $test_voice_number = false) - { - /* - * Runs initial tests to make sure everything is working fine - */ - $account_name = 'testuser@gauthify.com'; - //Setup - try{ - $this->delete_user($account_name); - } - catch(NotFoundError $e){} - function success() - { - print("Success \n"); - } - - print("1) Testing Creating a User..."); - $result = $this->create_user( - $account_name, - $account_name, - $email = 'firsttest@gauthify.com', - $sms_number = '9162627232', - $voice_number = '9162627234' - ); - print_r($result); - assert($result['unique_id'] == $account_name); - assert($result['display_name'] == $account_name); - assert($result['email'] == 'firsttest@gauthify.com'); - assert($result['sms_number'] == '+19162627232'); - assert($result['voice_number'] == '+19162627234'); - success(); - - print("2) Retrieving Created User..."); - $user = $this->get_user($account_name); - assert(is_array($user)); - print_r($user); - success(); - - print("3) Retrieving All Users..."); - $result = $this->get_all_users(); - assert(is_array($result)); - print_r($result);; - success(); - - print("4) Bad Auth Code..."); - $result = $this->check_auth($account_name, '112345'); - assert(is_bool($result)); - print_r($result); - success(); - - print("5) Testing one time pass (OTP)...."); - $result = $this->check_auth($account_name, $user['otp']); - assert(is_bool($result)); - print_r($result); - if (!$result) { - throw new ParameterError('Server error. OTP not working. Contact support@gauthify.com for help.', 500, '500', ''); - } - success(); - if ($test_email) { - print(sprintf("5A) Testing email to %s", $test_email)); - $result = $this->send_email($account_name, $test_email); - print_r($result); - success(); - } - - if ($test_sms_number) { - print(sprintf("5B) Testing SMS to %s", $test_sms_number)); - $this->send_sms($account_name, $test_sms_number); - success(); - } - if ($test_voice_number) { - print(sprintf("5C) Testing call to %s", $test_voice_number)); - $this->send_voice($account_name, $test_voice_number); - success(); - } - - print("6) Testing updating email, phone, and meta"); - $result = $this->update_user($account_name, $email = 'test@gauthify.com', - $sms_number = '9162627234', $sms_number = '9162627235', $meta = array('a' => 'b')); - print_r($result); - assert($result['email'] == 'test@gauthify.com'); - assert($result['sms_number'] == '+19162627234'); - assert($result['voice_number'] == '+19162627235'); - print_r($result); - assert($result['meta']['a'] == 'b'); - $current_key = $result['key']; - success(); - - print("7) Testing key/secret"); - $result = $this->update_user($account_name, null, null, null, null, true); - print($current_key); - print($result['key']); - assert($result['key'] != $current_key); - success(); - - print("8) Deleting Created User..."); - $result = $this->delete_user($account_name); - success(); - - print("9) Testing backup server..."); - $current = $this->access_points[0]; - $this->access_points[0] = 'https://blah.gauthify.com/v1/'; - $result = $this->get_all_users(); - $this->access_points[0] = $current; - print_r($result); - success(); - - } } diff --git a/src/GAuthifyTest.php b/src/GAuthifyTest.php new file mode 100644 index 0000000..aa47c08 --- /dev/null +++ b/src/GAuthifyTest.php @@ -0,0 +1,125 @@ +apikey = $apikey; + } + + public function quick_test($test_email = false, $test_sms_number = false, $test_voice_number = false) + { + /* + * Runs initial tests to make sure everything is working fine + */ + $account_name = 'testuser@gauthify.com'; + + $gauthify = new GAuthify($this->apikey); + + //Setup + try{ + $gauthify->delete_user($account_name); + } + catch(NotFoundError $e){} + + print("1) Testing Creating a User..."); + $result = $gauthify->create_user( + $account_name, + $account_name, + $email = 'firsttest@gauthify.com', + $sms_number = '9162627232', + $voice_number = '9162627234' + ); + print_r($result); + assert($result['unique_id'] == $account_name); + assert($result['display_name'] == $account_name); + assert($result['email'] == 'firsttest@gauthify.com'); + assert($result['sms_number'] == '+19162627232'); + assert($result['voice_number'] == '+19162627234'); + $this->success(); + + print("2) Retrieving Created User..."); + $user = $gauthify->get_user($account_name); + assert(is_array($user)); + print_r($user); + $this->success(); + + print("3) Retrieving All Users..."); + $result = $gauthify->get_all_users(); + assert(is_array($result)); + print_r($result);; + $this->success(); + + print("4) Bad Auth Code..."); + $result = $gauthify->check_auth($account_name, '112345'); + assert(is_bool($result)); + print_r($result); + $this->success(); + + print("5) Testing one time pass (OTP)...."); + $result = $gauthify->check_auth($account_name, $user['otp']); + assert(is_bool($result)); + print_r($result); + if (!$result) { + throw new ParameterError('Server error. OTP not working. Contact support@gauthify.com for help.', 500, '500', ''); + } + $this->success(); + if ($test_email) { + print(sprintf("5A) Testing email to %s", $test_email)); + $result = $gauthify->send_email($account_name, $test_email); + print_r($result); + $this->success(); + } + + if ($test_sms_number) { + print(sprintf("5B) Testing SMS to %s", $test_sms_number)); + $gauthify->send_sms($account_name, $test_sms_number); + $this->success(); + } + if ($test_voice_number) { + print(sprintf("5C) Testing call to %s", $test_voice_number)); + $gauthify->send_voice($account_name, $test_voice_number); + $this->success(); + } + + print("6) Testing updating email, phone, and meta"); + $result = $gauthify->update_user($account_name, $email = 'test@gauthify.com', + $sms_number = '9162627234', $sms_number = '9162627235', $meta = array('a' => 'b')); + print_r($result); + assert($result['email'] == 'test@gauthify.com'); + assert($result['sms_number'] == '+19162627234'); + assert($result['voice_number'] == '+19162627235'); + print_r($result); + assert($result['meta']['a'] == 'b'); + $current_key = $result['key']; + $this->success(); + + print("7) Testing key/secret"); + $result = $gauthify->update_user($account_name, null, null, null, null, true); + print($current_key); + print($result['key']); + assert($result['key'] != $current_key); + $this->success(); + + print("8) Deleting Created User..."); + $result = $gauthify->delete_user($account_name); + $this->success(); + + print("9) Testing backup server..."); + $current = $gauthify->access_points[0]; + $gauthify->access_points[0] = 'https://blah.gauthify.com/v1/'; + $result = $gauthify->get_all_users(); + $gauthify->access_points[0] = $current; + print_r($result); + $this->success(); + + } + + private function success() + { + print("success \n"); + } +} diff --git a/src/exceptions.php b/src/exceptions.php new file mode 100644 index 0000000..95a76ff --- /dev/null +++ b/src/exceptions.php @@ -0,0 +1,58 @@ +msg = $msg; + $this->http_status = $http_status; + $this->error_code = $error_code; + $this->response_body = $response_body; + parent::__construct($msg, $http_status); + } + +} + +class ApiKeyError extends GAuthifyError +{ + /* + * Raised when API Key is incorrect + */ +} + +class ParameterError extends GAuthifyError +{ + /* + * Raised when submitting bad parameters or missing parameters + */ +} + +class ConflictError extends GAuthifyError +{ + /* + * Raised when a conflicting result exists (e.g post an existing user) + */ +} + +class NotFoundError extends GAuthifyError +{ + /* + * Raised when a result isn't found for the parameters provided. + */ +} + +class ServerError extends GAuthifyError +{ + /* + * Raised for any other error that the server can give, mainly a 500 + */ +}