This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
104 lines (77 loc) · 2.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import jwt
import json
from datetime import datetime
import secrets
import typer
import string
import random
from uuid import uuid4
cli = typer.Typer()
def random_password(length: int) -> str:
chars = string.ascii_letters + string.digits
return str("").join(random.choices(chars, k=length))
@cli.command(name="random-password")
def get_random_password(length: int = 64):
"""generate a random password of uppercase letters, lowercase letters, and numbers"""
if length < 1:
typer.secho("enter a number greater than 0", fg=typer.colors.RED)
raise typer.Exit(1)
print(random_password(length))
@cli.command(name="uuid")
def get_uuid():
"""generate a random uuid (v4)"""
print(uuid4())
def anon_payload(iat: datetime = datetime.utcnow()):
return {
"role": "anon",
"iss": "supabase",
"iat": int(iat.timestamp()),
"exp": int(datetime.fromtimestamp(iat.timestamp()).replace(year=iat.year + 10).timestamp())
}
def service_payload(iat: datetime = datetime.utcnow()):
return {
"role": "service_role",
"iss": "supabase",
"iat": int(iat.timestamp()),
"exp": int(datetime.fromtimestamp(iat.timestamp()).replace(year=iat.year + 10).timestamp())
}
def get_anon(key: str):
return jwt.encode(payload=anon_payload(), key=key, algorithm="HS256")
@cli.command(name="anon-role")
def anon(key: str):
"""generate an anon role JWT based on a given key"""
if not key:
typer.secho("enter a valid key", fg=typer.colors.RED)
raise typer.Exit(1)
print(get_anon(key))
def get_service(key: str):
return jwt.encode(payload=service_payload(), key=key, algorithm="HS256")
@cli.command(name="service-role")
def service(key: str):
"""generate a service role JWT based on a given key"""
if not key:
typer.secho("enter a valid key", fg=typer.colors.RED)
raise typer.Exit(1)
print(get_service(key))
def _get_key(length: int):
return secrets.token_urlsafe(length)
@cli.command(name="key")
def get_key(length: int = 64):
"""generate a random key (minimum required by Supabase is 32 characters)"""
if length < 32:
typer.secho("Supabase requires a key greater than 32 characters in length", fg=typer.colors.RED)
raise typer.Exit(1)
print(_get_key(length))
@cli.command()
def all(length: int = 64):
"""get a full Supabase JWT set (random key, anon role JWT, and service role JWT)"""
key = _get_key(length)
service_key = service(key)
anon_key = anon(key)
print(json.dumps({
"service_role_key": service_key,
"anon_role_key": anon_key,
"secret": key
}))
if __name__ == "__main__":
cli()