simple-oauth2
Advanced tools
Comparing version 0.1.1 to 0.1.2
# Changelog | ||
## v0.1.0 () | ||
## v0.1.2 (22 Jan 2013) | ||
Updated documentation | ||
## v0.1.1 (21 Jan 2013) | ||
Added Password credentials flow | ||
## v0.1.0 (21 Jan 2013) | ||
First version Node client for OAuth2 |
// | ||
// Access Token class | ||
// ### Wrapper for the Access Token object | ||
// | ||
@@ -9,6 +9,6 @@ module.exports = function(config) { | ||
// Returns the OAuth2.AccessToken instance. | ||
// | ||
// ### Creates an OAuth2.AccessToken instance. | ||
// | ||
// * `token` - An object containing the token object returned from the OAuth2 server. | ||
// maintain state between the request and the callback. | ||
// | ||
@@ -23,5 +23,5 @@ function create(token) { | ||
// | ||
// Returns true if the token is expired, false otherwise. | ||
// ### Check if the access token is expired or not. | ||
// | ||
function expired() { | ||
function expired() { | ||
return (Date.compare(this.token.expires_at, new Date) == -1) ? false : true | ||
@@ -32,9 +32,10 @@ } | ||
// | ||
// Refresh the access token | ||
// ### Refresh the access token | ||
// | ||
// * `callback` - The callback function returning the results. | ||
// An error object is passed as first argument and the result as last. | ||
// An error object is passed as first argument and the new access | ||
// token as last. | ||
// | ||
function refresh(callback) { | ||
var params = { grant_type: 'refresh_token', refresh_token: this.token.refresh_token }; | ||
function refresh(callback) { | ||
var params = { grant_type: 'refresh_token', refresh_token: this.token.refresh_token }; | ||
core.api('POST', config.tokenPath, params, callback); | ||
@@ -41,0 +42,0 @@ } |
// | ||
// Authorization Code flow implementation | ||
// ### Authorization Code flow implementation | ||
// | ||
@@ -9,4 +9,3 @@ module.exports = function(config) { | ||
// Returns the OAuth2 authorization URI where the user decides to | ||
// grant or deny the resources' access. | ||
// ### Redirect the user to the authorization page | ||
// | ||
@@ -27,5 +26,5 @@ // * `params.redirectURI` - A String that represents the registered application URI where the | ||
// | ||
// Returns the Access Token object. | ||
// ### Returns the Access Token object. | ||
// | ||
// * `params.code` - Authorization code (from the authorization step). | ||
// * `params.code` - Authorization code (from previous step). | ||
// * `params.redirectURI` - A String that represents the callback uri. | ||
@@ -32,0 +31,0 @@ // * `callback` - The callback function returning the results. |
// | ||
// Password credentials flow implementation | ||
// ### Password credentials flow implementation | ||
// | ||
@@ -9,6 +9,6 @@ module.exports = function(config) { | ||
// | ||
// Returns the Access Token object. | ||
// ### Returns the Access Token object. | ||
// | ||
// * `params.username` - Authorization code (from the authorization step). | ||
// * `params.password` - A String that represents the callback uri. | ||
// * `params.username` - A string that represents the registered username. | ||
// * `params.password` - A string that represents the registered password. | ||
// * `params.scope` - A String that represents the application privileges. | ||
@@ -15,0 +15,0 @@ // * `callback` - The callback function returning the results. |
// | ||
// A NodeJS module for interfacing with OAuth2. It accepts | ||
// an object with the following valid params. | ||
// **Node.js client library for [OAuth2](http://oauth.net/2/)** | ||
// | ||
// **[Github repository](https://github.com/andreareginato/simple-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). | ||
// | ||
// ### Authorization Code flow implementation | ||
// | ||
// 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 Lelylan 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. | ||
// [Learn more about](auth-code.html). | ||
// | ||
// | ||
// // Set the client credentials | ||
// var credentials = { client: { | ||
// id: '<client-id>', | ||
// secret: '<client-secret>', | ||
// site: 'https://example.org' | ||
// }}; | ||
// | ||
// // Initialize the OAuth2 Library | ||
// var OAuth2 = require('simple-oauth2')(credentials); | ||
// | ||
// // Authorization OAuth2 URI | ||
// var authorization_uri = OAuth2.AuthCode.authorizeURL({ | ||
// redirect_uri: 'http://localhost:3000/callback' | ||
// }); | ||
// | ||
// // Redirect example using Express | ||
// // See http://expressjs.com/api.html#res.redirect | ||
// res.redirect(authorization_uri); | ||
// | ||
// // Get the access token object. | ||
// // The authorization code is given from the previous step. | ||
// var token; | ||
// OAuth2.AuthCode.getToken({ | ||
// code: 'authorization-code', | ||
// redirectURI: 'http://localhost:3000/callback' | ||
// }, function(error, result) { token = result }); | ||
// | ||
// // Create the access token wrapper | ||
// var token = OAuth2.AccessToken.create(json_token); | ||
// | ||
// // Check if the token is expired. If expired it is refreshed. | ||
// if (token.expired()) { | ||
// token.refresh(function(error, result) { | ||
// token = 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. [Learn more about](password.html). | ||
// | ||
// | ||
// // Get the access token object. | ||
// var token; | ||
// OAuth2.Password.getToken({ | ||
// username: 'username', | ||
// password: 'password' | ||
// }, function(error, result) { token = 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. [Learn more about](access-token.html). | ||
// | ||
// | ||
// // Get the access token object. | ||
// var token; | ||
// OAuth2.AuthCode.getToken({ | ||
// code: 'authorization-code', | ||
// redirectURI: 'http://localhost:3000/callback' | ||
// }, function(error, result) { token = result }); | ||
// | ||
// // Create the access token wrapper | ||
// var token = OAuth2.AccessToken.create(json_token); | ||
// | ||
// // Check if the token is expired. If expired it is refreshed. | ||
// if (token.expired()) { | ||
// token.refresh(function(error, result) { | ||
// token = result; | ||
// }) | ||
// } | ||
// | ||
// | ||
// ### Errors | ||
// | ||
// Exceptions are raised when a 4xx or 5xx status code is returned. | ||
// | ||
// OAtuh2.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); } | ||
// }); | ||
// // => { "status": "401", "message": "Unauthorized" } | ||
// | ||
// | ||
// ### Configurations | ||
// | ||
// Simple OAuth2 accepts an object with the following valid params. | ||
// | ||
// * `client.id` - Required registered Client ID. | ||
@@ -13,23 +130,10 @@ // * `client.secret` - Required registered Client secret. | ||
// | ||
// | ||
var appConfig = require('./config'); | ||
// Configuration merge | ||
function mergeDefaults(o1, o2) { | ||
for (var p in o2) { | ||
try { if (typeof o2[p] == 'object') { o1[p] = mergeDefaults(o1[p], o2[p]); } else if (typeof o1[p] == 'undefined') { o1[p] = o2[p]; } } | ||
catch(e) { o1[p] = o2[p]; } | ||
} | ||
return o1; | ||
} | ||
// Export the client we'll use to make requests | ||
module.exports = function(config) { | ||
// Base configuration | ||
function configure(config) { | ||
config = config || {}; | ||
mergeDefaults(config, appConfig); | ||
return config; | ||
@@ -41,2 +145,10 @@ } | ||
function mergeDefaults(o1, o2) { | ||
for (var p in o2) { | ||
try { if (typeof o2[p] == 'object') { o1[p] = mergeDefaults(o1[p], o2[p]); } else if (typeof o1[p] == 'undefined') { o1[p] = o2[p]; } } | ||
catch(e) { o1[p] = o2[p]; } | ||
} | ||
return o1; | ||
} | ||
return { | ||
@@ -48,1 +160,2 @@ 'AuthCode': require('./client/auth-code')(config), | ||
}; | ||
{ | ||
"name": "simple-oauth2", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Node.js client for OAuth2", | ||
@@ -5,0 +5,0 @@ "author": "Andrea Reginato <andrea.reginato@gmail.com>", |
# Simple OAuth2 | ||
Node.js client library for [Oauth2](http://oauth.net/2/) | ||
Node.js client library for [Oauth2](http://oauth.net/2/). | ||
Currently it supports Authorization Code and Resource Owner Password Credentials grant types. | ||
@@ -24,11 +25,4 @@ | ||
## Documentation | ||
* [Simple Oauth2 Docs](git://andreareginato.github.com/simple-oauth2) | ||
## Getting started | ||
### Get the Access Token | ||
```javascript | ||
@@ -49,48 +43,7 @@ var credentials = { client: { id: 'client-id', secret: 'client-secret', site: 'https://example.org' } }; | ||
### Refresh the Access Token | ||
## Documentation | ||
```javascript | ||
Check out the complete [Simple Oauth2 Documentation](http://andreareginato.github.com/simple-oauth2) | ||
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. | ||
```javascript | ||
// Authorization code flow | ||
var uri = OAuth2.AuthCode.authorizeURL({ redirect_uri: 'http://localhost:3000/callback'); | ||
var token = OAuth2.AuthCode.getToken({ code: 'authorization-code', redirectURI: 'http://localhost:3000/callback' }, callback); | ||
// Password credentials flow | ||
var token = OAuth2.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. | ||
```javascript | ||
OAtuh2.HTTPError | ||
``` | ||
Through the error message attribute you can access the JSON representation | ||
based on HTTP `status` and error `message`. | ||
```javascript | ||
OAuth2.AuthCode.getToken(function(error, token) { | ||
if (error) { console.log(error.message); } | ||
}); | ||
``` | ||
## Contributing | ||
@@ -97,0 +50,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22805
562
97