Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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),
});
}
value(): ValueOrPromise {
if (!this.metadata) return;
const {strategy} = this.metadata;
if (strategy === JWT_STRATEGY_NAME) {
const jwtStrategy = new JwtStrategy(
{
secretOrKey: JWT_SECRET,
jwtFromRequest: ExtractJwt.fromExtractors([
ExtractJwt.fromAuthHeaderAsBearerToken(),
ExtractJwt.fromUrlQueryParameter('access_token'),
]),
},
(payload, done) => this.verifyToken(payload, done),
);
// we will use Loopback's StrategyAdapter so we can leverage passport's strategy
// and also we don't have to implement a new strategy adapter.
return new StrategyAdapter(jwtStrategy, JWT_STRATEGY_NAME);
}
}
},
//Enable JWT strategy
jwt: {
strategy: JwtStrategy,
tokenOptions: {
expiresInSeconds: EXPIRES_IN_SECONDS,
secret: SECRET,
algorithm: ALGORITHM,
issuer: ISSUER,
audience: AUDIENCE
},
options: {
secretOrKey: SECRET,
issuer: ISSUER,
audience: AUDIENCE,
jwtFromRequest: ExtractJwt.fromExtractors([ExtractJwt.fromAuthHeader(), ExtractJwt.fromUrlQueryParameter('token')]) //Authorization: JWT JSON_WEB_TOKEN_STRING
}
}, /*
//Enable twitter strategy
twitter: {
name: 'Twitter',
protocol: 'oauth',
strategy: require('passport-twitter').Strategy,
options: {
consumerKey: 'your-consumer-key',
consumerSecret: 'your-consumer-secret'
}
}
/*,
//Enable facebook strategy
facebook: {
export default function addPassport(app, db) {
const { TOKEN_KEY, TOKEN_SECRET } = process.env;
passport.use(
new Strategy(
{
jwtFromRequest: ExtractJwt.fromExtractors([
req => {
let token = null;
if (req && req.cookies) {
token = req.cookies[TOKEN_KEY];
}
return token;
},
]),
secretOrKey: TOKEN_SECRET,
},
(jwtPayload, done) => {
if (!jwtPayload.userId) {
done(new Error('No userId in JWT'), false);
} else {
done(null, jwtPayload);
}
constructor(
private readonly authService: AuthService,
configService: ConfigService,
) {
super({
jwtFromRequest: ExtractJwt.fromExtractors([
req => req.cookies[configService.getEnvSettings().AuthCookieName],
]),
secretOrKey: configService.getEnvSettings().AuthEncKey,
});
}
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 {
done()
} else {
done(null, user)
}
},
)
const extactFromCookie = request => {
let token = null
if (request && request.cookies) token = request.cookies["token"]
return token
}
const jwtStrategy = new JWTStrategy(
{
jwtFromRequest: ExtractJwt.fromExtractors([extactFromCookie]),
secretOrKey: config.auth.jwt.secret,
},
async (jwt, done) => {
var user = await appLogic.models.users.tryGetUserById(jwt.id)
if (user instanceof NotFound) {
let err = new Error("Unauthorized")
//@ts-ignore
err.status = 401
done(err)
} else {
done(null, user)
}
},
)
const linkedInStrategy = new LinkedInStrategy(
export function getStrategies({ appLogic }: { appLogic: AppLogic }) {
var config = appLogic.config
const extactFromCookie = request => {
let token = null
if (request && request.cookies) token = request.cookies["token"]
return token
}
const jwtStrategy = new JWTStrategy(
{
jwtFromRequest: ExtractJwt.fromExtractors([extactFromCookie]),
secretOrKey: config.auth.jwt.secret,
},
async (jwt, done) => {
var user = await appLogic.models.users.tryGetById(jwt.id)
if (user instanceof NotFound) {
LogModelActions("jwt", "login failure", "unauthorized")
let err = new Error("Unauthorized")
//@ts-ignore
err.status = 401
done(err)
} else {
LogModelActions("jwt", "login success", user.id)
done(null, user)
}
},
)
export default function(passport) {
passport.use(
new JwtStrategy(
{
jwtFromRequest: ExtractJwt.fromExtractors([
ExtractJwt.fromUrlQueryParameter("state"),
ExtractJwt.fromAuthHeaderAsBearerToken(),
]),
secretOrKey: jwtSecret,
},
(jwtPayload, done) => done(null, jwtPayload.user)
)
);
}