2019-10-25 12:36:58 +00:00
|
|
|
""" Command line tools for the API server """
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
import dpath
|
|
|
|
from humanfriendly import parse_timespan
|
|
|
|
|
|
|
|
|
|
|
|
def setup():
|
2021-01-05 14:45:22 +00:00
|
|
|
from apiserver.database import db
|
2024-03-18 13:52:22 +00:00
|
|
|
|
2021-01-05 14:45:22 +00:00
|
|
|
db.initialize()
|
2019-10-25 12:36:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def gen_token(args):
|
2021-01-05 14:28:49 +00:00
|
|
|
from apiserver.bll.auth import AuthBLL
|
2024-03-18 13:52:22 +00:00
|
|
|
|
|
|
|
resp = AuthBLL.get_token_for_user(
|
|
|
|
args.user_id, args.company_id, int(parse_timespan(args.expiration))
|
|
|
|
)
|
|
|
|
print("Token:\n%s" % resp.token)
|
2019-10-25 12:36:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def safe_get(obj, glob, default=None, separator="/"):
|
|
|
|
try:
|
|
|
|
return dpath.get(obj, glob, separator=separator)
|
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
2024-03-18 13:52:22 +00:00
|
|
|
if __name__ == "__main__":
|
2019-10-25 12:36:58 +00:00
|
|
|
top_parser = ArgumentParser(__doc__)
|
|
|
|
|
2024-03-18 13:52:22 +00:00
|
|
|
subparsers = top_parser.add_subparsers(title="Sections")
|
|
|
|
|
|
|
|
token = subparsers.add_parser("token")
|
|
|
|
token_commands = token.add_subparsers(title="Commands")
|
|
|
|
token_create = token_commands.add_parser(
|
|
|
|
"generate", description="Generate a new token"
|
|
|
|
)
|
|
|
|
token_create.add_argument("--user-id", "-u", help="User ID", required=True)
|
|
|
|
token_create.add_argument("--company-id", "-c", help="Company ID", required=True)
|
|
|
|
token_create.add_argument(
|
|
|
|
"--expiration",
|
|
|
|
"-exp",
|
|
|
|
help="Token expiration (time span, shorthand suffixes are supported, default 1m)",
|
|
|
|
default=parse_timespan("1m"),
|
|
|
|
)
|
2019-10-25 12:36:58 +00:00
|
|
|
token_create.set_defaults(_func=gen_token)
|
|
|
|
|
|
|
|
args = top_parser.parse_args()
|
|
|
|
if args._func:
|
|
|
|
setup()
|
|
|
|
args._func(args)
|