Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Flinstone: {profile: {id: 'Flint'}, password: 'beta'},
George: {profile: {id: 'Curious'}, password: 'gamma'},
});
}
// Since it has to be user's job to provide the `verify` function and
// instantiate the passport strategy, we cannot add the imported `BasicStrategy`
// class as extension directly.
// We need to either wrap it as a strategy provider, and add the provider
// class as the extension. (When having something like the verify function to inject)
// Or just wrap the basic strategy instance and bind it to the app. (When nothing to inject)
function verify(username: string, password: string, cb: Function) {
users.find(username, password, cb);
}
const basicStrategy = new BasicStrategy(verify);
const basicAuthStrategy = new StrategyAdapter(
basicStrategy,
AUTH_STRATEGY_NAME,
);
async function givenAServer() {
app = new Application();
app.component(AuthenticationComponent);
app.component(RestComponent);
app
.bind('authentication.strategies.basicAuthStrategy')
.to(basicAuthStrategy)
.tag({
[CoreTags.EXTENSION_FOR]:
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
});
module.exports = function passportStrategy(passport) {
passport.use('basic', new BasicStrategy({}, function(email, password, done) { // Auth email/pass sent in header
User.findOne({'email': email}, function(err, user) {
if (err) return done('database error');
if (!user) return done('user not found');
user.checkPassword(password, function(err, result) {
if (err) console.log(err);
if (!result) return done('wrong password');
// return user if no auth errors
return done(null, user);
});
});
}));
};
// check whether the strategy has already been set up
if (this.isBasicStrategySetup) {
throw new Error('BasicStrategy has already been set up');
}
const configManager = this.crowi.configManager;
const isBasicEnabled = configManager.getConfig('crowi', 'security:passport-basic:isEnabled');
// when disabled
if (!isBasicEnabled) {
return;
}
debug('BasicStrategy: setting up..');
passport.use(new BasicStrategy(
(userId, password, done) => {
if (userId != null) {
return done(null, userId);
}
return done(null, false, { message: 'Incorrect credentials.' });
},
));
this.isBasicStrategySetup = true;
debug('BasicStrategy: setup is done');
}
return Array.isArray(value) ? value : []
}
})
// this.users = users
this.secret = secret
let jwtOpts = {
secretOrKey: this.secret,
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme('jwt')
}
// this is not officially documented
// see https://github.com/jaredhanson/passport/issues/27
this.pp = new passport.Passport()
this.pp.use(new BasicStrategy(this.handleBasicAuth.bind(this)))
this.pp.use(new JwtStrategy(jwtOpts, this.handleJwtAuth.bind(this)))
this.middleware = this.pp.initialize()
}
// if no user is found, return the message
if (!user)
return done(null, false, { message: 'No user with this email.' });
// if the user is found but password invalid
if (!user.validPassword(password))
return done(null, false, { message: 'Invalid email or password.' });
return done(null, user);
});
}));
//====== Basic Auth ======
passport.use('basic', new BasicStrategy(function(email, password, done) {
User.findOne({ email: email }, function (err, user) {
if (err)
return done(err);
// if no user is found, return the message
if (!user)
return done(null, false);
// if the user is found but password invalid
if (!user.validPassword(password))
return done(null, false);
return done(null, user);
});
}));
configuredBasicStrategy(verifyFn: BasicVerifyFunction): BasicStrategy {
return new BasicStrategy(verifyFn);
}
public constructor() {
super();
this._strategy = new BasicStrategy(this.verify);
}
module.exports = function (apiKey, authConfig, gateway) {
var p = pathUtil.join(gateway.middlewarePath, 'authentication', 'verify', authConfig.verify);
var verifyFunction = require(p);
passport.use(apiKey, new passport_http_1.BasicStrategy(verifyFunction));
};
module.exports = function (authConfig: BasicAuthentication, config: Configuration) {
let p = pathUtil.join(config.middlewarePath, 'authentication', 'verify', authConfig.verify);
let verifyFunction = require(p);
return new BasicStrategy(verifyFunction);
};
function configureWebSecurity(conf) {
const creds = getBasic(conf);
if (creds) {
passport.use(new passportHttp.BasicStrategy((username, password, done) => {
const name = creds.username,
passwd = creds.password,
correct = name === username && passwd === password;
done(null, correct ? basic : false);
}));
}
}