Skip to content

Commit

Permalink
feat: Authentication can be optionally disabled (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumare3 authored Aug 24, 2020
1 parent c8b3c37 commit d92a127
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ const PLUGINS_MODULE = process.env.PLUGINS_MODULE;
// If it has no protocol, it will be treated as relative to window.location.origin
const STATUS_URL = process.env.STATUS_URL;

const DISABLE_AUTH = process.env.DISABLE_AUTH;

module.exports = {
ADMIN_API_URL,
BASE_URL,
// Config env isn't sent to the client, so excluded from processEnv
CONFIG_CACHE_TTL_SECONDS,
CONFIG_DIR,
CORS_PROXY_PREFIX,
DISABLE_AUTH,
NODE_ENV,
PLUGINS_MODULE,
STATUS_URL,
processEnv: {
ADMIN_API_URL,
BASE_URL,
CORS_PROXY_PREFIX,
DISABLE_AUTH,
NODE_ENV,
STATUS_URL
}
Expand Down
10 changes: 10 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,13 @@ export function assertNever(
`Unhandled discriminated union member: ${JSON.stringify(value)}`
);
}

/**
* Function that returns boolean value for the string or number representation
*/
export function toBoolean(value?: string): boolean {
if (value == null) {
return false;
}
return ['true', 'True', 'TRUE', '1'].includes(value);
}
1 change: 1 addition & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface Env extends NodeJS.ProcessEnv {
CORS_PROXY_PREFIX: string;
DISABLE_ANALYTICS?: string;
STATUS_URL?: string;
DISABLE_AUTH?: string;
}
7 changes: 5 additions & 2 deletions src/models/AdminEntity/AdminEntity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import axios, { AxiosRequestConfig } from 'axios';
import { env } from 'common/env';
import { toBoolean } from 'common/utils';

import { generateAdminApiQuery } from './AdminApiQuery';

import { transformRequestError } from './transformRequestError';
import {
AdminEntityTransformer,
Expand Down Expand Up @@ -44,7 +47,7 @@ async function request(
const finalOptions = {
...options,
url: adminApiUrl(endpoint),
withCredentials: true
withCredentials: !toBoolean(env.DISABLE_AUTH)
};

try {
Expand All @@ -70,7 +73,7 @@ export async function getProtobufObject<ResponseType>(
headers,
method: 'get',
responseType: 'arraybuffer',
withCredentials: true
withCredentials: !toBoolean(env.DISABLE_AUTH)
};

const { data } = await axios.request(options);
Expand Down

0 comments on commit d92a127

Please sign in to comment.