Browse Source

Fixed bug in ensure_logged_in decorator

pull/3/head
Kenneth Bruen 3 years ago
parent
commit
24ea084434
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 46
      server/login.py
  2. 3
      server/run.sh

46
server/login.py

@ -30,30 +30,42 @@ def make_login():
token = ram_db.login_user(user.id) token = ram_db.login_user(user.id)
return returns.success(token=token) return returns.success(token=token)
def ensure_logged_in(fn): def ensure_logged_in(token=False, user_id=False):
@wraps(fn) def decorator(fn):
def wrapper(*args, **kargs): pass_token = token
token = request.headers.get('Authorization', None) pass_user_id = user_id
if token is None: @wraps(fn)
return returns.NO_AUTHORIZATION def wrapper(*args, **kargs):
if not token.startswith('Bearer '): token = request.headers.get('Authorization', None)
return returns.INVALID_AUTHORIZATION if token is None:
token = token[7:] return returns.NO_AUTHORIZATION
user_id = ram_db.get_user(token) if not token.startswith('Bearer '):
if user_id is None: return returns.INVALID_AUTHORIZATION
return returns.INVALID_AUTHORIZATION token = token[7:]
return fn(user_id=user_id, token=token, *args, **kargs) user_id = ram_db.get_user(token)
return wrapper if user_id is None:
return returns.INVALID_AUTHORIZATION
if pass_user_id and pass_token:
return fn(user_id=user_id, token=token, *args, **kargs)
elif pass_user_id:
return fn(user_id=user_id, *args, **kargs)
elif pass_token:
return fn(token=token, *args, **kargs)
else:
return fn(*args, **kargs)
return wrapper
return decorator
@login.post('/logout') @login.post('/logout')
@ensure_logged_in @ensure_logged_in(token=True)
@no_content @no_content
def logout(token: str): def logout(token: str):
ram_db.logout_user(token) ram_db.logout_user(token)
@login.get('/whoami') @login.get('/whoami')
@ensure_logged_in @ensure_logged_in(user_id=True)
def whoami(user_id): def whoami(user_id: int):
user: models.User | None = db_utils.get_user(user_id=user_id) user: models.User | None = db_utils.get_user(user_id=user_id)
if user is not None: if user is not None:
user = user.to_json() user = user.to_json()

3
server/run.sh

@ -0,0 +1,3 @@
#! /usr/bin/env sh
docker-compose stop
PORT=14000 docker-compose up -d --build
Loading…
Cancel
Save