progress on jwt
This commit is contained in:
+11
-6
@@ -8,38 +8,43 @@ from app.errors import register_error_handlers
|
|||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
from app.extensions import db, migrate
|
from app.extensions import db, migrate
|
||||||
|
from flask_jwt_extended import (
|
||||||
|
JWTManager,
|
||||||
|
jwt_required,
|
||||||
|
create_access_token,
|
||||||
|
get_jwt_identity,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
""" Factory function to create a Flask app instance """
|
"""Factory function to create a Flask app instance"""
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
# Load configuration
|
# Load configuration
|
||||||
app.config.from_object(Config)
|
app.config.from_object(Config)
|
||||||
|
|
||||||
|
|
||||||
CORS(app)
|
CORS(app)
|
||||||
|
|
||||||
|
JWTManager(app)
|
||||||
|
|
||||||
# Swagger Doc
|
# Swagger Doc
|
||||||
SWAGGER_URL = app.config.get("SWAGGER_URL")
|
SWAGGER_URL = app.config.get("SWAGGER_URL")
|
||||||
API_URL = app.config.get("API_URL")
|
API_URL = app.config.get("API_URL")
|
||||||
|
|
||||||
|
|
||||||
# Register blueprints
|
# Register blueprints
|
||||||
app.register_blueprint(api)
|
app.register_blueprint(api)
|
||||||
|
|
||||||
swagger_ui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL)
|
swagger_ui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL)
|
||||||
app.register_blueprint(swagger_ui_blueprint, url_prefix=SWAGGER_URL)
|
app.register_blueprint(swagger_ui_blueprint, url_prefix=SWAGGER_URL)
|
||||||
|
|
||||||
|
|
||||||
# Error Handlers
|
# Error Handlers
|
||||||
register_error_handlers(app)
|
register_error_handlers(app)
|
||||||
|
|
||||||
from . import models
|
from . import models
|
||||||
|
|
||||||
# Database and Migrations
|
# Database and Migrations
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
|
|
||||||
|
|
||||||
migrate.init_app(app, db)
|
migrate.init_app(app, db)
|
||||||
|
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|||||||
@@ -4,6 +4,13 @@ from app.api.services.base_service import BaseService
|
|||||||
from app.utils.logger import logger
|
from app.utils.logger import logger
|
||||||
from app.api.schemas.authorization import AuthorizeRequestSchema
|
from app.api.schemas.authorization import AuthorizeRequestSchema
|
||||||
from app.api.helpers.response_helper import ResponseHelper
|
from app.api.helpers.response_helper import ResponseHelper
|
||||||
|
from flask_jwt_extended import (
|
||||||
|
JWTManager,
|
||||||
|
jwt_required,
|
||||||
|
create_access_token,
|
||||||
|
create_refresh_token,
|
||||||
|
get_jwt_identity,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AuthorizationService(BaseService):
|
class AuthorizationService(BaseService):
|
||||||
@@ -22,12 +29,29 @@ class AuthorizationService(BaseService):
|
|||||||
try:
|
try:
|
||||||
logger.info("Processing Authorization request")
|
logger.info("Processing Authorization request")
|
||||||
|
|
||||||
|
if not request.is_json:
|
||||||
|
return ResponseHelper.bad_request(message="Missing JSON in request")
|
||||||
|
|
||||||
# Validate input data using the Authorization schema
|
# Validate input data using the Authorization schema
|
||||||
schema = AuthorizeRequestSchema()
|
schema = AuthorizeRequestSchema()
|
||||||
validated_data = schema.load(data) # Raises ValidationError if invalid
|
validated_data = schema.load(data) # Raises ValidationError if invalid
|
||||||
|
|
||||||
|
### TODO: Access Database credentials here ###
|
||||||
|
|
||||||
|
if (
|
||||||
|
validated_data["username"] != "username"
|
||||||
|
or validated_data["password"] != "password"
|
||||||
|
):
|
||||||
|
return ResponseHelper.unauthorized(message="Invalid credentials")
|
||||||
|
|
||||||
|
access_token = create_access_token(identity=validated_data["username"])
|
||||||
|
refresh_token = create_refresh_token(identity=validated_data["username"])
|
||||||
|
|
||||||
# Simulated processing logic
|
# Simulated processing logic
|
||||||
response_data = {"resultCode": "00", "resultDescription": "Successful"}
|
response_data = {
|
||||||
|
"access_token": access_token,
|
||||||
|
"refresh_token": refresh_token,
|
||||||
|
}
|
||||||
|
|
||||||
return ResponseHelper.success(
|
return ResponseHelper.success(
|
||||||
data=response_data, message="Authorization processed successfully"
|
data=response_data, message="Authorization processed successfully"
|
||||||
|
|||||||
+5
-5
@@ -1,16 +1,16 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
"""Base configuration for Flask app"""
|
"""Base configuration for Flask app"""
|
||||||
|
|
||||||
|
|
||||||
SWAGGER_URL = os.getenv("SWAGGER_URL", "/documentation")
|
SWAGGER_URL = os.getenv("SWAGGER_URL", "/documentation")
|
||||||
API_URL = os.getenv("API_URL", "/swagger.json")
|
API_URL = os.getenv("API_URL", "/swagger.json")
|
||||||
|
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
VALID_APP_ID = os.getenv("VALID_APP_ID", "app1")
|
VALID_APP_ID = os.getenv("VALID_APP_ID", "app1")
|
||||||
VALID_API_KEY = os.getenv("VALID_API_KEY", "test-api-key-12345")
|
VALID_API_KEY = os.getenv("VALID_API_KEY", "test-api-key-12345")
|
||||||
BASIC_AUTH_USERNAME = os.environ.get("BASIC_AUTH_USERNAME", "user")
|
BASIC_AUTH_USERNAME = os.environ.get("BASIC_AUTH_USERNAME", "user")
|
||||||
BASIC_AUTH_PASSWORD = os.environ.get("BASIC_AUTH_PASSWORD", "password")
|
BASIC_AUTH_PASSWORD = os.environ.get("BASIC_AUTH_PASSWORD", "password")
|
||||||
|
|
||||||
DATABASE_USER = os.environ.get("DATABASE_USER")
|
DATABASE_USER = os.environ.get("DATABASE_USER")
|
||||||
@@ -19,11 +19,11 @@ class Config:
|
|||||||
DATABASE_PORT = os.environ.get("DATABASE_PORT", 10532)
|
DATABASE_PORT = os.environ.get("DATABASE_PORT", 10532)
|
||||||
DATABASE_NAME = os.environ.get("DATABASE_NAME")
|
DATABASE_NAME = os.environ.get("DATABASE_NAME")
|
||||||
|
|
||||||
SQLALCHEMY_DATABASE_URI = (
|
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_NAME}"
|
||||||
f"postgresql+psycopg2://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_NAME}"
|
|
||||||
)
|
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
SIMBRELLA_BASE_URL = os.getenv("SIMBRELLA_BASE_URL", "http://127.0.0.1:6337")
|
SIMBRELLA_BASE_URL = os.getenv("SIMBRELLA_BASE_URL", "http://127.0.0.1:6337")
|
||||||
|
|
||||||
|
JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", "secret-key")
|
||||||
|
|
||||||
|
|
||||||
settings = Config()
|
settings = Config()
|
||||||
@@ -27,3 +27,6 @@ python-dotenv
|
|||||||
# Requests
|
# Requests
|
||||||
requests
|
requests
|
||||||
|
|
||||||
|
# JWT
|
||||||
|
flask-jwt-extended
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user