-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added in some basic (but working) OAuth2 support
no specs :( .. I suck.
- Loading branch information
Showing
4 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var sys= require('sys') | ||
var OAuth2= require('../lib/oauth2').OAuth2; | ||
|
||
var oa= new OAuth2("113560732012113", "f53a25cf96e19743fdcd189307bf47ac", "https://graph.facebook.com") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
var querystring= require('querystring'), | ||
crypto= require('crypto'), | ||
http= require('http'), | ||
URL= require('url'); | ||
|
||
var sys= require('sys'); | ||
|
||
exports.OAuth2= function(clientId, clientSecret, baseSite, authorizePath, accessTokenPath) { | ||
this._clientId= clientId; | ||
this._clientSecret= clientSecret; | ||
this._baseSite= baseSite; | ||
this._authorizeUrl= authorizePath || "/oauth/authorize" | ||
this._accessTokenUrl= accessTokenPath || "/oauth/access_token" | ||
} | ||
|
||
|
||
|
||
exports.OAuth2.prototype._getAccessTokenUrl= function( params ) { | ||
var params= params || {}; | ||
params['client_id'] = this._clientId; | ||
params['client_secret'] = this._clientSecret; | ||
params['type']= 'web_server'; | ||
|
||
return this._baseSite + this._accessTokenUrl + "?" + querystring.stringify(params); | ||
} | ||
|
||
exports.OAuth2.prototype._request= function(method, url, headers, access_token, callback) { | ||
|
||
var creds = crypto.createCredentials({ }); | ||
var parsedUrl= URL.parse( url, true ); | ||
if( parsedUrl.protocol == "https:" && !parsedUrl.port ) parsedUrl.port= 443; | ||
var httpClient = http.createClient(parsedUrl.port, parsedUrl.hostname, true, creds); | ||
|
||
var realHeaders= {}; | ||
if( headers ) { | ||
for(var key in headers) { | ||
realHeaders[key] = headers[key]; | ||
} | ||
} | ||
realHeaders['Host']= parsedUrl.host; | ||
if( access_token ) { | ||
if( ! parsedUrl.query ) parsedUrl.query= {}; | ||
parsedUrl.query["access_token"]= access_token; | ||
} | ||
|
||
var request = httpClient.request(method, parsedUrl.pathname + "?" + querystring.stringify(parsedUrl.query), realHeaders ); | ||
|
||
httpClient.addListener("secure", function () { | ||
/* // disable verification for now. | ||
var verified = httpClient.verifyPeer(); | ||
if(!verified) this.end(); */ | ||
}); | ||
|
||
var result= ""; | ||
request.addListener('response', function (response) { | ||
response.addListener("data", function (chunk) { | ||
result+= chunk | ||
}); | ||
response.addListener("end", function () { | ||
if( response.statusCode != 200 ) { | ||
callback( response.statusCode +" : " + result ); | ||
} else { | ||
callback(null, result, response); | ||
} | ||
}); | ||
}); | ||
|
||
request.end(); | ||
} | ||
|
||
|
||
exports.OAuth2.prototype.getAuthorizeUrl= function( params ) { | ||
var params= params || {}; | ||
params['client_id'] = this._clientId; | ||
params['type'] = 'web_server'; | ||
return this._baseSite + this._authorizeUrl + "?" + querystring.stringify(params); | ||
} | ||
|
||
exports.OAuth2.prototype.getOAuthAccessToken= function(code, params, callback) { | ||
var params= params || {}; | ||
params['code']= code; | ||
|
||
this._request("POST", this._getAccessTokenUrl(params), {}, null, function(error, data, response) { | ||
if( error ) callback(error); | ||
else { | ||
var results= querystring.parse(data); | ||
var access_token= results["access_token"]; | ||
var refresh_token= results["refresh_token"]; | ||
delete results["refresh_token"]; | ||
callback(null, access_token, refresh_token); | ||
} | ||
}); | ||
} | ||
|
||
exports.OAuth2.prototype.getProtectedResource= function(url, access_token, callback) { | ||
this._request("GET", url, {}, access_token, callback ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
name: oauth | ||
description: An implementation of an OAuth client. | ||
version: 0.0.5 | ||
version: 0.0.6 |