Using the OpenID Simple Registration Extension

Published on and tagged with cakephp  openid

A common use case in an application is the registration. The user has to provide a username and a password, and often additional information, too.

If you are using OpenID, the registration process can be simplified quite a bit for the user. Thanks to the OpenID Simple Registration Extension (or short “sreg”) your application can get some common data directly from the OpenID provider. In particular you can get the following data (that is if the user specified them and allows you to access them):

  • nickname
  • email
  • fullname
  • dob (= date of birth)
  • gender
  • postcode
  • country
  • language
  • timezone

With the OpenID component it is quite easy to use Simple Registration. The authenticate() method provides the two parameters “required” and “optional” for this purpose. They allow you to specify the fields which either are required for the registration process or optional. In the following example the “email” field is required, whereas “dob” and “fullname” are optional:

$this->Openid->authenticate($this->data['OpenidUrl']['openid'], 'http://'.$_SERVER['SERVER_NAME'].'/users/login', 'http://'.$_SERVER['SERVER_NAME'], array('email'), array('dob', 'fullname'));

Please be aware that “required” doesn’t mean it’s guaranteed you will get a value for the specified fields from the OpenID provider. It simply means those fields are required for the registration process, and if they are not in the response from the OpenID provider, you have to be prepared to deal with such a situation, i.e. you have to ask the user to manually enter the required data.

Here a simple example of how to extract the Simple Registration data from the OpenID response:

...
} elseif ($response->status == Auth_OpenID_SUCCESS) {
    $sregResponse = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
    $sreg = $sregResponse->contents();
    				
    if (@$sreg['email']) {
        echo 'The email address is: '.$sreg['email'];
    }
...

Happy baking :)

5 comments baked

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License