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.             $this->authn_engine->setHandler($this->handler);
  100.         }
  101.  
  102.         // load authorization engines
  103.         $engines $this->cfg->getAuthzEngines();
  104.         foreach ($engines as $engine{
  105.             $this->authz_engines[$enginenew $engine($this->cfg->getAuthzEngineConfFile($engine)$site);
  106.             $this->authz_engines[$engine]->setHandler($this->handler);
  107.         }
  108.  
  109.         $this->clean();
  110.     }
  111.  
  112.     protected function clean({
  113.         // clean
  114.         restore_exception_handler();
  115.         restore_error_handler();
  116.     }
  117.  
  118.     /**
  119.      * Attach a hook object to the appropriate entry point of the available
  120.      * authentication or authorization engines.
  121.      * @param name The name of the hook. Refer to each individual engine
  122.      *  for a complete list of available hooks.
  123.      * @param hook A hook object with the function or method to attach.
  124.      * @return true if the hook was successfully attached, false otherwise.
  125.      */
  126.     public function addHook($name$hook{
  127.         // register autoload function
  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.         set_exception_handler(array($this->handler"exceptionHandler"));
  154.         set_error_handler(array($this->handler"errorHandler"));
  155.  
  156.         // remove hook from authentication engine
  157.         $result $this->authn_engine->removeHook($name$hook);
  158.  
  159.         // remove hook from authorization engines
  160.         foreach ($this->authz_engines as $engine{
  161.             $result |= $engine->removeHook($name$hook);
  162.         }
  163.  
  164.         $this->clean();
  165.         return $result;
  166.     }
  167.  
  168.     /****************************
  169.      * Authentication interface *
  170.      ****************************/
  171.  
  172.     /**
  173.      * Perform a federated login for the user.
  174.      * @return AUTHN_SUCCESS if authentication succeeds, AUTHN_FAILED in
  175.      *  any other case.
  176.      */
  177.     public function authenticate({
  178.         // register autoload function
  179.         set_exception_handler(array($this->handler"exceptionHandler"));
  180.         set_error_handler(array($this->handler"errorHandler"));
  181.  
  182.         // check if we have an authentication engine configured
  183.         if (empty($this->authn_engine)) {
  184.             trigger_error(msg('authn-engine-err'array())E_USER_WARNING);
  185.             $this->clean();
  186.             return AUTHN_FAILED;
  187.         }
  188.  
  189.         trigger_error(msg("authenticating-via"array($this->cfg->getAuthnEngine())));
  190.  
  191.         $result false;
  192.         try {
  193.             $result $this->authn_engine->authenticate();
  194.         catch (PoAException $e{
  195.             trigger_error($eE_USER_WARNING);
  196.         }
  197.         if ($result{
  198.             trigger_error(msg('authn-success'array($this->cfg->getAuthnEngine()))E_USER_WARNING);
  199.         else {
  200.             trigger_error(msg('authn-err'array())E_USER_WARNING);
  201.         }
  202.  
  203.         $this->clean();
  204.     return $result;
  205.     }
  206.  
  207.     /**
  208.      * Query the current status of the user in the federation.
  209.      * @return AUTHN_SUCCESS if authentication succeeded, AUTHN_FAILED in
  210.      *  any other case.
  211.      */
  212.     public function isAuthenticated({
  213.         // register autoload function
  214.         set_exception_handler(array($this->handler"exceptionHandler"));
  215.         set_error_handler(array($this->handler"errorHandler"));
  216.  
  217.         // check if we have an authentication engine configured
  218.         if (empty($this->authn_engine)) {
  219.             trigger_error(msg('authn-engine-err'array())E_USER_WARNING);
  220.             $this->clean();
  221.             return AUTHN_FAILED;
  222.         }
  223.  
  224.         trigger_error(msg("check-authn-status"array($this->cfg->getAuthnEngine())));
  225.  
  226.         $result $this->authn_engine->isAuthenticated();
  227.         if ($result{
  228.             trigger_error(msg('authn-success'array($this->cfg->getAuthnEngine()))E_USER_WARNING);
  229.         else {
  230.             trigger_error(msg('authn-err'array())E_USER_WARNING);
  231.         }
  232.  
  233.         $this->clean();
  234.     return $result;
  235.     }
  236.  
  237.     /**
  238.      * Retrieve the attributes provided by the user when logged in.
  239.      * @return an associative array containing all attributes.
  240.      */
  241.     public function getAttributes({
  242.         // register autoload function
  243.         set_exception_handler(array($this->handler"exceptionHandler"));
  244.         set_error_handler(array($this->handler"errorHandler"));
  245.  
  246.         // check if we have an authentication engine configured
  247.         if (empty($this->authn_engine)) {
  248.             trigger_error(msg('authn-engine-err'array())E_USER_WARNING);
  249.             $this->clean();
  250.             return array();
  251.         }
  252.  
  253.         $this->clean();
  254.         return $this->authn_engine->getAttributes();
  255.     }
  256.  
  257.     /**
  258.      * Get the value (or values) of an attribute, if present.
  259.      * @param name The name of the attribute.
  260.      * @param namespace The namespace of the attribute, if any.
  261.      * @return the attribute value or an array containing all values.
  262.      *  Null in any other case.
  263.      */
  264.     public function getAttribute($name$namespace{
  265.         // register autoload function
  266.         set_exception_handler(array($this->handler"exceptionHandler"));
  267.         set_error_handler(array($this->handler"errorHandler"));
  268.  
  269.         // check if we have an authentication engine configured
  270.         if (empty($this->authn_engine)) {
  271.             trigger_error(msg('authn-engine-err'array())E_USER_WARNING);
  272.             $this->clean();
  273.             return null;
  274.         }
  275.  
  276.         $this->clean();
  277.         return $this->authn_engine->getAttribute($name$namespace);
  278.     }
  279.  
  280.     /**
  281.      * Remove the user's session and trigger a logout for the specified authentication
  282.      * protocol.
  283.      * @param slo Whether to perform a Single Log Out or a local logout.
  284.      * @return true if success, false in any other case.
  285.      */
  286.     public function logout($slo{
  287.         // register autoload function
  288.         set_exception_handler(array($this->handler"exceptionHandler"));
  289.         set_error_handler(array($this->handler"errorHandler"));
  290.  
  291.         // check if we have an authentication engine configured
  292.         if (empty($this->authn_engine)) {
  293.             trigger_error(msg('authn-engine-err'array())E_USER_WARNING);
  294.             $this->clean();
  295.             return AUTHN_FAILED;
  296.         }
  297.  
  298.         $this->clean();
  299.         return $this->authn_engine->logout($slo);
  300.     }
  301.  
  302.     /***************************
  303.      * Authorization interface *
  304.      ***************************/
  305.  
  306.     /**
  307.      * Perform authorization for the a given subject.
  308.      * Multiple authorization engines are supported, so
  309.      * authorization will succeed if any of these succeeds.
  310.      * @param user The subject queried.
  311.      * @param attrs The attributes of the user.
  312.      * @param engine The authorization engine(s) to use. All engines are used if none specified.
  313.      *  If more than one engine should be checked then this must be an array.
  314.      * @return AUTHZ_SUCCESS if any of the supported (or selected) engines succeeds or if no
  315.      *  authorization engine is configured. AUTHZ_FAILED if all the engines fail.
  316.      */
  317.     public function isAuthorized($user$attrs$engine null{
  318.         // register autoload function
  319.         set_exception_handler(array($this->handler"exceptionHandler"));
  320.         set_error_handler(array($this->handler"errorHandler"));
  321.  
  322.         // bypass if no authorization engine
  323.         if (empty($engine&& empty($this->authz_engines))
  324.             return true;
  325.  
  326.         $result false;
  327.         // check specific engines
  328.         if (!empty($engine)) {
  329.             $engines $engine;
  330.             if (!is_array($engine)) $engines array($engine);
  331.  
  332.             // iterate over engines
  333.             foreach ($engines as $e{
  334.                 if (!isset($this->authz_engines[$e])) {
  335.                     trigger_error(msg("authz-engine-err"array($e))E_USER_ERROR);
  336.                 }
  337.                 trigger_error(msg("query-authz-via"array($e)));
  338.                 $result |= $this->authz_engines[$e]->isAuthorized($user$attrs);
  339.             }
  340.         // check all configured engines
  341.         else {
  342.             trigger_error(msg("query-authz"array()));
  343.             // iterate over engines
  344.             foreach ($this->authz_engines as $e{
  345.                 $result |= $e->isAuthorized($user$attrs);
  346.             }
  347.         }
  348.  
  349.         if ($result{
  350.             trigger_error(msg('user-authz-ok'array($user))E_USER_WARNING);
  351.         else {
  352.             trigger_error(msg('user-authz-err'array($user))E_USER_WARNING);
  353.         }
  354.  
  355.         $this->clean();
  356.         return $result;
  357.     }
  358.  
  359.     /**
  360.      * Authorize a given subject with the data retrieved from federated login.
  361.      * Multiple authorization engines are supported, so
  362.      * authorization will be done in all of them.
  363.      * @param user The subject of authorization.
  364.      * @param attrs The attributes of the user.
  365.      * @param reference An internal reference that may be valuable for the engine, tipically
  366.      *  referring to a previous invitation or similar.
  367.      * @param expires The time (POSIX) when authorization will expire. Use 0 if authorization
  368.      *  should never expire. Defaults to 0.
  369.      * @param engine The authorization engine(s) to use. All engines are used if none specified.
  370.      *  If more than one engine should be checked then this must be an array.
  371.      * @return AUTHZ_SUCCESS if any of the supported engines succeeds or if no
  372.      *  authorization engine is configured. AUTHZ_FAILED if all the engines fail.
  373.      */
  374.     public function authorize($user$attrs$reference null$expires 0$engine null{
  375.         // register autoload function
  376.         set_exception_handler(array($this->handler"exceptionHandler"));
  377.         set_error_handler(array($this->handler"errorHandler"));
  378.  
  379.         $result false;
  380.         // check specific engines
  381.         if (!empty($engine)) {
  382.             $engines $engine;
  383.             if (!is_array($engine)) $engines array($engine);
  384.  
  385.             // iterate over engines
  386.             foreach ($engines as $e{
  387.                 if (!isset($this->authz_engines[$e])) {
  388.                     trigger_error(msg("authz-engine-err"array($e))E_USER_ERROR);
  389.                 }
  390.                 trigger_error(msg("authorize-user-via"array($user$e)));
  391.                 $result |= $this->authz_engines[$e]->authorize($user$attrs$reference$expires);
  392.             }
  393.         // check all configured engines
  394.         else {
  395.             // iterate over engines
  396.             foreach ($this->authz_engines as $name => $e{
  397.                 trigger_error(msg("authorize-user-via"array($user$name)));
  398.                 $result |= $e->authorize($user$attrs$reference$expires);
  399.             }
  400.         }
  401.  
  402.         $this->clean();
  403.         return $result;
  404.     }
  405.  
  406.     /**
  407.      * Revoke authorization for a given subject identified by an e-mail.
  408.      * @param mail The e-mail of the user.
  409.      * @param engine The authorization engine(s) to use. All engines are used if none specified.
  410.      *  If more than one engine should be checked then this must be an array.
  411.      * @return true if authorization is revoked correctly for all authorization
  412.      *  engines, false in any other case.
  413.      */
  414.     public function revoke($mail$engine null{
  415.         // register autoload function
  416.         set_exception_handler(array($this->handler"exceptionHandler"));
  417.         set_error_handler(array($this->handler"errorHandler"));
  418.  
  419.         $result false;
  420.         // check specific engines
  421.         if (!empty($engine)) {
  422.             $engines $engine;
  423.             if (!is_array($engine)) $engines array($engine);
  424.  
  425.             // iterate over engines
  426.             foreach ($engines as $e{
  427.                 if (!isset($this->authz_engines[$e])) {
  428.                     trigger_error(msg("authz-engine-err"array($e))E_USER_ERROR);
  429.                 }
  430.                 trigger_error(msg("revoke-user-via"array($e)));
  431.                 $result |= $this->authz_engines[$e]->revoke($user);
  432.             }
  433.         // check all configured engines
  434.         else {
  435.             trigger_error(msg("revoke"array()));
  436.             // iterate over engines
  437.             foreach ($this->authz_engines as $e{
  438.                 $result |= $e->revoke($user);
  439.             }
  440.         }
  441.  
  442.         $this->clean();
  443.         return $result;
  444.     }
  445.  
  446.     /**
  447.      * Returns the authorization engines configured for the current PoA, or
  448.      * the one specified.
  449.      * @param engine The name of the authorization engine to retrieve.
  450.      *  If more than one engine should be returned then this must be an array.
  451.      * @return The authorization engine(s) requested if it was previously configured.
  452.      *  If none was specified, all configured engines will be returned. An empty
  453.      *  array will be returned if no authorization engines were found.
  454.      */
  455.     public function getAuthorizationEngines($engine null{
  456.         // register autoload function
  457.         set_exception_handler(array($this->handler"exceptionHandler"));
  458.         set_error_handler(array($this->handler"errorHandler"));
  459.  
  460.         $list $this->authz_engines;
  461.         // check specific engines
  462.         if (!empty($engine)) {
  463.             $list array();
  464.             $engines $engine;
  465.             if (!is_array($engine)) $engines array($engine);
  466.  
  467.             // iterate over engines
  468.             foreach ($engines as $e{
  469.                 if (!isset($this->authz_engines[$e])) {
  470.                     trigger_error(msg("authz-engine-err"array($e))E_USER_ERROR);
  471.                 }
  472.                 $list[$e$this->authz_engines[$e];
  473.             }
  474.         }
  475.  
  476.         $this->clean();
  477.         return $list;
  478.     }
  479.  
  480.     /**
  481.      * Get the authorization levels that match for the user specified, according to the configuration.
  482.      * An array with the names of the levels matching the user is returned. An empty array is
  483.      * returned if no match is found.
  484.      * @param user The user identifier.
  485.      * @param attributes An array of attributes available for the user.
  486.      * @return An array with the names of the levels matching the user, if any. An empty array will
  487.      *  be returned if no match. Exception will be raised if no levels are defined for this PoA.
  488.      */
  489.     public function getAuthorizationLevels($user$attributes{
  490.  
  491.     }
  492.  
  493. }
  494.  
  495. ?>

Documentation generated on Wed, 13 Oct 2010 15:06:23 +0200 by phpDocumentor 1.4.3