Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modern Refactor of lib #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.*
test.php
/vendor/
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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(<api_key>)
$gauthify->quick_test(<test_email>(optional), <test_number>(optional))
include 'vendor/autoload.php';
use GAuthify\GAuthify;
$gauthify = new GAuthify(<api_key>);
$gauthify->quick_test(<test_email>(optional), <test_number>(optional));

Usage
--------------
Expand All @@ -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(<api_key>);


Expand Down Expand Up @@ -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{
<code here>
Expand Down
14 changes: 14 additions & 0 deletions bin/run-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env php
<?php
if (empty($argv[1]))
{
echo "run-tests apikey\n";
exit(1);
}

require __DIR__.'/../vendor/autoload.php';

use GAuthify\GAuthifyTest;

$test = new GAuthifyTest($argv[1]);
$test->quick_test();
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "jeichorn/g-authify-php",
"description": "GAuthify PHP Client Library",
"license": "MIT",
"authors": [
{
"name": "Joshua Eichorn",
"email": "[email protected]"
},
{
"name": "Parham Negahdar",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"GAuthify\\": "src/"
},
"classmap": [
"src/exceptions.php"
]
},
"require": {}
}
174 changes: 4 additions & 170 deletions gauthify.php → src/GAuthify.php
Original file line number Diff line number Diff line change
@@ -1,61 +1,6 @@
<?php
class GAuthifyError extends Exception
{
/*
* All errors
*/
public function __construct($msg, $http_status, $error_code, $response_body)
{
$this->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
{
Expand All @@ -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())
{
/*
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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 = '[email protected]';
//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 = '[email protected]',
$sms_number = '9162627232',
$voice_number = '9162627234'
);
print_r($result);
assert($result['unique_id'] == $account_name);
assert($result['display_name'] == $account_name);
assert($result['email'] == '[email protected]');
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 [email protected] 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 = '[email protected]',
$sms_number = '9162627234', $sms_number = '9162627235', $meta = array('a' => 'b'));
print_r($result);
assert($result['email'] == '[email protected]');
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();

}
}
Loading