From 36917c490341527c870e3f9486c2b55e455685be Mon Sep 17 00:00:00 2001
From: Maximilian Moser <maximilian.moser@tuwien.ac.at>
Date: Thu, 28 Apr 2022 16:11:13 +0200
Subject: [PATCH] Add a command to show a user's API tokens

---
 invenio_utilities_tuw/cli/users.py | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/invenio_utilities_tuw/cli/users.py b/invenio_utilities_tuw/cli/users.py
index 3719314..caf6952 100644
--- a/invenio_utilities_tuw/cli/users.py
+++ b/invenio_utilities_tuw/cli/users.py
@@ -117,3 +117,20 @@ def find_user(only_active, query, full_name):
 
     else:
         click.secho("no users registered.", fg="yellow")
+
+
+@users.command("tokens")
+@click.argument("user_id")
+@with_appcontext
+def show_user_tokens(user_id):
+    """Show the user's API tokens."""
+    user = get_user_by_identifier(user_id)
+    for token in user.oauth2tokens:
+        fg = "bright_black" if token.is_internal else "white"
+        # TODO optionally show scopes for the tokens
+
+        name = "N/A"
+        if token.client and token.client.name:
+            name = token.client.name
+
+        click.secho(f'{token.access_token}  "{name}"', fg=fg)
-- 
GitLab