• This repository has been archived on 09/Nov/2017
  • Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language
    PHP
  • Created about 16 years ago
  • Updated about 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An OpenID component for CakePHP

OpenID component for CakePHP

Purpose

An OpenID component for CakePHP 2.x. Supports SReg (simple registration extension) and AX (attribute exchange). For CakePHP 1.x, please check out the cakephp_1.x branch.

Installation

  • Copy the file Controller/Component/OpenidComponent.php to the Controller/Component folder of your application or plugin
  • Copy the Vendor/Auth folder to one of your vendors folders (vendors, app/Vendor, or app/Plugin/<pluginname>/Vendor)
  • Add the component to the $components array of your controller(s)
  • On Windows, add define('Auth_OpenID_RAND_SOURCE', null) to app/Config/bootstrap.php to use an insecure random number generator because the default random number generator used (/dev/urandom) is not available on Windows

Using the MySQLStore (optional)

By default, the OpenID component stores all data in app/tmp/openid. To store those data in a MySQL database, please follow these steps:

  • Copy the Vendor/pear folder to one of your vendors folders
  • Run the openid.sql script, available in Config/sql, to create the necessary tables
  • Configure the component to use a database by following one of these two steps:
    • To use the default database configuration defined in app/Config/database.php: public $components = array('Openid' => array('use_database' => true));
    • To use another database configuration: public $components = array('Openid' => array('database_config' => 'name_of_database_config'));

Accepting Google Apps OpenIDs (optional)

By default, the OpenID component doesn't accept Google Apps OpenIDs. The reason it's disabled by default is that it introduces an additional request to Google every time the authentication process is started.

To enable support for Google Apps OpenIDs, use the following config setting: public $components = array('Openid' => array('accept_google_apps' => true));

Example application

There is a very simple example application available to show you how to use the OpenID component. Its source code is available in the openid-component-example repo, and you can see the application in action on http://openid-example.42dh.com.

Example usage

First, we need a login form:

<?php
// app/View/Users/login.ctp
if (isset($error)) {
      echo '<p class="error">'.$error.'</p>';
}
echo $this->Form->create('User', array('type' => 'post', 'action' => 'login'));
echo $this->Form->input('OpenidUrl.openid', array('label' => false));
echo $this->Form->end('Login');
?>

Next, we have to write a controller to handle this form. Our controller has to perform the following tasks: show the login form, redirect the user to the OpenID provider after he submitted the login form, and last, but not least, handle the response from the OpenID provider.

<?php
// app/Controller/UsersController.php
class UsersController extends AppController {
    public $components = array('Openid');
    public $uses = array();

    public function login() {
        $realm = 'http://' . $_SERVER['HTTP_HOST'];
        $returnTo = $realm . '/users/login';

        if ($this->request->isPost() && !$this->Openid->isOpenIDResponse()) {
            try {
                $this->Openid->authenticate($this->data['OpenidUrl']['openid'], $returnTo, $realm);
            } catch (InvalidArgumentException $e) {
                $this->set('error', 'Invalid OpenID');
            } catch (Exception $e) {
                $this->set('error', $e->getMessage());
            }
        } elseif ($this->Openid->isOpenIDResponse()) {
            $response = $this->Openid->getResponse($returnTo);

            if ($response->status == Auth_OpenID_CANCEL) {
                $this->set('error', 'Verification cancelled');
            } elseif ($response->status == Auth_OpenID_FAILURE) {
                $this->set('error', 'OpenID verification failed: '.$response->message);
            } elseif ($response->status == Auth_OpenID_SUCCESS) {
                echo 'successfully authenticated!';
                exit;
            }
        }
    }
}

When testing this example, your OpenID provider might show you a warning that your site couldn't be verified (as far as I know only AOL shows such a warning). To get rid of this warning, please see the article Enabling your application for return URL verification.

Using the Simple Registration Extension (SReg)

The Simple Registration Extension allows you to retrieve nine commonly requested pieces of information: nickname, email, fullname, dob (date of birth), gender, postcode, country, language, and timezone. Please be aware that some OpenID providers (for example, Google) don't support SReg.

<?php
// app/Controller/UsersController.php
class UsersController extends AppController {
    public $components = array('Openid');

    public function login() {
        $realm = 'http://'.$_SERVER['HTTP_HOST'];
        $returnTo = $realm . '/users/login';

        if ($this->request->isPost() && !$this->Openid->isOpenIDResponse()) {
            $this->makeOpenIDRequest($this->data['OpenidUrl']['openid'], $returnTo, $realm);
        } elseif ($this->Openid->isOpenIDResponse()) {
            $this->handleOpenIDResponse($returnTo);
        }
    }

    private function makeOpenIDRequest($openid, $returnTo, $realm) {
        $required = array('email');
        $optional = array('nickname');
        $this->Openid->authenticate($openid, $returnTo, $realm, array('sreg_required' => $required, 'sreg_optional' => $optional));
    }

    private function handleOpenIDResponse($returnTo) {
        $response = $this->Openid->getResponse($returnTo);

        if ($response->status == Auth_OpenID_SUCCESS) {
            $sregResponse = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
            $sregContents = $sregResponse->contents();

            if ($sregContents) {
                if (array_key_exists('email', $sregContents)) {
                    debug($sregContents['email']);
                }
                if (array_key_exists('nickname', $sregContents)) {
                    debug($sregContents['nickname']);
                }
            }
        }
    }
}

Using Attribute Exchange (AX)

Attribute Exchange allows you to retrieve identity information from the OpenID provider, if supported. http://www.axschema.org/types contains a list with possible attribute names, though only a small subset is usually supported by the OpenID providers.

<?php
// app/Controller/UsersController.php
class UsersController extends AppController {
    public $components = array('Openid');

    public function login() {
        $realm = 'http://'.$_SERVER['HTTP_HOST'];
        $returnTo = $realm . '/users/login';

        if ($this->request->isPost() && !$this->Openid->isOpenIDResponse()) {
            $this->makeOpenIDRequest($this->data['OpenidUrl']['openid'], $returnTo, $realm);
        } elseif ($this->Openid->isOpenIDResponse()) {
            $this->handleOpenIDResponse($returnTo);
        }
    }

    private function makeOpenIDRequest($openid, $returnTo, $realm) {
        // some OpenID providers (e.g. MyOpenID) use 'schema.openid.net' instead of 'axschema.org'
        $attributes[] = Auth_OpenID_AX_AttrInfo::make('http://axschema.org/namePerson', 1, true, 'fullname');
        $this->Openid->authenticate($openid, $returnTo, $realm, array('ax' => $attributes));
    }

    private function handleOpenIDResponse($returnTo) {
        $response = $this->Openid->getResponse($returnTo);

        if ($response->status == Auth_OpenID_SUCCESS) {
            $axResponse = Auth_OpenID_AX_FetchResponse::fromSuccessResponse($response);

            if ($axResponse) {
                debug($axResponse->get('http://axschema.org/namePerson'));
                debug($axResponse->getSingle('http://axschema.org/namePerson'));
            }
        }
    }
}

Troubleshooting

If you encounter signature validation errors, it could be because of bugs in the GMP math library. In this case, add the following constant to app/config/bootstrap.php: define('Auth_OpenID_BUGGY_GMP', true);

Contact

Feel free to contact me via Twitter (@dhofstet) or by email ([email protected]) if you have any questions or feedback.

License

The OpenID component is licensed under the MIT license.

More Repositories

1

scss-syntax.vim

Vim syntax file for scss (Sassy CSS)
CSS
387
star
2

oauth-consumer

OAuth consumer for CakePHP realized as vendor class
PHP
74
star
3

oauth-consumer-component

OAuth consumer component for CakePHP
PHP
49
star
4

autocomplete-component

autocomplete-component for CakePHP
PHP
15
star
5

controller-list-component

A simple CakePHP component which returns a list of controllers
PHP
15
star
6

openid-component-example

Example app to show how the openid-component is used
PHP
13
star
7

selenium-helper

Selenium helper for CakePHP
13
star
8

node-url-expander

An URL expander for node.js
JavaScript
8
star
9

jquery-jknavigable

A jQuery plugin that allows you to navigate using the "j" and "k" keys
JavaScript
7
star
10

lessnoise

A web extension to filter Twitter's timeline in Firefox
JavaScript
5
star
11

jquery-disableIfEmpty

A simple jQuery plugin to disable a button if an observed element is empty
JavaScript
5
star
12

bash-completions

Some bash completion scripts
5
star
13

openid-switcher

A simple Sinatra application which allows you to switch the target url of your delegated OpenID
Ruby
3
star
14

site-search

A site search realized with Sinatra and Yahoo! BOSS
Ruby
3
star
15

less-noise

A Twitter client using Node.js
JavaScript
2
star
16

rbrowser

Following "Web Browser Engineering", using Rust to implement it
Rust
2
star
17

dotfiles

My setup, cherry pick what you like
Vim Script
1
star
18

node-fof

A simple CLI tool providing (almost) the same functionality as friendorfollow.com
CoffeeScript
1
star
19

smv

A simple cli tool to selectively move files from one directory to another
Haskell
1
star
20

rlox

Rust implementation of the Lox compiler from the book "Crafting Interpreters"
Rust
1
star