Ignore:
Timestamp:
06/26/13 21:17:41 (12 years ago)
Author:
hendrikvanantwerpen
Message:

Improved authentication

  • Authentication cannot be cancelled, removing a lot of weird race conditions.
  • We offer a request provider that has automatic retry in case of authentication failures.
  • Reduced dependency on LoginDialog? by having it act independent based on events. Other modules can just depend on 'session'.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/server/util/q-request.js

    r466 r468  
    1 var http = require('q-io/http'),
     1var q = require('q'),
     2    request = require('request'),
    23    url = require('url'),
    34    _ = require('underscore');
    45
    5 module.exports = function(urlOrObject, options) {
     6module.exports = function(url, options) {
    67
     8    var dfd = q.defer();
     9    dfd.response = q.defer();
     10   
    711    options = options
    812        ? _.clone(options)
    913        : {};
    10     options.url = _.isString(urlOrObject)
    11         ? url.parse(urlOrObject)
    12         : _.clone(urlOrObject);
     14    options.uri = url || options.uri;
     15   
     16    request(options,function(err,res,body){
     17        if ( err ) {
     18            dfd.response.reject(err);
     19        } else {
     20            if ( res.statusCode >= 200 && res.statusCode < 300 ) {
     21                dfd.resolve(body);
     22            } else {
     23                dfd.reject(body);
     24            }
     25        }
     26        dfd.response.resolve(res);
     27    });
    1328
    14     // wrap content into q-io thingy
    15     if ( options.body ) {
    16         options.body = {
    17             forEach: function(callback) {
    18                 callback(JSON.stringify(options.body));
    19             }
    20         };
    21     };
     29    return dfd.promise;
    2230   
    23     // add auth header since q-io doesn't support this
    24     if ( options.url.auth ) {
    25         options.headers.authorization = 'Basic '+(new Buffer(options.url.auth).toString("base64"));
    26     }
    27    
    28     // make request and collect results
    29     return http.request(options)
    30     .then(function(res){
    31         return res.body.read().then(function(content){
    32             return content.length > 0 ? JSON.parse(content) : null;
    33         });
    34     },function(res){
    35         return res.body.read().then(function(error){
    36             return error.length > 0 ? JSON.parse(error) : null;
    37         });
    38     });
    3931};
Note: See TracChangeset for help on using the changeset viewer.