Skip to content
Merged
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
16 changes: 2 additions & 14 deletions app/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from app.models.notification import PASSWORD_CHANGE as PASSWORD_CHANGE_NOTIF
from app.models.user import User
from app.api.helpers.storage import UPLOAD_PATHS
from app.api.helpers.auth import AuthManager

authorised_blueprint = Blueprint('authorised_blueprint', __name__, url_prefix='/')
ticket_blueprint = Blueprint('ticket_blueprint', __name__, url_prefix='/v1')
Expand Down Expand Up @@ -343,24 +344,11 @@ def order_invoices(order_identifier):


# Access for Environment details & Basic Auth Support
def check_auth_admin(username, password):
"""
This function is called to check for proper authentication & admin rights
"""
if username and password:
user = User.query.filter_by(_email=username).first()
if user:
if user.is_correct_password(password):
if user.is_admin:
return True
return False


def requires_basic_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth_admin(auth.username, auth.password):
if not auth or not AuthManager.check_auth_admin(auth.username, auth.password):
return make_response('Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
Expand Down
9 changes: 9 additions & 0 deletions app/api/helpers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ def is_verified_user():
@staticmethod
def is_accessible():
return current_user.is_authenticated

@staticmethod
def check_auth_admin(username, password):
# This function is called to check for proper authentication & admin rights
if username and password:
user = User.query.filter_by(_email=username).first()
if user and user.is_correct_password(password) and user.is_admin:
return True
return False
17 changes: 17 additions & 0 deletions tests/all/integration/api/helpers/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,22 @@ def test_is_accessible(self):
logout_user()
self.assertEqual(auth_manager.is_accessible(), False)

def test_check_auth_admin(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should also test with admin=False

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create two users and one admin and other non-admin and this should give correct response for each of them

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@poush Made the requested changes. Please have a look

"""Method to test proper authentication & admin rights for a user"""

with app.test_request_context():
auth_manager = auth.AuthManager()
auth_manager.init_login(app)
user = create_user(email='authtest@gmail.com', password='password')
user.is_admin = True
status = auth_manager.check_auth_admin('authtest@gmail.com', 'password')
self.assertEqual(True, status)

user = create_user(email='authtest2@gmail.com', password='password')
user.is_admin = False
status = auth_manager.check_auth_admin('authtest2@gmail.com', 'password')
self.assertEqual(False, status)


if __name__ == '__main__':
unittest.main()