Skip to content

Commit

Permalink
regtok: Display expiry time as datetime by default
Browse files Browse the repository at this point in the history
  • Loading branch information
govynnus committed Sep 13, 2021
1 parent fcd2f1d commit f0b6e00
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
42 changes: 33 additions & 9 deletions synadm/api.py
Expand Up @@ -652,40 +652,64 @@ def purge_history_status(self, purge_id):
"""
return self.query("get", f"v1/purge_history_status/{purge_id}")

def regtok_list(self, valid):
def regtok_list(self, valid, datetime):
""" List registration tokens
Args:
valid (bool): list only valid (if True) or invalid (if False)
valid (bool): List only valid (if True) or invalid (if False)
tokens. Default is to list all tokens regardless of validity.
datetime (bool): If True, replace the expiry_time field with a
human readable datetime. If False, expiry_time will be a unix
timestamp.
Returns:
string: JSON string containing the admin API's response or None if
an exception occured. See Synapse admin API docs for details.
"""
return self.query("get", "v1/registration_tokens", params={
result = self.query("get", "v1/registration_tokens", params={
"valid": (str(valid).lower() if isinstance(valid, bool) else None)
})

def regtok_details(self, token):
# Change expiry_time to a human readable format if requested
if datetime and result is not None and "registration_tokens" in result:
for i, regtok in enumerate(result["registration_tokens"]):
expiry_time = regtok["expiry_time"]
if expiry_time is not None:
datetime = self._datetime_from_timestamp(expiry_time)
result["registration_tokens"][i]["expiry_time"] = datetime

return result

def regtok_details(self, token, datetime):
""" Get details about the given registration token
Args:
token (string): the registration token in question
token (string): The registration token in question
datetime (bool): If True, replace the expiry_time field with a
human readable datetime. If False, expiry_time will be a unix
timestamp.
Returns:
string: JSON string containing the admin API's response or None if
an exception occured. See Synapse admin API docs for details.
"""
return self.query("get", f"v1/registration_tokens/{token}")
result = self.query("get", f"v1/registration_tokens/{token}")

# Change expiry_time to a human readable format if requested
if datetime and result is not None:
if result["expiry_time"] is not None:
datetime = self._datetime_from_timestamp(result["expiry_time"])
result["expiry_time"] = datetime

return result

def regtok_new(self, token, length, uses_allowed, expiry_ts, expire_at):
""" Create a new registration token
Args:
token (string): registration token to create. Default is randomly
token (string): Registration token to create. Default is randomly
generated by the server.
length (int): The length of the token to generate if the token is
not provided.
Expand Down Expand Up @@ -726,7 +750,7 @@ def regtok_update(self, token, uses_allowed, expiry_ts, expire_at):
""" Update a registration token
Args:
token (string): registration token to update.
token (string): Registration token to update.
uses_allowed (int): The number of times the token can be used to
complete a registration before it becomes invalid.
expiry_ts (int): The latest time the registration token is valid.
Expand Down Expand Up @@ -769,7 +793,7 @@ def regtok_delete(self, token):
""" Delete a registration token
Args:
token (string): the registration token to delete
token (string): The registration token to delete
Returns:
string: JSON string containing the admin API's response or None if
Expand Down
18 changes: 13 additions & 5 deletions synadm/cli/regtok.py
Expand Up @@ -34,12 +34,16 @@ def regtok():
@regtok.command(name="list")
@click.option(
"--valid/--invalid", "-v/-V", default=None, show_default=True,
help="list only valid/invalid tokens.")
help="List only valid/invalid tokens.")
@click.option(
"--datetime/--timestamp", "-d/-t", default=True, show_default=False,
help="""Display expiry time in a human readable format, or as a unix
timestamp in milliseconds. [default: datetime].""")
@click.pass_obj
def regtok_list_cmd(helper, valid):
def regtok_list_cmd(helper, valid, datetime):
""" List registration tokens
"""
regtoks = helper.api.regtok_list(valid)
regtoks = helper.api.regtok_list(valid, datetime)
if regtoks is None:
click.echo("Registration tokens could not be fetched.")
raise SystemExit(1)
Expand All @@ -57,11 +61,15 @@ def regtok_list_cmd(helper, valid):

@regtok.command(name="details")
@click.argument("token", type=str)
@click.option(
"--datetime/--timestamp", "-d/-t", default=True, show_default=False,
help="""Display expiry time in a human readable format, or as a unix
timestamp in milliseconds. [default: datetime].""")
@click.pass_obj
def regtok_details_cmd(helper, token):
def regtok_details_cmd(helper, token, datetime):
""" View details of the given token
"""
regtok = helper.api.regtok_details(token)
regtok = helper.api.regtok_details(token, datetime)
if regtok is None:
click.echo("Registration token could not be fetched.")
raise SystemExit(1)
Expand Down

0 comments on commit f0b6e00

Please sign in to comment.