UNPKG

3.75 kBMarkdownView Raw
1# Simple OAuth2
2
3Node.js client library for [Oauth2](http://oauth.net/2/)
4
5
6## Requirements
7
8Node client library is tested against Node ~0.8.x
9
10
11## Installation
12
13Install the client library using [npm](http://npmjs.org/):
14
15 $ npm install simple-oath2
16
17Install the client library using git:
18
19 $ git clone git://github.com/andrearegianto/simple-oauth2.git
20 $ cd simple-oauth2
21 $ npm install
22
23
24## Documentation
25
26* [Simple Oauth2 Docs](git://andreareginato.github.com/simple-oauth2)
27
28
29## Getting started
30
31### Get the Access Token
32
33```javascript
34var credentials = { client: { id: 'client-id', secret: 'client-secret', site: 'https://example.org' } };
35var OAuth2 = require('simple-oauth2')(credentials);
36
37// Returns the URI where to redirect your app
38var redirect = Oauth2.AuthCode.authorizeURL({ redirectURI: 'http://localhost:3000/callback', scope: 'user', state: '02afe928b');
39// => "https://example.org/oauth/authorization?response_type=code&client_id=client_id&redirect_uri=http://localhost:3000/callback&scope=user&state=02afe928b"
40
41// Get the access token object
42vat params = { code: 'authorization-code', redirectURI: 'http://localhost:3000/callback' }
43OAuth2.AuthCode.getToken(params, function(error, result) {
44 // save the token
45})
46```
47
48### Refresh the Access Token
49
50```javascript
51
52token = OAuth2.AccessToken.create(json_token);
53if (token.expired()) {
54 token.refresh(function(error, refreshedToken) { token = refreshedToken; })
55}
56```
57
58### Authorization Grants
59
60Currently the Authorization Code and Resource Owner Password Credentials grant types
61have helper strategy classes that simplify client use. They are available via the #authCode
62and #password methods respectively.
63
64```javascript
65// Authorization code flow
66var uri = OAuth2.AuthCode.authorizeURL({ redirect_uri: 'http://localhost:3000/callback');
67var token = OAuth2.AuthCode.getToken({ code: 'authorization-code', redirectURI: 'http://localhost:3000/callback' }, callback);
68
69// Password credentials flow
70var token = OAuth2.Password.getToken({ username: 'username', 'password': 'password' }, callback);
71```
72
73If the functions fails an error object is passed as first argument to the callback.
74The body response object is always the last argument.
75
76
77## Errors
78
79Exceptions are raised when a 4xx or 5xx status code is returned.
80
81```javascript
82OAtuh2.HTTPError
83```
84
85Through the error message attribute you can access the JSON representation
86based on HTTP `status` and error `message`.
87
88```javascript
89OAuth2.AuthCode.getToken(function(error, token) {
90 if (error) { console.log(error.message); }
91});
92```
93
94## Contributing
95
96Fork the repo on github and send a pull requests with topic branches. Do not forget to
97provide specs to your contribution.
98
99
100### Running specs
101
102* Fork and clone the repository (`dev` branch).
103* Run `npm install` for dependencies.
104* Run `make test` to execute all specs.
105* Run `make test-watch` to auto execute all specs when a file change.
106
107
108## Coding guidelines
109
110Follow [github](https://github.com/styleguide/) guidelines.
111
112
113## Feedback
114
115Use the [issue tracker](http://github.com/andreareginato/simple-oauth2/issues) for bugs.
116[Mail](mailto:andrea.reginato@.gmail.com) or [Tweet](http://twitter.com/andreareginato) us
117for any idea that can improve the project.
118
119
120## Links
121
122* [GIT Repository](http://github.com/andreareginato/simple-oauth2)
123* [Documentation](http://andreareginato.github.com/simple-oauth2)
124
125
126## Authors
127
128[Andrea Reginato](http://twitter.com/andreareginato)
129
130
131## Contributors
132
133Special thanks to the following people for submitting patches.
134
135
136## Changelog
137
138See [CHANGELOG](simple-oauth2/blob/master/CHANGELOG.md)
139
140
141## Copyright
142
143Copyright (c) 2013 [Lelylan](http://lelylan.com). See [LICENSE](simple-oauth2/blob/master/LICENSE.md) for details.