@@ -13,16 +13,16 @@ class OAuth {
13
13
public function __construct () {
14
14
$ this ->injector = Injector::getInstance ();
15
15
}
16
-
16
+
17
17
/**
18
- *
18
+ *
19
19
*/
20
20
public function setSslVerification ($ ssl_verification ) {
21
21
$ this ->injector ->ssl_verification = $ ssl_verification ;
22
22
}
23
-
23
+
24
24
/**
25
- *
25
+ *
26
26
*/
27
27
public function setSession (&$ session ) {
28
28
if (is_array ($ session )) {
@@ -91,14 +91,35 @@ public function generateStateToken() {
91
91
}
92
92
return $ unique_token ;
93
93
}
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
+ }
96
117
return $ credentials ;
97
118
}
98
-
119
+
99
120
public function auth ($ provider , $ options = array ()) {
121
+
100
122
// $options can contain code, credentials, or nothing. If nothing --> session call
101
-
102
123
if (!$ this ->initialized ) {
103
124
throw new NotInitializedException ('You must initialize the OAuth instance. ' );
104
125
}
@@ -116,8 +137,12 @@ public function auth($provider, $options = array()) {
116
137
'Content-Type ' => 'application/x-www-form-urlencoded '
117
138
)
118
139
));
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
+
121
146
if (isset ($ credentials ['provider ' ])) {
122
147
$ this ->injector ->session ['oauthio ' ]['auth ' ][$ credentials ['provider ' ]] = $ credentials ;
123
148
}
@@ -126,10 +151,10 @@ public function auth($provider, $options = array()) {
126
151
} else {
127
152
$ credentials = $ this ->injector ->session ['oauthio ' ]['auth ' ][$ provider ];
128
153
}
129
- $ credentials = $ this ->refreshCredentials ($ credentials );
130
- $ request = new Request ($ credentials );
154
+ $ credentials = $ this ->refreshCredentials ($ credentials, $ options [ ' force_refresh ' ] );
155
+ $ request_object = new Request ($ credentials );
131
156
132
- return $ request ;
157
+ return $ request_object ;
133
158
}
134
159
135
160
public function create ($ provider ) {
0 commit comments