You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+89-20Lines changed: 89 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -7,9 +7,17 @@ This SDK allows you to use OAuth.io's server-side flow from a PHP backend, to ha
7
7
8
8
You can use it with one of our front-end SDKs ([JavaScript][1], [PhoneGap][2], [iOs][3], [Android][4]), which will handle the user input for the OAuth flow.
9
9
10
-
This SDK is still under heavy development and some of the features described below may not work yet. You can get nightlies from the [develop branch](https://github.com/oauth-io/sdk-php/tree/develop) on the SDK's github page.
10
+
The current version of the SDK is `0.2.0`. Older versions are deprecated.
11
11
12
-
A release will be posted soon.
12
+
You can also get nightlies by checking out our `develop` branch.
13
+
14
+
Features
15
+
--------
16
+
17
+
- Server-side OAuth authentication flow
18
+
- Requests to API from the backend
19
+
- Unified user information (`.me()` method) requests when available
20
+
- Access token renewal with the refresh_token when available
13
21
14
22
Common use-Case
15
23
---------------
@@ -30,15 +38,17 @@ To authenticate a user, the flow follows these steps :
30
38
- oauth.io responds with the access_token, that you can then store on your backend as long as it's valid
31
39
- You can then make requests to the API using that access token, directly from your backend
32
40
41
+
As of `0.2.0` it is possible to get an automatically refreshed access token when a refresh token is available.
42
+
33
43
Installation
34
44
------------
35
45
36
-
You will soon be able to install it through Composer by adding the following dependency to your composer.json :
46
+
You can install it through Composer by adding the following dependency to your composer.json :
37
47
38
48
```json
39
49
"require": {
40
50
...
41
-
"oauth-io/oauth": "0.1.0"
51
+
"oauth-io/oauth": "0.2.0"
42
52
...
43
53
},
44
54
```
@@ -126,24 +136,36 @@ You have to give this token to your front-end, where you can show the user a pop
126
136
127
137
**Auth the user**
128
138
129
-
To be able to make requests to a provider's API using its access token, you have to call the `auth(code)` method. The code is retrieved from OAuth.io through the from the front-end SDK (see further down). You need to create an endpoint to allow the front-end to send it to the backend.
139
+
To be able to make requests to a provider's API using its access token, you have to call the `auth(provider, options)` method first. This method creates a request object from either a code you got from the front-end SDK (for the first time authentication), the session (if the user was authenticated during the same session), or a credentials array that you saved earlier.
130
140
131
-
Once you have that code, you can call the method like this :
141
+
To get a request object from a code (which automatically fills up the session for further use in other endpoints), you can do like this :
`$result` is an array containing the access token, which you can use your own way if you want, or thanks to the SDK's request system (see further down).
149
+
`$request_object` is an object that allows you to perform requests (see further down to learn how to), and that contains the user's credentials.
138
150
139
-
**Retrieving the code from the front-end**
151
+
You can get the credentials array if you need to save them for later use (or for a cron) like this :
152
+
153
+
```php
154
+
$credentials = $request_object->getCredentials();
155
+
```
156
+
157
+
The `$credentials` array contains the access token, refresh token and other information returned by the provider.
158
+
159
+
**Retrieving a code from the front-end**
140
160
141
161
```JavaScript
142
162
//In the front end, using the JavaScript SDK :
143
163
144
164
OAuth.initialize('your_key');
145
165
OAuth.popup('a_provider', {
146
-
state:'the_token_retrieved_from_your_backend'
166
+
// The state token you got from the backend
167
+
// through $oauth->generateStateToken():
168
+
state:'state_token'
147
169
})
148
170
.done(function (r) {
149
171
//You need to give r.code to your backend
@@ -162,15 +184,7 @@ OAuth.popup('a_provider', {
162
184
163
185
**Making requests to the API**
164
186
165
-
Once the user is authenticated, you can create a request object from the SDK `create('provider')` method :
You can refer to the OAuth.io me() feature to get more information about the fields that are returned by this method.
198
212
213
+
**Using the session**
214
+
215
+
Usually, you'll want to make calls to the API several times while the user is connected to your app. Once you've authenticated the user once with a code, the session is automatically configured to work with the SDK.
216
+
217
+
Thus, you just need to do this to get a request object:
218
+
219
+
```php
220
+
$request_object = $oauth->auth('the_provider');
221
+
```
222
+
223
+
**Saving credentials**
224
+
225
+
You can also save the user's credentials to make requests in a cron. You can get the credentials array from a request object like this :
226
+
227
+
```php
228
+
$credentials = $request_object->getCredentials();
229
+
// Here save the $credentials array for later use
230
+
```
231
+
232
+
233
+
Then, when you want to reuse these credentials, you can rebuild a $request_object from them:
If a refresh token is available and the access token is expired, the `auth` method will automatically use that refresh token to get a new access token.
244
+
245
+
You can force the renewal by passing the `force_refresh` field in the options array:
@@ -206,7 +266,16 @@ Please discuss issues and features on Github Issues. We'll be happy to answer to
206
266
207
267
**Pull requests**
208
268
209
-
You are welcome to fork this SDK and to make pull requests on Github. We'll review each of them, and integrate in a future release if they are relevant.
269
+
You are welcome to fork and make pull requests. We appreciate the time you spend working on this project and we will be happy to review your code and merge it if it brings nice improvements :)
270
+
271
+
If you want to do a pull request, please mind these simple rules :
272
+
273
+
-*One feature per pull request*
274
+
-*Write clear commit messages*
275
+
-*Unit test your feature* : if it's a bug fix for example, write a test that proves the bug exists and that your fix resolves it.
276
+
-*Write a clear description of the pull request*
277
+
278
+
If you do so, we'll be able to merge your pull request more quickly :)
210
279
211
280
The SDK is written as a Composer module. You can install its dependencies like this :
0 commit comments