Add token validation endpoint

master
esensar 2018-05-22 16:20:14 +02:00
parent 44d5d67b40
commit 8cb0661eb6
3 changed files with 31 additions and 1 deletions

View File

@ -39,7 +39,7 @@ class ProtectedResource(Resource):
def add_resources(): def add_resources():
from .resources.account import AccountResource, AccountListResource from .resources.account import AccountResource, AccountListResource
from .resources.token import TokenResource from .resources.token import TokenResource, ValidateTokenResource
from .resources.device import (DeviceResource, from .resources.device import (DeviceResource,
DeviceRecordingResource, DeviceRecordingResource,
DeviceListResource) DeviceListResource)
@ -47,6 +47,7 @@ def add_resources():
api.add_resource(AccountResource, '/v1/accounts/<int:account_id>') api.add_resource(AccountResource, '/v1/accounts/<int:account_id>')
api.add_resource(AccountListResource, '/v1/accounts') api.add_resource(AccountListResource, '/v1/accounts')
api.add_resource(TokenResource, '/v1/token') api.add_resource(TokenResource, '/v1/token')
api.add_resource(ValidateTokenResource, '/v1/token/validate')
api.add_resource(DeviceResource, '/v1/devices/<int:device_id>') api.add_resource(DeviceResource, '/v1/devices/<int:device_id>')
api.add_resource(DeviceRecordingResource, api.add_resource(DeviceRecordingResource,
'/v1/devices/<int:device_id>/recordings') '/v1/devices/<int:device_id>/recordings')

View File

@ -0,0 +1,22 @@
Validates token passed in Authorization header
---
tags:
- Token
responses:
200:
description: Valid token
schema:
required:
- status
- message
properties:
status:
type: string
default: success
message:
type: string
default: Valid token
401:
description: Bad credentials
schema:
$ref: '#/definitions/UnauthorizedError'

View File

@ -2,6 +2,7 @@ from flask_restful import Resource, abort
from webargs import fields from webargs import fields
from webargs.flaskparser import use_args from webargs.flaskparser import use_args
from flasgger import swag_from from flasgger import swag_from
from app.api import ProtectedResource
import app.accounts as accounts import app.accounts as accounts
@ -25,3 +26,9 @@ class TokenResource(Resource):
return {'status': 'success', 'token': token}, 200 return {'status': 'success', 'token': token}, 200
except ValueError: except ValueError:
abort(401, message='Invalid credentials', status='error') abort(401, message='Invalid credentials', status='error')
class ValidateTokenResource(ProtectedResource):
@swag_from('swagger/validate_token_spec.yaml')
def get(self):
return {'status': 'success', 'message': 'Valid token'}, 200