2018-05-07 11:57:53 +00:00
|
|
|
from flask_restful import Resource, abort
|
|
|
|
from webargs import fields
|
2018-10-08 19:50:34 +00:00
|
|
|
from marshmallow import Schema
|
2018-05-07 11:57:53 +00:00
|
|
|
from webargs.flaskparser import use_args
|
2018-05-07 14:13:07 +00:00
|
|
|
from flasgger import swag_from
|
2018-10-06 12:07:40 +00:00
|
|
|
from app.api.auth_protection import ProtectedResource
|
2018-10-06 11:38:27 +00:00
|
|
|
import app.accounts.api as accounts
|
2018-05-06 19:42:21 +00:00
|
|
|
|
|
|
|
|
2018-10-08 19:50:34 +00:00
|
|
|
class UserInfoSchema(Schema):
|
|
|
|
username = fields.Str(required=True)
|
|
|
|
password = fields.Str(required=True)
|
|
|
|
|
|
|
|
|
2018-05-06 19:42:21 +00:00
|
|
|
class TokenResource(Resource):
|
|
|
|
|
2018-10-08 19:50:34 +00:00
|
|
|
@use_args(UserInfoSchema(), locations=('json',))
|
2018-05-07 14:13:07 +00:00
|
|
|
@swag_from('swagger/create_token_spec.yaml')
|
2018-05-07 11:57:53 +00:00
|
|
|
def post(self, args):
|
2018-05-06 19:42:21 +00:00
|
|
|
try:
|
|
|
|
token = accounts.create_token(
|
|
|
|
args['username'],
|
|
|
|
args['password'])
|
|
|
|
if token:
|
2018-05-07 11:57:53 +00:00
|
|
|
return {'status': 'success', 'token': token}, 200
|
2018-05-06 19:42:21 +00:00
|
|
|
except ValueError:
|
|
|
|
abort(401, message='Invalid credentials', status='error')
|
2018-05-22 14:20:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ValidateTokenResource(ProtectedResource):
|
|
|
|
@swag_from('swagger/validate_token_spec.yaml')
|
|
|
|
def get(self):
|
|
|
|
return {'status': 'success', 'message': 'Valid token'}, 200
|