Pequena limpeza
This commit is contained in:
25
api/views/authentication.py
Normal file
25
api/views/authentication.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from functools import wraps
|
||||
from flask import request
|
||||
from api.app import app
|
||||
from .fetch import get_trainer_by_nick_fail, NotFound
|
||||
from .errors import AuthenticationFailure
|
||||
import jwt
|
||||
|
||||
# authenticação do trainer (decorator)
|
||||
def token_required(f):
|
||||
@wraps(f)
|
||||
def decorated(*args, **kwargs):
|
||||
try:
|
||||
token = request.headers["authorization"]
|
||||
data = jwt.decode(token, app.config["SECRET_KEY"], algorithms=["HS256"])
|
||||
trainer = get_trainer_by_nick_fail(data["username"])
|
||||
except (TypeError, KeyError):
|
||||
return AuthenticationFailure("JWT token required")
|
||||
except NotFound:
|
||||
return AuthenticationFailure("Trainer not found")
|
||||
except:
|
||||
return AuthenticationFailure("JWT token is invalid or expired")
|
||||
|
||||
return f(trainer, *args, **kwargs)
|
||||
return decorated
|
||||
|
@@ -1,15 +1,6 @@
|
||||
from functools import wraps
|
||||
from flask import request
|
||||
from api.models.trainer import Trainer
|
||||
from .errors import AuthenticationFailure
|
||||
from api.app import app
|
||||
import requests
|
||||
import json
|
||||
import jwt
|
||||
|
||||
class HTTPError(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
class NotFound(Exception):
|
||||
def __init__(self, message):
|
||||
@@ -33,25 +24,7 @@ def get_trainer_by_nick_fail(nickname):
|
||||
def get_pokemon_fail(trainer, id):
|
||||
return get_or_not_found(lambda : trainer.pokemons_list.filter_by(id=id).one())
|
||||
|
||||
# authenticação do trainer (decorator)
|
||||
def token_required(f):
|
||||
@wraps(f)
|
||||
def decorated(*args, **kwargs):
|
||||
try:
|
||||
token = request.headers["authorization"]
|
||||
data = jwt.decode(token, app.config["SECRET_KEY"], algorithms=["HS256"])
|
||||
trainer = get_trainer_by_nick_fail(data["username"])
|
||||
except (TypeError, KeyError):
|
||||
return AuthenticationFailure("JWT token required")
|
||||
except NotFound:
|
||||
return AuthenticationFailure("Trainer not found")
|
||||
except:
|
||||
return AuthenticationFailure("JWT token is invalid or expired")
|
||||
|
||||
return f(trainer, *args, **kwargs)
|
||||
return decorated
|
||||
|
||||
# helpers internos
|
||||
# helper interno
|
||||
def cant_fetch_error(pokemon):
|
||||
raise NotFound("Could not fetch data for pokemon with id {}".format(pokemon.pokemon_id))
|
||||
|
@@ -2,7 +2,7 @@ from api.models.pokemon_owned import pokemon_owned_schema, pokemon_owned_schemas
|
||||
from api.app import db
|
||||
from .parse_args import parse_limit, parse_offset, ParsingException, parse_json_obj
|
||||
from .errors import ParsingError, FetchError, ConflictingResources
|
||||
from .helper import *
|
||||
from .fetch import *
|
||||
from aiohttp import ClientSession
|
||||
import asyncio
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
Reference in New Issue
Block a user