From 82492dc023269bdb62a983501baee59aaf7577d0 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 17 Jan 2017 20:05:10 +0100
Subject: [PATCH] Fix oauth2 token generation for Google. The refresh token was
 not received.

---
 .../core/modules/oauth/google_oauthcallback.php   |  3 +++
 htdocs/core/modules/printing/printgcp.modules.php |  4 +++-
 htdocs/includes/OAuth/OAuth2/Service/Google.php   | 15 ++++++++++++++-
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/htdocs/core/modules/oauth/google_oauthcallback.php b/htdocs/core/modules/oauth/google_oauthcallback.php
index c69493ed9a5..001db7320a0 100644
--- a/htdocs/core/modules/oauth/google_oauthcallback.php
+++ b/htdocs/core/modules/oauth/google_oauthcallback.php
@@ -84,8 +84,11 @@ if ($action != 'delete' && empty($requestedpermissionsarray))
 $apiService = $serviceFactory->createService('Google', $credentials, $storage, $requestedpermissionsarray);
 
 // access type needed to have oauth provider refreshing token
+// alos note that a refresh token is sent only after a prompt
 $apiService->setAccessType('offline');
 
+$apiService->setApprouvalPrompt('force');
+
 $langs->load("oauth");
 
 
diff --git a/htdocs/core/modules/printing/printgcp.modules.php b/htdocs/core/modules/printing/printgcp.modules.php
index baa5106ae93..ebcb9f3cebf 100644
--- a/htdocs/core/modules/printing/printgcp.modules.php
+++ b/htdocs/core/modules/printing/printgcp.modules.php
@@ -450,6 +450,7 @@ class printing_printgcp extends PrintingDriver
         }
         $responsedata = json_decode($response, true);
         //$html .= '<pre>'.print_r($responsedata,true).'</pre>';
+        $html .= '<div class="div-table-responsive">';
         $html .= '<table width="100%" class="noborder">';
         $html .= '<tr class="liste_titre">';
         $html .= '<td>'.$langs->trans("Id").'</td>';
@@ -483,10 +484,11 @@ class printing_printgcp extends PrintingDriver
         else
         {
                 $html .= '<tr '.$bc[$var].'>';
-                $html .= '<td colspan="6" class="opacitymedium">'.$langs->trans("None").'</td>';
+                $html .= '<td colspan="7" class="opacitymedium">'.$langs->trans("None").'</td>';
                 $html .= '</tr>';
         }
         $html .= '</table>';
+        $html .= '</div>';
         
         $this->resprint = $html;
         
diff --git a/htdocs/includes/OAuth/OAuth2/Service/Google.php b/htdocs/includes/OAuth/OAuth2/Service/Google.php
index a8bb44d8238..0d49609dccb 100644
--- a/htdocs/includes/OAuth/OAuth2/Service/Google.php
+++ b/htdocs/includes/OAuth/OAuth2/Service/Google.php
@@ -140,12 +140,25 @@ class Google extends AbstractService
         $this->accessType = $accessType;
     }
 
+    // LDR CHANGE Add approval_prompt to force the prompt if value is set to 'force' so it force return of a "refresh token" in addition to "standard token"
+    public $approvalPrompt='auto';
+    public function setApprouvalPrompt($prompt)
+    {
+        if (!in_array($prompt, array('auto', 'force'), true)) {
+            // @todo Maybe could we rename this exception
+            throw new InvalidAccessTypeException('Invalid approuvalPrompt, expected either auto or force.');
+        }
+        $this->approvalPrompt = $prompt;
+    }
+    
     /**
      * {@inheritdoc}
      */
     public function getAuthorizationEndpoint()
     {
-        return new Uri('https://accounts.google.com/o/oauth2/auth?access_type=' . $this->accessType);
+        // LDR CHANGE Add approval_prompt to force the prompt if value is set to 'force' so it force return of a "refresh token" in addition to "standard token"
+        //return new Uri('https://accounts.google.com/o/oauth2/auth?access_type='.$this->accessType);
+        return new Uri('https://accounts.google.com/o/oauth2/auth?'.($this->approvalPrompt?'approval_prompt='.$this->approvalPrompt.'&':'').'access_type='.$this->accessType);
     }
 
     /**
-- 
GitLab