2017-03-13 15:03:26 +03:00
|
|
|
var _ = require('lodash'),
|
2017-12-14 05:01:23 +03:00
|
|
|
models = require('../../models'),
|
|
|
|
globalUtils = require('../../utils'),
|
|
|
|
common = require('../../lib/common'),
|
2017-12-14 15:26:48 +03:00
|
|
|
security = require('../../lib/security'),
|
2016-09-30 14:45:59 +03:00
|
|
|
strategies;
|
|
|
|
|
|
|
|
strategies = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ClientPasswordStrategy
|
|
|
|
*
|
|
|
|
* This strategy is used to authenticate registered OAuth clients. It is
|
|
|
|
* employed to protect the `token` endpoint, which consumers use to obtain
|
|
|
|
* access tokens. The OAuth 2.0 specification suggests that clients use the
|
|
|
|
* HTTP Basic scheme to authenticate (not implemented yet).
|
|
|
|
* Use of the client password strategy is implemented to support ember-simple-auth.
|
|
|
|
*/
|
|
|
|
clientPasswordStrategy: function clientPasswordStrategy(clientId, clientSecret, done) {
|
|
|
|
return models.Client.findOne({slug: clientId}, {withRelated: ['trustedDomains']})
|
|
|
|
.then(function then(model) {
|
|
|
|
if (model) {
|
|
|
|
var client = model.toJSON({include: ['trustedDomains']});
|
|
|
|
if (client.status === 'enabled' && client.secret === clientSecret) {
|
|
|
|
return done(null, client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return done(null, false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BearerStrategy
|
|
|
|
*
|
|
|
|
* This strategy is used to authenticate users based on an access token (aka a
|
|
|
|
* bearer token). The user must have previously authorized a client
|
|
|
|
* application, which is issued an access token to make requests on behalf of
|
|
|
|
* the authorizing user.
|
|
|
|
*/
|
|
|
|
bearerStrategy: function bearerStrategy(accessToken, done) {
|
|
|
|
return models.Accesstoken.findOne({token: accessToken})
|
|
|
|
.then(function then(model) {
|
|
|
|
if (model) {
|
|
|
|
var token = model.toJSON();
|
|
|
|
if (token.expires > Date.now()) {
|
|
|
|
return models.User.findOne({id: token.user_id})
|
|
|
|
.then(function then(model) {
|
2017-03-13 15:03:26 +03:00
|
|
|
if (!model) {
|
|
|
|
return done(null, false);
|
2016-09-30 14:45:59 +03:00
|
|
|
}
|
2017-03-13 15:03:26 +03:00
|
|
|
|
|
|
|
if (!model.isActive()) {
|
2017-12-12 00:47:46 +03:00
|
|
|
throw new common.errors.NoPermissionError({
|
|
|
|
message: common.i18n.t('errors.models.user.accountSuspended')
|
2017-03-13 15:03:26 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = model.toJSON(),
|
|
|
|
info = {scope: '*'};
|
|
|
|
|
|
|
|
return done(null, {id: user.id}, info);
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
return done(err);
|
2016-09-30 14:45:59 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return done(null, false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return done(null, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ghost Strategy
|
2016-10-03 17:11:43 +03:00
|
|
|
* ghostAuthRefreshToken: will be null for now, because we don't need it right now
|
2016-09-30 14:45:59 +03:00
|
|
|
*
|
|
|
|
* CASES:
|
|
|
|
* - via invite token
|
2017-03-13 15:03:26 +03:00
|
|
|
* - via normal sign in
|
2016-09-30 14:45:59 +03:00
|
|
|
* - via setup
|
|
|
|
*/
|
2016-10-03 17:11:43 +03:00
|
|
|
ghostStrategy: function ghostStrategy(req, ghostAuthAccessToken, ghostAuthRefreshToken, profile, done) {
|
2016-09-30 14:45:59 +03:00
|
|
|
var inviteToken = req.body.inviteToken,
|
|
|
|
options = {context: {internal: true}},
|
2017-03-13 15:03:26 +03:00
|
|
|
handleInviteToken, handleSetup, handleSignIn;
|
2016-09-30 14:45:59 +03:00
|
|
|
|
2017-01-31 13:04:09 +03:00
|
|
|
// CASE: socket hangs up for example
|
|
|
|
if (!ghostAuthAccessToken || !profile) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return done(new common.errors.NoPermissionError({
|
2017-01-31 13:04:09 +03:00
|
|
|
help: 'Please try again.'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-09-30 14:45:59 +03:00
|
|
|
handleInviteToken = function handleInviteToken() {
|
|
|
|
var user, invite;
|
2017-12-14 15:26:48 +03:00
|
|
|
inviteToken = security.url.decodeBase64(inviteToken);
|
2016-09-30 14:45:59 +03:00
|
|
|
|
|
|
|
return models.Invite.findOne({token: inviteToken}, options)
|
|
|
|
.then(function addInviteUser(_invite) {
|
|
|
|
invite = _invite;
|
|
|
|
|
|
|
|
if (!invite) {
|
2017-12-12 00:47:46 +03:00
|
|
|
throw new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('errors.api.invites.inviteNotFound')
|
2017-03-13 15:03:26 +03:00
|
|
|
});
|
2016-09-30 14:45:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (invite.get('expires') < Date.now()) {
|
2017-12-12 00:47:46 +03:00
|
|
|
throw new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('errors.api.invites.inviteExpired')
|
2017-03-13 15:03:26 +03:00
|
|
|
});
|
2016-09-30 14:45:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return models.User.add({
|
2016-11-08 17:21:25 +03:00
|
|
|
email: profile.email,
|
2017-04-04 16:56:04 +03:00
|
|
|
name: profile.name,
|
2017-12-11 21:14:05 +03:00
|
|
|
password: globalUtils.uid(50),
|
2017-03-13 15:03:26 +03:00
|
|
|
roles: [invite.toJSON().role_id],
|
|
|
|
ghost_auth_id: profile.id,
|
|
|
|
ghost_auth_access_token: ghostAuthAccessToken
|
2016-09-30 14:45:59 +03:00
|
|
|
}, options);
|
|
|
|
})
|
|
|
|
.then(function destroyInvite(_user) {
|
|
|
|
user = _user;
|
|
|
|
return invite.destroy(options);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return user;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
handleSetup = function handleSetup() {
|
2017-02-27 22:07:00 +03:00
|
|
|
return models.User.findOne({slug: 'ghost-owner', status: 'inactive'}, options)
|
2016-09-30 14:45:59 +03:00
|
|
|
.then(function fetchedOwner(owner) {
|
|
|
|
if (!owner) {
|
2017-12-12 00:47:46 +03:00
|
|
|
throw new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('errors.models.user.userNotFound')
|
2017-03-13 15:03:26 +03:00
|
|
|
});
|
2016-09-30 14:45:59 +03:00
|
|
|
}
|
|
|
|
|
2017-04-04 16:56:04 +03:00
|
|
|
// CASE: slug null forces regenerating the slug (ghost-owner is default and needs to be overridden)
|
2016-09-30 14:45:59 +03:00
|
|
|
return models.User.edit({
|
2016-11-08 17:21:25 +03:00
|
|
|
email: profile.email,
|
2017-04-04 16:56:04 +03:00
|
|
|
name: profile.name,
|
|
|
|
slug: null,
|
2017-03-13 15:03:26 +03:00
|
|
|
status: 'active',
|
|
|
|
ghost_auth_id: profile.id,
|
|
|
|
ghost_auth_access_token: ghostAuthAccessToken
|
2016-09-30 14:45:59 +03:00
|
|
|
}, _.merge({id: owner.id}, options));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-03-13 15:03:26 +03:00
|
|
|
handleSignIn = function handleSignIn() {
|
|
|
|
var user;
|
|
|
|
|
|
|
|
return models.User.findOne({ghost_auth_id: profile.id}, options)
|
|
|
|
.then(function (_user) {
|
|
|
|
user = _user;
|
|
|
|
|
|
|
|
if (!user) {
|
2017-12-12 00:47:46 +03:00
|
|
|
throw new common.errors.NotFoundError();
|
2017-03-13 15:03:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!user.isActive()) {
|
2017-12-12 00:47:46 +03:00
|
|
|
throw new common.errors.NoPermissionError({
|
|
|
|
message: common.i18n.t('errors.models.user.accountSuspended')
|
2017-03-13 15:03:26 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.User.edit({
|
|
|
|
email: profile.email,
|
2017-04-04 16:56:04 +03:00
|
|
|
name: profile.name,
|
2017-03-13 15:03:26 +03:00
|
|
|
ghost_auth_id: profile.id,
|
|
|
|
ghost_auth_access_token: ghostAuthAccessToken
|
|
|
|
}, _.merge({id: user.id}, options));
|
|
|
|
})
|
|
|
|
.then(function () {
|
2016-09-30 14:45:59 +03:00
|
|
|
return user;
|
2017-03-13 15:03:26 +03:00
|
|
|
});
|
|
|
|
};
|
2016-09-30 14:45:59 +03:00
|
|
|
|
2017-03-13 15:03:26 +03:00
|
|
|
if (inviteToken) {
|
|
|
|
return handleInviteToken()
|
|
|
|
.then(function (user) {
|
|
|
|
done(null, user, profile);
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
}
|
2016-09-30 14:45:59 +03:00
|
|
|
|
2017-03-13 15:03:26 +03:00
|
|
|
handleSignIn()
|
|
|
|
.then(function (user) {
|
2016-09-30 14:45:59 +03:00
|
|
|
done(null, user, profile);
|
|
|
|
})
|
2017-03-13 15:03:26 +03:00
|
|
|
.catch(function (err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
if (!(err instanceof common.errors.NotFoundError)) {
|
2017-03-13 15:03:26 +03:00
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSetup()
|
|
|
|
.then(function (user) {
|
|
|
|
done(null, user, profile);
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2016-09-30 14:45:59 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = strategies;
|