Skip to content

sjsnider/simple-oauth2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lelylan API for Node.js

Node.js client library for Oauth2

Requirements

Node client library is tested against Node ~0.8.x

Installation

Install the client library using npm:

$ npm install simple-oath2

Install the client library using git:

$ git clone git://github.com/andrearegianto/simple-oauth2.git
$ cd simple-oauth2
$ npm install

Documentation

  • Simple Oauth2 Docs

Getting started

Get the Access Token

var credentials = { client: { id: 'client-id', secret: 'client-secret', site: 'https://example.org' } };
var OAuth2 = require('simple-oauth2')(credentials);

// Returns the URI where to redirect your app
var redirect = Oauth2.AuthCode.authorizeURL({ redirectURI: 'http://localhost:3000/callback', scope: 'user', state: '02afe928b');
// => "https://example.org/oauth/authorization?response_type=code&client_id=client_id&redirect_uri=http://localhost:3000/callback&scope=user&state=02afe928b"

// Get the access token object
vat params = { code: 'authorization-code', redirectURI: 'http://localhost:3000/callback' }
client.authCode.getToken(params, function(error, result){
  // save the token
})

Refresh the Access Token

token = OAuth2.AccessToken.create(json_token);
if (token.expired()) {
  token.refresh(function(error, refreshedToken) { token = refreshedToken; })
}

Authorization Grants

Currently the Authorization Code and Resource Owner Password Credentials grant types have helper strategy classes that simplify client use. They are available via the #authCode and #password methods respectively.

// Authorization code flow var authURL = client.authCode.authorizeURL({ redirect_uri: 'http://localhost:3000/callback'); var token = client.authCode.getToken({ code: 'authorization-code', redirectURI: 'http://localhost:3000/callback' }, callback);

// Password credentials flow var token = client.password.getToken({ username: 'username', 'password': 'password' }, callback);

If the functions fails an error object is passed as first argument to the callback. The body response object is always the last argument.

Errors

Exceptions are raised when a 4xx or 5xx status code is returned.

SimpleOAtuh2.Error

Through the error message attribute you can access the JSON representation.

client.authCode.getToken(function(error, token) {
  if (error) { console.log(error.message); }
});

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. See LICENSE for details.

About

A simple Node.js client library for Oauth2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published