Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return done(err, false);
}
if (user) {
done(null, user);
} else {
done(null, false, {message: 'foo'});
// or you could create a new account
}
});
}));
opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('param_name');
opts.jwtFromRequest = ExtractJwt.fromExtractors([ExtractJwt.fromHeader('x-api-key'), ExtractJwt.fromBodyField('field_name'), ExtractJwt.fromUrlQueryParameter('param_name')]);
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.jwtFromRequest = (req: Request) => { return req.query.token; };
opts.secretOrKey = new Buffer('secret');
declare function findUser(condition: {id: string}, callback: (error: any, user :any) => void): void;
passport.use(JwtStrategy.name, new JwtStrategy(opts, function(jwt_payload, done) {
findUser({id: jwt_payload.sub}, function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
done(null, user);
} else {
done(null, false, {message: 'foo'});
// or you could create a new account
}
});
}));
opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('param_name');
opts.jwtFromRequest = ExtractJwt.fromExtractors([ExtractJwt.fromHeader('x-api-key'), ExtractJwt.fromBodyField('field_name'), ExtractJwt.fromUrlQueryParameter('param_name')]);
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.jwtFromRequest = (req: Request) => { return req.query.token; };
opts.secretOrKey = new Buffer('secret');
declare function findUser(condition: {id: string}, callback: (error: any, user :any) => void): void;
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
findUser({id: jwt_payload.sub}, function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
done(null, user);
} else {
done(null, false, {message: 'foo'});
// or you could create a new account
}
});
}));
opts.jwtFromRequest = ExtractJwt.fromHeader('x-api-key');
opts.jwtFromRequest = ExtractJwt.fromBodyField('field_name');
opts.jwtFromRequest = ExtractJwt.fromUrlQueryParameter('param_name');
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('param_name');
opts.jwtFromRequest = (req: Request) => { return req.query.token; };
declare function findUser(condition: {id: string}, callback: (error: any, user :any) => void): void;
function getExtractor(extractor: string, param: string) {
switch(extractor) {
case 'header': return ExtractJwt.fromHeader(param);
case 'queryParam': return ExtractJwt.fromUrlQueryParameter(param);
case 'authHeader': return ExtractJwt.fromAuthHeaderWithScheme(param);
case 'bodyField': return ExtractJwt.fromBodyField(param);
case 'cookie': return (req)=>{
let token = null;
if (req && req.cookies)
{
token = req.cookies[param];
}
return token;
};
}
return ExtractJwt.fromAuthHeader();
}
import passport from 'passport';
import config from '../../../../config/api';
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const jwtOptions = {
secretOrKey: config.token.secret,
jwtFromRequest: ExtractJwt.fromExtractors([
ExtractJwt.fromUrlQueryParameter('access_token'),
ExtractJwt.fromBodyField('access_token'),
ExtractJwt.fromAuthHeaderWithScheme('Bearer'),
]),
};
export default function configureJwt(User) {
passport.use(new JwtStrategy(jwtOptions, async (payload, done) => {
const NOW = new Date().getTime();
if (payload.exp < NOW) {
return done(null, false);
}
const user = await User.query().findById(payload.sub).first();
if (!user) {
return done(null, false, { message: 'This email is not registered.' });
} else {
return done(null, user);
}