
Simple OAuth2
Node.js client library for Oauth2.
OAuth2 lets users grant the access to the desired resources to third party applications,
giving them the possibility to enable and disable those accesses whenever they want.
Simple OAuth2 supports the following flows.
- Authorization Code Flow (for apps with servers that can store persistent information).
- Password Credentials (when previous flow can't be used or during development).
- Client Credentials Flow (the client can request an access token using only its client credentials)
Requirements
Node client library is tested against Node ~0.8.x
Installation
Install the client library using npm:
$ npm install simple-oauth2
Install the client library using git:
$ git clone git://github.com/andreareginato/simple-oauth2.git
$ cd simple-oauth2
$ npm install
Getting started
Express and Github example
var express = require('express'),
app = express();
var oauth2 = require('simple-oauth2')({
clientID: CLIENT_ID,
clientSecret: CLIENT_SECRET,
site: 'https://github.com/login',
tokenPath: '/oauth/access_token',
authorizationPath: '/oauth/authorize'
});
var authorization_uri = oauth2.authCode.authorizeURL({
redirect_uri: 'http://localhost:3000/callback',
scope: 'notifications',
state: '3(#0/!~'
});
app.get('/auth', function (req, res) {
res.redirect(authorization_uri);
});
app.get('/callback', function (req, res) {
var code = req.query.code;
console.log('/callback');
oauth2.authCode.getToken({
code: code,
redirect_uri: 'http://localhost:3000/callback'
}, saveToken);
function saveToken(error, result) {
if (error) { console.log('Access Token Error', error.message); }
token = oauth2.accessToken.create(result);
}
});
app.get('/', function (req, res) {
res.send('Hello<br><a href="/auth">Log in with Github</a>');
});
app.listen(3000);
console.log('Express server started on port 3000');
Credits to @lazybean
Authorization Code flow
The Authorization Code flow is made up from two parts. At first your application asks to
the user the permission to access their data. If the user approves the OAuth2 server sends
to the client an authorization code. In the second part, the client POST the authorization code
along with its client secret to the Lelylan in order to get the access token.
var credentials = {
clientID: '<client-id>',
clientSecret: '<client-secret>',
site: 'https://api.oauth.com'
};
var oauth2 = require('simple-oauth2')(credentials);
var authorization_uri = oauth2.authCode.authorizeURL({
redirect_uri: 'http://localhost:3000/callback',
scope: '<scope>',
state: '<state>'
});
res.redirect(authorization_uri);
var token;
oauth2.authCode.getToken({
code: '<code>',
redirect_uri: 'http://localhost:3000/callback'
}, saveToken);
function saveToken(error, result) {
if (error) { console.log('Access Token Error', error.message); }
token = oauth2.accessToken.create(result);
});
Password Credentials Flow
This flow is suitable when the resource owner has a trust relationship with the
client, such as its computer operating system or a highly privileged application.
Use this flow only when other flows are not viable or when you need a fast way to
test your application.
var token;
oauth2.password.getToken({
username: 'username',
password: 'password'
}, saveToken);
function saveToken(error, result) {
if (error) { console.log('Access Token Error', error.message); }
token = oauth2.accessToken.create(result);
oauth2.api('GET', '/users', {
access_token: token.token.access_token
}, function (err, data) {
console.log(data);
});
});
Client Credentials Flow
This flow is suitable when client is requesting access to the protected resources under its control.
var token;
var credentials = {
clientID: '<client-id>',
clientSecret: '<client-secret>',
site: 'https://api.oauth.com'
};
var oauth2 = require('simple-oauth2')(credentials);
oauth2.client.getToken({}, saveToken);
function saveToken(error, result) {
if (error) { console.log('Access Token Error', error.message); }
token = oauth2.accessToken.create(result);
});
Access Token object
When a token expires we need to refresh it. Simple OAuth2 offers the
AccessToken class that add a couple of useful methods to refresh the
access token when it is expired.
var token = {
'access_token': '<access-token>',
'refresh_token': '<refresh-token>',
'expires_in': '7200'
};
var token = oauth2.accessToken.create(token);
if (token.expired()) {
token.refresh(function(error, result) {
token = result;
})
}
When you've done with the token or you want to log out, you can
revoke the access token and refresh token.
token.revoke('access_token', function(error) {
token.revoke('refresh_token', function(error) {
console.log('token revoked.');
});
});
Errors
Exceptions are raised when a 4xx or 5xx status code is returned.
HTTPError
Through the error message attribute you can access the JSON representation
based on HTTP status
and error message
.
oauth2.authCode.getToken(function(error, token) {
if (error) { console.log(error.message); }
});
Configurations
Simple OAuth2 accepts an object with the following valid params.
clientID
- Required registered Client ID.
clientSecret
- Required registered Client secret.
site
- Required OAuth2 server site.
authorizationPath
- Authorization path for the OAuth2 server. Defaults to /oauth/authorize
.
tokenPath
- Access token path for the OAuth2 server. Defaults to /oauth/token
.
revocationPath
- Revocation token path for the OAuth2 server. Defaults to /oauth/revoke
.
useBasicAuthorizationHeader
- Whether or not the Authorization: Basic ...
header is set on the request.
Defaults to true
.
clientSecretParameterName
- Parameter name for the client secret. Defaults to client_secret
.
var credentials = {
clientID: '<client-id>',
clientSecret: '<client-secret>',
site: 'https://www.oauth2.com',
authorizationPath: '/oauth2/authorization',
tokenPath: '/oauth2/access_token',
revocationPath: '/oauth2/revoke'
};
var oauth2 = require('simple-oauth2')(credentials);
Contributing
Fork the repo on github and send a pull requests with topic branches. Do not forget to
provide specs to your contribution.
Running specs
- Fork and clone the repository (
dev
branch).
- Run
npm install
for dependencies.
- Run
make test
to execute all specs.
- Run
make test-watch
to auto execute all specs when a file change.
Coding guidelines
Follow github guidelines.
Feedback
Use the issue tracker for bugs.
Mail or Tweet us
for any idea that can improve the project.
Links
Authors
Andrea Reginato
Contributors
Special thanks to the following people for submitting patches.
Changelog
See CHANGELOG
Copyright
Copyright (c) 2013 Lelylan.
This project is released under the MIT License.