implements login view

This commit is contained in:
Gardient
2021-09-19 22:15:08 +03:00
parent 404dbb6870
commit cc44c4056e
5 changed files with 46 additions and 1 deletions

View File

@@ -32,7 +32,11 @@ def register_extensions(app: Flask):
def register_blueprints(app: Flask): def register_blueprints(app: Flask):
"""Register Flask blueprints.""" """Register Flask blueprints."""
pass api_blueprint = Blueprint('api', __name__, url_prefix='/api')
api_blueprint.register_blueprint(login.views.blueprint, url_prefix='/login')
app.register_blueprint(api_blueprint)
def register_errorhandlers(app: Flask): def register_errorhandlers(app: Flask):

3
api/login/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
"""API Login"""
from . import views

4
api/login/models.py Normal file
View File

@@ -0,0 +1,4 @@
class TokenResponse(object):
def __init__(self, token: str) -> None:
super().__init__()
self.token = token

11
api/login/serializers.py Normal file
View File

@@ -0,0 +1,11 @@
from marshmallow import Schema, fields
class LoginSchema(Schema):
username = fields.Str()
password = fields.Str(load_only=True)
class TokenResponseSchema(Schema):
token = fields.Str()
login_schema = LoginSchema()
token_response_schema = TokenResponseSchema()

23
api/login/views.py Normal file
View File

@@ -0,0 +1,23 @@
from flask import Blueprint, current_app
from flask_jwt_extended import jwt_required, create_access_token
from flask_apispec import use_kwargs, marshal_with
from api.exceptions import BadRequestException
import api.constants as constants
from .models import TokenResponse
from .serializers import login_schema, token_response_schema
blueprint = Blueprint('login', __name__)
@blueprint.route('', methods=['POST'])
@jwt_required(optional=True)
@use_kwargs(login_schema)
@marshal_with(token_response_schema)
def login_user(username, password, **kwargs):
print(f'user: {username}; pass: {password}; expected:{current_app.config[constants.API_PASS]}')
if username == constants.API_USER and password == current_app.config[constants.API_PASS]:
return TokenResponse(create_access_token(identity=username, fresh=True))
else:
raise BadRequestException("Wrong combination of username and password")