Skip to content

Commit c714fb5

Browse files
committed
Address comments
1 parent 385eb1c commit c714fb5

File tree

5 files changed

+12
-24
lines changed

5 files changed

+12
-24
lines changed

lib/pbench/server/api/auth.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def verify_auth(auth_token):
8989
"""
9090
Validates the auth token
9191
:param auth_token:
92-
:return: integer|string
92+
:return: User object/None
9393
"""
9494
try:
9595
payload = jwt.decode(
@@ -99,7 +99,6 @@ def verify_auth(auth_token):
9999
if ActiveTokens.valid(auth_token):
100100
user = User.query(id=user_id)
101101
return user
102-
return None
103102
except jwt.ExpiredSignatureError:
104103
try:
105104
ActiveTokens.delete(auth_token)
@@ -113,12 +112,10 @@ def verify_auth(auth_token):
113112
"User attempted Pbench expired token '{}', Token deleted from the database and no longer tracked",
114113
auth_token,
115114
)
116-
return None
117115
except jwt.InvalidTokenError:
118116
Auth.logger.warning("User attempted invalid Pbench token '{}'", auth_token)
119-
return None
120117
except Exception:
121118
Auth.logger.exception(
122119
"Exception occurred while verifying the auth token '{}'", auth_token
123120
)
124-
return None
121+
return None

lib/pbench/server/api/resources/users_api.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from flask_bcrypt import check_password_hash
55
from email_validator import EmailNotValidError
66
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
7-
from configparser import NoOptionError
87
from pbench.server.database.models.users import User
98
from pbench.server.database.models.active_tokens import ActiveTokens
109
from pbench.server.api.auth import Auth
@@ -139,13 +138,9 @@ def __init__(self, config, logger, auth):
139138
self.server_config = config
140139
self.logger = logger
141140
self.auth = auth
142-
try:
143-
self.token_expire_duration = self.server_config.get(
144-
"pbench-server", "token_expiration_duration"
145-
)
146-
except NoOptionError:
147-
# Defaults to 60 minutes
148-
self.token_expire_duration = 60
141+
self.token_expire_duration = self.server_config.get(
142+
"pbench-server", "token_expiration_duration"
143+
)
149144

150145
@Auth.token_auth.login_required(optional=True)
151146
def post(self):
@@ -198,7 +193,7 @@ def post(self):
198193
self.logger.warning(
199194
"No user found in the db for Username: {} while login", username
200195
)
201-
abort(403, message="No such user, please register first")
196+
abort(403, message="Bad login")
202197

203198
# Validate the password
204199
if not check_password_hash(user.password, password):
@@ -443,7 +438,7 @@ def put(self, username):
443438
field,
444439
post_data[field],
445440
)
446-
abort(400, message="Invalid data in update request payload")
441+
abort(403, message="Invalid update request payload")
447442
try:
448443
user.update(**post_data)
449444
except Exception:
@@ -500,7 +495,7 @@ def delete(self, username):
500495
else:
501496
if user.is_admin():
502497
self.logger.warning("Admin attempted to delete admin user")
503-
abort(405, message="Admin user can not be deleted")
498+
abort(403, message="Admin user can not be deleted")
504499
self.logger.info("User entry deleted for user with username {}", username)
505500

506501
response_object = {

lib/pbench/server/database/alembic/env.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
log.addHandler(handler)
3030

3131
# add your model's MetaData object here for 'autogenerate' support:
32-
# from myapp import mymodel
33-
# target_metadata = mymodel.Base.metadata
3432

3533
target_metadata = Database.Base.metadata
3634

lib/pbench/server/database/models/users.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ def is_admin(self):
121121
def is_admin_username(username):
122122
# TODO: Need to add an interface to fetch admins list instead of hard-coding the names, preferably via sql query
123123
admins = ["admin"]
124-
if username in admins:
125-
return True
126-
return False
124+
return username in admins
127125

128126
# TODO: Add password recovery mechanism

lib/pbench/test/unit/server/test_user_auth.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def test_non_registered_user_login(client, server_config):
219219
with client:
220220
response = login_user(client, server_config, "username", "12345")
221221
data = response.json
222-
assert data["message"] == "No such user, please register first"
222+
assert data["message"] == "Bad login"
223223
assert response.status_code == 403
224224

225225
@staticmethod
@@ -279,8 +279,8 @@ def test_update_user(client, server_config):
279279
headers=dict(Authorization="Bearer " + data_login["auth_token"]),
280280
)
281281
data = response.json
282-
assert response.status_code == 400
283-
assert data["message"] == "Invalid data in update request payload"
282+
assert response.status_code == 403
283+
assert data["message"] == "Invalid update request payload"
284284

285285
# Test password update
286286
response = client.put(

0 commit comments

Comments
 (0)