Skip to content

Commit 3b2fd36

Browse files
committed
Cleanup, Refactor and add more tests
1 parent dbdd077 commit 3b2fd36

File tree

4 files changed

+228
-151
lines changed

4 files changed

+228
-151
lines changed

lib/pbench/server/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def create_app():
127127
# JWT specific configuration
128128
app.config["JWT_SECRET_KEY"] = os.getenv("SECRET_KEY", "my_precious")
129129
app.config["BCRYPT_LOG_ROUNDS"] = int(app.config_server.get("bycrypt_log_rounds"))
130+
app.config["TOKEN_EXPIRATION_DURATION"] = app.config_server.get("token_expiration_duration")
130131

131132
app.bcrypt = Bcrypt(app)
132133

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,23 @@ def encode_auth_token(self, app, user_id):
3737
Generates the Auth Token
3838
:return: string
3939
"""
40-
try:
40+
expire = app.config.get("TOKEN_EXPIRATION_DURATION")
41+
if not expire:
42+
# Expiration is not defined, token is set to expire in a second
43+
payload = {
44+
"iat": datetime.datetime.utcnow(),
45+
"exp": datetime.datetime.utcnow() + datetime.timedelta(
46+
milliseconds=100),
47+
"sub": user_id,
48+
}
49+
else:
4150
payload = {
4251
"iat": datetime.datetime.utcnow(),
43-
"exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=5),
52+
"exp": datetime.datetime.utcnow() + datetime.timedelta(
53+
minutes=int(expire)),
4454
"sub": user_id,
4555
}
56+
try:
4657
return jwt.encode(
4758
payload, app.config.get("JWT_SECRET_KEY"), algorithm="HS256"
4859
)

0 commit comments

Comments
 (0)