Update GoogleAuth to use the new API
authorTim Düsterhus <duesterhus@woltlab.com>
Sun, 4 May 2014 19:09:51 +0000 (21:09 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 5 May 2014 18:19:25 +0000 (20:19 +0200)
wcfsetup/install/files/lib/action/GoogleAuthAction.class.php
wcfsetup/install/files/lib/form/RegisterForm.class.php

index 3cb3fa1093fcafb92153fe3a31692e3df93345ac..e5708e3543ac756a720e12b319358fabb53dbea5 100644 (file)
@@ -68,7 +68,7 @@ class GoogleAuthAction extends AbstractAction {
                        
                        try {
                                // fetch userdata
-                               $request = new HTTPRequest('https://www.googleapis.com/oauth2/v1/userinfo');
+                               $request = new HTTPRequest('https://www.googleapis.com/plus/v1/people/me');
                                $request->addHeader('Authorization', 'Bearer '.$data['access_token']);
                                $request->execute();
                                $reply = $request->getReply();
@@ -114,15 +114,17 @@ class GoogleAuthAction extends AbstractAction {
                                
                                // save data for connection
                                if (WCF::getUser()->userID) {
-                                       WCF::getSession()->register('__googleUsername', $userData['name']);
+                                       WCF::getSession()->register('__googleUsername', $userData['displayName']);
                                        WCF::getSession()->register('__googleData', $userData);
                                        
                                        HeaderUtil::redirect(LinkHandler::getInstance()->getLink('AccountManagement').'#3rdParty');
                                }
                                // save data and redirect to registration
                                else {
-                                       WCF::getSession()->register('__username', $userData['name']);
-                                       if (isset($userData['email'])) WCF::getSession()->register('__email', $userData['email']);
+                                       WCF::getSession()->register('__username', $userData['displayName']);
+                                       if (isset($userData['emails'][0]['value'])) {
+                                               WCF::getSession()->register('__email', $userData['emails'][0]['value']);
+                                       }
                                        
                                        WCF::getSession()->register('__googleData', $userData);
                                        
@@ -145,7 +147,7 @@ class GoogleAuthAction extends AbstractAction {
                // start auth by redirecting to google
                $token = StringUtil::getRandomID();
                WCF::getSession()->register('__googleInit', $token);
-               HeaderUtil::redirect("https://accounts.google.com/o/oauth2/auth?client_id=".rawurlencode(StringUtil::trim(GOOGLE_PUBLIC_KEY)). "&redirect_uri=".rawurlencode($callbackURL)."&state=".$token."&scope=https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email&response_type=code");
+               HeaderUtil::redirect("https://accounts.google.com/o/oauth2/auth?client_id=".rawurlencode(StringUtil::trim(GOOGLE_PUBLIC_KEY)). "&redirect_uri=".rawurlencode($callbackURL)."&state=".$token."&scope=profile+email&response_type=code");
                $this->executed();
                exit;
        }
index 925b1f1a52f0e4938e940e1b05958b59fde98650..81f9c98dd1f63475be04aebc4366946175ec3d31 100644 (file)
@@ -370,7 +370,7 @@ class RegisterForm extends UserAddForm {
                                                
                                                WCF::getSession()->unregister('__googleData');
                                                
-                                               if (isset($googleData['email']) && $googleData['email'] == $this->email) {
+                                               if (isset($googleData['emails'][0]['value']) && $googleData['emails'][0]['value'] == $this->email) {
                                                        $registerVia3rdParty = true;
                                                }
                                                
@@ -385,6 +385,18 @@ class RegisterForm extends UserAddForm {
                                                        }
                                                }
                                                if (isset($googleData['birthday'])) $saveOptions[User::getUserOptionID('birthday')] = $googleData['birthday'];
+                                               if (isset($googleData['placesLived'])) {
+                                                       // save primary location
+                                                       $saveOptions[User::getUserOptionID('location')] = current(array_map(
+                                                               function ($element) { return $element['value']; },
+                                                               array_filter($googleData['placesLived'], function ($element) { return isset($element['primary']) && $element['primary']; })
+                                                       ));
+                                               }
+                                               
+                                               // avatar
+                                               if (isset($googleData['image']['url'])) {
+                                                       $avatarURL = $googleData['image']['url'];
+                                               }
                                        }
                                break;
                        }