Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// @flow
import passport from 'koa-passport';
import { Strategy } from 'passport-local';
import User from './models/User';
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findOne({ id }).then(user => done(null, user || null), done);
});
passport.use(
'local',
new Strategy(
{
usernameField: 'email',
passwordField: 'password',
session: false,
},
async (email, password, done) => {
export function setup (server) {
// TODO: Serialize user data in the database?
passport.serializeUser((user, done) => {
done(null, user._id)
})
passport.deserializeUser((id, done) => {
User.findById(id)
.then((user) => done(null, user))
.catch((error) => done(error))
})
passport.use(github.strategy)
server.use(passport.initialize())
server.use(passport.session())
log.debug('Passport setup complete')
}
export function init(Strategy = GithubStrategy) {
const GITHUB_CLIENT_ID = nconf.get('GITHUB_CLIENT_ID')
const GITHUB_CLIENT_SECRET = nconf.get('GITHUB_CLIENT_SECRET')
const GITHUB_UI_URL = nconf.get('GITHUB_UI_URL')
const GITHUB_API_URL = nconf.get('GITHUB_API_URL')
const HOST_ADDR = nconf.get('HOST_ADDR')
/**
* Serialize user data into the session.
*/
passport.serializeUser((data, done) => {
log(`serializeUser id: ${data.id}`)
done(null, data)
})
/**
* Deserialize user profile out of the session.
*/
passport.deserializeUser((data, done) => {
log(`deserializeUser id: ${data.id}`)
User.findById(data.id)
.then(user => user
? user.toJSON()
: null)
.then(user => user
? done(null, {...user, ...data})
: done(new Error(`no user for id ${data.id}`)))
passport.use('local-signup', new LocalStrategy(opts, wrap(function *(email, password, done) {
try {
const [user] = yield db.select('email').from('users').where({ email }).limit(1);
if (user) {
throw new EmailExistError();
}
const hash = yield hashAsync(password, 10);
const [id] = yield db('users').insert({ email, hash }, 'id');
done(null, { id });
} catch (err) {
done(err);
}
})));
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(wrap(function *(id, done) {
try {
const [user] = yield db('users').select('id').where({ id }).limit(1);
if (!user) {
throw new Error('user does not exist');
}
done(null, user);
} catch (err) {
done(err);
}
}));
export const authInit = passport.initialize();
initStrategies() {
if (this.strategies.length > 0) {
this.parent.parent.logger.info(
"[VelopServer][Passport] Initialize " +
this.strategies.length +
" passport strategies"
);
this.parent.app.use(passport.initialize());
this.parent.app.use(passport.session());
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));
this.strategies.map(strategy => passport.use(strategy));
}
}
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
import passport from 'koa-passport';
import PassportSlack from 'passport-slack';
import config from './config';
import db from './db';
import { save, getById, updateAccessToken } from './repositories/user';
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
getById(id)
.then(user => done(null, user))
.catch(done);
});
const handleUser = (accessToken, refreshToken, profile, done) => {
profile.accessToken = accessToken;
profile.refreshToken = refreshToken;
return getById(profile.id)
.then(user => {
if (!user) {
const user = {
import { User } from 'models/users'
import { Strategy as LocalStrategy } from 'passport-local'
import FacebookTokenStrategy from 'passport-facebook-token'
import passport from 'koa-passport'
import config from './index'
passport.serializeUser((user, done) => {
done(null, user.id)
})
passport.deserializeUser(async (id, done) => {
try {
const user = await User.where({ id }).fetch()
done(null, user)
} catch (err) {
done(err)
}
})
passport.use('local', new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
}, async (email, password, done) => {
import passport from 'koa-passport'
import db from '../src/models/index'
import { Strategy } from 'passport-local'
passport.serializeUser((user, done) => {
done(null, user.id)
})
passport.deserializeUser(async (id, done) => {
try {
const user = await db.user.findOne({
where: {
id: id
},
attributes: { exclude: ['password'] }
})
done(null, user)
} catch (err) {
done(err)
}
})
'use strict';
import passport from 'koa-passport';
import compose from 'koa-compose';
import importDir from 'import-dir';
import User from '../models/user';
import { prefix } from '../api/config';
import * as provider from './provider';
const strategies = importDir('./strategies');
Object.keys(strategies).forEach(name => {
passport.use(name, strategies[name]);
});
passport.serializeUser((user, done) => done(null, user._id));
passport.deserializeUser((id, done) => {
(async () => {
try {
const user = await User.findById(id);
done(null, user);
} catch (error) {
done(error);
}
})();
});
export default function auth() {
return compose([
passport.initialize(),
passport.session(),
const configurePassport = async () => {
passport.use('oidc', await oidc.getPassportStrategy());
passport.serializeUser(handleSerializeUser);
passport.deserializeUser(handleDeserializeUser);
};