Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved the http/digest Authorization header parsing (still not conv…
…inced its right). Added support for facebook's OAuth2 sign-on.
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
var OAuth2= require("oauth2").OAuth2, | ||
http = require('http'); | ||
|
||
get('/facebook_callback', function() { | ||
var self= this; | ||
this.authenticate(['facebook'], function(error, authenticated) { | ||
// self.respond(200,"xxxx") | ||
self.redirect("/facebook") | ||
}); | ||
}); | ||
|
||
exports.Facebook= AuthStrategy.extend({ | ||
constructor: function(options){ | ||
AuthStrategy.prototype.constructor.call(this, options) | ||
this._oAuth= new OAuth2(options.appId, options.appSecret, "https://graph.facebook.com"); | ||
this.scope= options.scope || ""; | ||
}, | ||
|
||
authenticate: function(request, callback) { | ||
//todo: makw the call timeout .... | ||
var self= this; | ||
if( request.param("code") ) { | ||
this._oAuth.getOAuthAccessToken(request.param("code"), | ||
{redirect_uri: "http://testtwitter.com:3000/facebook_callback" }, function( error, access_token, refresh_token ){ | ||
if( error ) callback(error) | ||
else { | ||
request.session["access_token"]= access_token; | ||
if( refresh_token ) request.session["refresh_token"]= refresh_token; | ||
self._oAuth.getProtectedResource("https://graph.facebook.com/me", request.session["access_token"], function (error, data, response) { | ||
if( error ) { | ||
self.fail(callback); | ||
}else { | ||
self.success(JSON.parse(data), callback) | ||
} | ||
}) | ||
} | ||
}); | ||
} | ||
else { | ||
var redirectUrl= this._oAuth.getAuthorizeUrl({redirect_uri : "http://testtwitter.com:3000/facebook_callback", scope: this.scope }) | ||
request.redirect( redirectUrl ); | ||
self.halt(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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
describe 'Express' | ||
before_each | ||
Digest= require('express/plugins/auth.strategies/http/digest').Digest | ||
end | ||
describe 'Auth' | ||
describe 'Digest Strategy' | ||
it 'should split a complex Authorization header into its constituent credentials parts' | ||
var mockHeader= "Digest username=foo, realm=\"test\", nonce=\"b343d03296358b5d7f985500568b\", uri=\"/\", response=\"52bc08c966a3b16bedb62f1b4a5b40f8\"" | ||
|
||
var credentials= new Digest({})._splitAuthorizationHeader(mockHeader); | ||
credentials.username.should_be "foo" | ||
credentials.realm.should_be "test" | ||
credentials.nonce.should_be "b343d03296358b5d7f985500568b" | ||
credentials.uri.should_be "/" | ||
credentials.response.should_be "52bc08c966a3b16bedb62f1b4a5b40f8" | ||
end | ||
end | ||
end | ||
end |