diff --git a/config/default.js b/config/default.js index 3e16b55..d5b36ae 100644 --- a/config/default.js +++ b/config/default.js @@ -13,5 +13,9 @@ module.exports = config = { "read" : "/crudr/read", "update" : "/crudr/update", "delete" : "/crudr/delete" + }, + "action" : { + "register" : "/crudr/register", + "reset" : "/crudr/reset" } } \ No newline at end of file diff --git a/lib/crudr.js b/lib/crudr.js index fc0d896..81ca13c 100644 --- a/lib/crudr.js +++ b/lib/crudr.js @@ -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); diff --git a/lib/oauth.js b/lib/oauth.js index 31fd3b9..c0a8349 100755 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -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; @@ -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(); @@ -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 ); } } @@ -82,10 +107,6 @@ function isValid( key, host ) { } -Access.prototype.key = function() { - - return this; -}; // Helpers function strpos (haystack, needle, offset) {