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

Commit

Permalink
Update cars_app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ikostan committed Oct 5, 2019
1 parent 3b69930 commit ad99305
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions api/cars_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@
"""

import os
import sys
import logging
import random
import sys
import flask
from functools import wraps
from flask import Flask, request, jsonify, abort, render_template

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from api.helper import Helper
from data.cars import Cars
from data.users import Users


app = Flask(__name__)
# write logs for app filehandler of logging module
# is not creating log directory if dir does not exist
Expand All @@ -41,19 +44,7 @@
USER_LIST = Users().get_users()
REGISTERED_CARS = []


def check_auth(username, password):
"""
check if the given is valid
:param username:
:param password:
:return:
"""
user = [user for user in USER_LIST if user['name']
== username and user['password'] == password]
if len(user) == 1:
return True
return False
check_auth = Helper.check_auth


def authenticate_error(auth_flag):
Expand All @@ -79,6 +70,7 @@ def requires_auth(f):
:param f:
:return:
"""

@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
Expand All @@ -87,9 +79,10 @@ def decorated(*args, **kwargs):
if not auth:
auth_flag = False
return authenticate_error(auth_flag)
elif not check_auth(auth.username, auth.password):
elif not check_auth(auth.username, auth.password, USER_LIST):
return authenticate_error(auth_flag)
return f(*args, **kwargs)

return decorated


Expand Down

0 comments on commit ad99305

Please sign in to comment.