Skip to content
Browse files

Fixed Controller::header threw in an initial Auth for CouchDB happy 3…

…0h of coding - so beware - it might be a trap
  • Loading branch information...
1 parent c17fabb commit a1a1d569c08bfc233130996231ed2b31185d78fb jabis committed Jul 17, 2009
View
5 vendor/raccoon/Raccoon/Core/Cache.jx
@@ -64,6 +64,8 @@ this.Cache = function(){
return writes;
};
this.File = function(file,mode,input,force) {
+ if(file.test("user_")) return input;
+ //response.write(JSON.encode([file,mode]));
var f = new File(file);
var size,data,writes,ratio,clone,cEx;
input = input || {};
@@ -73,7 +75,8 @@ this.Cache = function(){
data = f.read();
} catch(err) { f.open("a"); data= ""; }
if((mode=="w") || (mode=="a")) {
- clone = f.copy("/tmp/"+session.getId()+"_"+Number.random(600,1800)+".json").open("w");
+ var pth = [global.Config["sessionPath"],[session.getId(),Number.random(600,1800)].join('_')].join('/');
+ clone = f.copy(pth).open("w");
try {
var n = f.toString();
clone.write(input);
View
2 vendor/raccoon/Raccoon/Core/Controller.jx
@@ -61,7 +61,7 @@ this.Controller = new Class({
},
redirect: function(url){
- response.headers({'Location': url});
+ response.header({'Location': url});
},
puts: function(str){
View
3 vendor/raccoon/Raccoon/Core/Extras.jx
@@ -69,4 +69,5 @@ this.Assets = new Class({
});
-})();
+})();
+
View
165 vendor/raccoon/Raccoon/CouchDB/Auth.jx
@@ -0,0 +1,165 @@
+/**
+ * CouchDB Authentication module for Raccoon
+ *
+ */
+(function(){
+
+this.Auth = new Class({
+
+ Implements:[Options],
+ options:{
+ dboptions:{
+ cache:false,
+ adapter:'couchdb',
+ host: 'http://localhost',
+ port: '5984',
+ db: 'users',
+ username: '',
+ password: ''
+ },
+ cookieExpiry: 4 //hours
+ },
+ initialize: function(options){
+ this.setOptions(options);
+ this.db = new CouchDB(this.options.dboptions);
+ this.User = new User();
+ //response.write(JSON.encode(request.cookie));
+
+ if(request.cookie["Auth"]) {
+ if(Raccoon.Cache.get('couchdb.cacheusers')) {
+ var cE = this.options.cookieExpiry;
+ var f = Raccoon.Cache.File(Raccoon.Cache.get('couchdb.usercache'),"r",false,false,false);
+ //response.write(f);
+ if(f !="") {
+ var u = [];
+ f= JSON.decode(f);
+ //response.write(typeOf(f))
+ f.each(function(obj){
+ u = obj.user;
+ //response.write(JSON.encode(obj.hash));
+ //u.push([obj.hash,request.cookie["Auth"]]);
+ //response.write(JSON.encode(Object.values(f)));
+ //if (Object.values(f).contains(request.cookie["Auth"])) response.write("yay");
+ //else response.write("boo");
+ });
+ Raccoon.Cache.set('couchdb.userlogged', f);
+ }
+ else Raccoon.Cache.set('couchdb.userlogged', []);
+ }
+ //response.write(JSON.encode(u));
+ Raccoon.fireEvent('onBeforeAutoLogin',request.cookie["Auth"]);
+ if(u) this.autologin(u,'auto');
+ //else this.logout();
+ }
+ return this;
+ },
+ doAuth: function(userdata) {
+ this.User.sets(userdata);
+ this.User.set('auth',true);
+ this.authenticate(userdata);
+ return this;
+ },
+ 'protected autologin': function(data,method){
+ switch(method) {
+ case 'auto':
+ var userdata = this.db.view('user','email',{key:data,include_docs:true});
+ var doc = JSON.decode(userdata);
+ //response.write(JSON.encode(doc));
+ if(doc && doc.rows.length > 0) {
+ this.doAuth(doc.rows[0].doc);
+ }
+ break;
+ case false:default:
+ break;
+ }
+ },
+ 'protected authenticate':function(data) {
+ this.isLogged = true;
+ var traw = data.email+data._rev+data.password;
+ var token = Util.sha1(traw);
+ var id = data._id;
+ data.authKey = token;
+ var d = data;
+ var now = new Date();
+ var hours = now.getHours()+this.options.cookieExpiry;
+ var nd = new Date().setHours(hours);
+ nd = new Date(nd);
+ var expires = nd;
+ response.cookie("Auth", token, expires);
+ if(Raccoon.Cache.get('couchdb.cacheusers')) {
+ var f = Raccoon.Cache.get('couchdb.usercache');
+ var arr = Raccoon.Cache.get('couchdb.userlogged');
+ arr = JSON.decode(arr) || [];
+ var t = new Date().getTime();
+ var objy = {'hash':token,'user':data.email,'time':t};
+ arr.each(function(obj) {
+ if(obj['hash'] == objy['hash']) { obj['time'] = new Date().getTime(); }
+ });
+ arr.push(objy);
+ //if(n) arr.push(objy);
+ var authed = Raccoon.Cache.File(f,"w",JSON.encode(arr),false,false);
+ }
+ return this;
+ },
+ logout:function() {
+ var now = new Date();
+ var minutes = now.getHours()-4;
+ var nd = new Date().setHours(minutes);
+ nd = new Date(nd);
+ var expires = nd;
+ response.cookie("Auth", null, expires);
+ },
+ 'protected getAllUsers':function() {
+ return this.db.request('GET','_all_docs',{include_docs:true});
+ },
+ get:function(key) {
+ return this[key];
+ },
+ getUser:function(){
+ return this.User;
+ },
+ getAuth:function(){
+ return this.User.get('auth');
+ },
+ checkUser: function(data){
+ this.docs = this.db.view('user','x',{key:data});
+ }
+
+});
+
+this.User = new Class({
+ initialize:function() {
+ this.auth=false;
+ this.roles=["visitor"];
+ return this;
+ },
+ get:function(key) {
+ return this[key];
+ },
+ set:function(key,value) {
+ this[key] = value;
+ return this;
+ },
+ sets:function(obj) {
+ switch(typeOf(obj)) {
+ case "array":
+ obj.each(function(value,key) {
+ this.set(key,value);
+ });
+ break;
+ case "object":
+ try {
+ for (var key in obj) {
+ this.set(key, obj[key]);
+ }
+ } catch(err) { response.write(err); }
+ break;
+ default:
+ this.set(key,value);
+ break;
+ }
+ return this;
+ }
+});
+
+})();
View
15 vendor/raccoon/Raccoon/CouchDB/CouchDB.jx
@@ -31,7 +31,8 @@ var CouchDB = new Class({
this.hostURL = (this.options.port) ? [this.options.host, this.options.port].join(':') : this.options.host;
this.dbURL = [this.hostURL, this.options.db].join('/') + '/';
this.curl = new (Process ? Curl : HTTPCurl)();
- this.cache = (Raccoon.Cache.get('couchdb.cache'))? Raccoon.Cache : false;
+ this.cache = (Raccoon.Cache.get('couchdb.cache'))? true : false;
+ if(this.options.noCache) this.cache =false;
if (this.options.username && this.options.password) {
var auth = Util.base64encode([this.options.username, this.options.password].join(':'));
this.curl.setHeader('Authorization', 'Basic ' + auth);
@@ -86,25 +87,19 @@ var CouchDB = new Class({
view: function(design, view, args){
// Commented out items: For cleanup.
if(this.cache) {
- var cache = this.cache;
- var keys = Object.keys(args);
+ var cache = Raccoon.Cache;
var value = Object.values(args);
value = String.from(value).replace(/[^0-9A-Za-z]/g,'_');
- if(keys.contains('key')) var key = args['key'];
- else var key = "";
var match = [design,view,value].join('_')+".json";
var x = cache.get(match);
var item = [cache.get('dir'),match].join('');
if(cache.check(item)){
-
var url = ['_design/', design, '/_view/', view].join('');
-
var resp = this.request('GET', url, args);
- var retval = cache.File(item,"w",JSON.encode(resp.body),true);
+ var retval = cache.File(item,"w",JSON.encode(resp.body),true,false);
return retval;
} else {
- var retval = cache.File(item,"r",false,false);
- //response.write(retval);
+ var retval = cache.File(item,"r",false,false,false);
return retval;
}
} else {
View
25 vendor/raccoon/raccoon.jx
@@ -15,10 +15,11 @@ Code and Documentation:
include('./Raccoon/Core/MooTools.jx');
include('./Raccoon/Core/Extras.jx');
-include('./Raccoon/Core/Cache.jx');
-include('./Raccoon/Core/Controller.jx');
-include('./Raccoon/CouchDB/CouchDB.jx');
+include('./Raccoon/Core/Controller.jx');
+include('./Raccoon/CouchDB/CouchDB.jx');
+include('./Raccoon/CouchDB/Auth.jx');
+include('./Raccoon/Core/Cache.jx');
(function(global){
request.rawurl = request.get['url'] || "";
@@ -54,6 +55,8 @@ var Raccoon = this.Raccoon = new Class({
if (config.database) {
if(config.database.cache){
cache.set(config.database.adapter+'.cache',true);
+ cache.set(config.database.adapter+'.cacheusers',true);
+ cache.set(config.database.adapter+'.usercache',currentDir + '/../_usercache/users.json');
var cacheDir = currentDir + '/_cache/';
var cachefiles = new Directory(cacheDir).listFiles();
cache.set('dir',cacheDir);
@@ -96,6 +99,7 @@ var Raccoon = this.Raccoon = new Class({
for (var x = 0, y = config.routes['map'].length; x < y; x++) {
this.addRoute.apply(this, config.routes['map'][x]);
}
+ this.loadAuthentication();
},
'protected loadControllers': function(){
@@ -105,6 +109,21 @@ var Raccoon = this.Raccoon = new Class({
include(dir + ctrls[x]);
}
},
+
+ 'protected loadAuthentication':function(){
+
+ var auth = Raccoon.Auth = new Auth({
+ dboptions:{
+ cache:false,
+ adapter:'couchdb',
+ host: 'http://localhost',
+ port: '5984',
+ db: 'users',
+ username: 'jabis',
+ password: 'perkele'
+ }
+ });
+ },
addRoute: function(map, klass, action, method){
this.routes.push({

0 comments on commit a1a1d56

Please sign in to comment.
Something went wrong with that request. Please try again.