UNPKG

3.69 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// Authorization code flow
65var uri = OAuth2.AuthCode.authorizeURL({ redirect_uri: 'http://localhost:3000/callback');
66var token = OAuth2.AuthCode.getToken({ code: 'authorization-code', redirectURI: 'http://localhost:3000/callback' }, callback);
67
68// Password credentials flow
69var token = OAuth2.Password.getToken({ username: 'username', 'password': 'password' }, callback);
70
71If the functions fails an error object is passed as first argument to the callback.
72The body response object is always the last argument.
73
74## Errors
75
76Exceptions are raised when a 4xx or 5xx status code is returned.
77
78```javascript
79OAtuh2.HTTPError
80```
81
82Through the error message attribute you can access the JSON representation.
83
84```javascript
85OAuth2.AuthCode.getToken(function(error, token) {
86 if (error) { console.log(error.message); }
87});
88```
89
90## Contributing
91
92Fork the repo on github and send a pull requests with topic branches. Do not forget to
93provide specs to your contribution.
94
95
96### Running specs
97
98* Fork and clone the repository (`dev` branch).
99* Run `npm install` for dependencies.
100* Run `make test` to execute all specs.
101* Run `make test-watch` to auto execute all specs when a file change.
102
103
104## Coding guidelines
105
106Follow [github](https://github.com/styleguide/) guidelines.
107
108
109## Feedback
110
111Use the [issue tracker](http://github.com/andreareginato/simple-oauth2/issues) for bugs.
112[Mail](mailto:andrea.reginato@.gmail.com) or [Tweet](http://twitter.com/andreareginato) us
113for any idea that can improve the project.
114
115
116## Links
117
118* [GIT Repository](http://github.com/andreareginato/simple-oauth2)
119* [Documentation](http://andreareginato.github.com/simple-oauth2)
120
121
122## Authors
123
124[Andrea Reginato](http://twitter.com/andreareginato)
125
126
127## Contributors
128
129Special thanks to the following people for submitting patches.
130
131
132## Changelog
133
134See [CHANGELOG](simple-oauth2/blob/master/CHANGELOG.md)
135
136
137## Copyright
138
139Copyright (c) 2013 [Lelylan](http://lelylan.com). See [LICENSE](simple-oauth2/blob/master/LICENSE.md) for details.