-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add script for creating user tokens
- Loading branch information
Showing
4 changed files
with
51 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import argparse | ||
import datetime | ||
|
||
from sqlmodel import select | ||
from transcribee_backend.auth import generate_user_token | ||
from transcribee_backend.db import SessionContextManager | ||
from transcribee_backend.helpers.time import now_tz_aware | ||
from transcribee_backend.models.user import User | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--username", required=True) | ||
parser.add_argument("--valid-days", required=True) | ||
args = parser.parse_args() | ||
with SessionContextManager(path="management_command:create_user_token") as session: | ||
valid_days = int(args.valid_days) | ||
if valid_days < 0: | ||
print("Valid days must be positive") | ||
exit(1) | ||
|
||
valid_until = now_tz_aware() + datetime.timedelta(days=valid_days) | ||
|
||
user = session.exec( | ||
select(User).where(User.username == args.username) | ||
).one_or_none() | ||
|
||
if user is None: | ||
print(f"User {args.user} not found") | ||
exit(1) | ||
|
||
key, user_token = generate_user_token(user, valid_until=valid_until) | ||
session.add(user_token) | ||
print(f"User token created and valid until {valid_until}") | ||
print(f"Secret: {key}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters