Changeset 1206

Show
Ignore:
Timestamp:
07/18/10 18:57:41 (3 years ago)
Author:
d0nut
Message:

finished openid implementation

Location:
openid
Files:
4 added
9 modified

Legend:

Unmodified
Added
Removed
  • openid/de.xml

    r1203 r1206  
    66                <item name="wcf.acp.option.module_openid.description"><![CDATA[Erlaubt den Login mit Hilfe eines openid Accounts.]]></item> 
    77        </category> 
     8        <category name="wcf.openid"> 
     9                <item name="wcf.openid.login.description"><![CDATA[Sie können sich mit ihrem existieren Account bestimmter Anbieter bei uns authentifizieren.<br/> 
     10                        Das ganze funktioniert ÃŒber die s.g. OpenID Schnittstelle - es werden keine Zugangsdaten ausgetauscht.<br/>]]></item> 
     11                <item name="wcf.openid.login"><![CDATA[OpenID]]></item> 
     12        </category> 
    813</language> 
  • openid/en.xml

    r1203 r1206  
    66                <item name="wcf.acp.option.module_openid.description"><![CDATA[Allows users to login to the wcf with a openid account.]]></item> 
    77        </category> 
     8        <category name="wcf.openid"> 
     9                <item name="wcf.openid.login.description"><![CDATA[Click your OpenID account provider to login<br/>]]></item> 
     10                <item name="wcf.openid.login"><![CDATA[OpenID]]></item> 
     11        </category> 
    812</language> 
  • openid/eventlistener.xml

    r1203 r1206  
    66                        <eventclassname>UserLoginForm</eventclassname> 
    77                        <eventname>assignVariables</eventname> 
     8                        <inherit>1</inherit> 
    89                        <listenerclassfile>lib/system/event/listener/UserLoginOpenIDListener.class.php</listenerclassfile> 
    910                </eventlistener> 
  • openid/files/lib/data/openid/OpenID.class.php

    r1204 r1206  
    11<?php 
    2 require_once(WCF_DIR.'lib/data/user/avatar/AvatarEditor.class.php'); 
    32require_once(WCF_DIR.'lib/data/user/UserEditor.class.php'); 
    43 
    54/** 
    6  * 
     5 * embeds the openid system into the wcf 
     6 * registers include pathes and cares for all dependencies 
     7 *  
     8 * @author      Torben Brodt 
     9 * @copyright   2010 easy-coding.de 
     10 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html> 
     11 * @package     de.easy-coding.wcf.openid 
    712 */ 
    813class OpenID { 
    914 
    1015        /** 
    11          * 
    12          */ 
    13         public function __construct() { 
    14                 $path_extra = dirname(dirname(dirname(__FILE__))); 
     16         * form instance needed for finish action 
     17         * 
     18         * @var UserLoginForm|OpenIDPage 
     19         */ 
     20        protected $eventObj; 
     21 
     22        /** 
     23         * sets include pathes 
     24         *  
     25         * @param       UserLoginForm|OpenIDPage        $eventObj 
     26         */ 
     27        public function __construct($eventObj = null) { 
     28                $this->eventObj = $eventObj; 
     29 
     30                $path_extra = dirname(__FILE__); 
    1531                $path = ini_get('include_path'); 
    1632                $path = $path_extra . PATH_SEPARATOR . $path; 
     
    1834 
    1935                /** 
     36                 * session wrapper 
     37                 */ 
     38                require_once "Auth/Yadis/Manager.php"; 
     39 
     40                /** 
     41                 * session wrapper 
     42                 */ 
     43                require_once "OpenIDSession.class.php"; 
     44 
     45                /** 
    2046                 * Require the OpenID consumer code. 
    2147                 */ 
     
    2652                 * OpenID information. 
    2753                 */ 
    28                 require_once "Auth/OpenID/DumbStore.php"; 
     54                require_once "Auth/OpenID/FileStore.php"; 
    2955 
    3056                /** 
     
    3965        } 
    4066 
    41         protected function &getStore() { 
    42                 return new Auth_OpenID_DumbStore(); 
    43         } 
    44  
    45         protected function &getConsumer() { 
     67        /** 
     68         * returns file store 
     69         */ 
     70        protected function getStore() { 
     71                $store_path = FileUtil::getTemporaryFilename('openid_'); 
     72                $store_path = TMP_DIR."/_openid".WCF_N; 
     73                return new Auth_OpenID_FileStore($store_path); 
     74        } 
     75 
     76        /** 
     77         * get authenticated user 
     78         */ 
     79        protected function getConsumer() { 
     80 
     81                $session = new OpenIDSession(); 
     82         
    4683                /** 
    4784                 * Create a consumer object using the store object created 
     
    4986                 */ 
    5087                $store = $this->getStore(); 
    51                 $consumer =& new Auth_OpenID_Consumer($store); 
     88                $consumer =& new Auth_OpenID_Consumer($store, $session); 
    5289                return $consumer; 
    5390        } 
    5491 
     92        /** 
     93         * gets openid handler url 
     94         * 
     95         * @return      string 
     96         */ 
    5597        public static function getReturnTo() { 
    56                 return PAGE_URL.'index.php?page=OpenID'; 
    57         } 
    58  
     98                return PAGE_URL.'/index.php?page=OpenID'; 
     99        } 
     100 
     101        /** 
     102         * gets root url 
     103         * 
     104         * @return      string 
     105         */ 
    59106        public static function getTrustRoot() { 
    60                 return PAGE_URL.'index.php'; 
    61         } 
    62  
    63         /** 
    64          * $_GET['openid_identifier'] 
    65          * $_GET['policies'] 
     107                return PAGE_URL; 
     108        } 
     109 
     110        /** 
     111         * call api and try authentication 
    66112         */ 
    67113        public function tryAuthentication($openid, $policy_uris = array()) { 
     
    73119                // No auth request means we can't begin OpenID. 
    74120                if (!$auth_request) { 
    75                         $this->error = ("Authentication error; not a valid OpenID."); 
    76                         return; 
     121                        throw new Exception("Authentication error; not a valid OpenID."); 
    77122                } 
    78123 
     
    105150                        // message. 
    106151                        if (Auth_OpenID::isFailure($redirect_url)) { 
    107                                 $this->error = ("Could not redirect to server: " . $redirect_url->message); 
    108                                 return; 
     152                                throw new Exception("Could not redirect to server: " . $redirect_url->message); 
    109153                        } else { 
    110154                                // Send redirect. 
    111155                                header("Location: ".$redirect_url); 
     156                                exit; 
    112157                        } 
    113158                } else { 
     
    119164                        // otherwise, render the HTML. 
    120165                        if (Auth_OpenID::isFailure($form_html)) { 
    121                                 $this->error = ("Could not redirect to server: " . $form_html->message); 
    122                                 return; 
     166                                throw new Exception("Could not redirect to server: " . $form_html->message); 
    123167                        } else { 
    124                                 print $form_html; 
     168                         
     169                                // used by openid 2, formular and redirect are printed out 
     170                                echo $form_html; 
     171                                exit; 
    125172                        } 
    126173                } 
     
    128175 
    129176        /** 
    130          * 
     177         * got answer, save user 
    131178         */ 
    132179        public function finishAuthentication() { 
     
    157204 
    158205                        if ($response->endpoint->canonicalID) { 
    159                                 $StringUtil::encodeHTMLd_canonicalID = StringUtil::encodeHTML($response->endpoint->canonicalID); 
    160                                 $success .= '  (XRI CanonicalID: '.$StringUtil::encodeHTMLd_canonicalID.') '; 
     206                                $encoded_canonicalID = StringUtil::encodeHTML($response->endpoint->canonicalID); 
     207                                $success .= '  (XRI CanonicalID: '.$encoded_canonicalID.') '; 
    161208                        } 
    162209 
     
    168215 
    169216                                        foreach ($pape_resp->auth_policies as $uri) { 
    170                                                 $StringUtil::encodeHTMLd_uri = StringUtil::encodeHTML($uri); 
    171                                                 $success .= "<li><tt>$StringUtil::encodeHTMLd_uri</tt></li>"; 
     217                                                $encoded_uri = StringUtil::encodeHTML($uri); 
     218                                                $success .= "<li><tt>$encoded_uri</tt></li>"; 
    172219                                        } 
    173220 
     
    204251                } 
    205252        } 
    206          
    207253 
    208254        /** 
     
    213259         */ 
    214260        protected function getOpenIDEnabledUser($me) { 
    215                 $sql = "SELECT          utb.userID 
    216                         FROM            wcf".WCF_N."_user_to_openid utb 
    217                         WHERE           utb.openidID = ".intval($me['id'])." 
    218                         AND             utb.identifier = '".escapeString($me['identifier'])."'"; 
     261                $sql = "SELECT          userID 
     262                        FROM            wcf".WCF_N."_user_to_openid 
     263                        WHERE           openID = '".sha1($me['identifier'])."'"; 
    219264                $row = WCF::getDB()->getFirstRow($sql); 
    220265 
    221266                $user = $row ? new User($row['userID']) : null; 
    222                 return $user->userID ? $user : null; 
     267                return $user && $user->userID ? $user : null; 
    223268        } 
    224269 
     
    232277        protected function addOpenIDUser($me, $user) { 
    233278                $sql = "REPLACE INTO    wcf".WCF_N."_user_to_openid 
    234                                         (openidID, identifier, userID) 
    235                         VALUES          (".intval($me['id']).", '".escapeString($me['identifier'])."', ".intval($user->userID).")"; 
     279                                        (openID, userID) 
     280                        VALUES          ('".sha1($me['identifier'])."', ".intval($user->userID).")"; 
    236281 
    237282                return WCF::getDB()->sendQuery($sql); 
     
    243288        public function finishUser($me) { 
    244289 
     290                // take default username from hostname 
     291                if($me['name'] === null) { 
     292                        $host = parse_url($me['identifier'], PHP_URL_HOST)." ID #1"; 
     293                        $host = preg_replace("/^www\./", "", $host); 
     294                        $me['name'] = $host; 
     295                } 
     296                 
    245297                // openid permissions granted, does an login exist? 
    246298                $user = $this->getOpenIDEnabledUser($me); 
     
    258310                if($user) { 
    259311 
    260                         die('login temporary disabled'); 
    261312                        // UserLoginForm should not write cookie, since interfaces only support unhashed password 
    262313                        $this->eventObj->useCookies = 0; 
     
    307358                // get a valid username 
    308359                $username = $this->findUsername($me['name']); 
     360                 
     361                // take default email 
     362                if($me['email'] === null) { 
     363                        $me['email'] = sha1($me['identifier']).'@openid'; 
     364                } 
    309365 
    310366                // create new user 
  • openid/files/lib/page/OpenIDPage.class.php

    r1204 r1206  
    11<?php 
    22// wcf imports 
    3 require_once(WCF_DIR.'lib/page/AbstractPage.class.php'); 
     3require_once(WCF_DIR.'lib/form/UserLoginForm.class.php'); 
    44require_once(WCF_DIR.'lib/data/openid/OpenID.class.php'); 
    55 
     
    1212 * @package     de.easy-coding.wcf.openid 
    1313 */ 
    14 class OpenIDPage extends AbstractPage { 
     14class OpenIDPage extends UserLoginForm { 
    1515 
     16        /** 
     17         *  
     18         * @var string 
     19         */ 
    1620        protected $identifier; 
    1721         
     
    2327                 
    2428                $this->identifier = isset($_GET['identifier']) ? $_GET['identifier'] : null; 
    25         } 
    26          
    27         /** 
    28          * @see Page::readData() 
    29          */ 
    30         public function readData() { 
    31                 parent::readData(); 
    3229                 
    33                 $openid = new OpenID(); 
     30                $openid = new OpenID($this); 
    3431                 
    3532                if($this->identifier) { 
     
    3936                } 
    4037        } 
    41          
    42         /** 
    43          * @see Page::assignVariables() 
    44          */ 
    45         public function assignVariables() { 
    46                 parent::assignVariables(); 
    47                  
    48                  
    49                 WCF::getTPL()->assign(array( 
    50                         'entry' => $this->entry, 
    51                 )); 
    52         } 
    5338} 
    5439?> 
  • openid/files/lib/system/event/listener/UserLoginOpenIDListener.class.php

    r1204 r1206  
    3232                        return; 
    3333                } 
    34                  
    35                 // TODO: remove DEBUG 
    36                 if(!isset($_GET['openid'])) { 
    37                         return; 
    38                 } 
    3934 
    4035                $this->eventObj = $eventObj; 
  • openid/install.sql

    r1203 r1206  
    22CREATE TABLE wcf1_user_to_openid ( 
    33        userID INT(10) NOT NULL DEFAULT 0, 
    4         identifier VARCHAR(255) NOT NULL DEFAULT '', 
    5         openID INT(10) NOT NULL DEFAULT 0, 
     4        openID char(64) NOT NULL DEFAULT '', 
    65        UNIQUE(userID), 
    7         UNIQUE(identifier, openID) 
     6        UNIQUE(openID) 
    87) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
  • openid/package.xml

    r1204 r1206  
    66                <packagedescription language="de"><![CDATA[Dieses Plugin verbindet openid mit dem WCF. So ist z.B. ein Direktlogin via openid möglich.]]></packagedescription> 
    77                <packagedescription><![CDATA[This enables all users to login with their openid account.]]></packagedescription> 
    8                 <version>1.0.0 Beta 1</version> 
     8                <version>1.0.0 RC 1</version> 
    99                <date>DATE</date> 
    1010                <plugin>com.woltlab.wcf</plugin> 
  • openid/templates/openidLogin.tpl

    r1204 r1206  
     1<script type="text/javascript"> 
     2function openid(elem, msg) { 
     3        var x = prompt(msg); 
     4        if(x) { 
     5                elem.href = elem.href.replace(/\\1/, x); 
     6                return true; 
     7        } 
     8 
     9        return false; 
     10} 
     11</script> 
    112<div class="formElement"> 
    213        <div class="formField"> 
    3                 <form method="get" action="{$openid_url}"> 
    4                         <script type="text/javascript"> 
    5                         function openid(elem, msg) { 
    6                                 var x = prompt(msg); 
    7                                 if(x) { 
    8                                         elem.href = elem.href.replace(/\\1/, x); 
    9                                         return true; 
    10                                 } 
    11          
    12                                 return false; 
    13                         } 
    14                         </script> 
    15          
    16                         Sie können sich mit ihrem existieren Account bestimmter Anbieter bei uns authentifizieren.<br/> 
    17                         Das ganze funktioniert ÃŒber die s.g. OpenID Schnittstelle - es werden keine Zugangsdaten ausgetauscht.<br/> 
     14                <fieldset> 
     15                        <legend><img src="{icon}openidS.png{/icon}" alt="" />{lang}wcf.openid.login{/lang}</legend> 
     16{* 
     17                        <form method="get" action="{$openid_url}"> 
     18*} 
     19                        {lang}wcf.openid.login.description{/lang} 
    1820 
    1921                        <a href="{$openid_url}&identifier=https://www.google.com/accounts/o8/id">Google</a> 
    2022                        <a href="{$openid_url}&identifier=http://yahoo.com/">Yahoo</a> 
    2123                        <a href="{$openid_url}&identifier=http://openid.aol.com/\1" onclick="return openid(this)">AOL</a> 
     24                        <a href="{$openid_url}&identifier=http://www.flickr.com/">Flickr</a> 
    2225                        <a href="{$openid_url}&identifier=http://\1.myopenid.com/" onclick="return openid(this)">myOpenID</a> 
    23  
    24                         <p>... oder geben Sie ihre OpenID manuell ein:<br/> 
    25                         <input type="text" name="identifier" class="openid" value="https://www.google.com/accounts/o8/id" /></p> 
    26                         <p> 
    27                                 <input type="submit" value="Weiter &raquo;" /> 
    28                         </p> 
    29                 </form> 
     26                        <a href="{$openid_url}&identifier=http://technorati.com/people/technorati/\1" onclick="return openid(this)">Technorati</a> 
     27                        <a href="{$openid_url}&identifier=http://\1.wordpress.com/" onclick="return openid(this)">Wordpress</a> 
     28                        <a href="{$openid_url}&identifier=http://\1.blogspot.com/" onclick="return openid(this)">Blogspot</a> 
     29{* 
     30                                <p>... oder geben Sie ihre OpenID manuell ein:<br/> 
     31                                <input type="text" name="identifier" class="openid" value="https://www.google.com/accounts/o8/id" /></p> 
     32                                <p> 
     33                                        <input type="submit" value="Weiter &raquo;" /> 
     34                                </p> 
     35                        </form> 
     36*} 
     37                </fieldset> 
    3038        </div> 
    3139</div>