Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Angular frontend #16

Open
wants to merge 30 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ celerybeat-schedule
# SageMath parsed files
*.sage.py

# dotenv
# Environment variable stuff
.env
environment.*.ts

# virtualenv
.venv
Expand Down
37 changes: 15 additions & 22 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,33 @@
"name": "bi-redirector",
"scripts": {},
"env": {
"BOX_CLIENT_ID": {
"required": true
"AUTH0_CLIENT_ID": {
"required": "true"
},
"BOX_CLIENT_SECRET": {
"required": true
"AUTH0_DOMAIN": {
"required": "true"
},
"BOX_DESTINATION_FOLDER": {
"required": true
"AUTH0_AUDIENCE": {
"required": "true"
},
"DOWNLOAD_PATH": {
"required": true
"AUTH0_CLIENT_SECRET": {
"required": "true"
},
"FILE_LIST": {
"required": true
"AUTH0_CALLBACK_URL": {
"value": "http://localhost:5000/api/authcallback"
},
"PORT": {
"required": true
},
"REPORT_PASSWORD": {
"required": true
},
"REPORT_SERVER": {
"required": true
},
"REPORT_USERNAME": {
"required": true
"SECRET_KEY": {
"generator": "secret"
}
},
"formation": {},
"addons": [
"papertrail",
"heroku-redis"
],
"buildpacks": [
{
"url": "heroku/nodejs"
},
{
"url": "heroku/python"
}
Expand Down
10 changes: 5 additions & 5 deletions biredirect/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
Import class
"""
from biredirect.boxstores import BoxKeysStoreRedis # noqa
from biredirect.redirector import APP # noqa
from biredirect.reportserver import ReportServer # noqa
from biredirect.settings import * # noqa
from biredirect.utils import * # noqa
from biredirect.dbservice import DBService # noqa
from biredirect.settings import REDIS_URL, SECRET_KEY # noqa

# Start shared database service
DB = DBService(REDIS_URL)
3 changes: 1 addition & 2 deletions biredirect/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import sys

from biredirect.redirector import APP
from biredirect.settings import PORT


def main():
flask_app = APP
if os.environ.get('DEBUG'):
flask_app.debug = True

flask_app.run('0.0.0.0', port=PORT, use_reloader=False, debug=True)
flask_app.run('0.0.0.0', use_reloader=False, debug=True)


# Only run if script is run directly and not by an import
Expand Down
34 changes: 23 additions & 11 deletions biredirect/boxstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
"""
from boxsdk import OAuth2

from biredirect.settings import BOX_CLIENT_ID, BOX_CLIENT_SECRET, REDIS_DB
from biredirect import DB
from biredirect.utils import get_config


class BoxKeysStoreRedis:
"""
Read/save tokens from Heroku Redis
"""
@classmethod
def get_oauth(cls):

def __init__(self, client_id=None, client_secret=None):
if client_id:
self._client_id = client_id
else:
self._client_id = get_config('BOX_CLIENT_ID')

if client_secret:
self._client_secret = client_secret
else:
self._client_secret = get_config('BOX_CLIENT_SECRET')

def get_oauth(self):
"""
Creates an Oauth object

Expand All @@ -20,20 +32,20 @@ def get_oauth(cls):
:rtype:
:class:`boxsdk.Oauth2`
"""
box_token = REDIS_DB.hgetall('box')
box_token = DB.get_hash_keys('box')
if 'accessToken' in box_token.keys():
oauth = OAuth2(
client_id=BOX_CLIENT_ID,
client_secret=BOX_CLIENT_SECRET,
store_tokens=cls._store_tokens,
client_id=self._client_id,
client_secret=self._client_secret,
store_tokens=self._store_tokens,
access_token=box_token['accessToken'],
refresh_token=box_token['refreshToken']
)
else:
oauth = OAuth2(
client_id=BOX_CLIENT_ID,
client_secret=BOX_CLIENT_SECRET,
store_tokens=cls._store_tokens
client_id=self._client_id,
client_secret=self._client_secret,
store_tokens=self._store_tokens
)
return oauth

Expand All @@ -49,4 +61,4 @@ def _store_tokens(cls, access_token, refresh_token):
data = {}
data['accessToken'] = access_token
data['refreshToken'] = refresh_token
REDIS_DB.hmset('box', data)
DB.set_hash_keys('box', data)
94 changes: 94 additions & 0 deletions biredirect/dbservice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from redis import StrictRedis

from biredirect.settings import REDIS_URL


class DBService(object):

LUA_NEW_ITEM = """
local r1 = redis.call("HMSET", KEYS[1] .. ":" .. KEYS[2], unpack(ARGV))
local r2 = redis.call("SADD", KEYS[1], KEYS[2])
return r1, r2
"""
CONFIG_KEY = "config"
BOX_KEY = "box"

def __init__(self, database_url=REDIS_URL):
self._redis = StrictRedis.from_url(database_url,
decode_responses=True)
# Register scripts
self._new_config = self._redis.register_script(self.LUA_NEW_ITEM)

# Config
def insert_config(self, data):
args = []
for key, value in data.items():
args.append(key)
args.append(value)
result = self._new_config(
keys=[self.CONFIG_KEY, data['name']],
args=args)
if result == 'OK':
return self.get_config(data['name'])
self.delete_config(data['name'])
return None

def get_configs(self):
config_names = self._redis.smembers(self.CONFIG_KEY)
pipe = self._redis.pipeline()
for config_name in config_names:
pipe.hgetall(f'{self.CONFIG_KEY}:{config_name}')

configs = pipe.execute()
for config in configs:
if 'secure' in config:
config['secure'] = config['secure'] == 'True'
return configs

def get_config(self, config_name):
config = self.get_hash_keys(f'{self.CONFIG_KEY}:{config_name}')
if 'secure' in config:
config['secure'] = config['secure'] == 'True'
return config

def get_config_value(self, config_name):
return self.get_hash_key(f'{self.CONFIG_KEY}:{config_name}', 'value')

def update_config(self, config_name, data):
args = []
for key, value in data.items():
args.append(key)
args.append(value)
if self.set_hash_keys(f'{self.CONFIG_KEY}:{config_name}', data):
config = self.get_config(config_name)
return config
return None

def delete_config(self, config_name):
pipe = self._redis.pipeline()
pipe.delete(f'{self.CONFIG_KEY}:{config_name}')
pipe.srem(self.CONFIG_KEY, config_name)
result = pipe.execute()
if result == [1, 1]:
return 'Ok'
return None

def config_exists(self, config_name):
return self._redis.sismember(self.CONFIG_KEY, config_name) == 1

# Box
def set_crsf_token(self, csrf_token):
self._redis.hset(self.BOX_KEY, 'csrf_token', csrf_token)

def get_crsf_token(self):
return self.get_hash_key(self.BOX_KEY, 'csrf_token')

# Common
def get_hash_keys(self, name):
return self._redis.hgetall(name)

def get_hash_key(self, name, key):
return self._redis.hget(name, key)

def set_hash_keys(self, name, mapping):
return self._redis.hmset(name, mapping)
Loading