Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

102 lines (70 sloc) 3.389 kb

About

Read the doc for the PHP class in OneID.md .

This tutorial uses a small PHP library that implements the relevant calls to the OneID web services.

The demo uses a OneID Helper Service. To use the Helper Service you will also need to download an API Key from https://keychain.oneid.com/register into the same directory as the demo. Name the api key file api_key.json. By downloading the API Key you agree to the OneID Developer Agreement. The API Key gives you access to OneID Helper Services,

The OneID Helper Service is not required, but is used in the example code. Without the Helpers Services, you will need to validate credentials and create nonces on your own.

If you have any questions, contact support@oneid.com.

Prerequisites

PHP5 is required.

apt-get install php5-cli php5-curl

Running the sample code

git clone https://github.com/OneID/oneid-php-sdk.git
cd oneid-php-sdk/examples/oneid_demo/
vi api_key.json   ### Copy&Paste the API key from register

php -S localhost:8000

Now point your Browser to http://localhost:8000

Replace "localhost" with your server's name as needed.

Implementing

STEP 1.

Implementing OneID sign-in is a three step process, one for each page on your server. On the page where you want the sign-in button, you include a script tag and place the login button. The PHP library has helper variables and functions to make this simple:

<?php require("oneid.php"); 
          $oneid = new OneID();
          echo $oneid->oneid_script; // This inserts the script tag 
    ?>

    // This inserts the sign-in button
    <div id="login-button"/>
    <script type="text/javascript">
        OneID.init({
            buttons: {
                "signin #login-button": [{
                    challenge: {
                        "callback": "/validate.php"
                        "attr": "email name address"
                    }
                }]
            }
        });
    </script>

STEP 2.

validate.php is called by the OneID client during the authentication protocol. The code is:

<?php
require("oneid.php");
$oneid = new OneID();

$response = $oneid->response(); 
// This function handles validation of
// the response from OneID and returns an 
// array of attributes for the user.

session_start();

if($oneid->success($response)) {
        // here you do what you need to link the OneID with the current user.
        $_SESSION['email'] = $response['attr']['personal_info']['email'];
        $_SESSION['OneID'] = $response['uid'];
} 
else { 
// if it fails, clear whatever link may have been set previously
        unset($_SESSION['OneID']);
        unset($_SESSION['email']);
        $_SESSION=array_values($_SESSION);
} 

echo $oneid->redirect('account.php', $response);

// $oneid->redirect will return JSON that will send the browser to the specified page and pass any errorcodes from authentication and verification back to the OneID client.

?>

STEP 3.

account.php creates the destination URL where you want the client’s browser to land after signing in successfully. The code is:

<?php session_start(); ?>
Welcome, <?php echo $_SESSION['email']; ?>  your OneID is <?php echo $_SESSION['OneID'];
?>
Jump to Line
Something went wrong with that request. Please try again.