From a5610610c9ad1f66e27517657fb3287f15e72bb3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zolt=C3=A1n?= <Zoltán@zolij-pc.lan>
Date: Thu, 17 Jul 2014 23:37:50 +0200
Subject: [PATCH] refresh token segitsegevel access token frissitese

---
 php/AuthSCHClient.class.php | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/php/AuthSCHClient.class.php b/php/AuthSCHClient.class.php
index b280caa..d20a128 100644
--- a/php/AuthSCHClient.class.php
+++ b/php/AuthSCHClient.class.php
@@ -30,6 +30,8 @@ class AuthSCHClient {
             } else {
                 // load tokendata from session
                 $this->tokens = unserialize($_SESSION['tokens']);
+                if($this->tokens->lastUpdate + 3600 < time())
+                    $this->reauthenticate();
             }
         } else {
             $this->tokens = $tokens;
@@ -96,6 +98,20 @@ class AuthSCHClient {
 
     }
     
+    private function reauthenticate() {
+        $data = array(
+            'grant_type'=>'refresh_token',
+            'refresh_token'=>$this->tokens->refresh_token,
+        );
+        
+        $ch = $this->curlExec("oauth2/token", $data);
+        $response = json_decode($ch);
+        if($tokens === null || !isset($tokens->access_token) || empty($tokens->access_token))
+                throw new Exception ("invalid token data");
+        
+        $this->tokens->access_token = $tokens->access_token;
+    }
+    
     public function getData() {
         return json_decode(file_get_contents(self::$host . 'api/profile/?access_token=' . $this->tokens->access_token));
     }
-- 
GitLab