# 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_claims from flask_restplus import fields, Resource from backend import db from backend.api import api_control, get_jwt_identity, Recorder, RecorderCommand, pprint from backend.recorder_adapters.helpers import execute_recorder_command control_command_response_model = api_control.model('Control Command Response', { 'time': fields.DateTime(required=False, description='Creation date of the recorder'), 'ok': fields.Boolean(required=True, description='Field indicating whether command execution was successful.'), 'output': fields.String(required=False, description='Command output in case of success'), 'error': fields.String(required=False, description='Error description in case of a problem.'), }) @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={}, 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_claims()} #TODO: right check! (acl, etc.) args = self.control_command_parser.parse_args() recorder = Recorder.get_by_identifier(args.get('recorder_id')) if recorder is None: api_control.abort(404, "Recorder not found!") command = RecorderCommand.get_by_identifier(args.get('command_id')) if command is None: api_control.abort(404, "Command not found!") success, output = execute_recorder_command(recorder, command, args.get('parameters', None)) if success: return {'time': datetime.utcnow(), 'output': output, 'ok': success} return {'time': datetime.utcnow(), 'error': output, 'ok': success}