Skip to content

Commit

Permalink
Feature: token max age for create token command line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Jan 11, 2022
1 parent b74817b commit c766b55
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
6 changes: 6 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def test_jwtcli():
assert status == 0
assert len(stdout.split('.')) == 3

# Max age
when('auth create --maxage 10')
assert stderr == ''
assert status == 0
assert len(stdout.split('.')) == 3


if __name__ == '__main__':
app.climain(['auth', 'c', '{"foo": "bar"}'])
2 changes: 1 addition & 1 deletion yhttp/ext/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .install import install
from .authentication import Authenticator

__version__ = '3.7.2'
__version__ = '3.8.0'
5 changes: 2 additions & 3 deletions yhttp/ext/auth/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ def set_refreshtoken(self, req, id, attrs=None):
return entry

def _exp(self, seconds):
# TODO: Stupid explicit is better than comprehensive implicit.
return datetime.now(tz=timezone.utc) + timedelta(seconds=seconds)

def dump_refreshtoken(self, id, attrs=None):
Expand Down Expand Up @@ -318,10 +317,10 @@ def token_leeway(self):
def token_algorithm(self):
return self.settings.token.algorithm

def dump(self, id, attrs=None):
def dump(self, id, attrs=None, maxage=None):
payload = {
'id': id,
'exp': self._exp(self.token_maxage)
'exp': self._exp(maxage or self.token_maxage)
}
if attrs:
payload.update(attrs)
Expand Down
5 changes: 4 additions & 1 deletion yhttp/ext/auth/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class Create(SubCommand):
Argument(
'payload', default='', nargs='?', help='example: {"foo": "bar"}'
),
Argument(
'--maxage', type=int, help='Token maxage in seconds.'
),
]

def __call__(self, args):
Expand All @@ -21,7 +24,7 @@ def __call__(self, args):
else:
payload = ''

print(jwt.dump(payload))
print(jwt.dump(payload, maxage=args.maxage))


class AuthenticatorCLI(SubCommand):
Expand Down

0 comments on commit c766b55

Please sign in to comment.