Skip to content

Commit

Permalink
Sepparate auth to reduce code
Browse files Browse the repository at this point in the history
  • Loading branch information
PidgeyL committed Feb 10, 2017
1 parent 69f3d1c commit c4772a1
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions web/advanced_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,22 @@ def __init__(self):
#############
# Decorator #
#############
def getAuth():
method, auth = (request.headers.get('Authorization')+" ").split(" ", 1) # Adding and removing space to ensure decent split
name, key = (':'+auth.strip()).rsplit(":", 1)
name = name[1:] # Adding and removing colon to ensure decent split
return method, name, key

def authErrors():
# Check auth
if not request.headers.get('Authorization'):
return ({'status': 'error', 'reason': 'Authentication needed'}, 401)
method, auth = (request.headers.get('Authorization')+" ").split(" ", 1) # Adding and removing space to ensure decent split
method, name, token = Advanced_API.getAuth()
data = None
if method.lower() not in ['basic', 'token']:
data = ({'status': 'error', 'reason': 'Authorization method not allowed'}, 400)
else:
try:
name, token = (':'+auth.strip()).rsplit(":", 1)
name = name[1:] # Adding and removing colon to ensure decent split
if method.lower() == 'basic':
authenticator = AuthenticationHandler()
if not authenticator.validateUser(name, token): data = ({'status': 'error', 'reason': 'Authentication failed'}, 401)
Expand Down Expand Up @@ -145,16 +149,12 @@ def api_admin_remove_blacklist(self):

@token_required # Of course only the login credentials would work
def api_admin_get_token(self):
method, auth = (request.headers.get('Authorization')+" ").split(" ", 1)
name, token = (':'+auth.strip()).rsplit(":", 1)
name = name[1:]
method, name, key = Advanced_API.getAuth()
return db.getToken(name)

@token_required
def api_admin_generate_token(self):
method, auth = (request.headers.get('Authorization')+" ").split(" ", 1)
name, token = (':'+auth.strip()).rsplit(":", 1)
name = name[1:]
method, name, key = Advanced_API.getAuth()
return db.generateToken(name)

@token_required
Expand Down

0 comments on commit c4772a1

Please sign in to comment.