# Copyright (c) 2019. Tobias Kurze """ This module provides functions related to authentication through the API. For example: listing of available auth providers or registration of users. Login through API does not start a new session, but instead returns JWT. """ import json from datetime import datetime from flask_jwt_extended import jwt_required, get_current_user, get_jwt from flask_restx import fields, Resource from backend import db from backend.api import api_control, get_jwt_identity control_command_response_model = api_control.model('Control Command Response', { 'time': fields.DateTime(required=False, description='Creation date of the recorder'), 'state': fields.String(min_length=3, required=True, description='The recorder\'s name'), 'output': fields.String(required=False, description='The recorder\'s description'), 'error': fields.String(required=False, description='The recorder\'s description'), }) @api_control.route('') class ControlCommand(Resource): control_command_parser = api_control.parser() control_command_parser.add_argument('recorder_id', type=int, default=1, required=True) control_command_parser.add_argument('command_id', type=int, default=1, required=True) control_command_parser.add_argument('parameters', default=json.dumps({'p1': 'v1'}), type=dict, required=False, location='json') @jwt_required @api_control.doc('run_command') @api_control.expect(control_command_parser) @api_control.marshal_with(control_command_response_model, skip_none=False, code=201) def post(self): print(get_current_user()) print(get_jwt_identity()) current_user = {'user': get_current_user(), 'claims': get_jwt()} args = self.control_command_parser.parse_args() return {'time': datetime.utcnow(), 'output': args, 'state': current_user}