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("PoAUtils.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.      * @param file The path to the configuration file.
  61.      */
  62.     public function __construct($site$file null{
  63.         $this->local_site = $site;
  64.  
  65.         // manage generic session
  66.         if (!isset($_COOKIE[$site.'_session'])) {
  67.             $id mt_rand();
  68.             @setcookie($site.'_session'$id);
  69.             $_COOKIE[$site.'_session'$id;
  70.         }
  71.  
  72.         $this->handler = new PoAEventHandler($site);
  73.  
  74.         // register autoload function
  75.         spl_autoload_register(array($this->handler,"autoloadHandler"));
  76.  
  77.         // configure
  78.         try {
  79.             $this->cfg = new PoAConfigurator($site$file);
  80.         catch (Exception $e// unrecoverable!!
  81.             // we have no logging, so do our best here
  82.             // put a message in the error log and in STDOUT and exit
  83.             error_log($e);
  84.             $this->handler->abort(E_USER_ERROR$e);
  85.         }
  86.  
  87.         // initialize logger
  88.         $this->log = new PoALog($this->cfg->getLogLevel()$this->cfg->getLogFile());
  89.  
  90.         // initialize error handling
  91.         $this->handler->setDebug($this->cfg->isDebug());
  92.         $this->handler->setLogger($this->log);
  93.         set_exception_handler(array($this->handler"exceptionHandler"));
  94.         set_error_handler(array($this->handler"errorHandler"));
  95.  
  96.         // load authentication engine
  97.         $engine $this->cfg->getAuthnEngine();
  98.         if (class_exists($engine)) {
  99.             $this->authn_engine = new $engine($this->cfg->getAuthnEngineConfFile()$site);
  100.             $this->authn_engine->setHandler($this->handler);
  101.         }
  102.  
  103.         // load authorization engines
  104.         $engines $this->cfg->getAuthzEngines();
  105.         foreach ($engines as $engine{
  106.             $this->authz_engines[$enginenew $engine($this->cfg->getAuthzEngineConfFile($engine)$site);
  107.             $this->authz_engines[$engine]->setHandler($this->handler);
  108.         }
  109.  
  110.         $this->clean();
  111.     }
  112.  
  113.     protected function clean({
  114.         // clean
  115.         restore_exception_handler();
  116.         restore_error_handler();
  117.     }
  118.  
  119.     /**
  120.      * Attach a hook object to the appropriate entry point of the available
  121.      * authentication or authorization engines.
  122.      * @param name The name of the hook. Refer to each individual engine
  123.      *  for a complete list of available hooks.
  124.      * @param hook A hook object with the function or method to attach.
  125.      * @return true if the hook was successfully attached, false otherwise.
  126.      */
  127.     public function addHook($name$hook{
  128.         // register autoload function
  129.         set_exception_handler(array($this->handler"exceptionHandler"));
  130.         set_error_handler(array($this->handler"errorHandler"));
  131.  
  132.         // add hook for authentication engine
  133.         $result $this->authn_engine->addHook($name$hook);
  134.  
  135.         // add hook for authorization engines
  136.         foreach ($this->authz_engines as $engine{
  137.             $result |= $engine->addHook($name$hook);
  138.         }
  139.  
  140.         $this->clean();
  141.         return $result;
  142.     }
  143.  
  144.     /**
  145.      * Remove a hook from the specified entry point of the available
  146.      * authentication or authorization engines.
  147.      * @param name The name of the hook. Refer to each individual engine
  148.      *  for a complete list of available hooks.
  149.      * @param hook The hook object which shall be removed.
  150.      * @return true if the hook was successfully removed, false otherwise.
  151.      */
  152.     public function removeHook($name$hook{
  153.         // register autoload function
  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.         set_exception_handler(array($this->handler"exceptionHandler"));
  181.         set_error_handler(array($this->handler"errorHandler"));
  182.  
  183.         // check if we have an authentication engine configured
  184.         if (empty($this->authn_engine)) {
  185.             trigger_error(PoAUtils::msg('authn-engine-err'array())E_USER_WARNING);
  186.             $this->clean();
  187.             return AUTHN_FAILED;
  188.         }
  189.  
  190.         trigger_error(PoAUtils::msg("authenticating-via"array($this->cfg->getAuthnEngine())));
  191.  
  192.         $result false;
  193.         try {
  194.             $result $this->authn_engine->authenticate();
  195.         catch (PoAException $e{
  196.             trigger_error($eE_USER_WARNING);
  197.         }
  198.         if ($result{
  199.             trigger_error(PoAUtils::msg('authn-success'array($this->cfg->getAuthnEngine()))E_USER_WARNING);
  200.         else {
  201.             trigger_error(PoAUtils::msg('authn-err'array())E_USER_WARNING);
  202.         }
  203.  
  204.         $this->clean();
  205.     return $result;
  206.     }
  207.  
  208.     /**
  209.      * Query the current status of the user in the federation.
  210.      * @return AUTHN_SUCCESS if authentication succeeded, AUTHN_FAILED in
  211.      *  any other case.
  212.      */
  213.     public function isAuthenticated({
  214.         // register autoload function
  215.         set_exception_handler(array($this->handler"exceptionHandler"));
  216.         set_error_handler(array($this->handler"errorHandler"));
  217.  
  218.         // check if we have an authentication engine configured
  219.         if (empty($this->authn_engine)) {
  220.             trigger_error(PoAUtils::msg('authn-engine-err'array())E_USER_WARNING);
  221.             $this->clean();
  222.             return AUTHN_FAILED;
  223.         }
  224.  
  225.         trigger_error(PoAUtils::msg("check-authn-status"array($this->cfg->getAuthnEngine())));
  226.  
  227.         $result $this->authn_engine->isAuthenticated();
  228.         if ($result{
  229.             trigger_error(PoAUtils::msg('authn-success'array($this->cfg->getAuthnEngine()))E_USER_WARNING);
  230.         else {
  231.             trigger_error(PoAUtils::msg('authn-err'array())E_USER_WARNING);
  232.         }
  233.  
  234.         $this->clean();
  235.     return $result;
  236.     }
  237.  
  238.     /**
  239.      * Retrieve the attributes provided by the user when logged in.
  240.      * @return an associative array containing all attributes.
  241.      */
  242.     public function getAttributes({
  243.         // register autoload function
  244.         set_exception_handler(array($this->handler"exceptionHandler"));
  245.         set_error_handler(array($this->handler"errorHandler"));
  246.  
  247.         // check if we have an authentication engine configured
  248.         if (empty($this->authn_engine)) {
  249.             trigger_error(PoAUtils::msg('authn-engine-err'array())E_USER_WARNING);
  250.             $this->clean();
  251.             return array();
  252.         }
  253.  
  254.         $this->clean();
  255.         return $this->authn_engine->getAttributes();
  256.     }
  257.  
  258.     /**
  259.      * Get the value (or values) of an attribute, if present.
  260.      * @param name The name of the attribute.
  261.      * @param namespace The namespace of the attribute, if any.
  262.      * @return the attribute value or an array containing all values.
  263.      *  Null in any other case.
  264.      */
  265.     public function getAttribute($name$namespace{
  266.         // register autoload function
  267.         set_exception_handler(array($this->handler"exceptionHandler"));
  268.         set_error_handler(array($this->handler"errorHandler"));
  269.  
  270.         // check if we have an authentication engine configured
  271.         if (empty($this->authn_engine)) {
  272.             trigger_error(PoAUtils::msg('authn-engine-err'array())E_USER_WARNING);
  273.             $this->clean();
  274.             return null;
  275.         }
  276.  
  277.         $this->clean();
  278.         return $this->authn_engine->getAttribute($name$namespace);
  279.     }
  280.  
  281.     /**
  282.      * Remove the user's session and trigger a logout for the specified authentication
  283.      * protocol.
  284.      * @param slo Whether to perform a Single Log Out or a local logout.
  285.      * @return true if success, false in any other case.
  286.      */
  287.     public function logout($slo{
  288.         // register autoload function
  289.         set_exception_handler(array($this->handler"exceptionHandler"));
  290.         set_error_handler(array($this->handler"errorHandler"));
  291.  
  292.         // check if we have an authentication engine configured
  293.         if (empty($this->authn_engine)) {
  294.             trigger_error(PoAUtils::msg('authn-engine-err'array())E_USER_WARNING);
  295.             $this->clean();
  296.             return AUTHN_FAILED;
  297.         }
  298.  
  299.         $this->clean();
  300.         return $this->authn_engine->logout($slo);
  301.     }
  302.  
  303.     /***************************
  304.      * Authorization interface *
  305.      ***************************/
  306.  
  307.     /**
  308.      * Perform authorization for the a given subject.
  309.      * Multiple authorization engines are supported, so
  310.      * authorization will succeed if any of these succeeds.
  311.      * @param user The subject queried.
  312.      * @param attrs The attributes of the user.
  313.      * @param engine The authorization engine(s) to use. All engines are used if none specified.
  314.      *  If more than one engine should be checked then this must be an array.
  315.      * @return AUTHZ_SUCCESS if any of the supported (or selected) engines succeeds or if no
  316.      *  authorization engine is configured. AUTHZ_FAILED if all the engines fail.
  317.      */
  318.     public function isAuthorized($user$attrs$engine null{
  319.         // register autoload function
  320.         set_exception_handler(array($this->handler"exceptionHandler"));
  321.         set_error_handler(array($this->handler"errorHandler"));
  322.  
  323.         // bypass if no authorization engine
  324.         if (empty($engine&& empty($this->authz_engines))
  325.             return true;
  326.  
  327.         $result false;
  328.         // check specific engines
  329.         if (!empty($engine)) {
  330.             $engines $engine;
  331.             if (!is_array($engine)) $engines array($engine);
  332.  
  333.             // iterate over engines
  334.             foreach ($engines as $e{
  335.                 if (!isset($this->authz_engines[$e])) {
  336.                     trigger_error(PoAUtils::msg("authz-engine-err"array($e))E_USER_ERROR);
  337.                 }
  338.                 trigger_error(PoAUtils::msg("query-authz-via"array($e)));
  339.                 $result |= $this->authz_engines[$e]->isAuthorized($user$attrs);
  340.             }
  341.         // check all configured engines
  342.         else {
  343.             trigger_error(PoAUtils::msg("query-authz"array()));
  344.             // iterate over engines
  345.             foreach ($this->authz_engines as $e{
  346.                 $result |= $e->isAuthorized($user$attrs);
  347.             }
  348.         }
  349.  
  350.         if ($result{
  351.             trigger_error(PoAUtils::msg('user-authz-ok'array($user))E_USER_WARNING);
  352.         else {
  353.             trigger_error(PoAUtils::msg('user-authz-err'array($user))E_USER_WARNING);
  354.         }
  355.  
  356.         $this->clean();
  357.         return $result;
  358.     }
  359.  
  360.     /**
  361.      * Authorize a given subject with the data retrieved from federated login.
  362.      * Multiple authorization engines are supported, so
  363.      * authorization will be done in all of them.
  364.      * @param user The subject of authorization.
  365.      * @param attrs The attributes of the user.
  366.      * @param reference An internal reference that may be valuable for the engine, tipically
  367.      *  referring to a previous invitation or similar.
  368.      * @param expires The time (POSIX) when authorization will expire. Use 0 if authorization
  369.      *  should never expire. Defaults to 0.
  370.      * @param engine The authorization engine(s) to use. All engines are used if none specified.
  371.      *  If more than one engine should be checked then this must be an array.
  372.      * @return AUTHZ_SUCCESS if any of the supported engines succeeds or if no
  373.      *  authorization engine is configured. AUTHZ_FAILED if all the engines fail.
  374.      */
  375.     public function authorize($user$attrs$reference null$expires 0$engine null{
  376.         // register autoload function
  377.         set_exception_handler(array($this->handler"exceptionHandler"));
  378.         set_error_handler(array($this->handler"errorHandler"));
  379.  
  380.         $result false;
  381.         // check specific engines
  382.         if (!empty($engine)) {
  383.             $engines $engine;
  384.             if (!is_array($engine)) $engines array($engine);
  385.  
  386.             // iterate over engines
  387.             foreach ($engines as $e{
  388.                 if (!isset($this->authz_engines[$e])) {
  389.                     trigger_error(PoAUtils::msg("authz-engine-err"array($e))E_USER_ERROR);
  390.                 }
  391.                 trigger_error(PoAUtils::msg("authorize-user-via"array($user$e)));
  392.                 $result |= $this->authz_engines[$e]->authorize($user$attrs$reference$expires);
  393.             }
  394.         // check all configured engines
  395.         else {
  396.             // iterate over engines
  397.             foreach ($this->authz_engines as $name => $e{
  398.                 trigger_error(PoAUtils::msg("authorize-user-via"array($user$name)));
  399.                 $result |= $e->authorize($user$attrs$reference$expires);
  400.             }
  401.         }
  402.  
  403.         $this->clean();
  404.         return $result;
  405.     }
  406.  
  407.     /**
  408.      * Revoke authorization for a given subject identified by an e-mail.
  409.      * @param mail The e-mail of the user.
  410.      * @param engine The authorization engine(s) to use. All engines are used if none specified.
  411.      *  If more than one engine should be checked then this must be an array.
  412.      * @return true if authorization is revoked correctly for all authorization
  413.      *  engines, false in any other case.
  414.      */
  415.     public function revoke($mail$engine null{
  416.         // register autoload function
  417.         set_exception_handler(array($this->handler"exceptionHandler"));
  418.         set_error_handler(array($this->handler"errorHandler"));
  419.  
  420.         $result false;
  421.         // check specific engines
  422.         if (!empty($engine)) {
  423.             $engines $engine;
  424.             if (!is_array($engine)) $engines array($engine);
  425.  
  426.             // iterate over engines
  427.             foreach ($engines as $e{
  428.                 if (!isset($this->authz_engines[$e])) {
  429.                     trigger_error(PoAUtils::msg("authz-engine-err"array($e))E_USER_ERROR);
  430.                 }
  431.                 trigger_error(PoAUtils::msg("revoke-user-via"array($e)));
  432.                 $result |= $this->authz_engines[$e]->revoke($user);
  433.             }
  434.         // check all configured engines
  435.         else {
  436.             trigger_error(PoAUtils::msg("revoke"array()));
  437.             // iterate over engines
  438.             foreach ($this->authz_engines as $e{
  439.                 $result |= $e->revoke($user);
  440.             }
  441.         }
  442.  
  443.         $this->clean();
  444.         return $result;
  445.     }
  446.  
  447.     /**
  448.      * Returns the authorization engines configured for the current PoA, or
  449.      * the one specified.
  450.      * @param engine The name of the authorization engine to retrieve.
  451.      *  If more than one engine should be returned then this must be an array.
  452.      * @return The authorization engine(s) requested if it was previously configured.
  453.      *  If none was specified, all configured engines will be returned. An empty
  454.      *  array will be returned if no authorization engines were found.
  455.      */
  456.     public function getAuthorizationEngines($engine null{
  457.         // register autoload function
  458.         set_exception_handler(array($this->handler"exceptionHandler"));
  459.         set_error_handler(array($this->handler"errorHandler"));
  460.  
  461.         $list $this->authz_engines;
  462.         // check specific engines
  463.         if (!empty($engine)) {
  464.             $list array();
  465.             $engines $engine;
  466.             if (!is_array($engine)) $engines array($engine);
  467.  
  468.             // iterate over engines
  469.             foreach ($engines as $e{
  470.                 if (!isset($this->authz_engines[$e])) {
  471.                     trigger_error(PoAUtils::msg("authz-engine-err"array($e))E_USER_ERROR);
  472.                 }
  473.                 $list[$e$this->authz_engines[$e];
  474.             }
  475.         }
  476.  
  477.         $this->clean();
  478.         return $list;
  479.     }
  480.  
  481.     /**
  482.      * Get the authorization levels that match for the user specified, according to the configuration.
  483.      * An array with the names of the levels matching the user is returned. An empty array is
  484.      * returned if no match is found.
  485.      * @param user The user identifier.
  486.      * @param attributes An array of attributes available for the user.
  487.      * @return An array with the names of the levels matching the user, if any. An empty array will
  488.      *  be returned if no match. Exception will be raised if no levels are defined for this PoA.
  489.      */
  490.     public function getAuthorizationLevels($user$attributes{
  491.         // register autoload function
  492.         set_exception_handler(array($this->handler"exceptionHandler"));
  493.         set_error_handler(array($this->handler"errorHandler"));
  494.  
  495.         $levels $this->cfg->getAuthzLevels();
  496.         $verified array();
  497.         if (empty($levels)) {
  498.             trigger_error(PoAUtils::msg('authz-levels-err'array())E_USER_ERROR);
  499.         }
  500.  
  501.         foreach ($levels as $level{
  502.             // TODO
  503.         }
  504.  
  505.         return $verified;
  506.     }
  507.  
  508. }
  509.  
  510. ?>

Documentation generated on Tue, 25 Jan 2011 11:24:37 +0100 by phpDocumentor 1.4.3