saeven.net consulting inc.

Implementing OpenID using PHP5 and MySQL 4.1+ on MyISAM

Like yourself, we wanted to implement OpenID into one of our products/sites using PHP5 and MySQL, were unfortunately restricted to a MyISAM table type (can't create the keysize required by the InnoDB-formatted table that JanRain's OpenID class requires) and did not want to install PEAR::DB to avoid unnecessary abstraction. This webpage provides you with an alternative implementation atop JanRain's OpenID class, using a custom store that we've devised, and a service class that takes out most of the guess work. The class and intellectual property in what follows is provided under the GPL V3.

1. Preparing your Environment

a. Download the JanRain OpenID 2.0.0 (we'll call this JROID) from http://openidenabled.com/

b. Install the JROID classes by extracting the package, and copying its contents into your PHP installation's PEAR folder. Do ensure that the PEAR folder is in your open_basedir if used, and include_path (php.ini settings).

 

2. Creating the necessary MySQL Tables

In your project/site's database, create the following two tables:

CREATE TABLE `openid_associations` (
`server_url` varchar(64) NOT NULL,
`handle` varchar(255) NOT NULL,
`secret` blob NOT NULL,
`issued` int(10) unsigned NOT NULL,
`lifetime` int(10) unsigned NOT NULL,
`assoc_type` varchar(64) NOT NULL,
PRIMARY KEY (`server_url`,`handle`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='OpenID Server Associations'

CREATE TABLE `openid_nonces` (
`server_url` varchar(255) NOT NULL,
`issued` int(10) unsigned NOT NULL,
`salt` char(40) NOT NULL,
PRIMARY KEY (`server_url`,`issued`,`salt`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Nonce table for OpenID authentication';

 

3. Download and Use our JROID Store

a. Download the store and service class from here.

b. You can then use the package as follows on the code side, adjusting inclusion paths as required:

    function getData( $str ){
		if( isset( $_REQUEST[$str] ) )
        	return $_REQUEST[$str];
        
        return "";
    }

    include( "/path/to/OpenIDStore.php" );
    include( "/path/to/OpenIDService.php" );
    
    // connect to your MySQL server, 
    //  and select the appropriate database, define this as DB
    //   ... connection code here ...
    define( 'DB', $mysql_connection );

    // define the callback domain
    define( 'DOMAIN', 'http://yourcallbackdomain.com' );
// OpenID Login request if( getData('event') == "openid_login" && ($oid = getData('openid_login')) ){ try{ $sstr = OpenIDService::performOpenIDLogin( $oid ); } catch( Exception $x ){ $sstr = $x->getMessage(); } } // OpenID postback confirmation else if( getData( 'janrain_nonce' ) ){ $sreg = OpenIDService::confirmOpenIDLogin(); var_dump( $sreg ); }

Note that you'll necessarily have to build the required forms that will post the data to pages that contain the service calls in the code above. In the sample above, the code assumes an initial post that contains two GP vars:

openid_login : the user input OpenID (text field)
event: openid_login (hidden field)

We hope that this package helps you! If you have trouble with the implementation of OpenID on your site, feel free to contact us through http://crm.saeven.net/contact.php. Welcome to OpenID!

 

(c) 2008, saeven.net consulting inc.