Skip to content

Commit

Permalink
#3 Adding register method in auth lib
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Jun 21, 2012
1 parent 368ed41 commit 6370252
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
4 changes: 4 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ module.exports = config = {
"read" : "/crudr/read",
"update" : "/crudr/update",
"delete" : "/crudr/delete"
},
"action" : {
"register" : "/crudr/register",
"reset" : "/crudr/reset"
}
}
20 changes: 19 additions & 1 deletion lib/crudr.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,28 @@ function setupRoutes( server ){

// add crud events...

//

// register app
server.get( config.action.register , function(req, res){
// validate the request
result = oauth.register( req.headers['host'] );
res.send( result );
});

// reset app key
/*
server.get( config.action.reset , function(req, res){
// validate the request
result = oauth.reset( req.headers['host'] );
res.send( result );
});
*/

// handle authentication
server.get( config.oauth.authorize , function(req, res){
// validate the request
result = oauth( req );
result = oauth.authorize( req );
res.redirect( req.query.redirect_uri + '?' + serialize (result ) );
//var token = ( result && typeof(result.token) != "undefined" ) ? result.token : false;
//res.redirect( req.query.redirect_uri + '?access_token='+ token + '?access_token='+ token);
Expand Down
37 changes: 29 additions & 8 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var fs = require("fs"),
access = path.normalize( __dirname + '/../access/'),
config = require('../config/default');

module.exports = Access;
//module.exports = Access;

function Access( req ) {
module.exports.authorize = function ( req ) {

var host = req.headers['host'];
var key = req.query.client_id;
Expand All @@ -28,6 +28,31 @@ function Access( req ) {

};

module.exports.register = function ( host ) {
nconf.use('file', { file: access+host+".json" });
nconf.load();

// check to see if the key is initialized
if( typeof( nconf.get("key") ) == "undefined" || typeof( nconf.get("secret") ) == "undefined" ){
createKeyPair( host );
}

return nconf.get("key");
}


module.exports.reset = function ( host ) {
nconf.use('file', { file: access+host+".json" });
nconf.load();

// create key regardless if it exists
createKeyPair( host );

return nconf.get("key");
}



// generate token by validating the host (domain) and adding the current date (accurate to the month = token resets every month)
function createToken( secret ){
var now = new Date();
Expand All @@ -50,9 +75,9 @@ function loadCreds( host ){
nconf.use('file', { file: access+host+".json" });
nconf.load();

// check to see if the key is initialized
if( typeof( nconf.get("key") ) == "undefined" || typeof( nconf.get("secret") ) == "undefined" ){
// check to see if the key is initialized
createKeyPair( host );
//createKeyPair( host );
}

}
Expand Down Expand Up @@ -82,10 +107,6 @@ function isValid( key, host ) {
}


Access.prototype.key = function() {

return this;
};

// Helpers
function strpos (haystack, needle, offset) {
Expand Down

0 comments on commit 6370252

Please sign in to comment.