<?php
class OpenidComponent {
private $controller = null ;
public function __construct($controller) {
$ this ->controller = $controller;
define( 'Auth_Yadis_CURL_OVERRIDE' , true );
}
public function authenticate($openidUrl, $returnTo, $realm, $required = array(), $optional = array()) {
if (trim($openidUrl) != '' ) {
if ($ this ->isEmail($openidUrl)) {
$openidUrl = $ this ->transformEmailToOpenID($openidUrl);
}
$consumer = $ this ->getConsumer();
$authRequest = $consumer->begin($openidUrl);
if (!isset($authRequest) || !$authRequest) {
throw new InvalidArgumentException( 'Invalid OpenID' );
}
if ($authRequest->shouldSendRedirect()) {
$redirectUrl = $authRequest->redirectUrl($realm, $returnTo);
if (Auth_OpenID::isFailure($redirectUrl)) {
throw new Exception( 'Could not redirect to server: ' .$redirectUrl->message);
} else {
$ this ->controller->redirect($redirectUrl);
}
} else {
$formId = 'openid_message' ;
$formHtml = $authRequest->formMarkup($realm, $returnTo, false , array( 'id' => $formId));
if (Auth_OpenID::isFailure($formHtml)) {
throw new Exception( 'Could not redirect to server: ' .$formHtml->message);
} else {
return '<html><head><title> OpenId </title></head>' .
"<body onload='document.getElementById(\"" .$formId. "\").submit()'>" .
$formHtml. '</body></html>' ;
}
}
}
}
public function getResponse($currentUrl) {
$consumer = $ this ->getConsumer();
$response = $consumer->complete($currentUrl, $ this ->getQuery());
return $response;
}
private function getConsumer() {
require_once 'Auth/OpenID/Consumer.php' ;
return new Auth_OpenID_Consumer($ this ->getFileStore());
}
private function getQuery() {
$query = Auth_OpenID::getQuery();
// unset the url parameter automatically added by app/webroot/.htaccess
// as it causes problems with the verification of the return_to url
unset($query[ 'url' ]);
return $query;
}
private function isEmail($ string ) {
return strpos($ string , '@' );
}
private function transformEmailToOpenID($email) {
if (include_once 'My/Auth/Yadis/Email.php' ) {
return Auth_Yadis_Email_getID($email);
}
throw new InvalidArgumentException( 'Invalid OpenID' );
}
private function getFileStore() {
require_once 'Auth/OpenID/FileStore.php' ;
$storePath = Zend_Registry::getInstance()->configuration->openidFileStore;
if (!file_exists($storePath) && !mkdir($storePath,0777)) {
throw new Exception( 'Could not create the FileStore directory ' .$storePath. '. Please check the effective permissions.' );
}
return new Auth_OpenID_FileStore($storePath);
}
}
* This source code was highlighted with Source Code Highlighter .
public function openid(){
if ( null === $ this ->_openid) {
require_once APPLICATION_PATH . '/models/openid.php' ;
$ this ->_openid = new OpenidComponent($ this );
}
return $ this ->_openid;
}
* This source code was highlighted with Source Code Highlighter .
public function redirect($url){
$ this ->_redirect($url);
}
* This source code was highlighted with Source Code Highlighter .
public function openidAction(){
error_reporting(E_ERROR);
$auth = Zend_Auth::getInstance();
$flashMessenger = $ this ->_helper->FlashMessenger;
$ this ->_helper->layout->disableLayout();
$ this ->_helper->viewRenderer->setNoRender();
$identifier = trim($ this ->getRequest()->getParam( "openid_identifier" ));
$openidComponent = $ this ->openid();
try {
$ret = $openidComponent->authenticate($identifier,Zend_Registry::getInstance()->configuration->webhost. '/login/openidcallback/' ,Zend_Registry::getInstance()->configuration->webhost, $required = array(), $optional = array());
if ($ret){
echo $ret;
}
} catch (Exception $e){
Zend_Registry::getInstance()->logger->ERR( "openid error:" .$e->getMessage().$e->getTraceAsString());
$flashMessenger->addMessage( " openID!" );
return $ this ->_redirect( '/login/' );
}
}
* This source code was highlighted with Source Code Highlighter .
public function openidcallbackAction(){
$openidComponent = $ this ->openid();
$response = $openidComponent->getResponse(Zend_Registry::getInstance()->configuration->webhost. '/login/openidcallback/' );
$flashMessenger = $ this ->_helper->FlashMessenger;
if ($response->status == Auth_OpenID_CANCEL) {
$flashMessenger->addMessage( ' !' );
return $ this ->_redirect( '/login/' );
} else if ($response->status == Auth_OpenID_FAILURE) {
$flashMessenger->addMessage( " : $response->message !" );
return $ this ->_redirect( '/login/' );
} else if ($response->status == Auth_OpenID_SUCCESS) {
$auth = Zend_Auth::getInstance();
$openid = $response->getDisplayIdentifier();
$model = $ this ->getUserModel();
//look for user, if not found suggest to choose userName on site
$user = $model->findByOpenid($openid);
if ($user){
$auth->getStorage()->write($user);
return $ this ->_redirect( "/" ); //
}
$flashMessenger->addMessage( ' . , !' );
$model = Lookup:: get ()->user();
$newUser = $model->create();
$newUser->save();
$auth->getStorage()->write($newUser);
$openid = $response->getDisplayIdentifier();
$model->addUserOpenidURL($newUser, $openid);
return $ this ->_redirect( '/settings/choosename/' );
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/54734/
All Articles