Changeset 466 for Dev/trunk/src/server


Ignore:
Timestamp:
06/26/13 14:43:57 (12 years ago)
Author:
hendrikvanantwerpen
Message:

Added authentication (fixed user now).

Location:
Dev/trunk/src/server
Files:
2 added
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/server/app.js

    r464 r466  
    44var fs = require("fs");
    55var path = require("path");
    6 var proxy = require("./simple-http-proxy");
     6var proxy = require("./util/simple-http-proxy");
    77var _ = require("underscore");
    88
     
    3131        }
    3232    }));
     33    passport.serializeUser(function(user, done) {
     34        done(null, user.username);
     35    });
     36    passport.deserializeUser(function(id, done) {
     37        done(null, {username: id});
     38    });
    3339
    3440    var app = express();
     
    5662    });
    5763
     64   
    5865    // url to login (might work on others as well?)
    5966    // you should then have a session to work with
    60     app.post('/api/login');
     67    // should return a user info object
     68    function returnUser(req,res) {
     69        res.send(200, req.user);
     70    }
     71    app.post(
     72        '/api/login',
     73        passport.authenticate('local'),
     74        returnUser);
    6175
     76    // ensure we're authenticated on API calls
     77    app.use('/api', function(req,res,next){
     78        if (!req.user) {
     79            return res.send(401,{error:"Login before accessing API."});
     80        } else {
     81            return next();
     82        }
     83    });
     84   
     85    app.get(
     86        '/api/login',
     87        returnUser);
     88
     89    app.post(
     90        '/api/logout', function(req,res){
     91            req.logout();
     92            res.send(200,{});
     93        });
     94
     95    app.get('/api/surveyRun/:id/response/csv',
     96            function(req, res) {
     97                var id = req.params.id;
     98                // query CouchDB and build the CSV file
     99                res.set({
     100                    'Content-Type': 'text/csv',
     101                    'Content-Disposition': 'attachment; filename=responses-'+id+'.csv'
     102                });
     103                res.send(200, "Response for surveyRun "+id);
     104            });
     105   
    62106    // forward to couch
    63     app.use('/data/couch', proxy(settings.couchDbURL));
     107    app.use('/api/data', proxy(settings.couchDbURL));
    64108
    65109    return app;
  • Dev/trunk/src/server/config/config-couchdb.js

    r464 r466  
    11var q = require('q');
    2 var HTTP = require('q-io/http');
    3 var URL = require('url');
     2var request = require('../util/request');
    43var _ = require('underscore');
    54var util = require('util');
     
    2726    }
    2827
    29     function request(method,path,content) {
     28    function dbRequest(method,path,content) {
    3029        var url = couchDbURL+path;
    31         var parsedURL = URL.parse(url);
    3230        var options = {
    33             url: url,
    3431            method: method,
    3532            headers: {
     
    3734                'accept': 'application/json'
    3835            },
    39             body: {
    40                 forEach: function(callback) {
    41                     callback(JSON.stringify(content || {}));
    42                 }
    43             }
     36            body: content
    4437        };
    45         // because q-io doesn't support auth properly, we have to
    46         // build the f*ing wheel again.
    47         if ( parsedURL.auth ) {
    48             var auth = new Buffer(parsedURL.auth).toString("base64");
    49             options.headers.authorization = 'Basic '+auth;
    50         }
    51         return HTTP.request(options)
    52         .then(function(res){
    53             return res.body.read().then(function(content){
    54                 return JSON.parse(content.toString() || "{}");
    55             });
    56         },function(res){
    57             return res.body.read().then(function(error){
    58                 console.warn(error); // q.all doesn't do errors, so let's show them here
    59                 return JSON.parse(error.toString() || "{}");
    60             });
    61         });
     38        return request(url,options);
    6239    }
    6340
    6441    console.log("Configuring CouchDB for QED");
    6542    console.log("Checking CouchDB version");
    66     return request('GET','')
     43    return dbRequest('GET','')
    6744    .then(function(res){
    6845        if (res.version !== "1.2.0" ) {
     
    7350    }).then(function(res){
    7451        console.log("Checking database 'qed'");
    75         return request('GET','qed')
     52        return dbRequest('GET','qed')
    7653        .then(function(res){
    7754            console.log("Database 'qed' found.");
    7855        },function(err){
    7956            console.log("Creating database 'qed'");
    80             return request('PUT','qed');
     57            return dbRequest('PUT','qed');
    8158        });
    8259    }).then(function(){
     
    9269                case "update":
    9370                    console.log(docUrl+" updating.");
    94                     return request('GET',docUrl)
     71                    return dbRequest('GET',docUrl)
    9572                    .then(function(oldDoc){
    9673                        _.extend(oldDoc,doc);
    97                         return request('PUT',docUrl,oldDoc);
     74                        return dbRequest('PUT',docUrl,oldDoc);
    9875                    },function(){
    99                         return request('PUT',docUrl,doc);
     76                        return dbRequest('PUT',docUrl,doc);
    10077                    });
    10178                break;
     
    10380                default:
    10481                    console.log(docUrl+" replacing.");
    105                     return request('GET',docUrl)
     82                    return dbRequest('GET',docUrl)
    10683                    .then(function(oldDoc){
    10784                        _.extend(doc,_.pick(oldDoc,'_id','_rev'));
    108                         return request('PUT',docUrl,doc);
     85                        return dbRequest('PUT',docUrl,doc);
    10986                    },function(){
    110                         return request('PUT',docUrl,doc);
     87                        return dbRequest('PUT',docUrl,doc);
    11188                    });
    11289                break;
Note: See TracChangeset for help on using the changeset viewer.