Pull email when registering through Twitter
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 3 Apr 2017 14:14:46 +0000 (16:14 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 3 Apr 2017 14:15:17 +0000 (16:15 +0200)
Closes #2212

wcfsetup/install/files/lib/action/TwitterAuthAction.class.php
wcfsetup/install/files/lib/form/RegisterForm.class.php

index 8164c5e389f9c569de5749e2cbd8dfba27d2cc45..aa8f8711945532d4697fff855471c39950b0255d 100644 (file)
@@ -112,7 +112,23 @@ class TwitterAuthAction extends AbstractAction {
                                        // fetch user data
                                        $twitterData = null;
                                        try {
-                                               $request = new HTTPRequest('https://api.twitter.com/1.1/users/show.json?screen_name=' . $data['screen_name']);
+                                               $oauthHeader = [
+                                                       'oauth_consumer_key' => StringUtil::trim(TWITTER_PUBLIC_KEY),
+                                                       'oauth_nonce' => StringUtil::getRandomID(),
+                                                       'oauth_signature_method' => 'HMAC-SHA1',
+                                                       'oauth_timestamp' => TIME_NOW,
+                                                       'oauth_version' => '1.0',
+                                                       'oauth_token' => $data['oauth_token']
+                                               ];
+                                               $getData = [
+                                                       'include_email' => 'true',
+                                                       'skip_status' => 'true'
+                                               ];
+                                               $signature = $this->createSignature('https://api.twitter.com/1.1/account/verify_credentials.json', array_merge($oauthHeader, $getData), $data['oauth_token_secret'], 'GET');
+                                               $oauthHeader['oauth_signature'] = $signature;
+                                               
+                                               $request = new HTTPRequest('https://api.twitter.com/1.1/account/verify_credentials.json?skip_status=true&include_email=true');
+                                               $request->addHeader('Authorization', 'OAuth '.$this->buildOAuthHeader($oauthHeader));
                                                $request->execute();
                                                $reply = $request->getReply();
                                                $twitterData = json_decode($reply['body'], true);
@@ -120,6 +136,7 @@ class TwitterAuthAction extends AbstractAction {
                                        catch (SystemException $e) { /* ignore errors */ }
                                        
                                        WCF::getSession()->register('__username', $data['screen_name']);
+                                       if (isset($twitterData['email'])) WCF::getSession()->register('__email', $twitterData['email']);
                                        
                                        if ($twitterData !== null) $data = $twitterData;
                                        WCF::getSession()->register('__twitterData', $data);
@@ -206,7 +223,7 @@ class TwitterAuthAction extends AbstractAction {
         * @param       string $tokenSecret
         * @return      string
         */
-       public function createSignature($url, array $parameters, $tokenSecret = '') {
+       public function createSignature($url, array $parameters, $tokenSecret = '', $method = 'POST') {
                $tmp = [];
                foreach ($parameters as $key => $val) {
                        $tmp[rawurlencode($key)] = rawurlencode($val);
@@ -220,7 +237,7 @@ class TwitterAuthAction extends AbstractAction {
                        $parameterString .= $key.'='.$val;
                }
                
-               $base = "POST&".rawurlencode($url)."&".rawurlencode($parameterString);
+               $base = $method."&".rawurlencode($url)."&".rawurlencode($parameterString);
                $key = rawurlencode(StringUtil::trim(TWITTER_PRIVATE_KEY)).'&'.rawurlencode($tokenSecret);
                
                return base64_encode(hash_hmac('sha1', $base, $key, true));
index f449065831111aa89397ac56d772fc4a6362a22b..7186362f5e32fec5f411dd7a482bd20f1e78ce96 100644 (file)
@@ -326,12 +326,21 @@ class RegisterForm extends UserAddForm {
                                        // Twitter
                                        if (WCF::getSession()->getVar('__twitterData')) {
                                                $twitterData = WCF::getSession()->getVar('__twitterData');
-                                               $this->additionalFields['authData'] = 'twitter:'.$twitterData['user_id'];
+                                               $this->additionalFields['authData'] = 'twitter:'.(isset($twitterData['id']) ? $twitterData['id'] : $twitterData['user_id']);
                                                
                                                WCF::getSession()->unregister('__twitterData');
                                                
+                                               if (WCF::getSession()->getVar('__email') && WCF::getSession()->getVar('__email') == $this->email) {
+                                                       $registerVia3rdParty = true;
+                                               }
+                                               
                                                if (isset($twitterData['description']) && User::getUserOptionID('aboutMe') !== null) $saveOptions[User::getUserOptionID('aboutMe')] = $twitterData['description'];
                                                if (isset($twitterData['location']) && User::getUserOptionID('location') !== null) $saveOptions[User::getUserOptionID('location')] = $twitterData['location'];
+                                               
+                                               // avatar
+                                               if (isset($twitterData['profile_image_url'])) {
+                                                       $avatarURL = $twitterData['profile_image_url'];
+                                               }
                                        }
                                break;
                                case 'facebook':