Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;
const mongoose = require("mongoose");
const Trainer = require("../server/database/models/Trainer");
// const keys = require('../config/keys');
require("env2")("./config/config.env");
// set up options
const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = process.env.secretOrKey;
// opts.secretOrKey = keys.secretOrKey;
module.exports = (passport) => {
passport.use(
// use mongoose findById method and pass it the id stemming from the bearer toke auth object named jwt payload
new JwtStrategy(opts, (jwt_payload, done) => {
console.log("JWT", jwt_payload);
Trainer.findById(jwt_payload.id)
.then((trainer) => {
// use done function without error and either user or false
if (trainer) {
return done(null, trainer);
}
* - Basic Auth
*
* Pass a strategy when initializing module routes to setup this strategy for the complete module: Example: new UserRoutes('jwt')
*
* To setup a strategy for individual endpoints in a module pass the strategy on isAuthorized call
* Example: isAuthorized('basic')
*/
export class AuthService {
private defaultStrategy: PassportStrategy;
private jwtStrategy: JwtStrategy;
private basicStrategy: BasicAuthStrategy;
private readonly strategyOptions: StrategyOptions = {
audience: 'aionic-client',
issuer: 'aionic-core',
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'aionic-secret-api-key'
};
// JWT options
private readonly signOptions: SignOptions = {
audience: this.strategyOptions.audience,
expiresIn: '8h',
issuer: this.strategyOptions.issuer
};
public constructor(defaultStrategy: PassportStrategy = 'jwt') {
// Setup default strategy -> use jwt if none is provided
this.defaultStrategy = defaultStrategy;
this.jwtStrategy = new JwtStrategy(this.strategyOptions);
this.basicStrategy = new BasicAuthStrategy();
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require('mongoose');
const User = mongoose.model('users');
const keys = require('../config/keys');
const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrKey;
module.exports = (passport) => {
passport.use(
new JwtStrategy(opts, (jwt_payload, done) => {
User.findById(jwt_payload.id)
.then((user) => {
if (user) {
return done(null, user);
}
return done(null, false);
})
.catch(err => console.log(err));
}),
);
};
init(app: Application) {
app.use(passport.initialize());
passport.use(new Strategy({
secretOrKey: config.getJwtSecret(),
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
}, (payload: any, done: VerifiedCallback) => done(undefined, payload)));
}
constructor(
private readonly authService: AuthService,
configService: ConfigService
) {
super({
jwtFromRequest: ExtractJwt.fromExtractors([
ExtractJwt.fromAuthHeaderAsBearerToken(),
ExtractJwt.fromUrlQueryParameter("access_token"),
req => {
const token = req.cookies && req.cookies.access_token;
return token;
}
]),
secretOrKey: configService.get("JWT_SECRET_KEY")
});
}
import { createParamDecorator } from '@nestjs/common';
import { ExtractJwt } from 'passport-jwt';
const extractor = ExtractJwt.fromAuthHeaderAsBearerToken();
export const RawToken = createParamDecorator((data, req) => {
return extractor(req);
});
constructor(
private readonly _authService: AuthService,
private readonly _configurationService: ConfigurationService,
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: _configurationService.get(Configuration.JWT_KEY),
});
}
constructor(private readonly authService: AuthService, @Inject(CONFIG_TOKEN) config: AppProperties) {
super(
{
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
passReqToCallback: true,
secretOrKey: config.token.secret,
},
async (req, payload, next) => await this.verify(req, payload, next)
);
passport.use(this);
}
import passport from "passport";
import { ExtractJwt, Strategy } from "passport-jwt";
import models from "../models";
import config from "../config";
passport.use(
new Strategy(
{
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: config.JWT_SECRET
},
async (jwtPayload, done) => {
try {
const user = await models.User.findOne({
where: { id: jwtPayload.id },
raw: true
});
if (!user) {
return done(new Error(), false);
}
return done(null, user);
} catch (err) {
return done(new Error(), false);
}
}