Skip to content

Commit

Permalink
added integerfield for expiration date
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardKoschicek committed Jan 21, 2025
1 parent 70884da commit 0be583c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
10 changes: 2 additions & 8 deletions openatlas/models/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@
class Token:

@staticmethod
def generate_token(expiration: str, token_name: str, user_: User) -> None:
expires_delta = None
match expiration:
case '0':
expires_delta = timedelta(days=1)
case '1':
expires_delta = timedelta(days=90)
def generate_token(expiration: int, token_name: str, user_: User) -> str:
access_token = create_access_token(
identity=user_.username,
additional_claims={'role': user_.group},
expires_delta=expires_delta) # type: ignore
expires_delta=timedelta(days=expiration) if expiration else False)
decoded_token = decode_token(access_token, allow_expired=True)
valid_until = datetime.max
if expire := decoded_token.get('exp'):
Expand Down
18 changes: 9 additions & 9 deletions openatlas/views/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from werkzeug.utils import redirect
from werkzeug.wrappers import Response
from wtforms import RadioField, SelectField, StringField
from wtforms.fields.numeric import IntegerField
from wtforms.fields.simple import HiddenField

from openatlas import app
Expand All @@ -25,10 +26,10 @@


class GenerateTokenForm(FlaskForm):
expiration = RadioField(
expiration = IntegerField(
_('expiration'),
choices=[('0','One day'),('1','90 days'), ('2', 'no expiration date')],
default='0')
default=30,
description='0 = ' + _("no expiration date"))
token_name = StringField(
_('token name'),
default=f"Token_{datetime.today().strftime('%Y-%m-%d')}")
Expand Down Expand Up @@ -95,8 +96,7 @@ def api_token(user_id: int = 0) -> str | Response:
_('valid until'),
_('user'),
_('creator'),
_('revoked'),
_('delete')])
_('revoked')])
for token in Token.get_tokens(user_id, revoked, valid):
delete_link = link(
_('delete'),
Expand All @@ -114,8 +114,8 @@ def api_token(user_id: int = 0) -> str | Response:
token['jti'],
token['valid_from'],
token['valid_until'],
User.get_by_id(token['user_id']).username,
User.get_by_id(token['creator_id']).username,
link(User.get_by_id(token['user_id'])),
link(User.get_by_id(token['creator_id'])),
token['revoked'],
revoke_link,
delete_link])
Expand All @@ -141,7 +141,7 @@ def generate_token() -> str | Response:
token = ''
Transaction.begin()
try:
token = Token.generate_token( expiration, token_name, user_)
token = Token.generate_token(expiration, token_name, user_)
Transaction.commit()
flash(f"{_('token stored for')}: {user_.username}", 'info')
except Exception as e: # pragma: no cover
Expand All @@ -159,7 +159,7 @@ def generate_token() -> str | Response:
form.token_text.data = request.cookies.get('jwt_token')
return render_template(
'content.html',
content=display_form(form, manual_page='profile'),
content=display_form(form, manual_page='admin'), # todo manual
title=_('admin'),
crumbs=[
[_('admin'), f"{url_for('admin_index')}"],
Expand Down

0 comments on commit 0be583c

Please sign in to comment.