Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
207 lines (153 sloc) 5.49 KB
<?php
/*
* Copyright 2012 by OneID
*/
// core service and javascript resources
// set your servers to swap from production to alternative deployments
$oneid_servers = ""; // use production
$oneid_server = "https://keychain".$oneid_servers.".oneid.com";
$oneid_script = '<script src="https://api'.$oneid_servers.'.oneid.com/js/includeexternal.js" type="text/javascript"></script>';
$oneid_form_script = '<script src="https://api'.$oneid_servers.'.oneid.com/form/form.js" type="text/javascript"></script>';
// Set your values here
$oneid_referral_code = "yyy";
// Load key file
$key = json_decode(file_get_contents("api_key" . $oneid_servers . ".json"),true);
$oneid_api_id = $key['API_ID'];
$oneid_api_key = $key['API_KEY'];
function _call_OneID($method, $post = null) {
global $oneid_server, $oneid_api_id, $oneid_api_key;
//$scope = "/repo";
$scope = "";
$ch = curl_init($oneid_server . $scope. "/" . $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $oneid_api_id . ":" . $oneid_api_key);
if ($oneid_servers == "-local"){
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
}
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);
}
function _dump_call_OneID($method, $post = null) {
global $oneid_server, $oneid_api_id, $oneid_api_key;
//$scope = "/repo";
$scope = "";
$ch = curl_init($oneid_server . $scope. "/" . $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $oneid_api_id . ":" . $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;
}
function OneID_SetCredentials($id, $key) {
global $oneid_api_id, $oneid_api_key;
$oneid_api_id = $id;
$oneid_api_key = $key;
}
function OneID_MakeNonce() {
$arr = _call_OneID("make_nonce");
return $arr['nonce'];
//How to make a nonce locally.
/*$time = time();
$nonce = gmstrftime('%Y-%m-%dT%H:%M:%SZ', $time);
$length = 6;
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$chars .= 'abcdefghijklmnopqrstuvwxyz';
$chars .= '1234567890';
$unique = '';
for ($i = 0; $i < $length; $i++) {
$unique .= substr($chars, (rand() % (strlen($chars))), 1);
}
return $nonce . $unique;
*/
}
function OneID_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 = _call_OneID("validate", json_encode($validate_data));
if (!OneID_IsSuccess($validate)) {
$validate['failed']="failed";
return $validate;
}
return array_merge($response, $validate);
}
function OneID_LoginChallenge($callback_url,$attr=""){
if (!$attr){
$attr = "personal_info[email] personal_info[first_name] personal_info[last_name]";
}
return array(
"attr" => $attr, // OneID attributes to create an account here
"callback" => $callback_url
);
}
function OneID_Button($callback_url, $attr="") {
/*
* You could also put this javascript in your front end templates yourself.
*
*/
$params = json_encode(
array(
"challenge" => OneID_LoginChallenge($callback_url, $attr )
)
);
$js = "<span class='oneid_login_ctr'></span>";
$js.= "<script type='text/javascript'>";
$js.= "OneIdExtern.registerApiReadyFunction(function(){";
$js.= "OneId.loginButton('.oneid_login_ctr'," . $params .")";
$js.= "});";
$js.="</script>";
return $js;
// return '<img class="oneidlogin" id="oneidlogin" data-challenge=\'{"nonce":"' . OneID_MakeNonce() . '","attr":"personal_info[email]","callback":"'. $callback . '"}\' src="https://api'.$oneid_servers.'.oneid.com/images/oneid_signin.png" onclick="OneId.login()">';
}
function OneID_FormFill($attrs) {
/*
* You could also put this javascript in your front end templates yourself.
*
*/
$js = "<span class='oneid_quickfill_ctr'></span>";
$js.= "<script type='text/javascript'>";
$js.= "OneIdExtern.registerApiReadyFunction(function(){";
$js.= "OneId.accuFillButton('.oneid_quickfill_ctr'," . $attrs .")";
$js.= "});";
$js.="</script>";
return $js;
}
function OneID_Redirect($page,$response) {
return ('{"error":"'
. $response['error']
. '","errorcode":"'
. $response['errorcode']
. '","url":"'
. $page
. '","response":'
. json_encode($response)
. '}');
}
function OneID_Provision($attrs) {
global $oneid_referral_code;
$user_data = json_encode($attrs);
$js = "<div class='oneid_create_ctr'></div>";
$js.= "<script type='text/javascript'>";
$js.= "OneIdExtern.registerApiReadyFunction(function(){";
$js.= "OneId.createOneIdButton('.oneid_create_ctr'," . $user_data .")";
$js.= "});";
$js.="</script>";
return $js;
}
function OneID_IsSuccess($response) {
return $response && $response["errorcode"] === 0;
}