Skip to content

Commit

Permalink
Passport Fix (#1955)
Browse files Browse the repository at this point in the history
* fix: Fixed bug in passport access

* fix: resolved issues with postMessage and static urls
  • Loading branch information
wyattjoh committed Oct 2, 2018
1 parent 6bff2de commit 7b97a8f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion client/coral-auth-callback/src/index.js
Expand Up @@ -4,7 +4,7 @@ import { createPostMessage } from 'coral-framework/services/postMessage';

document.addEventListener('DOMContentLoaded', () => {
const staticConfig = getStaticConfiguration();
const { STATIC_ORIGIN: origin } = staticConfig;
const { BASE_ORIGIN: origin } = staticConfig;
const postMessage = createPostMessage(origin);

// Get the auth element and parse it as JSON by decoding it.
Expand Down
2 changes: 1 addition & 1 deletion client/coral-framework/services/bootstrap.js
Expand Up @@ -136,7 +136,7 @@ export async function createContext({
});

const staticConfig = getStaticConfiguration();
let { LIVE_URI: liveUri, STATIC_ORIGIN: origin } = staticConfig;
let { LIVE_URI: liveUri, BASE_ORIGIN: origin } = staticConfig;
if (liveUri == null) {
// The protocol must match the origin protocol, secure/insecure.
const protocol = location.protocol === 'https:' ? 'wss' : 'ws';
Expand Down
2 changes: 2 additions & 0 deletions middleware/staticTemplate.js
Expand Up @@ -5,6 +5,7 @@ const { merge } = require('lodash');

const {
BASE_URL,
BASE_ORIGIN,
BASE_PATH,
MOUNT_PATH,
STATIC_URL,
Expand All @@ -29,6 +30,7 @@ const TALK_CLIENT_ENV = Object.keys(process.env)
LIVE_URI: WEBSOCKET_LIVE_URI,
STATIC_URL,
STATIC_ORIGIN,
BASE_ORIGIN,
}
);

Expand Down
15 changes: 10 additions & 5 deletions services/passport.js
Expand Up @@ -19,6 +19,7 @@ const ms = require('ms');
const _ = require('lodash');
const { attachStaticLocals } = require('../middleware/staticTemplate');
const { encodeJSONForHTML } = require('./response');
const { STATIC_URL, BASE_URL } = require('../url');

// Create a redis client to use for authentication.
const { createClientFactory } = require('./redis');
Expand Down Expand Up @@ -97,10 +98,14 @@ const HandleGenerateCredentials = (req, res, next) => (err, user) => {
res.json({ user, token });
};

const generateAuthPopupCallbackCSP = req =>
req.locals.STATIC_URL && req.locals.BASE_URL !== req.locals.STATIC_URL
? `default-src 'self' ${req.locals.STATIC_URL};`
: "default-src 'self';";
/**
* authPopupCallbackCSP is the header sent via Content-Security-Policy when
* a social callback request is being made.
*/
const authPopupCallbackCSP = (() =>
STATIC_URL && BASE_URL !== STATIC_URL
? `default-src 'self' ${STATIC_URL.replace(/\/$/, '')};`
: "default-src 'self';")();

/**
* Returns the response to the login attempt via a popup callback with some JS.
Expand All @@ -111,7 +116,7 @@ const HandleAuthPopupCallback = (req, res, next) => (err, user) => {
res.header('Pragma', 'no-cache');

// Ensure the only scripts that can run here are those on the Talk domain.
res.header('Content-Security-Policy', generateAuthPopupCallbackCSP(req));
res.header('Content-Security-Policy', authPopupCallbackCSP);

// Attach static locals to the response locals object.
attachStaticLocals(res.locals);
Expand Down
3 changes: 3 additions & 0 deletions url.js
Expand Up @@ -11,6 +11,8 @@ const BASE_URL = trailingSlash(ROOT_URL);
// The BASE_PATH is simply the path component of the BASE_URL.
const BASE_PATH = new URL(BASE_URL).pathname;

const BASE_ORIGIN = new URL(BASE_URL).origin;

// The MOUNT_PATH is derived from the BASE_PATH, if it is provided and enabled.
// This will mount all the application routes onto it.
const MOUNT_PATH = ROOT_URL_MOUNT_PATH ? BASE_PATH : '/';
Expand All @@ -22,6 +24,7 @@ const STATIC_ORIGIN = new URL(STATIC_URI).origin;

module.exports = {
BASE_URL,
BASE_ORIGIN,
BASE_PATH,
MOUNT_PATH,
STATIC_URL,
Expand Down

0 comments on commit 7b97a8f

Please sign in to comment.