Skip to content

Commit

Permalink
Persist tokens to a text file
Browse files Browse the repository at this point in the history
  • Loading branch information
ML-Chen committed Apr 20, 2020
1 parent d020a47 commit f6ae6fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tokens.txt

*.pyc
/backend
/backend/db.sqlite3
Expand Down
10 changes: 8 additions & 2 deletions server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ class Auth(NamedTuple):
user_type: str

# Maps tokens to their username and user type
# TODO: store tokens in and retrieve tokens from database. Not that important. Or, load tokens from a local text file so that whenever I restart the server I don't have to log users back in again.
tokens: Dict[str, Auth] = {}
# Security warning: please ensure that tokens.txt contains no malicious Python code and is a normal dictionary
try:
with open('tokens.txt', 'r') as f:
tokens: Dict[str, Auth] = eval(f.readline())
except IOError as e:
tokens = {}

def db_api(procedure: str, http_methods: List[str], inputs: List[Tuple[str, Dict[str, Any]]], get_result: int = 0,
restrict_by_username: bool = False, restrict_by_food_truck: bool = False) -> Callable[[None], Response]:
Expand Down Expand Up @@ -140,6 +144,8 @@ def new_api() -> Response:
response = jsonify(result)
response.set_cookie('token', new_token)
print(f'Tokens: {tokens}')
with open('tokens.txt', 'w+') as f:
f.write(str(tokens))
else:
response = jsonify(result)
print(f'Result: {result}')
Expand Down

0 comments on commit f6ae6fe

Please sign in to comment.