1 | # Simple OAuth2
|
2 |
|
3 | Node.js client library for [Oauth2](http://oauth.net/2/)
|
4 |
|
5 |
|
6 | ## Requirements
|
7 |
|
8 | Node client library is tested against Node ~0.8.x
|
9 |
|
10 |
|
11 | ## Installation
|
12 |
|
13 | Install the client library using [npm](http://npmjs.org/):
|
14 |
|
15 | $ npm install simple-oath2
|
16 |
|
17 | Install 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
|
34 | var credentials = { client: { id: 'client-id', secret: 'client-secret', site: 'https://example.org' } };
|
35 | var OAuth2 = require('simple-oauth2')(credentials);
|
36 |
|
37 | // Returns the URI where to redirect your app
|
38 | var 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
|
42 | vat params = { code: 'authorization-code', redirectURI: 'http://localhost:3000/callback' }
|
43 | OAuth2.AuthCode.getToken(params, function(error, result) {
|
44 | // save the token
|
45 | })
|
46 | ```
|
47 |
|
48 | ### Refresh the Access Token
|
49 |
|
50 | ```javascript
|
51 |
|
52 | token = OAuth2.AccessToken.create(json_token);
|
53 | if (token.expired()) {
|
54 | token.refresh(function(error, refreshedToken) { token = refreshedToken; })
|
55 | }
|
56 | ```
|
57 |
|
58 | ### Authorization Grants
|
59 |
|
60 | Currently the Authorization Code and Resource Owner Password Credentials grant types
|
61 | have helper strategy classes that simplify client use. They are available via the #authCode
|
62 | and #password methods respectively.
|
63 |
|
64 | ```javascript
|
65 | // Authorization code flow
|
66 | var uri = OAuth2.AuthCode.authorizeURL({ redirect_uri: 'http://localhost:3000/callback');
|
67 | var token = OAuth2.AuthCode.getToken({ code: 'authorization-code', redirectURI: 'http://localhost:3000/callback' }, callback);
|
68 |
|
69 | // Password credentials flow
|
70 | var token = OAuth2.Password.getToken({ username: 'username', 'password': 'password' }, callback);
|
71 | ```
|
72 |
|
73 | If the functions fails an error object is passed as first argument to the callback.
|
74 | The body response object is always the last argument.
|
75 |
|
76 |
|
77 | ## Errors
|
78 |
|
79 | Exceptions are raised when a 4xx or 5xx status code is returned.
|
80 |
|
81 | ```javascript
|
82 | OAtuh2.HTTPError
|
83 | ```
|
84 |
|
85 | Through the error message attribute you can access the JSON representation
|
86 | based on HTTP `status` and error `message`.
|
87 |
|
88 | ```javascript
|
89 | OAuth2.AuthCode.getToken(function(error, token) {
|
90 | if (error) { console.log(error.message); }
|
91 | });
|
92 | ```
|
93 |
|
94 | ## Contributing
|
95 |
|
96 | Fork the repo on github and send a pull requests with topic branches. Do not forget to
|
97 | provide 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 |
|
110 | Follow [github](https://github.com/styleguide/) guidelines.
|
111 |
|
112 |
|
113 | ## Feedback
|
114 |
|
115 | Use 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
|
117 | for 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 |
|
133 | Special thanks to the following people for submitting patches.
|
134 |
|
135 |
|
136 | ## Changelog
|
137 |
|
138 | See [CHANGELOG](simple-oauth2/blob/master/CHANGELOG.md)
|
139 |
|
140 |
|
141 | ## Copyright
|
142 |
|
143 | Copyright (c) 2013 [Lelylan](http://lelylan.com). See [LICENSE](simple-oauth2/blob/master/LICENSE.md) for details.
|