Documentation
authenticate_user(username, password)
Authenticate a user by verifying the provided password against the stored hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str
|
The username to authenticate. |
required |
password
|
str
|
The plaintext password to verify. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if authentication is successful, False otherwise. |
Source code in secure_file_storage/src/auth.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
create_files_table()
Create the 'files' table in the 'metadata.db' SQLite database if it does not exist.
Returns:
| Type | Description |
|---|---|
|
None |
Source code in secure_file_storage/src/auth.py
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 | |
create_user_table()
Create the 'users' table in the 'metadata.db' SQLite database if it does not exist.
The table has two columns
- username (TEXT): Primary key for user identification.
- password_hash (TEXT): Stores the bcrypt hashed password.
Returns:
| Type | Description |
|---|---|
|
None |
Source code in secure_file_storage/src/auth.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
register_user(username, password)
Register a new user with a hashed password in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str
|
The username to register. |
required |
password
|
str
|
The plaintext password for the user. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if registration succeeded, False if username already exists. |
Source code in secure_file_storage/src/auth.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
decrypt_file(encrypted_path, key)
Decrypt a previously encrypted file using the provided key.
The decrypted file is saved by removing the '.enc' suffix from the encrypted file's name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encrypted_path
|
str
|
Path to the encrypted '.enc' file. |
required |
key
|
bytes
|
Raw decryption key provided by the user. |
required |
Source code in secure_file_storage/src/encryption.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
encrypt_file(file_path, key)
Encrypt the contents of a file using the provided key.
The encrypted output is saved to a new file with '.enc' appended to the original filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the plaintext file to encrypt. |
required |
key
|
bytes
|
Raw encryption key provided by the user. |
required |
Source code in secure_file_storage/src/encryption.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
generate_key()
Generate a secure random key for symmetric encryption.
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
A base64-encoded 32-byte key suitable for Fernet encryption. |
Source code in secure_file_storage/src/encryption.py
5 6 7 8 9 10 11 12 | |
ensure_env()
Ensures that a .env file exists in the current directory.
If the .env file does not exist, this function creates it and writes a randomly generated SECRET_KEY and a COMPOSE_BAKE variable.
Returns:
| Type | Description |
|---|---|
|
None |
Source code in secure_file_storage/src/setup_env.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
hash_file(path)
Calculate the SHA-256 hash of a file's contents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the file to hash. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
Hexadecimal SHA-256 digest of the file. |
Source code in secure_file_storage/src/utils.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Main module for Secure File Storage application
main()
Entry point to start the Flask web application.
Prints a warning if not running inside a virtual environment, then runs the Flask server on host 0.0.0.0 and port 5000.
Source code in secure_file_storage/main.py
29 30 31 32 33 34 35 36 37 38 39 40 | |
Contains the version of the package
get_version()
Retrieve the current project version from 'pyproject.toml'.
Returns:
| Name | Type | Description |
|---|---|---|
str |
Version string from the 'project.version' field. |
Source code in secure_file_storage/version.py
6 7 8 9 10 11 12 13 14 | |
remove_testuser()
Remove the user with username 'testuser' from the database.
This function is useful for cleaning up test data before running tests.
Source code in tests/test_auth.py
5 6 7 8 9 10 11 12 13 14 | |
test_register_and_authenticate()
Test the user registration and authentication workflow.
- Creates the users table if it doesn't exist.
- Removes any existing 'testuser' entry.
- Registers a new user 'testuser' with password 'testpass'.
- Asserts that the user can be authenticated successfully.
Source code in tests/test_auth.py
17 18 19 20 21 22 23 24 25 26 27 28 29 | |
test_encrypt_decrypt(tmp_path)
Test the file encryption and decryption process.
- Creates a temporary file with known content.
- Encrypts the file with a generated key.
- Deletes the original file.
- Decrypts the encrypted file.
- Checks that the decrypted content matches the original.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tmp_path
|
Path
|
Pytest fixture providing a temporary directory. |
required |
Source code in tests/test_encryption.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |