Show
Ignore:
Timestamp:
02/11/10 17:34:27 (2 years ago)
Author:
gbastien
Message:

* Merged recent changes in the trunk into this branch
* New token architecture not yet fully functional, nor tested, but ...

  • can now edit token templates for different context of tokens (from the edit netork interface at the bottom of page)
  • Users connected through a wifidog gateway can view their connection token information
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/newtoken/wifidog/classes/Token.php

    r1445 r1446  
    5555class Token  
    5656{ 
    57     public static function generateConnectionToken($mac, $network, $user) { 
     57    /** 
     58     * Generate a new access token for the connection 
     59     * 
     60     * @param string $mac              The mac address of the computer connecting 
     61     * 
     62     * @param string $network          The network the person is connecting to 
     63     * 
     64     * @param string $node             The node from which the connection originates 
     65     * 
     66     * TODO: eventually, the user should not be necessary for userless authentication.  But much 
     67     * of the code would need to be changed in order to support this kind of authentication. 
     68     * @param string $user             The user authenticating 
     69     *  
     70     * @param string $node_ip=null     The ip address from which the connection originates 
     71     * 
     72     * @return tokenid | false 
     73     * 
     74     */ 
     75    public static function generateConnectionToken($mac, $network, $node, $user, $node_ip = null) { 
    5876        $retval = false; 
    5977        if ($user->isUserValid()) { 
    6078            $db = AbstractDb::getObject(); 
    61             $session = Session::getObject(); 
    62              
     79                         
    6380            // Delete unused connection token 
    6481 
    6582            $token = self :: generateToken(); 
    66             if ($_SERVER['REMOTE_ADDR']) { 
     83            if (is_null($node_ip) && $_SERVER['REMOTE_ADDR']) { 
    6784                $node_ip = $db->escapeString($_SERVER['REMOTE_ADDR']); 
    6885            } 
    6986             
    70             if ($session && $node_ip && $session->get(SESS_NODE_ID_VAR)) { 
     87            if ($node_ip && $node) { 
    7188                //echo "$session && $node_ip && {$session->get(SESS_NODE_ID_VAR)}"; 
    72                 $node_id = $db->escapeString($session->get(SESS_NODE_ID_VAR)); 
    73                 $abuseControlFault = User::isAbuseControlViolated($user, $mac, Node::getObject($node_id)); 
     89                $node_id = $db->escapeString($node->getId()); 
     90                $abuseControlFault = User::isAbuseControlViolated($user, $mac, $node); 
    7491                if($abuseControlFault) { 
    7592                    throw new Exception ($abuseControlFault); 
     
    8299                $sql = "DELETE FROM connections USING tokens "."WHERE tokens.token_id=connections.token_id AND token_status='".TOKEN_UNUSED."' AND user_id = '".$user->getId()."';\n"; 
    83100                $db->execSqlUpdate($sql, false); 
    84                 // TODO:  Try to find a reusable token before creating a brand new one! 
    85101 
    86102               // Check if we have any token templates 
    87                 // TODO: token templates should be from a specific template and there should always be a template 
    88103                $templates = TokenTemplate::getTemplatesForNetwork($network); 
    89104                if (count($templates) == 0) { 
    90105                    // No templates found 
    91                     // create and return new token - we don't enforce token limits 
     106                    // create and return new token - we don't enforce token limits, this is for backward compatibility 
    92107                    $token = self :: generateToken(); 
    93108                    $sql = "INSERT INTO tokens (token_owner, token_issuer, token_id, token_status) VALUES ('" . $user->getId() . "', '" . $user->getId() . "', '$token', '" . TOKEN_UNUSED . "');\n"; 
     
    144159                                $max_incoming_data_value = (!is_null($template->getMaxIncomingData())?$template->getMaxIncomingData():"null"); 
    145160                                $max_outgoing_data_value = (!is_null($template->getMaxOutgoingData())?$template->getMaxOutgoingData():"null"); 
    146                                 if (!is_null($template->getMaxUsageDuration())) 
     161                                 
     162                                if (!is_null($template->getMaxConnectionDuration())) 
     163                                    $expiry_date = "CURRENT_TIMESTAMP + '" . $template->getMaxConnectionDuration() . "'"; 
     164                                elseif (!is_null($template->getMaxUsageDuration())) 
    147165                                    $expiry_date = "CURRENT_TIMESTAMP + '" . $template->getMaxUsageDuration() . "'"; 
    148166                                elseif (!is_null($template->getMaxWallClockDuration())) 
     
    164182            } 
    165183        } 
    166         
     184        self::setCurrentToken($retval); 
    167185        return $retval; 
    168186    }  
    169187     
    170                 /** 
    171      * Generate a new access token for the specified user 
    172      * 
    173      * @param string $validated_user       The user to generate a token for 
    174      * 
    175      * @param string $creator              The user generating the token 
    176      * 
    177      * @param string $token_template_id    The token template that the token should be associated with. 
    178      *                                     If templates aren't used then this the auth process will create 
    179      *                                     tokens automatically and we don't need to create tokens elsewhere. 
    180      * 
    181      * @param string $token_lot=null       Optional. The token lot the token is part of. 
    182      * 
    183      * @return tokenid 
    184      * 
    185      */ 
    186     public static function generateTokenForUser($validated_user, $creator, $token_template_id, $token_lot = null) { 
    187         $db = AbstractDb::getObject(); 
    188         $token = self::generateToken(); 
    189  
    190         $db->execSqlUpdate("INSERT INTO tokens (token_owner, token_issuer, token_id, token_template_id, token_status) VALUES ('" . $validated_user->getId() . "', '" . $creator->getId() . "', '$token', '" . $token_template_id . "', '" . TOKEN_UNUSED . "');"); 
    191  
    192         return $token; 
    193     } 
    194      
    195188    public static function generateToken() { 
    196189        return md5(uniqid(rand(), 1)); 
     190    } 
     191     
     192    /** 
     193     * Instantiate the current user 
     194     * 
     195     * @return mixed A User object, or null if there was an error 
     196 
     197     */ 
     198    public static function getCurrentToken() { 
     199        require_once ('classes/Session.php'); 
     200        $session = Session::getObject(); 
     201        $sessTokenId = $session->get('SESS_TOKEN_ID'); 
     202         
     203       /* if(!empty($sessCurrentUserId)){ 
     204            try { 
     205                $user = self :: getObject($sessCurrentUserId); 
     206                //$user = new User($session->get(SESS_USER_ID_VAR)); 
     207            } catch (Exception $e) { 
     208                $session->set(SESS_TOKEN_ID, null); 
     209            } 
     210        }*/ 
     211        return $sessTokenId; 
     212    } 
     213 
     214    /** 
     215     * Associates the user passed in parameter with the session 
     216     * 
     217     * This should NOT be called by anything except the Authenticators 
     218     * 
     219     * @param object $user User a user object, or null 
     220     * 
     221     * @return bool True if everything went well setting the session 
     222 
     223     */ 
     224    public static function setCurrentToken($tokenId) { 
     225 
     226        try { 
     227            $session = Session::getObject(); 
     228            $session->set('SESS_TOKEN_ID', $tokenId); 
     229            return true; 
     230        } catch (Exception $e) { 
     231            return false; 
     232        } 
     233    } 
     234     
     235                /** Set Smarty template values.  Standardization routine.  
     236     * // TODO: implement this*/ 
     237    public static function assignSmartyValues($smarty) { 
     238         
     239        $tokenId = Token :: getCurrentToken(); 
     240      
     241        /** 
     242         * Define user security levels for the template 
     243         * 
     244         * These values are used in the default template of WiFoDog but could be 
     245         * used in a customized template to restrict certain links to specific 
     246         * user access levels.  Note however that they will all be deprecateb by the 
     247         * new roles system. 
     248         */ 
     249        $smarty->assign('hasConnection', !empty($tokenId) ? true : false); 
     250        $smarty->assign('tokenId', $tokenId); 
     251        /*$smarty->assign('userIsValid', $user && !$user->isSplashOnlyUser() ? true : false); 
     252        $smarty->assign('userDEPRECATEDisSuperAdmin', $user && $user->DEPRECATEDisSuperAdmin()); 
     253 
     254        if (isset ($_REQUEST['debug_request']) && ($user && $user->DEPRECATEDisSuperAdmin())) { 
     255            // Tell Smarty everything it needs to know 
     256            $smarty->assign('debugRequested', true); 
     257            $smarty->assign('debugOutput', print_r($_REQUEST, true)); 
     258        }*/ 
    197259    } 
    198260}