phpPoA2
[ class tree: phpPoA2 ] [ index: phpPoA2 ] [ all elements ]

Source for file PoA.php

Documentation is available at PoA.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2005-2010 RedIRIS, http://www.rediris.es/
  4.  *
  5.  *  This file is part of phpPoA2.
  6.  *
  7.  *  phpPoA2 is free software: you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation, either version 3 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  phpPoA2 is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with phpPoA2. If not, see <http://www.gnu.org/licenses/>.
  19.  *
  20.  * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  21.  * @version 2.0
  22.  * @author Jaime Perez <jaime.perez@rediris.es>
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * @ignore
  28.  */
  29. set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__).
  30.                                     PATH_SEPARATOR.dirname(__FILE__)."/messages".
  31.                                     PATH_SEPARATOR.dirname(__FILE__)."/lib".
  32.                                     PATH_SEPARATOR.dirname(__FILE__)."/lib/db".
  33.                                     PATH_SEPARATOR.dirname(__FILE__)."/lib/authn".
  34.                                     PATH_SEPARATOR.dirname(__FILE__)."/lib/authz");
  35.  
  36. require_once("definitions.php");
  37. require_once("utils.php");
  38. require_once("PoAEventHandler.php");
  39. include_once("AutoPoA.php");
  40. include_once("LitePoA.php");
  41.  
  42. /**
  43.  * Standard class that implements all the functionallity of the phpPoA.
  44.  * @package phpPoA2
  45.  */
  46. class PoA {
  47.     protected $local_site;
  48.     protected $cfg;
  49.     protected $log;
  50.     protected $authn_engine;
  51.     protected $attributes;
  52.     protected $authz_engines;
  53.     protected $db_manager;
  54.     protected $autoload;
  55.     protected $handler;
  56.  
  57.     /**
  58.      * Main constructor. Configures the PoA and performs initialization.
  59.      * @param site The identifier to determine which configuration to apply.
  60.      */
  61.     public function __construct($site{
  62.         $this->local_site = $site;
  63.  
  64.         // manage generic session
  65.         if (!isset($_COOKIE[$site.'_session'])) {
  66.             $id mt_rand();
  67.             @setcookie($site.'_session'$id);
  68.             $_COOKIE[$site.'_session'$id;
  69.         }
  70.  
  71.         $this->handler = new PoAEventHandler($site);
  72.  
  73.         // register autoload function
  74.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  75.  
  76.         // configure
  77.         try {
  78.             $this->cfg = new PoAConfigurator($site);
  79.         catch (Exception $e// unrecoverable!!
  80.             // we have no logging, so do our best here
  81.             // put a message in the error log and in STDOUT and exit
  82.             error_log($e);
  83.             $this->handler->abort(E_USER_ERROR$e);
  84.         }
  85.  
  86.         // initialize logger
  87.         $this->log = new PoALog($this->cfg->getLogLevel()$this->cfg->getLogFile());
  88.  
  89.         // initialize error handling
  90.         $this->handler->setDebug($this->cfg->isDebug());
  91.         $this->handler->setLogger($this->log);
  92.         set_exception_handler(array($this->handler"exceptionHandler"));
  93.         set_error_handler(array($this->handler"errorHandler"));
  94.  
  95.         // load authentication engine
  96.         $engine $this->cfg->getAuthnEngine();
  97.         if (class_exists($engine)) {
  98.             $this->authn_engine = new $engine($this->cfg->getAuthnEngineConfFile()$site);
  99.         }
  100.  
  101.         // load authorization engines
  102.         $engines $this->cfg->getAuthzEngines();
  103.         foreach ($engines as $engine{
  104.             $this->authz_engines[$enginenew $engine($this->cfg->getAuthzEngineConfFile($engine)$site);
  105.         }
  106.  
  107.         $this->clean();
  108.     }
  109.  
  110.     protected function clean({
  111.         // clean
  112.         spl_autoload_unregister(array($this->handler"autoloadHandler"));
  113.         restore_exception_handler();
  114.         restore_error_handler();
  115.     }
  116.  
  117.     /**
  118.      * Attach a hook object to the appropriate entry point of the available
  119.      * authentication or authorization engines.
  120.      * @param name The name of the hook. Refer to each individual engine
  121.      *  for a complete list of available hooks.
  122.      * @param hook A hook object with the function or method to attach.
  123.      * @return true if the hook was successfully attached, false otherwise.
  124.      */
  125.     public function addHook($name$hook{
  126.         // register autoload function
  127.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  128.         set_exception_handler(array($this->handler"exceptionHandler"));
  129.         set_error_handler(array($this->handler"errorHandler"));
  130.  
  131.         // add hook for authentication engine
  132.         $result $this->authn_engine->addHook($name$hook);
  133.  
  134.         // add hook for authorization engines
  135.         foreach ($this->authz_engines as $engine{
  136.             $result |= $engine->addHook($name$hook);
  137.         }
  138.  
  139.         $this->clean();
  140.         return $result;
  141.     }
  142.  
  143.     /**
  144.      * Remove a hook from the specified entry point of the available
  145.      * authentication or authorization engines.
  146.      * @param name The name of the hook. Refer to each individual engine
  147.      *  for a complete list of available hooks.
  148.      * @param hook The hook object which shall be removed.
  149.      * @return true if the hook was successfully removed, false otherwise.
  150.      */
  151.     public function removeHook($name$hook{
  152.         // register autoload function
  153.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  154.         set_exception_handler(array($this->handler"exceptionHandler"));
  155.         set_error_handler(array($this->handler"errorHandler"));
  156.  
  157.         // remove hook from authentication engine
  158.         $result $this->authn_engine->removeHook($name$hook);
  159.  
  160.         // remove hook from authorization engines
  161.         foreach ($this->authz_engines as $engine{
  162.             $result |= $engine->removeHook($name$hook);
  163.         }
  164.  
  165.         $this->clean();
  166.         return $result;
  167.     }
  168.  
  169.     /****************************
  170.      * Authentication interface *
  171.      ****************************/
  172.  
  173.     /**
  174.      * Perform a federated login for the user.
  175.      * @return AUTHN_SUCCESS if authentication succeeds, AUTHN_FAILED in
  176.      *  any other case.
  177.      */
  178.     public function authenticate({
  179.         // register autoload function
  180.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  181.         set_exception_handler(array($this->handler"exceptionHandler"));
  182.         set_error_handler(array($this->handler"errorHandler"));
  183.  
  184.         // check if we have an authentication engine configured
  185.         if (empty($this->authn_engine)) {
  186.             trigger_error(msg('authn-engine-err'array())E_USER_WARNING);
  187.             $this->clean();
  188.             return AUTHN_FAILED;
  189.         }
  190.  
  191.         trigger_error(msg("authenticating-via"array($this->cfg->getAuthnEngine())));
  192.  
  193.         $result false;
  194.         try {
  195.             $result $this->authn_engine->authenticate();
  196.         catch (PoAException $e{
  197.             trigger_error($eE_USER_WARNING);
  198.         }
  199.         if ($result{
  200.             trigger_error(msg('authn-success'array($this->cfg->getAuthnEngine()))E_USER_WARNING);
  201.         else {
  202.             trigger_error(msg('authn-err'array())E_USER_WARNING);
  203.         }
  204.  
  205.         $this->clean();
  206.     return $result;
  207.     }
  208.  
  209.     /**
  210.      * Query the current status of the user in the federation.
  211.      * @return AUTHN_SUCCESS if authentication succeeded, AUTHN_FAILED in
  212.      *  any other case.
  213.      */
  214.     public function isAuthenticated({
  215.         // register autoload function
  216.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  217.         set_exception_handler(array($this->handler"exceptionHandler"));
  218.         set_error_handler(array($this->handler"errorHandler"));
  219.  
  220.         // check if we have an authentication engine configured
  221.         if (empty($this->authn_engine)) {
  222.             trigger_error(msg('authn-engine-err'array())E_USER_WARNING);
  223.             $this->clean();
  224.             return AUTHN_FAILED;
  225.         }
  226.  
  227.         trigger_error(msg("check-authn-status"array($this->cfg->getAuthnEngine())));
  228.  
  229.         $result $this->authn_engine->isAuthenticated();
  230.         if ($result{
  231.             trigger_error(msg('authn-success'array($this->cfg->getAuthnEngine()))E_USER_WARNING);
  232.         else {
  233.             trigger_error(msg('authn-err'array())E_USER_WARNING);
  234.         }
  235.  
  236.         $this->clean();
  237.     return $result;
  238.     }
  239.  
  240.     /**
  241.      * Retrieve the attributes provided by the user when logged in.
  242.      * @return an associative array containing all attributes.
  243.      */
  244.     public function getAttributes({
  245.         // register autoload function
  246.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  247.         set_exception_handler(array($this->handler"exceptionHandler"));
  248.         set_error_handler(array($this->handler"errorHandler"));
  249.  
  250.         // check if we have an authentication engine configured
  251.         if (empty($this->authn_engine)) {
  252.             trigger_error(msg('authn-engine-err'array())E_USER_WARNING);
  253.             $this->clean();
  254.             return array();
  255.         }
  256.  
  257.         $this->clean();
  258.         return $this->authn_engine->getAttributes();
  259.     }
  260.  
  261.     /**
  262.      * Get the value (or values) of an attribute, if present.
  263.      * @param name The name of the attribute.
  264.      * @param namespace The namespace of the attribute, if any.
  265.      * @return the attribute value or an array containing all values.
  266.      *  Null in any other case.
  267.      */
  268.     public function getAttribute($name$namespace{
  269.         // register autoload function
  270.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  271.         set_exception_handler(array($this->handler"exceptionHandler"));
  272.         set_error_handler(array($this->handler"errorHandler"));
  273.  
  274.         // check if we have an authentication engine configured
  275.         if (empty($this->authn_engine)) {
  276.             trigger_error(msg('authn-engine-err'array())E_USER_WARNING);
  277.             $this->clean();
  278.             return null;
  279.         }
  280.  
  281.         $this->clean();
  282.         return $this->authn_engine->getAttribute($name$namespace);
  283.     }
  284.  
  285.     /**
  286.      * Remove the user's session and trigger a logout for the specified authentication
  287.      * protocol.
  288.      * @param slo Whether to perform a Single Log Out or a local logout.
  289.      * @return true if success, false in any other case.
  290.      */
  291.     public function logout($slo{
  292.         // register autoload function
  293.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  294.         set_exception_handler(array($this->handler"exceptionHandler"));
  295.         set_error_handler(array($this->handler"errorHandler"));
  296.  
  297.         // check if we have an authentication engine configured
  298.         if (empty($this->authn_engine)) {
  299.             trigger_error(msg('authn-engine-err'array())E_USER_WARNING);
  300.             $this->clean();
  301.             return AUTHN_FAILED;
  302.         }
  303.  
  304.         $this->clean();
  305.         return $this->authn_engine->logout($slo);
  306.     }
  307.  
  308.     /***************************
  309.      * Authorization interface *
  310.      ***************************/
  311.  
  312.     /**
  313.      * Perform authorization for the a given subject.
  314.      * Multiple authorization engines are supported, so
  315.      * authorization will succeed if any of these succeeds.
  316.      * @param user The subject queried.
  317.      * @param attrs The attributes of the user.
  318.      * @param engine The authorization engine(s) to use. All engines are used if none specified.
  319.      *  If more than one engine should be checked then this must be an array.
  320.      * @return AUTHZ_SUCCESS if any of the supported (or selected) engines succeeds or if no
  321.      *  authorization engine is configured. AUTHZ_FAILED if all the engines fail.
  322.      */
  323.     public function isAuthorized($user$attrs$engine null{
  324.         // register autoload function
  325.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  326.         set_exception_handler(array($this->handler"exceptionHandler"));
  327.         set_error_handler(array($this->handler"errorHandler"));
  328.  
  329.         // bypass if no authorization engine
  330.         if (empty($engine&& empty($this->authz_engines))
  331.             return true;
  332.  
  333.         $result false;
  334.         // check specific engines
  335.         if (!empty($engine)) {
  336.             $engines $engine;
  337.             if (!is_array($engine)) $engines array($engine);
  338.  
  339.             // iterate over engines
  340.             foreach ($engines as $e{
  341.                 if (!isset($this->authz_engines[$e])) {
  342.                     trigger_error(msg("authz-engine-err"array($e))E_USER_ERROR);
  343.                 }
  344.                 trigger_error(msg("query-authz-via"array($e)));
  345.                 $result |= $this->authz_engines[$e]->isAuthorized($user$attrs);
  346.             }
  347.         // check all configured engines
  348.         else {
  349.             trigger_error(msg("query-authz"array()));
  350.             // iterate over engines
  351.             foreach ($this->authz_engines as $e{
  352.                 $result |= $e->isAuthorized($user$attrs);
  353.             }
  354.         }
  355.  
  356.         if ($result{
  357.             trigger_error(msg('user-authz-ok'array($user))E_USER_WARNING);
  358.         else {
  359.             trigger_error(msg('user-authz-err'array($user))E_USER_WARNING);
  360.         }
  361.  
  362.         $this->clean();
  363.         return $result;
  364.     }
  365.  
  366.     /**
  367.      * Authorize a given subject with the data retrieved from federated login.
  368.      * Multiple authorization engines are supported, so
  369.      * authorization will be done in all of them.
  370.      * @param user The subject of authorization.
  371.      * @param attrs The attributes of the user.
  372.      * @param reference An internal reference that may be valuable for the engine, tipically
  373.      *  referring to a previous invitation or similar.
  374.      * @param expires The time (POSIX) when authorization will expire. Use 0 if authorization
  375.      *  should never expire. Defaults to 0.
  376.      * @param engine The authorization engine(s) to use. All engines are used if none specified.
  377.      *  If more than one engine should be checked then this must be an array.
  378.      * @return AUTHZ_SUCCESS if any of the supported engines succeeds or if no
  379.      *  authorization engine is configured. AUTHZ_FAILED if all the engines fail.
  380.      */
  381.     public function authorize($user$attrs$reference null$expires 0$engine null{
  382.         // register autoload function
  383.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  384.         set_exception_handler(array($this->handler"exceptionHandler"));
  385.         set_error_handler(array($this->handler"errorHandler"));
  386.  
  387.         $result false;
  388.         // check specific engines
  389.         if (!empty($engine)) {
  390.             $engines $engine;
  391.             if (!is_array($engine)) $engines array($engine);
  392.  
  393.             // iterate over engines
  394.             foreach ($engines as $e{
  395.                 if (!isset($this->authz_engines[$e])) {
  396.                     trigger_error(msg("authz-engine-err"array($e))E_USER_ERROR);
  397.                 }
  398.                 trigger_error(msg("authorize-user-via"array($e)));
  399.                 $result |= $this->authz_engines[$e]->authorize($user$attrs$reference$expires);
  400.             }
  401.         // check all configured engines
  402.         else {
  403.             // iterate over engines
  404.             foreach ($this->authz_engines as $name => $e{
  405.                 trigger_error(msg("authorize-user-via"array($user$name)));
  406.                 $result |= $e->authorize($user$attrs$reference$expires);
  407.             }
  408.         }
  409.  
  410.         $this->clean();
  411.         return $result;
  412.     }
  413.  
  414.     /**
  415.      * Revoke authorization for a given subject identified by an e-mail.
  416.      * @param mail The e-mail of the user.
  417.      * @param engine The authorization engine(s) to use. All engines are used if none specified.
  418.      *  If more than one engine should be checked then this must be an array.
  419.      * @return true if authorization is revoked correctly for all authorization
  420.      *  engines, false in any other case.
  421.      */
  422.     public function revoke($mail$engine null{
  423.         // register autoload function
  424.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  425.         set_exception_handler(array($this->handler"exceptionHandler"));
  426.         set_error_handler(array($this->handler"errorHandler"));
  427.  
  428.         $result false;
  429.         // check specific engines
  430.         if (!empty($engine)) {
  431.             $engines $engine;
  432.             if (!is_array($engine)) $engines array($engine);
  433.  
  434.             // iterate over engines
  435.             foreach ($engines as $e{
  436.                 if (!isset($this->authz_engines[$e])) {
  437.                     trigger_error(msg("authz-engine-err"array($e))E_USER_ERROR);
  438.                 }
  439.                 trigger_error(msg("revoke-user-via"array($e)));
  440.                 $result |= $this->authz_engines[$e]->revoke($user);
  441.             }
  442.         // check all configured engines
  443.         else {
  444.             trigger_error(msg("revoke"array()));
  445.             // iterate over engines
  446.             foreach ($this->authz_engines as $e{
  447.                 $result |= $e->revoke($user);
  448.             }
  449.         }
  450.  
  451.         $this->clean();
  452.         return $result;
  453.     }
  454.  
  455.     /**
  456.      * Returns the authorization engines configured for the current PoA, or
  457.      * the one specified.
  458.      * @param engine The name of the authorization engine to retrieve.
  459.      *  If more than one engine should be returned then this must be an array.
  460.      * @return The authorization engine(s) requested if it was previously configured.
  461.      *  If none was specified, all configured engines will be returned. An empty
  462.      *  array will be returned if no authorization engines were found.
  463.      */
  464.     public function getAuthorizationEngines($engine null{
  465.         // register autoload function
  466.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  467.         set_exception_handler(array($this->handler"exceptionHandler"));
  468.         set_error_handler(array($this->handler"errorHandler"));
  469.  
  470.         $list $this->authz_engines;
  471.         // check specific engines
  472.         if (!empty($engine)) {
  473.             $list array();
  474.             $engines $engine;
  475.             if (!is_array($engine)) $engines array($engine);
  476.  
  477.             // iterate over engines
  478.             foreach ($engines as $e{
  479.                 if (!isset($this->authz_engines[$e])) {
  480.                     trigger_error(msg("authz-engine-err"array($e))E_USER_ERROR);
  481.                 }
  482.                 $list[$e$this->authz_engines[$e];
  483.             }
  484.         }
  485.  
  486.         $this->clean();
  487.         return $list;
  488.     }
  489.  
  490. }
  491.  
  492. ?>

Documentation generated on Thu, 26 Aug 2010 13:38:55 +0200 by phpDocumentor 1.4.3