-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmktoken.py
126 lines (117 loc) · 3.55 KB
/
mktoken.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python3
# Copyright (C) 2024 iDigitalFlame
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from os import environ
from django import setup
from sys import exit, stderr
from argparse import ArgumentParser
environ["DJANGO_SETTINGS_MODULE"] = "scorebot.settings"
from scorebot_core.models import token_create_new, AccessToken
from scorebot.utils.constants import CONST_CORE_ACCESS_KEY_LEVELS
def _vet(args):
if not isinstance(args.days, int):
print("-d/--days is not valid!", file=stderr)
return 1
if args.days <= 0:
print("-d/--days must be greater than zero!", file=stderr)
return 1
if isinstance(args.raw, int) and args.raw < 0:
print("-r/--raw-perms must be greater than or equal to zero!", file=stderr)
return 1
v = list()
if isinstance(args.perms, str) and len(args.perms) > 0:
for i in args.perms.split(","):
x = i.strip()
try:
v.append(int(x))
except ValueError:
if x not in CONST_CORE_ACCESS_KEY_LEVELS:
print(f'permission name "{x}" is invalid!', file=stderr)
return 1
v.append(x)
del x
return _main(args, v)
def _main(args, perms):
try:
setup()
except Exception as err:
print(f"Django setup failed: {err}!", file=stderr)
return 1
try:
t = token_create_new(args.days)
except Exception as err:
print(f"Token generation failed: {err}!", file=stderr)
return 1
try:
a = AccessToken(token=t, level=args.raw)
a.save()
except Exception as err:
print(f"AccessToken generation failed: {err}!", file=stderr)
return 1
if len(perms) > 0:
for i in perms:
a[i] = True
a.save()
del a
if args.json:
print(f'{{ "token": "{str(t.uuid)}" }}')
else:
print(str(t.uuid))
del t
return 0
if __name__ == "__main__":
p = ArgumentParser(description="Scorebot Token Generator")
p.add_argument(
"-d",
"--days",
type=int,
dest="days",
help="days to expire Token in (defaults to 90 days, set to -1 to disable)",
action="store",
metavar="days",
default=90,
required=False,
)
p.add_argument(
"-p",
"--perms",
type=str,
dest="perms",
help="comma seperated list of permissions to set (takes str and int)",
action="store",
metavar="perms",
required=False,
)
p.add_argument(
"-r",
"--raw-perms",
type=int,
dest="raw",
help="permissions to set as a raw number",
action="store",
default=None,
metavar="perm_number",
required=False,
)
p.add_argument(
"-j",
"--json",
dest="json",
help="output to json",
action="store_true",
required=False,
)
exit(_vet(p.parse_args()))