HTTP Basic Auth

596 views
Skip to first unread message

Nick Parsons

unread,
Jan 18, 2013, 12:05:19 PM1/18/13
to res...@googlegroups.com
I've added app.use(restify.authorizationParser()); and I'm passing in a valid Authorization header with a username/password combi, however the req.authorization variable is not being set. I can see that basic authorization is being set if I console out req (http://cl.ly/image/3t2Q0I2m1i15). 

var app = restify.createServer({
name: config.NAME,
  version: config.VERSION,
  log: log
});

app.use(restify.acceptParser(app.acceptable));
app.use(restify.authorizationParser());
app.use(restify.fullResponse());
app.use(restify.queryParser({mapParams: true}));
app.use(restify.multipartBodyParser());
app.use(restify.jsonBodyParser({mapParams: false}));
app.use(restify.gzipResponse());

// authorize user
app.pre(function auth(req, res, next) {
    console.log(req.authorization);
    next();
});

Mark Cavage

unread,
Jan 18, 2013, 12:40:19 PM1/18/13
to res...@googlegroups.com
Hi Nick,

Your console.log is in a pre handler, which is running before the authz parser.  Change 'pre' to 'use'.

m

Nick Parsons

unread,
Jan 18, 2013, 12:42:15 PM1/18/13
to res...@googlegroups.com
Ah, that did the trick! For some reason I thought pre was triggered before the route was called. Still learning. Thanks!

Mark Cavage

unread,
Jan 18, 2013, 12:45:19 PM1/18/13
to res...@googlegroups.com
No worries - pre runs before "routing" not before calling the route. You use it to change what the routing logic would find (or in the case of 'pause' purely as something to run before the event loop is relinquished).

m

Nick Parsons

unread,
Jan 18, 2013, 2:07:55 PM1/18/13
to res...@googlegroups.com
HTTP Basic Auth is working great when calling from my REST client. However, the app that is consuming the API is built w/ Backbone. We tried overriding Backbone sync to pass along the Authorization header but it is failing on an options call -- from what I understand a "pre-flight" call to see if cross origin calls are allowed. Screenshot here: http://cl.ly/image/0j2v240A0p2f

This is one of the few things left before being able to push to production. Any thoughts?

Ryan Kelly

unread,
Feb 17, 2013, 5:58:23 PM2/17/13
to res...@googlegroups.com
Hi Nick, did you figure this one out? My preflight requests work with curl, but I am experiencing the same thing as you when using backbone.js

Ryan Kelly

unread,
Feb 18, 2013, 8:44:46 PM2/18/13
to res...@googlegroups.com
I have had an issue calling my restify api from backbone.js as well. everything works well with a curl command, specifying a different origin. However, it is not working with my backbone.js application. I have added the stack trace and my server.js settings here - https://gist.github.com/remotevision/4982339

any thoughts?
Reply all
Reply to author
Forward
0 new messages