Skip to content

Api key factory

cli()

A simple CLI tool to generate API keys and their corresponding SHA-256 hashes.

Source code in src/api_key_factory/api_key_factory.py
10
11
12
13
14
15
16
@click.group()
@click.version_option("1.3.13", prog_name="api_key_factory")
def cli() -> None:
    """A simple CLI tool to generate API keys and their corresponding
    SHA-256 hashes.
    """
    pass

generate(output_dir, num, prefix)

Command to generate API keys and their corresponding SHA-256 hashes.

Parameters:

Name Type Description Default
output_dir Path

Directory to output keys and hashes.

required
num int

Number of API keys to generate. Default 1.

required
prefix str

Prefix at the beginning of the key.

required

Raises:

Type Description
ClickException

Error when writing output files

Source code in src/api_key_factory/api_key_factory.py
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
@cli.command()
@click.option(
    "-d",
    "--dir",
    "output_dir",
    type=click.Path(exists=False, file_okay=False, dir_okay=True, writable=True),
    required=False,
    help="Directory to output keys and hashes. If not set output to stdout.",
)
@click.option(
    "-n",
    "--num",
    "num",
    type=click.IntRange(min=1),
    default=1,
    help="Number of API keys to generate",
)
@click.option(
    "-p",
    "--prefix",
    "prefix",
    type=str,
    default="",
    help="Add a prefix at the beginning of the key",
)
def generate(
    output_dir: click.Path | None,
    num: int,
    prefix: str,
) -> None:
    """Command to generate API keys and their corresponding SHA-256 hashes.

    Args:
        output_dir (click.Path): Directory to output keys and hashes.
        num (int): Number of API keys to generate. Default 1.
        prefix (str): Prefix at the beginning of the key.

    Raises:
        click.ClickException: Error when writing output files
    """
    if output_dir is not None:
        try:
            # Create directory if it doesn't exist
            dir = Dir(output_dir, True)
        except OSError as error:
            raise click.ClickException("Output directory can not be created!\n" + str(error))

        if len(prefix) > 0:
            keys_filename = f"{prefix}_keys.txt"
            hashes_filename = f"{prefix}_hashes.txt"
        else:
            keys_filename = "keys.txt"
            hashes_filename = "hashes.txt"

        try:
            keys_file = File(keys_filename, dir.path, True)
            hashes_file = File(hashes_filename, dir.path, True)
        except (FileExistsError, PermissionError) as error:
            raise click.ClickException(f"File already exists in directory {dir.path}!\n" + str(error))

        for _ in range(num):
            key = Key(prefix)
            keys_file.add_content(f"{key.get_value()}\n")
            hashes_file.add_content(f"{key.get_hash()}\n")

        try:
            keys_file.save()
            keys_file.protect()
            hashes_file.save()
        except (OSError, PermissionError) as error:
            raise click.ClickException("The file cannot be written to!\n" + str(error))

        click.echo(f"Success! {num} keys and hashes have been written to the files:")
        click.echo(f" - {dir.path}/{keys_filename}")
        click.echo(f" - {dir.path}/{hashes_filename}")
    else:
        for _ in range(num):
            key = Key(prefix)
            click.echo(f"{key.get_value()}   {key.get_hash()}")