Skip to content

Commit

Permalink
Merge pull request #302 from samkelleher/trunk
Browse files Browse the repository at this point in the history
feat: Enable mock TLS termination on calls to Hydra
  • Loading branch information
kieckhafer authored Aug 5, 2020
2 parents a4a0f34 + c9552f1 commit 02a2fe1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 5 additions & 1 deletion imports/plugins/core/core/server/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import envalid from "envalid";

const { num, str } = envalid;
const { num, str, bool } = envalid;

export default envalid.cleanEnv(process.env, {
MOCK_TLS_TERMINATION: bool({
default: false,
desc: "The OAuth server requires 'X-Forwarded-Proto' header to be set to 'https'."
}),
OAUTH2_ADMIN_URL: str({
desc: "An OAuth2 OpenID Connect compliant URL",
example: "http://hydra.reaction.localhost:4445"
Expand Down
16 changes: 12 additions & 4 deletions imports/plugins/core/core/server/startup/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import { Meteor } from "meteor/meteor";
import config from "../config.js";

const {
MOCK_TLS_TERMINATION,
OAUTH2_ADMIN_URL,
OAUTH2_CLIENT_ID,
ROOT_URL
} = config;

let mockTlsTermination = {};
if (MOCK_TLS_TERMINATION) {
mockTlsTermination = {
"X-Forwarded-Proto": "https"
};
}

const makeAbsolute = (relativeUrl, baseUrl = ROOT_URL) => {
const url = new URL(relativeUrl, baseUrl);
return url.href;
Expand Down Expand Up @@ -52,7 +60,7 @@ export async function ensureHydraClient() {

const getClientResponse = await fetch(makeAbsolute(`/clients/${OAUTH2_CLIENT_ID}`, OAUTH2_ADMIN_URL), {
method: "GET",
headers: { "Content-Type": "application/json" }
headers: { "Content-Type": "application/json", ...mockTlsTermination }
});

if (![200, 404].includes(getClientResponse.status)) {
Expand All @@ -66,7 +74,7 @@ export async function ensureHydraClient() {

const updateClientResponse = await fetch(makeAbsolute(`clients/${OAUTH2_CLIENT_ID}`, OAUTH2_ADMIN_URL), {
method: "PUT",
headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json", ...mockTlsTermination },
body: JSON.stringify(hydraClient)
});

Expand All @@ -81,7 +89,7 @@ export async function ensureHydraClient() {

const response = await fetch(makeAbsolute("/clients", OAUTH2_ADMIN_URL), {
method: "POST",
headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json", ...mockTlsTermination },
body: JSON.stringify(hydraClient)
});

Expand Down Expand Up @@ -110,7 +118,7 @@ export async function ensureHydraClient() {
*/
export async function expandAuthToken(token) {
const response = await fetch(makeAbsolute("/oauth2/introspect", OAUTH2_ADMIN_URL), {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
headers: { "Content-Type": "application/x-www-form-urlencoded", ...mockTlsTermination },
method: "POST",
body: `token=${encodeURIComponent(token)}`
});
Expand Down

0 comments on commit 02a2fe1

Please sign in to comment.