Skip to content

Commit

Permalink
change auth method
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhsherl committed Jun 14, 2019
1 parent f436db5 commit 6684ab0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
1 change: 0 additions & 1 deletion core/server/api/v0.1/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ authentication = {

function processInvitation(invitation) {
const data = invitation.user[0];
console.log(data);
return rcUtils.getUser(rc_uid, rc_token, data.rc_username)
.then((user) => {
if (user.success && user.user) {
Expand Down
2 changes: 2 additions & 0 deletions core/server/api/v2/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const session = {
*/
return models.User.findOne({id: options.context.user});
},

add(object) {
if (!object || !object.rc_id || !object.rc_token) {
return Promise.reject(new common.errors.UnauthorizedError({
Expand Down Expand Up @@ -53,6 +54,7 @@ const session = {
});
});
},

delete() {
return Promise.resolve((req, res, next) => {
auth.session.destroySession(req, res, next);
Expand Down
35 changes: 33 additions & 2 deletions core/server/api/v2/utils/rc-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const Promise = require('bluebird');
const request = require('request');
const { forEach } = require('lodash');
const models = require('../../../models');
const auth = require('../../../services/auth');
const settingsCache = require('../../../services/settings/cache');
const common = require('../../../lib/common');

Expand All @@ -8,9 +11,7 @@ function getRCUrl() {
}

function buildMeUrl(url = null) {
console.log('hrere');
const base = url || getRCUrl();
console.log(base);
return base + '/api/v1/me';
}

Expand All @@ -25,6 +26,17 @@ function getHeader(id, token) {
};
}

function getIdToken(req) {
let id, token;
forEach(req.headers.cookie.split(';'), (v) => {
if (v.includes('rc_uid'))
id = v.split('=')[1];
if (v.includes('rc_token'))
token = v.split('=')[1];
});
return { id, token };
}

module.exports = {
checkAdmin(url, id, token) {
let user;
Expand Down Expand Up @@ -108,5 +120,24 @@ module.exports = {
resolve(user);
});
});
},

createSession(req) {
const { id, token } = getIdToken(req);
if (!id || !token)
return req;
return models.User.findOne({ rc_id: id }).then((user) => {
if (!user) {
return req;
}
return this.getMe(id, token)
.then((u) => {
if (!u.success) {
return req;
}
req.user = user;
return req;
});
});
}
};
1 change: 1 addition & 0 deletions core/server/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
ONE_DAY_MS: 86400000,
ONE_WEEK_MS: 604800000,
ONE_MONTH_MS: 2628000000,
THREE_MONTH_MS: 7795200000,
SIX_MONTH_MS: 15768000000,
ONE_YEAR_MS: 31536000000
};
34 changes: 28 additions & 6 deletions core/server/services/auth/session/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const session = require('express-session');
const common = require('../../../lib/common');
const constants = require('../../../lib/constants');
const config = require('../../../config');
const rcUtils = require('../../../api/v2/utils/rc-utils');
const settingsCache = require('../../settings/cache');
const models = require('../../../models');
const SessionStore = require('./store');
Expand Down Expand Up @@ -37,9 +38,9 @@ const getSession = (req, res, next) => {
saveUninitialized: false,
name: 'ghost-admin-api-session',
cookie: {
maxAge: constants.SIX_MONTH_MS,
httpOnly: true,
path: urlService.utils.getSubdir() + '/ghost',
maxAge: constants.THREE_MONTH_MS,
httpOnly: false,
path: urlService.utils.getSubdir() + '/',
sameSite: 'lax',
secure: urlService.utils.isSSL(config.get('url'))
}
Expand Down Expand Up @@ -85,10 +86,11 @@ const cookieCsrfProtection = (req) => {

const origin = getOrigin(req);

if (req.session.origin !== origin) {
// Check the origin allow Ghost and RC server_url
if (req.session.origin !== origin && settingsCache.get('server_url') !== origin) {
throw new common.errors.BadRequestError({
message: common.i18n.t('errors.middleware.auth.mismatchedOrigin', {
expected: req.session.origin,
expected: req.session.origin + ' OR ' + settingsCache.get('server_url'),
actual: origin
})
});
Expand Down Expand Up @@ -116,7 +118,27 @@ const authenticate = (req, res, next) => {

if (!req.session || !req.session.user_id) {
req.user = null;
return next();
return rcUtils.createSession(req)
.then((req) => {
if(req.user) {
getSession(req, res, function (err) {
if (err) {
return next(err);
}
const origin = getOrigin(req);
if (!origin) {
return next(new common.errors.BadRequestError({
message: common.i18n.t('errors.middleware.auth.unknownOrigin')
}));
}
req.session.user_id = req.user.id;
req.session.origin = origin;
req.session.user_agent = req.get('user-agent');
req.session.ip = req.ip;
});
}
return next();
});
}

models.User.findOne({id: req.session.user_id})
Expand Down

0 comments on commit 6684ab0

Please sign in to comment.