diff --git a/php/AuthSCHClient.class.php b/php/AuthSCHClient.class.php index b280caa20875a1e609ed5ae5844aa245243e6d26..d20a1281b80866e72195c816c75889e5d701c57e 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)); }