Skip to content

Commit

Permalink
Merge pull request #49 from GabrielInTheWorld/dev-env
Browse files Browse the repository at this point in the history
Fixes default auth-key in development environment
  • Loading branch information
GabrielInTheWorld committed Nov 12, 2020
2 parents d0d183c + d75168c commit cb3c6dd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ RUN pip install --no-cache-dir -r ./libraries/pip-auth/requirements.txt -r ./lib

EXPOSE 9004

ENV AUTH_TOKEN_KEY auth-dev-key
ENV AUTH_COOKIE_KEY auth-dev-key
ENV OS4_DEVELOPMENT 1

ENTRYPOINT [ "./entrypoint.sh" ]
CMD ["npm", "run", "dev"]
11 changes: 9 additions & 2 deletions auth/libraries/pip-auth/authlib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

from .exceptions import KeyException

AUTH_DEV_KEY = "auth-dev-key"
DEVELEOPMENT_VARIABLE = "OS4_DEVELOPMENT"


class Environment:
def __init__(self) -> None:
self.__load_keys()

def __load_keys(self) -> None:
auth_token_key = os.getenv("AUTH_TOKEN_KEY")
auth_cookie_key = os.getenv("AUTH_COOKIE_KEY")
default = AUTH_DEV_KEY if self.is_dev_mode() else None
auth_token_key = os.getenv("AUTH_TOKEN_KEY", default)
auth_cookie_key = os.getenv("AUTH_COOKIE_KEY", default)
if not auth_token_key:
raise KeyException("No AUTH_TOKEN_KEY defined.")

Expand All @@ -24,3 +28,6 @@ def get_token_key(self):

def get_cookie_key(self):
return self.auth_cookie_key

def is_dev_mode(self):
return os.getenv(DEVELEOPMENT_VARIABLE) == "1"
12 changes: 7 additions & 5 deletions auth/src/api/services/key-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Config } from '../../config';
import { KeyHandler } from '../interfaces/key-handler';

const AUTH_DEV_KEY = 'auth-dev-key';

export class KeyService extends KeyHandler {
protected tokenKey = '';
protected cookieKey = '';
Expand All @@ -20,15 +23,14 @@ export class KeyService extends KeyHandler {
}

private loadKeys(): void {
if (!process.env.AUTH_TOKEN_KEY) {
if (!process.env.AUTH_TOKEN_KEY && !Config.isDevMode()) {
throw new Error('No token key defined.');
}
this.tokenKey = process.env.AUTH_TOKEN_KEY;
this.tokenKey = process.env.AUTH_TOKEN_KEY || AUTH_DEV_KEY;

if (!process.env.AUTH_COOKIE_KEY) {
if (!process.env.AUTH_COOKIE_KEY && !Config.isDevMode()) {
throw new Error('No cookie key defined.');
}

this.cookieKey = process.env.AUTH_COOKIE_KEY;
this.cookieKey = process.env.AUTH_COOKIE_KEY || AUTH_DEV_KEY;
}
}
6 changes: 4 additions & 2 deletions auth/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'fs';

export namespace Config {
export const DATABASE_PATH = 'database/';
export const DATASTORE_READER = getReaderUrl();
Expand All @@ -10,4 +8,8 @@ export namespace Config {
}
return `http://${process.env.DATASTORE_READER_HOST}:${parseInt(process.env.DATASTORE_READER_PORT, 10)}`;
}

export function isDevMode(): boolean {
return process.env.OS4_DEVELOPMENT === '1';
}
}

0 comments on commit cb3c6dd

Please sign in to comment.