oxopenid.php

Go to the documentation of this file.
00001 <?php
00002 
00003 $iOldErrorReproting = error_reporting();
00004 error_reporting($iOldErrorReproting & ~E_STRICT);
00005 
00006 $sPathExtra = getShopBasePath()."core/openid";
00007 $sPath = ini_get('include_path');
00008 $sPath = $sPath . PATH_SEPARATOR . $sPathExtra;
00009 ini_set('include_path', $sPath);
00010 
00011 if ( !defined('Auth_OpenID_RAND_SOURCE') ) {
00012     if ( $sRandSource = oxConfig::getInstance()->getConfigParam( 'sAuthOpenIdRandSource' ) ) {
00013         define( 'Auth_OpenID_RAND_SOURCE', $sRandSource );
00014     } elseif ( PHP_OS == 'WINNT' || ( @fopen( '/dev/urandom', 'r' ) === false ) ) {
00018         define( 'Auth_OpenID_RAND_SOURCE', null );
00019     }
00020 }
00021 
00022 require_once "openid/Auth/OpenID/Consumer.php";
00023 require_once "openid/Auth/OpenID/FileStore.php";
00024 require_once "openid/Auth/OpenID/SReg.php";
00025 
00026 error_reporting($iOldErrorReproting);
00032 class oxOpenId extends oxBase
00033 {
00042     public function authenticateOid( $sOpenId, $sReturnUrl )
00043     {
00044         $myConfig = $this->getConfig();
00045         // create OpenID consumer
00046         $oConsumer = $this->_getConsumer();
00047 
00048         // begin sign-in process
00049         // creates an authentication request to the OpenID provider
00050         $oAuth = $oConsumer->begin($sOpenId);
00051         if ( !$oAuth ) {
00052             //not valid OpenId
00053             $oEx = oxNew( 'oxUserException' );
00054             $oEx->setMessage( 'EXCEPTION_USER_NOVALIDOPENID' );
00055             throw $oEx;
00056         }
00057 
00058         // create request for registration data
00059         $oAuth->addExtension( Auth_OpenID_SRegRequest::build( array( 'email', 'fullname', 'gender', 'country' ), array( 'postcode' ) ) );
00060 
00061         // redirect to OpenID provider for authentication
00062         $sUrl = $oAuth->redirectURL( $myConfig->getSslShopUrl(), $sReturnUrl);
00063         oxUtils::getInstance()->redirect( $sUrl, false );
00064 
00065     }
00066 
00074     public function getOidResponse( $sReturnUrl )
00075     {
00076         // create OpenID consumer
00077         $oConsumer = $this->_getConsumer();
00078         $oResponse = $oConsumer->complete( $sReturnUrl );
00079 
00080         // authentication results
00081         if ( $oResponse->status == Auth_OpenID_SUCCESS ) {
00082             // get registration information
00083             $oSRreg = $this->_getResponse();
00084             $oRet   = $oSRreg->fromSuccessResponse( $oResponse );
00085             $aData  = $oRet->contents();
00086         } elseif ( $oResponse->status == Auth_OpenID_CANCEL ) {
00087             //Verification Cancelled by user
00088             $oEx = oxNew( 'oxUserException' );
00089             $oEx->setMessage( 'EXCEPTION_USER_OPENIDCANCELED' );
00090             throw $oEx;
00091         } else {
00092             //OpenID authentication failed
00093             $oEx = oxNew( 'oxUserException' );
00094             $oLang = oxLang::getInstance();
00095             $oEx->setMessage( sprintf($oLang->translateString( 'EXCEPTION_USER_OPENIDVALIDFAILED', $oLang->getBaseLanguage() ), $oResponse->message) );
00096             throw $oEx;
00097         }
00098         return $aData;
00099     }
00100 
00106     protected function _getConsumer()
00107     {
00108         $oConsumer = new Auth_OpenID_Consumer( $this->_getStore(), oxOpenIdSession::getInstance(), 'oxOpenIdGenericConsumer' );
00109 
00110         return $oConsumer;
00111     }
00112 
00118     protected function _getResponse()
00119     {
00120         $oSRreg = new Auth_OpenID_SRegResponse();
00121 
00122         return $oSRreg;
00123     }
00124 
00131     protected function _getStore()
00132     {
00133         // create file storage area for OpenID data
00134         // $oStore = new Auth_OpenID_FileStore( oxConfig::getInstance()->getConfigParam( 'sCompileDir' ) );
00135         // create db storage area for OpenID data
00136         $oStore = new oxOpenIdDb();
00137         $oStore->createTables();
00138         return $oStore;
00139     }
00140 }