8000 Added auto token refresh · oauth-io/sdk-php@e135280 · GitHub
[go: up one dir, main page]

Skip to content

Commit e135280

Browse files
committed
Added auto token refresh
1 parent 9badc3c commit e135280

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/OAuth_io/OAuth.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ class OAuth {
1313
public function __construct() {
1414
$this->injector = Injector::getInstance();
1515
}
16-
16+
1717
/**
18-
*
18+
*
1919
*/
2020
public function setSslVerification($ssl_verification) {
2121
$this->injector->ssl_verification = $ssl_verification;
2222
}
23-
23+
2424
/**
25-
*
25+
*
2626
*/
2727
public function setSession(&$session) {
2828
if (is_array($session)) {
@@ -91,14 +91,35 @@ public function generateStateToken() {
9191
}
9292
return $unique_token;
9393
}
94-
95-
public function refreshCredentials($credentials) {
10000
94+
95+
public function refreshCredentials($credentials, $force = false) {
96+
$date = new DateTime();
97+
if (isset($credentials['refresh_token']) && ((isset($credentials['expires']) && $date->getTimestamp() > $credentials['expires']) || $force)) {
98+
$request = $this->injector->getRequest();
99+
$response = $request->make_request(array(
100+
'method' => 'POST',
101+
'url' => $this->injector->config['oauthd_url'] . '/auth/refresh_token/' . $credentials['provider'],
102+
'body' => http_build_query(array(
103+
'token' => $options['refresh_token'],
104+
'key' => $this->injector->config['app_key'],
105+
'secret' => $this->injector->config['app_secret']
106+
)) ,
107+
'headers' => array(
108+
'Content-Type' => 'application/x-www-form-urlencoded'
109+
)
110+
));
111+
$refreshed = json_decode(json_encode($response->body) , true);
112+
113+
foreach ($refreshed as $k => $v) {
114+
$credentials[$k] = $v;
115+
}
116+
}
96117
return $credentials;
97118
}
98-
119+
99120
public function auth($provider, $options = array()) {
121+
100122
// $options can contain code, credentials, or nothing. If nothing --> session call
101-
102123
if (!$this->initialized) {
103124
throw new NotInitializedException('You must initialize the OAuth instance.');
104125
}
@@ -116,8 +137,12 @@ public function auth($provider, $options = array()) {
116137
'Content-Type' => 'application/x-www-form-urlencoded'
117138
)
118139
));
119-
$credentials = json_decode(json_encode($response->body), true);
120-
140+
$credentials = json_decode(json_encode($response->body) , true);
141+
if (isset($credentials['expires_in'])) {
142+
$date = new \DateTime();
F57D 143+
$credentials['expires'] = $date->getTimestamp() + $credentials->expires_in;
144+
}
145+
121146
if (isset($credentials['provider'])) {
122147
$this->injector->session['oauthio']['auth'][$credentials['provider']] = $credentials;
123148
}
@@ -126,10 +151,10 @@ public function auth($provider, $options = array()) {
126151
} else {
127152
$credentials = $this->injector->session['oauthio']['auth'][$provider];
128153
}
129-
$credentials = $this->refreshCredentials($credentials);
130-
$request = new Request($credentials);
154+
$credentials = $this->refreshCredentials($credentials, $options['force_refresh']);
155+
$request_object = new Request($credentials);
131156

132-
return $request;
157+
return $request_object;
133158
}
134159

135160
public function create($provider) {

src/OAuth_io/Request.php renamed to src/OAuth_io/RequestObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace OAuth_io;
33

4-
class Request {
4+
class RequestObject {
55

66
private $injector;
77
private $credentials;

0 commit comments

Comments
 (0)
0