-
Notifications
You must be signed in to change notification settings - Fork 0
Auth routes #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Auth routes #7
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0e323f0
uploading to test on server
sarahmc253 5e5b208
tetsing on server again
sarahmc253 5ca886c
auth routes and jwt tokens
sarahmc253 77e12be
fixing gitignore
sarahmc253 398afa6
coderabbit comments
sarahmc253 c637de0
hopefully last code rabbot
sarahmc253 12ca87d
fixing json handling
sarahmc253 07338ed
fixing rehashing stuff
sarahmc253 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| .env | ||
| venv/ | ||
| client-cpp/build/ | ||
| .claude/settings.json | ||
| .claude/settings.local.json | ||
| .vscode/settings.json | ||
| .claude/ | ||
| __pycache__/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| Flask==3.1.3 | ||
| mysql-connector-python==9.3.0 | ||
| python-dotenv==1.2.2 | ||
| flask>=3.1.3 | ||
| flask-jwt-extended>=4.7.4 | ||
| argon2-cffi>=25.1.0 | ||
| mysql-connector-python>=9.7.0 | ||
| web3>=7.16.0 | ||
| python-dotenv>=1.2.2 | ||
| gunicorn>=26.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,156 @@ | ||
| from flask import Blueprint, request, jsonify | ||
| from argon2 import PasswordHasher | ||
| from argon2.exceptions import VerifyMismatchError | ||
| from flask_jwt_extended import create_access_token | ||
| import uuid | ||
| from datetime import datetime, timezone | ||
| import mysql.connector | ||
| from .. import get_db | ||
|
|
||
| # Argon2id params (explicit to ensure stability across library versions): | ||
| # time_cost=3 — number of iterations; increases CPU cost for attackers | ||
| # memory_cost=65536 — 64MB RAM required per hash; resists GPU/ASIC brute force | ||
| # parallelism=4 — threads per hash; matched to typical server core count | ||
| # hash_len=32 — 256-bit output; exceeds minimum security margin | ||
| # salt_len=16 — 128-bit random salt; prevents precomputation attacks | ||
| ph = PasswordHasher(time_cost=3, memory_cost=65536, parallelism=4, hash_len=32, salt_len=16) | ||
| # Pre-computed at startup for timing attack mitigation in /login — see route for usage. | ||
| _DUMMY_HASH = ph.hash("dummy") | ||
|
|
||
| auth_bp = Blueprint('auth', __name__) | ||
|
|
||
| REQUIRED_FIELDS = [ | ||
| 'username', 'email', 'password', | ||
| 'x25519_public_key', 'hpke_wrapped_private_key', 'argon2id_kek_salt', | ||
| ] | ||
|
|
||
| def _invalid_fields(data, fields): | ||
| """Return fields that are missing, not a string, or blank after stripping.""" | ||
| return [ | ||
| f for f in fields | ||
| if not isinstance(data.get(f), str) or not data[f].strip() | ||
| ] | ||
|
|
||
| @auth_bp.route('/register', methods=['POST']) | ||
| def register(): | ||
| data = request.get_json() | ||
|
|
||
| if not data: | ||
| return jsonify({'error': 'Request body must be JSON'}), 400 | ||
|
|
||
| missing = [f for f in ['username', 'email', 'password'] if not data.get(f)] | ||
| if missing: | ||
| return jsonify({'error': f"Missing required fields: {', '.join(missing)}"}), 400 | ||
| invalid = _invalid_fields(data, REQUIRED_FIELDS) | ||
| if invalid: | ||
| return jsonify({'error': f"Missing or invalid fields: {', '.join(invalid)}"}), 400 | ||
|
|
||
| password_hash = ph.hash(data['password']) | ||
| # Hash format: $argon2id$v=19$m=...,t=...,p=...$<base64-salt>$<base64-hash> | ||
| argon2id_salt = password_hash.split('$')[4] | ||
|
|
||
| user_id = str(uuid.uuid4()) | ||
| now = datetime.now(timezone.utc) | ||
|
|
||
| db = get_db() | ||
| cursor = db.cursor() | ||
| try: | ||
| cursor.execute( | ||
| """ | ||
| INSERT INTO users | ||
| (id, username, email, argon2id_server_hash, argon2id_server_salt, | ||
| x25519_public_key, hpke_wrapped_private_key, argon2id_kek_salt, | ||
| tofu_key_pinned_at) | ||
| VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) | ||
| """, | ||
| ( | ||
| user_id, data['username'], data['email'], | ||
| password_hash, argon2id_salt, | ||
| data['x25519_public_key'], data['hpke_wrapped_private_key'], | ||
| data['argon2id_kek_salt'], now, | ||
| ), | ||
| ) | ||
| db.commit() | ||
| except mysql.connector.IntegrityError as e: | ||
| db.rollback() | ||
| if e.errno == 1062: | ||
| return jsonify({'error': 'Username or email already exists'}), 409 | ||
| raise | ||
| except Exception: | ||
| db.rollback() | ||
| raise | ||
| finally: | ||
| cursor.close() | ||
|
|
||
| return jsonify({'message': 'User registered successfully'}), 201 | ||
|
|
||
|
|
||
| @auth_bp.route('/login', methods=['POST']) | ||
| def login(): | ||
| data = request.get_json() | ||
|
|
||
| if not data: | ||
| return jsonify({'error': 'Request body must be JSON'}), 400 | ||
|
|
||
| invalid = _invalid_fields(data, ['username', 'password']) | ||
| if invalid: | ||
| return jsonify({'error': f"Missing or invalid fields: {', '.join(invalid)}"}), 400 | ||
|
|
||
| db = get_db() | ||
| cursor = db.cursor(dictionary=True) | ||
| try: | ||
| cursor.execute( | ||
| """ | ||
| SELECT id, username, argon2id_server_hash, | ||
| hpke_wrapped_private_key, argon2id_kek_salt, x25519_public_key | ||
| FROM users | ||
| WHERE username = %s | ||
| """, | ||
| (data['username'],), | ||
| ) | ||
| user = cursor.fetchone() | ||
| finally: | ||
| cursor.close() | ||
|
|
||
| if user is None: | ||
| # Run a verify against a dummy hash so response time is indistinguishable | ||
| # from a wrong-password attempt — prevents username enumeration via timing. | ||
| try: | ||
| ph.verify(_DUMMY_HASH, data['password']) | ||
| except Exception: | ||
| pass | ||
| return jsonify({'error': 'Invalid credentials'}), 401 | ||
|
|
||
| try: | ||
| ph.verify(user['argon2id_server_hash'], data['password']) | ||
| except VerifyMismatchError: | ||
| return jsonify({'error': 'Invalid credentials'}), 401 | ||
|
|
||
| if ph.check_needs_rehash(user['argon2id_server_hash']): | ||
| new_hash = ph.hash(data['password']) | ||
| new_salt = new_hash.split('$')[4] | ||
| cursor = db.cursor() | ||
| try: | ||
| cursor.execute( | ||
| """ | ||
| UPDATE users | ||
| SET argon2id_server_hash = %s, argon2id_server_salt = %s | ||
| WHERE id = %s | ||
| """, | ||
| (new_hash, new_salt, user['id']), | ||
| ) | ||
| db.commit() | ||
| except Exception: | ||
| db.rollback() | ||
| raise | ||
| finally: | ||
| cursor.close() | ||
|
|
||
| token = create_access_token( | ||
| identity=str(user['id']), | ||
| additional_claims={'username': user['username']}, | ||
| ) | ||
|
|
||
| return jsonify({ | ||
| 'token': token, | ||
| 'hpke_wrapped_private_key': user['hpke_wrapped_private_key'], | ||
| 'argon2id_kek_salt': user['argon2id_kek_salt'], | ||
| 'x25519_public_key': user['x25519_public_key'], | ||
| }), 200 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.