UNPKG

8.08 kBMarkdownView Raw
1# Simple OAuth2
2
3Node.js client library for [Oauth2](http://oauth.net/2/).
4
5OAuth2 lets users grant the access to the desired resources to third party applications,
6giving them the possibility to enable and disable those accesses whenever they want.
7
8Simple OAuth2 supports the following flows.
9
10* Authorization Code Flow (for apps with servers that can store persistent information).
11* Password Credentials (when previous flow can't be used or during development).
12* [Client Credentials Flow](http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.4) (the client can request an access token using only its client credentials)
13
14## Requirements
15
16Node client library is tested against Node ~0.8.x
17
18
19## Installation
20
21Install the client library using [npm](http://npmjs.org/):
22
23 $ npm install simple-oauth2
24
25Install the client library using git:
26
27 $ git clone git://github.com/andrearegianto/simple-oauth2.git
28 $ cd simple-oauth2
29 $ npm install
30
31
32## Getting started
33
34### Express and Github example
35
36```javascript
37var express = require('express'),
38 app = express();
39
40var OAuth2 = require('simple-oauth2')({
41 clientID: CLIENT_ID,
42 clientSecret: CLIENT_SECRET,
43 site: 'https://github.com/login',
44 tokenPath: '/oauth/access_token'
45});
46
47// Authorization uri definition
48var authorization_uri = OAuth2.AuthCode.authorizeURL({
49 redirect_uri: 'http://localhost:3000/callback',
50 scope: 'notifications',
51 state: '3(#0/!~'
52});
53
54// Initial page redirecting to Github
55app.get('/auth', function (req, res) {
56 res.redirect(authorization_uri);
57});
58
59// Callback service parsing the authorization token and asking for the access token
60app.get('/callback', function (req, res) {
61 var code = req.query.code;
62 console.log('/callback');
63 OAuth2.AuthCode.getToken({
64 code: code,
65 redirect_uri: 'http://localhost:3000/callback'
66 }, saveToken);
67
68 function saveToken(error, result) {
69 if (error) { console.log('Access Token Error', error.message); }
70 token = OAuth2.AccessToken.create(result);
71 }
72});
73
74app.get('/', function (req, res) {
75 res.send('Hello World');
76});
77
78app.listen(3000);
79
80console.log('Express server started on port 3000');
81```
82
83Credits to [@lazybean](https://github.com/lazybean)
84
85### Authorization Code flow
86
87The Authorization Code flow is made up from two parts. At first your application asks to
88the user the permission to access their data. If the user approves the OAuth2 server sends
89to the client an authorization code. In the second part, the client POST the authorization code
90along with its client secret to the Lelylan in order to get the access token.
91
92```javascript
93// Set the client credentials and the OAuth2 server
94var credentials = {
95 clientID: '<client-id>',
96 clientSecret: '<client-secret>',
97 site: 'https://api.oauth.com'
98};
99
100// Initialize the OAuth2 Library
101var OAuth2 = require('simple-oauth2')(credentials);
102
103// Authorization OAuth2 URI
104var authorization_uri = OAuth2.AuthCode.authorizeURL({
105 redirect_uri: 'http://localhost:3000/callback',
106 scope: '<scope>',
107 state: '<state>'
108});
109
110// Redirect example using Express (see http://expressjs.com/api.html#res.redirect)
111res.redirect(authorization_uri);
112
113// Get the access token object (the authorization code is given from the previous step).
114var token;
115OAuth2.AuthCode.getToken({
116 code: '<code>',
117 redirect_uri: 'http://localhost:3000/callback'
118}, saveToken);
119
120// Save the access token
121function saveToken(error, result) {
122 if (error) { console.log('Access Token Error', error.message); }
123 token = OAuth2.AccessToken.create(result);
124});
125```
126
127
128### Password Credentials Flow
129
130This flow is suitable when the resource owner has a trust relationship with the
131client, such as its computer operating system or a highly privileged application.
132Use this flow only when other flows are not viable or when you need a fast way to
133test your application.
134
135```javascript
136// Get the access token object.
137var token;
138OAuth2.Password.getToken({
139 username: 'username',
140 password: 'password' 
141}, saveToken);
142
143// Save the access token
144function saveToken(error, result) {
145 if (error) { console.log('Access Token Error', error.message); }
146 token = OAuth2.AccessToken.create(result);
147});
148```
149
150### Client Credentials Flow
151
152This flow is suitable when client is requesting access to the protected resources under its control.
153
154```javascript
155// Get the access token object.
156var token;
157var credentials = {
158 clientID: '<client-id>',
159 clientSecret: '<client-secret>',
160 site: 'https://api.oauth.com'
161};
162
163// Initialize the OAuth2 Library
164var OAuth2 = require('simple-oauth2')(credentials);
165
166// Get the access token object for the client
167OAuth2.Client.getToken(saveToken);
168
169// Save the access token
170function saveToken(error, result) {
171 if (error) { console.log('Access Token Error', error.message); }
172 token = OAuth2.AccessToken.create(result);
173});
174```
175
176### Access Token object
177
178When a token expires we need to refresh it. Simple OAuth2 offers the
179AccessToken class that add a couple of useful methods to refresh the
180access token when it is expired.
181
182```javascript
183// Sample of a JSON access token (you got it through previous steps)
184var token = {
185 'access_token': '<access-token>',
186 'refresh_token': '<refresh-token>',
187 'expires_in': '7200'
188};
189
190// Create the access token wrapper
191var token = OAuth2.AccessToken.create(token);
192
193// Check if the token is expired. If expired it is refreshed.
194if (token.expired()) {
195 token.refresh(function(error, result) {
196 token = result;
197 })
198}
199```
200
201
202### Errors
203
204Exceptions are raised when a 4xx or 5xx status code is returned.
205
206 HTTPError
207
208Through the error message attribute you can access the JSON representation
209based on HTTP `status` and error `message`.
210
211```javascript
212OAuth2.AuthCode.getToken(function(error, token) {
213 if (error) { console.log(error.message); }
214});
215// => { "status": "401", "message": "Unauthorized" }
216```
217
218
219### Configurations
220
221Simple OAuth2 accepts an object with the following valid params.
222
223* `clientID` - Required registered Client ID.
224* `clientSecret` - Required registered Client secret.
225* `site` - Required OAuth2 server site.
226* `authorizationPath` - Authorization path for the OAuth2 server. Defaults to `/oauth/authorize`.
227* `tokenPath` - Access token path for the OAuth2 server. Defaults to `/oauth/token`.
228* `useBasicAuthorizationHeader` - Whether or not the `Authorization: Basic ...` header is set on the request.
229Defaults to `true`.
230* `clientSecretParameterName` - Parameter name for the client secret. Defaults to `client_secret`.
231
232```javascript
233// Set the configuration settings
234var credentials = {
235 clientID: '<client-id>',
236 clientSecret: '<client-secret>',
237 site: 'https://www.oauth2.com',
238 authorizationPath: '/oauth2/authorization',
239 tokenPath: '/oauth2/access_token'
240};
241
242// Initialize the OAuth2 Library
243var OAuth2 = require('simple-oauth2')(credentials);
244```
245
246
247## Contributing
248
249Fork the repo on github and send a pull requests with topic branches. Do not forget to
250provide specs to your contribution.
251
252
253### Running specs
254
255* Fork and clone the repository (`dev` branch).
256* Run `npm install` for dependencies.
257* Run `make test` to execute all specs.
258* Run `make test-watch` to auto execute all specs when a file change.
259
260
261## Coding guidelines
262
263Follow [github](https://github.com/styleguide/) guidelines.
264
265
266## Feedback
267
268Use the [issue tracker](http://github.com/andreareginato/simple-oauth2/issues) for bugs.
269[Mail](mailto:andrea.reginato@.gmail.com) or [Tweet](http://twitter.com/andreareginato) us
270for any idea that can improve the project.
271
272
273## Links
274
275* [GIT Repository](http://github.com/andreareginato/simple-oauth2)
276* [Documentation](http://andreareginato.github.com/simple-oauth2)
277
278
279## Authors
280
281[Andrea Reginato](http://twitter.com/andreareginato)
282
283
284## Contributors
285
286Special thanks to the following people for submitting patches.
287
288
289## Changelog
290
291See [CHANGELOG](https://github.com/andreareginato/simple-oauth2/blob/master/CHANGELOG.md)
292
293
294## Copyright
295
296Copyright (c) 2013 [Lelylan](http://lelylan.com).
297
298This project is released under the [MIT License](http://opensource.org/licenses/MIT).