Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,27 +219,33 @@ app.set('cosJwt', require('./libs/cosJwt')(app));
app.set('cosUpload', require('./libs/cosUpload')(app));

//Config smartId
const smartId = require('smart-id-rest')();
smartId.init({
hostname: config.services.smartId.hostname,
apiPath: config.services.smartId.apiPath,
authorizeToken: config.services.smartId.authorizeToken,
relyingPartyUUID: config.services.smartId.relyingPartyUUID,
replyingPartyName: config.services.smartId.replyingPartyName,
issuers: config.services.signature.certificates.issuers
});
app.set('smartId', smartId);
if (config.services.smartId) {
const smartId = require('smart-id-rest')();
smartId.init({
hostname: config.services.smartId.hostname,
apiPath: config.services.smartId.apiPath,
authorizeToken: config.services.smartId.authorizeToken,
relyingPartyUUID: config.services.smartId.relyingPartyUUID,
replyingPartyName: config.services.smartId.replyingPartyName,
issuers: config.services.signature.certificates.issuers
});
app.set('smartId', smartId);
}

//Config mobiilId
const mobileId = require('mobiil-id-rest')();
mobileId.init({
hostname: config.services.mobileId.hostname,
apiPath: config.services.mobileId.apiPath,
authorizeToken: config.services.mobileId.authorizeToken,
relyingPartyUUID: config.services.mobileId.relyingPartyUUID,
replyingPartyName: config.services.mobileId.replyingPartyName,
issuers: config.services.signature.certificates.issuers
});
app.set('mobileId', mobileId);
if (config.services.mobileId) {
const mobileId = require('mobiil-id-rest')();
mobileId.init({
hostname: config.services.mobileId.hostname,
apiPath: config.services.mobileId.apiPath,
authorizeToken: config.services.mobileId.authorizeToken,
relyingPartyUUID: config.services.mobileId.relyingPartyUUID,
replyingPartyName: config.services.mobileId.replyingPartyName,
issuers: config.services.signature.certificates.issuers
});
app.set('mobileId', mobileId);
}

app.set('cosSignature', require('./libs/cosSignature')(app));

if (typeof config.email === 'string') {
Expand Down
84 changes: 44 additions & 40 deletions libs/passport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,50 +141,54 @@ module.exports = function (app) {
});

// GOOGLE
passport.use(new GoogleStrategy(
{
clientID: config.passport.google.clientId,
clientSecret: config.passport.google.clientSecret,
callbackURL: urlLib.getApi(config.passport.google.callbackUrl),
userProfileURL: 'https://www.googleapis.com/oauth2/v3/userinfo',
passReqToCallback: true // http://passportjs.org/guide/authorize/#association_in_verify_callback
},
async function (req, accessToken, refreshToken, profile, done) {
logger.debug('Google responded with profile: ', profile);

try {
const user = await _findOrCreateUser(UserConnection.CONNECTION_IDS.google, User.SOURCES.google, profile, req);

return done(null, user.toJSON());
} catch (err) {
console.log(err);
done(err);
if (passport.google) {
passport.use(new GoogleStrategy(
{
clientID: config.passport.google.clientId,
clientSecret: config.passport.google.clientSecret,
callbackURL: urlLib.getApi(config.passport.google.callbackUrl),
userProfileURL: 'https://www.googleapis.com/oauth2/v3/userinfo',
passReqToCallback: true // http://passportjs.org/guide/authorize/#association_in_verify_callback
},
async function (req, accessToken, refreshToken, profile, done) {
logger.debug('Google responded with profile: ', profile);

try {
const user = await _findOrCreateUser(UserConnection.CONNECTION_IDS.google, User.SOURCES.google, profile, req);

return done(null, user.toJSON());
} catch (err) {
console.log(err);
done(err);
}
}
}
));
));
}

// FACEBOOK
passport.use(new FacebookStrategy(
{
clientID: config.passport.facebook.clientId,
clientSecret: config.passport.facebook.clientSecret,
callbackURL: urlLib.getApi(config.passport.facebook.callbackUrl),
enableProof: false,
profileFields: ['id', 'displayName', 'cover', 'email'],
passReqToCallback: true
},
async function (req, accessToken, refreshToken, profile, done) {
logger.info('Facebook responded with profile: ', profile);
try {
const user = await _findOrCreateUser(UserConnection.CONNECTION_IDS.facebook, User.SOURCES.facebook, profile, req);

return done(null, user.toJSON());
} catch (err) {
console.log(err);
done(err);
if (passport.facebook) {
passport.use(new FacebookStrategy(
{
clientID: config.passport.facebook.clientId,
clientSecret: config.passport.facebook.clientSecret,
callbackURL: urlLib.getApi(config.passport.facebook.callbackUrl),
enableProof: false,
profileFields: ['id', 'displayName', 'cover', 'email'],
passReqToCallback: true
},
async function (req, accessToken, refreshToken, profile, done) {
logger.info('Facebook responded with profile: ', profile);
try {
const user = await _findOrCreateUser(UserConnection.CONNECTION_IDS.facebook, User.SOURCES.facebook, profile, req);

return done(null, user.toJSON());
} catch (err) {
console.log(err);
done(err);
}
}
}
));
));
}

// LOCAL
passport.use(new LocalStrategy(
Expand Down
89 changes: 46 additions & 43 deletions routes/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -887,55 +887,58 @@ module.exports = function (app) {
};


// Passport endpoints
app.get(config.passport.google.url, function (req, res, next) {
// Store original request in a cookie, so that we know where to redirect back on callback from the partner (FB, Google...)
setStateCookie(req, res, COOKIE_NAME_COS_AUTH_STATE);

passport.authenticate('google', {
scope: ['https://www.googleapis.com/auth/userinfo.email'],
prompt: 'select_account',
keepSessionInfo: true
})(req, res, next);
});
if (config.passport.google) {
// Passport endpoints
app.get(config.passport.google.url, function (req, res, next) {
// Store original request in a cookie, so that we know where to redirect back on callback from the partner (FB, Google...)
setStateCookie(req, res, COOKIE_NAME_COS_AUTH_STATE);

passport.authenticate('google', {
scope: ['https://www.googleapis.com/auth/userinfo.email'],
prompt: 'select_account',
keepSessionInfo: true
})(req, res, next);
});


app.get(
config.passport.google.callbackUrl,
passport.authenticate('google', {
failureRedirect: urlLib.getFe('/account/login'),
keepSessionInfo: true
}),
function (req, res) {
setAuthCookie(req, res, req.user.id);
handleCallbackRedirect(req, res);
}
);

app.get(
config.passport.google.callbackUrl,
passport.authenticate('google', {
failureRedirect: urlLib.getFe('/account/login'),
keepSessionInfo: true
}),
function (req, res) {
setAuthCookie(req, res, req.user.id);
handleCallbackRedirect(req, res);
}
);
}

app.get(config.passport.facebook.url, function (req, res, next) {
// Store original request in a cookie, so that we know where to redirect back on callback from the partner (FB, Google...)
setStateCookie(req, res, COOKIE_NAME_COS_AUTH_STATE);
if (config.passport.google) {
app.get(config.passport.facebook.url, function (req, res, next) {
// Store original request in a cookie, so that we know where to redirect back on callback from the partner (FB, Google...)
setStateCookie(req, res, COOKIE_NAME_COS_AUTH_STATE);

passport.authenticate('facebook', {
scope: ['email'],
keepSessionInfo: true,
display: req.query.display ? 'popup' : null
})(req, res, next);
});
passport.authenticate('facebook', {
scope: ['email'],
keepSessionInfo: true,
display: req.query.display ? 'popup' : null
})(req, res, next);
});


app.get(
config.passport.facebook.callbackUrl,
passport.authenticate('facebook', {
failureRedirect: urlLib.getFe('/account/login'),
keepSessionInfo: true
}),
function (req, res) {
setAuthCookie(req, res, req.user.id);
handleCallbackRedirect(req, res);
}
);
app.get(
config.passport.facebook.callbackUrl,
passport.authenticate('facebook', {
failureRedirect: urlLib.getFe('/account/login'),
keepSessionInfo: true
}),
function (req, res) {
setAuthCookie(req, res, req.user.id);
handleCallbackRedirect(req, res);
}
);
}


/**
Expand Down