From 8cb0661eb6c8f82b8030d99dabfeabe53b84e857 Mon Sep 17 00:00:00 2001 From: esensar Date: Tue, 22 May 2018 16:20:14 +0200 Subject: [PATCH] Add token validation endpoint --- app/api/__init__.py | 3 ++- .../swagger/validate_token_spec.yaml | 22 +++++++++++++++++++ app/api/resources/token.py | 7 ++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 app/api/resources/swagger/validate_token_spec.yaml diff --git a/app/api/__init__.py b/app/api/__init__.py index b307e92..96d3ce7 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -39,7 +39,7 @@ class ProtectedResource(Resource): def add_resources(): from .resources.account import AccountResource, AccountListResource - from .resources.token import TokenResource + from .resources.token import TokenResource, ValidateTokenResource from .resources.device import (DeviceResource, DeviceRecordingResource, DeviceListResource) @@ -47,6 +47,7 @@ def add_resources(): api.add_resource(AccountResource, '/v1/accounts/') api.add_resource(AccountListResource, '/v1/accounts') api.add_resource(TokenResource, '/v1/token') + api.add_resource(ValidateTokenResource, '/v1/token/validate') api.add_resource(DeviceResource, '/v1/devices/') api.add_resource(DeviceRecordingResource, '/v1/devices//recordings') diff --git a/app/api/resources/swagger/validate_token_spec.yaml b/app/api/resources/swagger/validate_token_spec.yaml new file mode 100644 index 0000000..ea5511b --- /dev/null +++ b/app/api/resources/swagger/validate_token_spec.yaml @@ -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' diff --git a/app/api/resources/token.py b/app/api/resources/token.py index 345127f..d98f2b4 100644 --- a/app/api/resources/token.py +++ b/app/api/resources/token.py @@ -2,6 +2,7 @@ from flask_restful import Resource, abort from webargs import fields from webargs.flaskparser import use_args from flasgger import swag_from +from app.api import ProtectedResource import app.accounts as accounts @@ -25,3 +26,9 @@ class TokenResource(Resource): return {'status': 'success', 'token': token}, 200 except ValueError: 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