jaredhanson / jaredhanson/passport-http

Request Stream Event can not work with POST mode

Open
#4 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
261
Forks
112
PR merge metrics
No merged PRs in 30d

Description

hi there, I just occurred a problem now,
( express or restify ) seems could not triggered request stream event anymore
after set a " asynchronous verification "

here is the simple demo which I test.

```
var express = require('express'),
app = express(),
passport = require('passport'),
BasicStrategy = require('passport-http').BasicStrategy;

var users = [
{ id: 1, username: 'bob', password: 'secret', email: 'bob@example.com' }
, { id: 2, username: 'joe', password: 'birthday', email: 'joe@example.com' }
];

function findByUsername(username, fn) {
for (var i = 0, len = users.length; i < len; i++) {
var user = users[i];
if (user.username === username) {
return fn(null, user);
}
}
return fn(null, null);
}

passport.use(new BasicStrategy(
function(username, password, done) {
process.nextTick(function () {
findByUsername(username, function(err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (user.password != password) { return done(null, false); }
return done(null, user);
})
});
}));

app.configure(function() {
app.use(express.logger());
app.use(passport.initialize());
app.use(app.router);
});

app.post('/upload',
passport.authenticate('basic', { session: false }),
function(req, res, next) {

var dataLength = 0;

req.on('data', function(chunk) {
console.log('loading');
dataLength += chunk.length;

}).on('end', function() {
console.log('load end');
console.log('contentLength: %s', req.headers['content-length']);
console.log('dataLength: : %s', dataLength);
res.send(200);

});
});

app.listen(8080, function() {
console.log('server is running');
});
```

in normal way, the console should print like:

server is running
loading
loading
loading
load end
contentLength: 355968
dataLength: : 355968

(content-length should match with the real data-length which we got)
(but in this simple code, most time can not triggered stream event, or data-length would be loss)

but if use asynchronous verification, console won't print any info from the stream event,
even it would get fail if we try to using req.pipe() to upload the data to amazon s3 by know.
and without asynchronous verification, I can not get account info from mongodb.

is there any way to verify account by asynchronous way and make sure request stream work fine ?
ps: What I Upload is a mp3 file which about 300kbs, I had try this issue with jpg image too.

Contributor guide

Open the contributing guide

Research direction

Start at the passport.authenticate middleware used by the POST /upload route and trace how asynchronous verification interacts with the request stream. Reproduce the example with a file upload, then verify that req data and end events still fire and that the received length matches content-length.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, javascript, mongodb, node.js
Domain
authentication, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.