def authenticate(user=None): # noqa: E501 """Authenticate Authenticate with the API # noqa: E501 :param user: The user authentication object. :type user: dict | bytes :rtype: UserAuth """ if connexion.request.is_json: user = UserAuth.from_dict(connexion.request.get_json()) # noqa: E501 credentials = mapUserAuthToCredentials(user) auth = ApitaxAuthentication.login(credentials) if (not auth): return ErrorResponse(status=401, message="Invalid credentials") access_token = create_access_token(identity={ 'username': user.username, 'role': auth['role'] }) refresh_token = create_refresh_token(identity={ 'username': user.username, 'role': auth['role'] }) return AuthResponse(status=201, message='User ' + user.username + ' was authenticated as ' + auth['role'], access_token=access_token, refresh_token=refresh_token, auth=UserAuth(username=auth['credentials'].username, api_token=auth['credentials'].token))
def refresh_token(): # noqa: E501 """Refreshes login token using refresh token Refreshes login token using refresh token # noqa: E501 :rtype: UserAuth """ current_user = get_jwt_identity() if (not current_user): return ErrorResponse(status=401, message="Not logged in") access_token = create_access_token(identity=current_user) return AuthResponse(status=201, message='Refreshed Access Token', access_token=access_token, auth=UserAuth())
def rename_script(rename=None): # noqa: E501 """Rename a script Rename a script # noqa: E501 :param rename: The data needed to save this script :type rename: dict | bytes :rtype: Response """ if connexion.request.is_json: rename = Rename.from_dict(connexion.request.get_json()) # noqa: E501 if (not hasAccess()): return redirectUnauthorized() driver = LoadedDrivers.getDefaultDriver() if (not driver.renameScript(rename.original.name, rename.new.name)): return ErrorResponse(status=500, message='Cannot rename to an existing file.') return Response(status=200, body={'file-name': rename.new.name})
def command(execute=None): # noqa: E501 """Execute a Command Execute a command # noqa: E501 :param execute: The data needed to execute this command :type execute: dict | bytes :rtype: Response """ if connexion.request.is_json: execute = Execute.from_dict(connexion.request.get_json()) # noqa: E501 if (not hasAccess()): return redirectUnauthorized() try: connector = None parameters = {} if (execute.command.parameters): parameters = execute.command.parameters credentials = Credentials() options = Options() if (execute.command.options): if ('debug' in execute.command.options): options.debug = execute.command.options['debug'] if ('sensitive' in execute.command.options): options.sensitive = execute.command.options['sensitive'] if (execute.auth): credentials = mapUserAuthToCredentials(execute.auth, credentials) if (not execute.auth.api_token): options.sensitive = True connector = Connector(options=options, credentials=credentials, command=execute.command.command, parameters=parameters) commandHandler = connector.execute() response = Response( status=commandHandler.response.getResponseStatusCode(), body=commandHandler.response.getResponseBody()) if (options.debug): response.log = connector.logBuffer return response except: State.log.error(traceback.format_exc()) if ('debug' in execute.command.options and execute.command.options['debug']): return ErrorResponse( status=500, message= "Uncaught exception occured during processing. To get a larger stack trace, visit the logs.", state=traceback.format_exc(3)) else: return ErrorResponse(status=500, message="")