mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
no-issue * Added default for getting origin of request This function is used to attach the origin of the request to the session, and later check that requests using the session are coming from the same origin. This protects us against CSRF attacks as requests in the browser MUST originate from the same origin on which the user logged in. Previously, when we could not determine the origin we would return null, as a "safety" net. This updates the function to use a secure and sensible default - which is the origin of the Ghost-Admin application, and if that's not set - the origin of the Ghost application. This will make dealing with magic links simpler as you can not always guaruntee the existence of these headers when visiting via a hyperlink * Removed init fns and getters from session service This simplifies the code here, making it easier to read and maintain * Moved express-session initialisation to own file This is complex enough that it deserves its own module * Added createSessionFromToken to session service * Wired up the createSessionFromToken middleware
49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
const adapterManager = require('../../adapter-manager');
|
|
const createSessionService = require('@tryghost/session-service');
|
|
const sessionFromToken = require('@tryghost/mw-session-from-token');
|
|
const createSessionMiddleware = require('./middleware');
|
|
|
|
const expressSession = require('./express-session');
|
|
|
|
const models = require('../../../models');
|
|
const urlUtils = require('../../../lib/url-utils');
|
|
const url = require('url');
|
|
|
|
function getOriginOfRequest(req) {
|
|
const origin = req.get('origin');
|
|
const referrer = req.get('referrer') || urlUtils.getAdminUrl() || urlUtils.getSiteUrl();
|
|
|
|
if (!origin && !referrer) {
|
|
return null;
|
|
}
|
|
|
|
if (origin) {
|
|
return origin;
|
|
}
|
|
|
|
const {protocol, host} = url.parse(referrer);
|
|
if (protocol && host) {
|
|
return `${protocol}//${host}`;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
const sessionService = createSessionService({
|
|
getOriginOfRequest,
|
|
getSession: expressSession.getSession,
|
|
findUserById({id}) {
|
|
return models.User.findOne({id});
|
|
}
|
|
});
|
|
|
|
module.exports = createSessionMiddleware({sessionService});
|
|
|
|
const ssoAdapter = adapterManager.getAdapter('sso');
|
|
// Looks funky but this is a "custom" piece of middleware
|
|
module.exports.createSessionFromToken = sessionFromToken({
|
|
callNextWithError: false,
|
|
createSession: sessionService.createSessionForUser,
|
|
findUserByLookup: ssoAdapter.getUserForIdentity,
|
|
getLookupFromToken: ssoAdapter.getIdentityFromCredentials,
|
|
getTokenFromRequest: ssoAdapter.getRequestCredentials
|
|
});
|