This repository was archived by the owner on Mar 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcli.py
More file actions
93 lines (70 loc) · 2.42 KB
/
cli.py
File metadata and controls
93 lines (70 loc) · 2.42 KB
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
"""fastapi_security command-line interface"""
import argparse
import sys
import textwrap
from getpass import getpass
from typing import Optional, Sequence, Text
from fastapi_security.basic import generate_digest
def _wrap_paragraphs(s):
paragraphs = s.strip().split("\n\n")
wrapped_paragraphs = [
"\n".join(textwrap.wrap(paragraph)) for paragraph in paragraphs
]
return "\n\n".join(wrapped_paragraphs)
main_parser = argparse.ArgumentParser(
description=_wrap_paragraphs(__doc__),
formatter_class=argparse.RawDescriptionHelpFormatter,
)
subcommand_parsers = main_parser.add_subparsers(
help="Specify a sub-command",
dest="subcommand",
required=True,
)
gendigest_description = """
Generate digest for basic_auth_with_digest credentials.
Example:
$ fastapi-security gendigest --salt=very-strong-salt
Password:
Confirm password:
Here is your digest:
0jFS-cNapwQf_lpyULF7_hEelbl_zreNVHbxqKwKIFmPRQ09bYTEDQLrr_UEWZc9fdYFiU5F3il3rovJQ_UEpg==
$ cat fastapi_security_conf.py
from fastapi_security import FastAPISecurity
security = FastAPISecurity()
security.init_basic_auth_with_digest(
[
{'user': 'me', 'password': '0jFS-cNapwQf_lpyULF7_hEelbl_zreNVHbxqKwKIFmPRQ09bYTEDQLrr_UEWZc9fdYFiU5F3il3rovJQ_UEpg=='}
],
salt='very-strong-salt',
)
"""
gendigest_parser = subcommand_parsers.add_parser(
"gendigest",
description=gendigest_description,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
gendigest_parser.add_argument(
"--salt",
help="Salt value used in fastapi_security configuration.",
required=True,
)
def gendigest(parsed_args):
# if not parsed_args.salt:
# print("Cannot generate digest: --salt must be non-empty",
# file=sys.stderr)
# sys.exit(1)
password = getpass(prompt="Password: ")
password_confirmation = getpass(prompt="Confirm password: ")
if password != password_confirmation:
print("Cannot generate digest: passwords don't match", file=sys.stderr)
sys.exit(1)
print("\nHere is your digest:", file=sys.stderr)
print(generate_digest(password, salt=parsed_args.salt))
def main(args: Optional[Sequence[Text]] = None):
parsed_args = main_parser.parse_args(args)
if parsed_args.subcommand == "gendigest":
return gendigest(parsed_args)
main_parser.print_usage(file=sys.stderr)
sys.exit(2) # invalid usage: missing subcommand
if __name__ == "__main__":
main()