Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
if (!isMatch)
{
return done(null, false);
}
console.log('user'+user);
return done(null,user);
});
});
});
// Setup options for JWT Strategy
const jwtOptions = {
jwtFromRequest: ExtractJwt.fromHeader('authorization'),
secretOrKey: config.secret
};
// Create JWT strategy
const jwtLogin = new JwtStrategy(jwtOptions, function(payload, done) {
// See if the user ID in the payload exists in our database
// If it does, call 'done' with that other
// otherwise, call done without a user object
User.findById(payload.sub, function(err, user) {
if (err) { return done(err, false); }
if (user) {
done(null, user);
} else {
done(null, false);
}
if (!isMatch) {
return done(null, false);
}
return done(null, user);
});
});
}));
//==========================
// JWT Strategy
//==========================
var jwtOptions = {
jwtFromRequest: ExtractJwt.fromHeader('authorization'),
secretOrKey: process.env.SECRET
};
passport.use(new JwtStrategy(jwtOptions, function (payload, done) {
User.findById(payload.sub, function (err, user) {
if (err) {
return done(err, false);
}
if (user) {
done(null, user);
} else {
done(null, false);
}
});
}));
constructor(private readonly configService: ConfigService, private readonly authService: AuthService) {
super({
// Only take the JWT token from either "authorization" or "Authorization" headers
jwtFromRequest: ExtractJwt.fromExtractors([
ExtractJwt.fromHeader('authorization'),
ExtractJwt.fromHeader('Authorization'),
]),
secretOrKey: configService.get(EnvVariables.JWT_SECRET),
});
}
import passport from 'passport';
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const config = require('../../../config/config');
const sessionConfig = config.get('session');
const jwtOptions = {
jwtFromRequest: ExtractJwt.fromHeader('authorization'),
secretOrKey: sessionConfig.secret,
};
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 {
user.stripPassword();
return done(null, user);
}
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();
}
if (!user) { return done(null, false); }
user.comparePassword(password, (err, isMatch) => {
if (err) { return done(err); }
if (!isMatch) { return done(null, false); }
if (user.role < 1) { return done(null, false); }
return done(null, user);
});
});
});
const jwtOptions = {
jwtFromRequest: ExtractJwt.fromHeader('authorization'),
secretOrKey: dbConfig.secret,
};
const jwtLogin = new JwtStrategy(jwtOptions, (payload, done) => {
User.findById(payload.sub, (err, user) => {
if (err) { return done(err, false); }
if (user) {
done(null, user);
} else {
done(null, false);
}
});
});
passport.use(jwtLogin);
}
if(!isMatch) {
return done(null, false);
}
return done(null, user);
});
}).catch((e) => {
done(e);
});
});
//options for JWT strategy
const jwtOptions= {
jwtFromRequest: ExtractJwt.fromHeader('x-auth'),
secretOrKey: config.secret
};
const jwtLogin = new JwtStrategy(jwtOptions, function(payload, done) {
User.findById(payload.sub, function(err, user) {
if(err) {
return done(err, false);
}
if(user) {
done(null, user);
} else {
done(null, false);
}
});