forked from methods/NandS_BookAPIV.2
-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/upgrade to jwt route protection #11
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
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
9885558
add Flask-PyMongo extension and init in create_app factory
codesungrape 7fd0265
Add users_db_setup fixture to clean users collection
codesungrape b728891
Add tests/test_auth.py with register and duplicate-email tests
codesungrape 18ab2fd
Run 'make format' command for formatting files
codesungrape 7a3c268
Use Flask Blueprint and restructure app layout
codesungrape 4fc903f
Update tests to reflect post Flask Blueprint restructure
codesungrape 9719e03
Patch mongo connection with mock for Flask-Pymongo
codesungrape 52809eb
Run 'make format' command for formatting
codesungrape 84e3d30
Add flask-bcrypt dependency to requirements.txt
codesungrape c5e262c
Add user registration logic with validation and hashing
codesungrape 3ff6916
Add edge case tests for empty/invalid JSON
codesungrape b26742d
Change all errors to use 'message' for consistency
codesungrape dee4d85
Add test for missing fields with parametrize to avoid DRY
codesungrape ee0b1bf
Run formatting makefile command
codesungrape 23a7c74
Rename variable to avoid Pylint duplicate code err
codesungrape 06051c7
Refactor/ app/__init__.py handles imports and registration
codesungrape 6819bcd
Run formatting and fic spelling/typos
codesungrape e8df69e
Break circular import cycle with an extensions module
codesungrape 62a77e8
Update openapi.yml for /auth/register endpoint.
codesungrape b0a9a43
Update openapi.yml
codesungrape 7fb13a8
Update app/extensions.py
codesungrape 889a975
Update app/extensions.py
codesungrape 8d04e6d
Update tests/test_auth.py
codesungrape a008464
Run formatting
codesungrape d17ce4f
Add failing test for invalid email formats
codesungrape e779ede
Update test to test behavior vs the msg
codesungrape a931f25
Install email-validator lib, use in register_user()
codesungrape 6fe3843
Run formatting
codesungrape 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
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
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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| """Module for Flask extensions.""" | ||
|
|
||
| from flask_pymongo import PyMongo | ||
|
|
||
| # Createempty PyMongo extension object globally | ||
| # This way, we can import it in other files and avoid a code smell: tighly-coupled, cyclic error | ||
codesungrape marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mongo = PyMongo() | ||
Empty file.
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,76 @@ | ||||||
| # pylint: disable=cyclic-import | ||||||
| """Routes for authorization for the JWT upgrade""" | ||||||
|
|
||||||
| import bcrypt | ||||||
| from email_validator import EmailNotValidError, validate_email | ||||||
| from flask import Blueprint, jsonify, request | ||||||
| from werkzeug.exceptions import BadRequest | ||||||
|
|
||||||
| from app.extensions import mongo | ||||||
|
|
||||||
| auth_bp = Blueprint("auth_bp", __name__, url_prefix="/auth") | ||||||
|
|
||||||
|
|
||||||
| @auth_bp.route("/register", methods=["POST"]) | ||||||
| def register_user(): | ||||||
| """ | ||||||
| Registers a new user. | ||||||
| Takes a JSON payload with "email" and "password". | ||||||
| It verfies it is not a duplicate email, | ||||||
| Hashes the password and stores the new user in the database. | ||||||
| """ | ||||||
|
|
||||||
| # VALIDATION the incoming data/request payload | ||||||
| try: | ||||||
| data = request.get_json() | ||||||
| if not data: | ||||||
| return jsonify({"message": "Request body cannot be empty"}), 400 | ||||||
|
|
||||||
| email = data.get("email") | ||||||
| password = data.get("password") | ||||||
|
|
||||||
| if not email or not password: | ||||||
| return jsonify({"message": "Email and password are required"}), 400 | ||||||
|
|
||||||
| # email-validator | ||||||
| try: | ||||||
| valid = validate_email(email, check_deliverability=False) | ||||||
|
|
||||||
| # use the normalized email for all subsequent operations | ||||||
| email = valid.normalized | ||||||
| except EmailNotValidError as e: | ||||||
| return jsonify({"message": str(e)}), 400 | ||||||
|
|
||||||
| except BadRequest: | ||||||
| return jsonify({"message": "Invalid JSON format"}), 400 | ||||||
|
|
||||||
| # Check for Duplicate User | ||||||
| # Easy access with Flask_PyMongo's 'mongo' | ||||||
| if mongo.db.users.find_one({"email": email}): | ||||||
| return jsonify({"message": "Email is already registered"}), 409 | ||||||
|
|
||||||
| # Password Hashing | ||||||
| # Generate a salt and hash the password | ||||||
| # result is a byte object representing the final hash | ||||||
| hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()) | ||||||
|
|
||||||
| # Database Insertion | ||||||
| user_id = mongo.db.users.insert_one( | ||||||
| { | ||||||
| "email": email, | ||||||
| # The hash is stored as a string in the DB | ||||||
| "password_hash": hashed_password.decode("utf-8"), | ||||||
| } | ||||||
| ).inserted_id | ||||||
| print(user_id) | ||||||
|
||||||
| print(user_id) | |
| logging.info("Registered new user with id: %s", user_id) |
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
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 |
|---|---|---|
|
|
@@ -7,4 +7,7 @@ pymongo | |
| python-dotenv | ||
| mongomock | ||
| black | ||
| isort | ||
| isort | ||
| flask_pymongo | ||
| flask-bcrypt | ||
| email-validator | ||
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
Oops, something went wrong.
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.