Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
158 lines (140 sloc) 4.88 KB
<?php
/**
* OneID's API functions
*
* @copyright Copyright 2012 by OneID
*/
class OneID {
/**
*
* The default attributes which the OneID Sign In button would request : user's first name, last name, and email address.
*/
const DEFAULT_ATTRIBUTES_REQUESTED = "email[email] name[first_name] name[last_name]";
/**
*
* @var string $oneid_servers Flag for which OneID environment you are using
*/
public static $oneid_servers;
/**
*
* @var string $oneid_script Javascript tag to be rendered in pages that wish to include the OneID JS API
*/
public static $oneid_script;
/**
*
* @var string
*/
private $oneid_api_id;
/**
*
* @var string
*/
private $oneid_api_key;
/**
*
* @var string $oneid_server URL for OneID helper keychain
*/
private $oneid_server;
/**
* Creates a OneID object for your use
*
* @param string $api_id Your OneID API ID credentials
* @param string $api_key Your OneID API Key credential
* @param string $server Which OneID server environment you want to point to
*
* @return OneID A OneID object
*/
function __construct($api_id=null, $api_key=null, $server = "") {
if ($api_id == null) {
/**
* Load key file
* Requires an api_key.json file in the same directory (add the server name in if applicable)
*/
$oneid_key_file = json_decode(file_get_contents("api_key" . $server . ".json"),true);
$api_id = $oneid_key_file['API_ID'];
$api_key = $oneid_key_file['API_KEY'];
}
$this->oneid_api_id = $api_id;
$this->oneid_api_key = $api_key;
$this->oneid_servers = $server;
$this->oneid_server = "https://keychain" . $server . ".oneid.com";
$this->oneid_script = '<script src="https://api' . $server . '.oneid.com/js/oneid.js" type="text/javascript"></script>';
}
/**
* Runs a curl object on the url page of a valid OneID method
*
* @param string $method name of a valid OneID method
* @param mixed $post Data to post to OneID.
* @return array An associative array of the parsed JSON response from OneID
*/
private function _call_OneID($method, $post = null) {
$ch = curl_init($this->oneid_server . "/" . $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $this->oneid_api_id . ":" . $this->oneid_api_key);
if ($post !== null) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$json = curl_exec($ch);
curl_close($ch);
return json_decode($json, true);
}
/**
* Assignes OneID API ID and Key to local variables
*
* @param string $id API ID credetial
* @param string $key API Key credential
*
* @return void
*/
function set_credentials($id, $key) {
$this->oneid_api_id = $id;
$this->oneid_api_key = $key;
}
/**
* Reads in the PHP input, and calls OneID's validator service.
*
* @return array The incoming data from OneID, as well as any data the validator responded with
*/
function response() {
$response = json_decode(file_get_contents('php://input'), true);
// Build object of what needs to go to validation server.
$validate_data = array(
"nonces" => $response["nonces"],
"attr_claim_tokens" => $response["attr_claim_tokens"],
"uid" => $response["uid"]
);
$validate = $this->_call_OneID("validate", json_encode($validate_data));
if (!$this->success($validate)) {
$validate['failed'] = "failed";
return $validate;
}
return array_merge($response, $validate);
}
/**
* Generates the json structure required to redirect to a new page
*
* @param string $page the url of the web page to redirect to
* @param array $response contains any errors and error codes that occured
* on the OneID request
* @return array
*/
function redirect($page, $response) {
return json_encode(array(
"error" => $response['error'],
"errorcode" => $response['errorcode'],
"url" => $page,
"response" => $response)
);
}
/**
* Checks a web page's error codend returns true if there were no errors
*
* @param array $response The data response from OneID's validator service
*
* @return bool Decides whether or not the OneID signatures validated
*/
function success($response) {
return $response && $response["errorcode"] === 0;
}
}
Something went wrong with that request. Please try again.