CommunityID/modules/default/controllers/OpenidController.php

366 lines
13 KiB
PHP
Raw Normal View History

2019-07-17 20:08:50 +00:00
<?php
/*
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
* @license http://creativecommons.org/licenses/BSD/ BSD License
* @author Keyboard Monkey Ltd
* @since CommunityID 0.9
* @package CommunityID
* @packager Keyboard Monkeys
*/
2019-07-17 20:16:19 +00:00
class OpenidController extends CommunityID_Controller_Action
2019-07-17 20:08:50 +00:00
{
2019-07-17 20:16:19 +00:00
protected $_numCols = 1;
2019-07-17 20:08:50 +00:00
public function providerAction()
{
2019-07-17 20:16:19 +00:00
$server = $this->_getOpenIdProvider();
$request = $server->decodeRequest();
$sites = new Model_Sites();
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
if (!$request) {
2019-07-17 20:08:50 +00:00
$this->_helper->viewRenderer->setNeverRender(true);
2019-07-17 20:16:19 +00:00
header('HTTP/1.0 403 Forbidden');
Zend_Registry::get('logger')->log("OpenIdController::providerAction: FORBIDDEN", Zend_Log::DEBUG);
echo 'Forbidden';
return;
}
// association and other transactions, handled automatically by the framework
if (!in_array($request->mode, array('checkid_immediate', 'checkid_setup'))) {
return $this->_sendResponse($server, $server->handleRequest($request));
}
// can't process immediate requests if user is not logged in
if ($request->immediate && $this->user->role == Users_Model_User::ROLE_GUEST) {
return $this->_sendResponse($server, $request->answer(false));
}
if ($request->idSelect()) {
if ($this->user->role == Users_Model_User::ROLE_GUEST) {
$this->_forward('login');
} else {
if ($sites->isTrusted($this->user, $request->trust_root)) {
$this->_forward('proceed', null, null, array('allow' => true));
} elseif ($sites->isNeverTrusted($this->user, $request->trust_root)) {
$this->_forward('proceed', null, null, array('allow' => false));
} else {
if ($request->immediate) {
return $this->_sendResponse($server, $request->answer(false));
}
$this->_forward('trust');
}
}
} else {
if (!$request->identity) {
die('No identifier sent by OpenID relay');
}
if ($this->user->role == Users_Model_User::ROLE_GUEST) {
$this->_forward('login');
} else {
// user is logged-in already. Check the requested identity is his
if ($this->user->openid != $request->identity) {
Zend_Auth::getInstance()->clearIdentity();
if ($this->immediate) {
return $this->_sendResponse($server, $request->answer(false));
}
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
$this->_forward('login');
} else {
if ($sites->isTrusted($this->user, $request->trust_root)) {
$this->_forward('proceed', null, null, array('allow' => true));
} elseif ($sites->isNeverTrusted($this->user, $request->trust_root)) {
$this->_forward('proceed', null, null, array('deny' => true));
2019-07-17 20:08:50 +00:00
} else {
2019-07-17 20:16:19 +00:00
$this->_forward('trust');
2019-07-17 20:08:50 +00:00
}
}
}
}
}
2019-07-17 20:16:19 +00:00
/**
* We don't use the session with the login form to simplify the dynamic appearance of the captcha
*/
2019-07-17 20:08:50 +00:00
public function loginAction()
{
2019-07-17 20:16:19 +00:00
$server = $this->_getOpenIdProvider();
$request = $server->decodeRequest();
$authAttempts = new Users_Model_AuthAttempts();
$attempt = $authAttempts->get();
$this->view->useCaptcha = $attempt && $attempt->surpassedMaxAllowed();
$this->view->form = new Form_OpenidLogin(null, $this->view->base, $attempt && $attempt->surpassedMaxAllowed());
if (!$request->idSelect()) {
$this->view->form->openIdIdentity->setValue(htmlspecialchars($request->identity));
2019-07-17 20:08:50 +00:00
}
2019-07-17 20:16:19 +00:00
$this->view->queryString = $this->_queryString();
2019-07-17 20:08:50 +00:00
}
public function authenticateAction()
{
2019-07-17 20:16:19 +00:00
$server = $this->_getOpenIdProvider();
$request = $server->decodeRequest();
$authAttempts = new Users_Model_AuthAttempts();
$attempt = $authAttempts->get();
$form = new Form_OpenidLogin(null, $this->view->base, $attempt && $attempt->surpassedMaxAllowed());
2019-07-17 20:08:50 +00:00
$formData = $this->_request->getPost();
$form->populate($formData);
if (!$form->isValid($formData)) {
2019-07-17 20:16:19 +00:00
$this->_forward('login');
return;
2019-07-17 20:08:50 +00:00
}
2019-07-17 20:16:19 +00:00
$users = new Users_Model_Users();
$result = $users->authenticate($form->getValue('openIdIdentity'),
$form->getValue('password'), true);
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
if ($result) {
if ($attempt) {
$attempt->delete();
}
$sites = new Model_Sites();
if ($sites->isTrusted($users->getUser(), $request->trust_root)) {
$this->_forward('proceed', null, null, array('allow' => true));
} elseif ($sites->isNeverTrusted($users->getUser(), $request->trust_root)) {
$this->_forward('proceed', null, null, array('deny' => true));
} else {
$this->_forward('trust');
}
} else {
if (!$attempt) {
$authAttempts->create();
} else {
$attempt->addFailure();
$attempt->save();
}
$this->_forward('login');
}
2019-07-17 20:08:50 +00:00
}
public function trustAction()
{
$server = $this->_getOpenIdProvider();
2019-07-17 20:16:19 +00:00
$request = $server->decodeRequest();
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
$this->view->siteRoot = $request->trust_root;
$this->view->identityUrl = $this->user->openid;
$this->view->queryString = $this->_queryString();
2019-07-17 20:08:50 +00:00
$this->view->fields = array();
$this->view->policyUrl = false;
2019-07-17 20:16:19 +00:00
// The class Auth_OpenID_SRegRequest is included in the following file
require 'libs/Auth/OpenID/SReg.php';
$sregRequest = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
$props = $sregRequest->allRequestedFields();
$args = $sregRequest->getExtensionArgs();
if (isset($args['required'])) {
$required = explode(',', $args['required']);
} else {
$required = false;
}
2019-07-17 20:08:50 +00:00
if (is_array($props) && count($props) > 0) {
2019-07-17 20:16:19 +00:00
$sregProps = array();
foreach ($props as $field) {
$sregProps[$field] = $required && in_array($field, $required);
}
$personalInfoForm = new Users_Form_PersonalInfo(null, $this->user, $sregProps);
2019-07-17 20:08:50 +00:00
$this->view->fields = $personalInfoForm->getElements();
2019-07-17 20:16:19 +00:00
if (isset($args['policy_url'])) {
$this->view->policyUrl = $args['policy_url'];
2019-07-17 20:08:50 +00:00
}
}
}
2019-07-17 20:16:19 +00:00
public function proceedAction()
2019-07-17 20:08:50 +00:00
{
// needed for unit tests
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNeverRender(true);
$server = $this->_getOpenIdProvider();
2019-07-17 20:16:19 +00:00
$request = $server->decodeRequest();
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
if ($request->idSelect()) {
$id = $this->user->openid;
} else {
$id = null;
}
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
$response = $request->answer(true, null, $id);
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
// The class Auth_OpenID_SRegRequest is included in the following file
require 'libs/Auth/OpenID/SReg.php';
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
$sregRequest = Auth_OpenID_SRegRequest::fromOpenIDRequest($request);
$props = $sregRequest->allRequestedFields();
$args = $sregRequest->getExtensionArgs();
if (isset($args['required'])) {
$required = explode(',', $args['required']);
} else {
$required = false;
}
if (is_array($props) && count($props) > 0) {
$sregProps = array();
foreach ($props as $field) {
$sregProps[$field] = $required && in_array($field, $required);
2019-07-17 20:08:50 +00:00
}
2019-07-17 20:16:19 +00:00
$personalInfoForm = new Users_Form_PersonalInfo(null, $this->user, $sregProps);
$formData = $this->_request->getPost();
$personalInfoForm->populate($formData);
// not planning on validating stuff here yet, but I call this
// for the date element to be filled properly
$personalInfoForm->isValid($formData);
$sregResponse = Auth_OpenID_SRegResponse::extractResponse($sregRequest,
$personalInfoForm->getUnqualifiedValues());
$sregResponse->toMessage($response->fields);
}
if ($this->_getParam('allow')) {
if ($this->_getParam('forever')) {
$sites = new Model_Sites();
$sites->deleteForUserSite($this->user, $request->trust_root);
$siteObj = $sites->createRow();
$siteObj->user_id = $this->user->id;
$siteObj->site = $request->trust_root;
$siteObj->creation_date = date('Y-m-d');
if (isset($personalInfoForm)) {
$trusted = array();
// using this key name for BC pre 1.1 when we used Zend_OpenId
$trusted['Zend_OpenId_Extension_Sreg'] = $personalInfoForm->getUnqualifiedValues();
} else {
$trusted = true;
}
$siteObj->trusted = serialize($trusted);
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
$siteObj->save();
2019-07-17 20:08:50 +00:00
}
2019-07-17 20:16:19 +00:00
$this->_saveHistory($request->trust_root, Model_History::AUTHORIZED);
2019-07-17 20:08:50 +00:00
2019-07-17 20:16:19 +00:00
$webresponse = $server->encodeResponse($response);
foreach ($webresponse->headers as $k => $v) {
header("$k: $v");
}
header('Connection: close');
echo $webresponse->body;
} elseif ($this->_getParam('deny')) {
if ($this->_getParam('forever')) {
$sites = new Model_Sites();
$sites->deleteForUserSite($this->user, $request->trust_root);
$siteObj = $sites->createRow();
$siteObj->user_id = $this->user->id;
$siteObj->site = $request->trust_root;
$siteObj->creation_date = date('Y-m-d');
$siteObj->trusted = serialize(false);
$siteObj->save();
}
$this->_saveHistory($request->trust_root, Model_History::DENIED);
header('HTTP/1.1 302 Found');
header('Content-Type: text/plain; charset=us-ascii');
header('Connection: close');
header('Location: ' . $request->getCancelUrl());
2019-07-17 20:08:50 +00:00
}
}
2019-07-17 20:10:53 +00:00
2019-07-17 20:16:19 +00:00
private function _saveHistory($site, $result)
{
$histories = new Model_Histories();
2019-07-17 20:08:50 +00:00
$history = $histories->createRow();
$history->user_id = $this->user->id;
$history->date = date('Y-m-d H:i:s');
2019-07-17 20:16:19 +00:00
$history->site = $site;
2019-07-17 20:08:50 +00:00
$history->ip = $_SERVER['REMOTE_ADDR'];
$history->result = $result;
$history->save();
}
private function _getOpenIdProvider()
{
2019-07-17 20:16:19 +00:00
$connection = new CommunityID_OpenId_DatabaseConnection(Zend_Registry::get('db'));
$store = new Auth_OpenID_MySQLStore($connection, 'associations', 'nonces');
$server = new Auth_OpenID_Server($store, $this->_helper->ProviderUrl($this->_config));
2019-07-17 20:08:50 +00:00
return $server;
}
2019-07-17 20:16:19 +00:00
private function _sendResponse(Auth_OpenID_Server $server, Auth_OpenID_ServerResponse $response)
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNeverRender(true);
$webresponse = $server->encodeResponse($response);
if ($webresponse->code != AUTH_OPENID_HTTP_OK) {
header(sprintf("HTTP/1.1 %d ", $webresponse->code), true, $webresponse->code);
}
foreach ($webresponse->headers as $k => $v) {
header("$k: $v");
}
header('Connection: close');
echo $webresponse->body;
}
/**
* Circumvent PHP's automatic replacement of dots by underscore in var names in $_GET and $_POST
*/
private function _queryString()
{
$unfilteredVars = array_merge($_GET, $_POST);
$varsTemp = array();
$vars = array();
$extensions = array();
foreach ($unfilteredVars as $key => $value) {
if (substr($key, 0, 10) == 'openid_ns_') {
$extensions[] = substr($key, 10);
$varsTemp[str_replace('openid_ns_', 'openid.ns.', $key)] = $value;
} else {
$varsTemp[str_replace('openid_', 'openid.', $key)] = $value;
}
}
foreach ($extensions as $extension) {
foreach ($varsTemp as $key => $value) {
if (strpos($key, "openid.$extension") === 0) {
$prefix = "openid.$extension.";
$key = $prefix . substr($key, strlen($prefix));
}
$vars[$key] = $value;
}
}
if (!$extensions) {
$vars = $varsTemp;
}
return '?' . http_build_query($vars);
}
2019-07-17 20:08:50 +00:00
}