Changeset 692

Show
Ignore:
Timestamp:
2008-07-24 08:56:26 (3 months ago)
Author:
michaeltwofish
Message:

Log exceptions. Closes #48. I don't think the OpenID plugin should throw exceptions in so many places. It's entirely ungraceful at the moment.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/openid/trunk/openid.plugin.php

    r61 r692  
    22class OpenID extends Plugin 
    33{ 
    4     public function info() { 
     4    public function info() 
     5    { 
    56        return array( 
    67            'name' => 'OpenID', 
     
    1415            ); 
    1516    } 
    16      
    17     public function filter_rewrite_rules( $db_rules ) { 
    18         $db_rules[]= new RewriteRule( array( 
     17 
     18    public function filter_rewrite_rules( $db_rules ) 
     19    { 
     20        $db_rules[] = new RewriteRule( array( 
    1921            'name' => 'openid', 
    2022            'parse_regex' => '%^openid/?(?P<user>[^/]*)/?$%i', // For Server, if previous matched, don't look. 
     
    2729            'description' => 'OpenID Authentification' 
    2830            ) ); 
    29          
     31 
    3032        return $db_rules; 
    3133    } 
    3234 
    33     public function act( $action ) { 
     35    public function act( $action ) 
     36    { 
    3437        if ( isset( $_GET['openid_mode'] ) ) { 
    3538            switch ( $_GET['openid_mode'] ) { 
     
    3841                    break; 
    3942                case 'cancel': 
    40                     throw new Exception( 'Authorization failed: user cancelled the authorization.' ); 
     43                    EventLog::log( 'Authorization failed: User cancelled authorization.', 'info', 'authentication', 'OpenID' ); 
     44                    throw new Exception( 'Authorization failed: User cancelled authorization.' ); 
    4145                    break; 
    4246            } 
     
    4650        } 
    4751        else { 
     52            EventLog::log( 'Authorization failed: unknown error.', 'err', 'authentication', 'OpenID' ); 
    4853            throw new Exception( 'Authorization failed: unknown error.' ); 
    4954        } 
    5055    } 
    5156 
    52     public function action_plugin_activation( $file ) {      
     57    public function action_plugin_activation( $file ) 
     58    { 
    5359        if ( realpath( $file ) == __FILE__ ) { 
    5460            if ( !extension_loaded('curl') && !@dl('curl') ) { 
    55                 throw new Exception( 'Could not load CURL, you need CURL for OpenID to work.' ); 
     61                EventLog::log( 'Could not load CURL, which is needed for OpenID to work.', 'err', 'authentication', 'OpenID' ); 
     62                throw new Exception( 'Could not load CURL, which is needed for OpenID to work.' ); 
    5663            } 
    5764            EventLog::register_type( 'authentification', 'OpenID' ); 
     
    5966    } 
    6067 
    61     public function action_plugin_deactivation( $file ) { 
     68    public function action_plugin_deactivation( $file ) 
     69    { 
    6270        if ( realpath( $file ) == __FILE__ ) { 
    6371            EventLog::unregister_type( 'OpenID' ); 
     
    6573    } 
    6674 
    67     public function action_init() { 
     75    public function action_init() 
     76    { 
    6877        if ( session_id() == '' ) { 
    6978            session_start(); 
     
    7382    } 
    7483 
    75     public function action_theme_loginform_before() { 
     84    public function action_theme_loginform_before() 
     85    { 
    7686        if ( isset( $_GET['openid_url'] ) ) { 
    7787            echo '<hr><div class="alert"><strong>If you have an existing account</strong>, sign in so we can assign your OpenID identifer to it.</div>'; 
    7888        } 
    7989    } 
    80      
    81     public function action_theme_loginform_after() { 
     90 
     91    public function action_theme_loginform_after() 
     92    { 
    8293     // @todo Remove the !isset( $_GET['openid_url'] ) once registration works in Habari. 
    8394        if ( ( Controller::get_action() != 'register' ) && !isset( $_GET['openid_url'] ) ) { 
     
    96107    } 
    97108 
    98     public function action_theme_loginform_controls() { 
     109    public function action_theme_loginform_controls() 
     110    { 
    99111        if ( isset( $_GET['openid_url'] ) ) { 
    100112            echo '<input type="hidden" value="'.$_GET['openid_url'].'" name="habari_openid_url">'; 
    101113        } 
    102114    } 
    103      
     115 
    104116    /* Uncomment once registration is supported by Habari. 
    105     public function action_theme_registerform_controls() { 
     117    public function action_theme_registerform_controls() 
     118    { 
    106119        if ( isset( $_GET['openid_url'] ) ) { 
    107120            echo '<input type="hidden" value="'.$_GET['openid_url'].'" name="habari_openid_url">'; 
     
    109122    } */ 
    110123 
    111     public function action_theme_admin_user( $user ) { 
    112         $openid_url= isset( $user->info->openid_url ) ? $user->info->openid_url : ''; 
     124    public function action_theme_admin_user( $user ) 
     125    { 
     126        $openid_url = isset( $user->info->openid_url ) ? $user->info->openid_url : ''; 
    113127        echo '<p><label for="openid_url">OpenID Identifier:</label></p>'; 
    114128        echo '<p><input type="text" value="'.$openid_url.'" name="habari_openid_url" disabled></p>'; 
    115129    } 
    116      
    117     public function action_user_identify() { 
     130 
     131    public function action_user_identify() 
     132    { 
    118133        if ( ( Controller::get_action() == 'login' ) && !empty( $_POST['openid_url'] ) ) { 
    119134            self::openid_start(); 
     
    122137 
    123138    // TODO: Add more security against form hijacking (for instance, check against server sent data) 
    124     public function action_user_authenticate_successful( $user ) { 
     139    public function action_user_authenticate_successful( $user ) 
     140    { 
    125141        if ( !empty( $_POST['habari_openid_url'] ) ) { 
    126             $user->info->openid_url= $_POST['habari_openid_url']; 
    127         } 
    128     } 
    129  
    130     function getOpenIDURL() { 
     142            $user->info->openid_url = $_POST['habari_openid_url']; 
     143        } 
     144    } 
     145 
     146    function getOpenIDURL() 
     147    { 
    131148        if ( empty( $_POST['openid_url'] ) ) { 
     149            EventLog::log( 'Expected an OpenID URL.', 'err', 'authentication', 'OpenID' ); 
    132150            throw new Exception( 'Expected an OpenID URL.' ); 
    133151        } 
    134          
     152 
    135153        return $_POST['openid_url']; 
    136154    } 
    137      
    138     function getReturnTo() { 
     155 
     156    function getReturnTo() 
     157    { 
    139158        return URL::get('openid'); 
    140159    } 
    141      
    142     function getTrustRoot() { 
     160 
     161    function getTrustRoot() 
     162    { 
    143163        return Site::get_url('habari'); 
    144164    } 
    145      
    146     function getStore() { 
    147         $store_path= "/tmp/_php_consumer_test"; 
    148          
     165 
     166    function getStore() 
     167    { 
     168        $store_path = "/tmp/_php_consumer_test"; 
     169 
    149170        if ( !file_exists( $store_path ) && !mkdir( $store_path ) ) { 
     171            EventLog::log( 'Could not create the FileStore directory: ' . $store_path, 'err', 'authentication', 'OpenID' ); 
    150172            throw new Exception( 'Could not create the FileStore directory: ' . $store_path . '. Please check the effective permissions.' ); 
    151173        } 
    152          
     174 
    153175        return new Auth_OpenID_FileStore( $store_path ); 
    154176    } 
    155      
    156     function getConsumer() { 
     177 
     178    function getConsumer() 
     179    { 
    157180        require_once "Auth/OpenID/Consumer.php"; 
    158181        require_once "Auth/OpenID/FileStore.php"; 
    159182        require_once "Auth/OpenID/SReg.php"; 
    160         $store= self::getStore(); 
     183        $store = self::getStore(); 
    161184        return new Auth_OpenID_Consumer( $store ); 
    162185    } 
    163      
    164     function openid_start() { 
    165         $openid= self::getOpenIDURL(); 
    166         $consumer= self::getConsumer(); 
    167          
    168         $auth_request= $consumer->begin( $openid ); 
    169          
     186 
     187    function openid_start() 
     188    { 
     189        $openid = self::getOpenIDURL(); 
     190        $consumer = self::getConsumer(); 
     191 
     192        $auth_request = $consumer->begin( $openid ); 
     193 
    170194        if ( !$auth_request ) { 
    171             throw new Exception( 'Authentication error; not a valid OpenID.' ); 
    172         } 
    173          
    174         $sreg_request= Auth_OpenID_SRegRequest::build( array( 'nickname' ), array( 'fullname', 'email' ) ); 
    175          
     195            EventLog::log( 'Authentication error: Not a valid OpenID.', 'err', 'authentication', 'OpenID' ); 
     196            throw new Exception( 'Authentication error: Not a valid OpenID.' ); 
     197        } 
     198 
     199        $sreg_request = Auth_OpenID_SRegRequest::build( array( 'nickname' ), array( 'fullname', 'email' ) ); 
     200 
    176201        if ( $sreg_request ) { 
    177202            $auth_request->addExtension( $sreg_request ); 
    178203        } 
    179          
     204 
    180205        if ( $auth_request->shouldSendRedirect() ) { 
    181             $redirect_url= $auth_request->redirectURL( self::getTrustRoot(), self::getReturnTo() ); 
    182              
     206            $redirect_url = $auth_request->redirectURL( self::getTrustRoot(), self::getReturnTo() ); 
     207 
    183208            if ( Auth_OpenID::isFailure( $redirect_url ) ) { 
     209                EventLog::log( 'Could not redirect to server: ' . $redirect_url->message, 'err', 'authentication', 'OpenID' ); 
    184210                throw new Exception( 'Could not redirect to server: ' . $redirect_url->message ); 
    185211            } 
     
    189215        } 
    190216        else { 
    191             $form_id= 'openid_message'; 
    192             $form_html= $auth_request->formMarkup( self::getTrustRoot(), self::getReturnTo(), false, array( 'id' => $form_id ) ); 
    193              
     217            $form_id = 'openid_message'; 
     218            $form_html = $auth_request->formMarkup( self::getTrustRoot(), self::getReturnTo(), false, array( 'id' => $form_id ) ); 
     219 
    194220            if ( Auth_OpenID::isFailure( $form_html ) ) { 
     221                EventLog::log( 'Could not prepare redirection form: ' . $form_html->message, 'err', 'authentication', 'OpenID' ); 
    195222                throw new Exception( 'Could not prepare redirection form: ' . $form_html->message ); 
    196223            } 
     
    209236        } 
    210237    } 
    211      
    212     function openid_end() { 
    213         $consumer= self::getConsumer(); 
    214         $return_to= self::getReturnTo(); 
    215         $response= $consumer->complete( $return_to ); 
    216          
     238 
     239    function openid_end() 
     240    { 
     241        $consumer = self::getConsumer(); 
     242        $return_to = self::getReturnTo(); 
     243        $response = $consumer->complete( $return_to ); 
     244 
    217245        switch( $response->status ) { 
    218246            case Auth_OpenID_CANCEL: 
     247                EventLog::log( 'Verification cancelled.', 'err', 'authentication', 'OpenID' ); 
    219248                throw new Exception( 'Verification cancelled.' ); 
    220249                break; 
    221250            case Auth_OpenID_FAILURE: 
     251                EventLog::log( 'OpenID authentication failed: ' . $response->message, 'err', 'authentication', 'OpenID' ); 
    222252                throw new Exception( 'OpenID authentication failed: ' . $response->message ); 
    223253                break; 
    224254            case Auth_OpenID_SUCCESS: 
    225                 $openid= $response->getDisplayIdentifier(); 
    226                 $esc_identity= htmlspecialchars( $openid, ENT_QUOTES ); 
    227                  
    228                 $user= Users::get_by_info( 'openid_url', $openid ); 
     255                $openid = $response->getDisplayIdentifier(); 
     256                $esc_identity = htmlspecialchars( $openid, ENT_QUOTES ); 
     257 
     258                $user = Users::get_by_info( 'openid_url', $openid ); 
    229259                if ( count( $user ) != 0 ) { 
    230260                    if ( count( $user ) > 1 ) { 
    231                         throw new Exception( 'Authentication error: more than one user have this OpenID.' ); 
     261                        EventLog::log( 'Authentication error: More than one user has this OpenID.', 'err', 'authentication', 'OpenID' ); 
     262                        throw new Exception( 'Authentication error: More than one user has this OpenID.' ); 
    232263                    } 
    233264                    $user[0]->remember(); 
    234265                    EventLog::log( 'Successful login for ' . $user[0]->username, 'info', 'authentication', 'OpenID' ); 
    235                      
     266 
    236267                    header( "HTTP/1.1 100 Continue" ); 
    237268                    header( "Location: " . Site::get_url( 'admin' ) );