Skip to content

Commit

Permalink
Improved the http/digest Authorization header parsing (still not conv…
Browse files Browse the repository at this point in the history
…inced its right).

Added support for facebook's OAuth2 sign-on.
  • Loading branch information
ciaranj committed May 2, 2010
1 parent bc9c208 commit c0379b0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/express/plugins/auth.strategies/facebook.js
@@ -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);
}
}
});
19 changes: 19 additions & 0 deletions spec/spec.auth.strategies.http.digest.js
@@ -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

0 comments on commit c0379b0

Please sign in to comment.