Add basic auth

This commit is contained in:
Ivan Vazhenin
2023-03-12 16:37:41 +03:00
parent 3d999a5fc4
commit fde2d5513e
7 changed files with 149 additions and 4 deletions

25
main.py
View File

@@ -1,6 +1,9 @@
from uuid import uuid4
from xmlrpc.server import SimpleXMLRPCServer from xmlrpc.server import SimpleXMLRPCServer
import logging import logging
import os import os
import requests.graphql
from pygost import gost34112012256
# Expose a function # Expose a function
@@ -14,9 +17,18 @@ def aud_add(message):
return 'OK' return 'OK'
def auth_response(message, id, client): def auth_response(challenge, server_id, is_server):
logging.debug(id) logging.debug(challenge)
return {'error': False} logging.debug(server_id)
logging.debug(is_server)
msg = '%s%s%s' % (challenge, server_id, '12345678')
response = gost34112012256.new(msg.encode('utf-8')).digest().hex()
return {'error': False, 'response': response}
def auth_challenge():
logging.debug('get challenge')
return uuid4().hex
def main(): def main():
@@ -25,6 +37,7 @@ def main():
server.register_function(list_contents) server.register_function(list_contents)
server.register_function(aud_add) server.register_function(aud_add)
server.register_function(auth_response) server.register_function(auth_response)
server.register_function(auth_challenge)
try: try:
print('Use Control-C to exit') print('Use Control-C to exit')
server.serve_forever() server.serve_forever()
@@ -32,5 +45,11 @@ def main():
print('Exiting') print('Exiting')
def test():
print(requests.graphql.get_classifier())
# print(requests.graphql.get_catalog())
if __name__ == '__main__': if __name__ == '__main__':
main() main()
#test()

76
requests/catalog.py Normal file
View File

@@ -0,0 +1,76 @@
from datetime import datetime
import classifier
import graphql
import xml.etree.ElementTree as ET
class CatalogRequestInfo:
is_full = False
user_id = 0
class Catalog:
CD_SERVER_COLLECTIONS_NAME = "CD-Server Collections Set"
CD_SERVER_COLLECTIONS_VERSION = "1.0"
CD_SERVER_COLLECTION_TYPE = "ISSUE"
COLLECTIONS_TAG_NAME = "COLLECTIONS"
NAME_ATTRIBUTE_NAME = "Name"
VERSION_ATTRIBUTE_NAME = "Version"
CREATED_ATTRIBUTE_NAME = "Created"
FULL_ATTRIBUTE_NAME = "Full"
COLLECTION_TAG_NAME = "COLLECTION"
TYPE_ATTRIBUTE_NAME = "Type"
CHARTS_TAG_NAME = "CHARTS"
CHART_TAG_NAME = "CHART"
METADATA_VERSION_ATTRIBUTE_NAME = "MetadataVersion"
ID_ATTRIBUTE_NAME = "ID"
DELETED_ATTRIBUTE_NAME = "Deleted"
CLOSED_ATTRIBUTE_NAME = "Closed"
FOLDERS_TAG_NAME = "FOLDERS"
FOLDER_TAG_NAME = "FOLDER"
REF_TAG_NAME = "REF"
CHART_ATTRIBUTE_NAME = "Chart"
COLLECTION_NAME = 'CD-Server Collection'
request_info: CatalogRequestInfo
def write_catalog(self):
date = datetime.now()
collections = ET.Element(self.COLLECTIONS_TAG_NAME, {
self.NAME_ATTRIBUTE_NAME: self.COLLECTION_NAME,
self.VERSION_ATTRIBUTE_NAME: self.CD_SERVER_COLLECTIONS_VERSION,
self.CREATED_ATTRIBUTE_NAME: self.date_to_string(date),
})
if self.request_info.is_full:
collections.set(self.FULL_ATTRIBUTE_NAME, 'true')
if classifier.CLASSIFIER_VERSION:
collections.set(self.METADATA_VERSION_ATTRIBUTE_NAME, classifier.CLASSIFIER_VERSION)
self.write_collections(collections, date)
return ET.tostring(collections, encoding='unicode', xml_declaration=True)
def date_to_string(self, date):
return ''
def write_collections(self, collections, date):
collection = ET.SubElement(collections, self.COLLECTION_TAG_NAME, {
self.NAME_ATTRIBUTE_NAME: self.COLLECTION_NAME,
self.CREATED_ATTRIBUTE_NAME: self.date_to_string(date),
self.TYPE_ATTRIBUTE_NAME: self.CD_SERVER_COLLECTION_TYPE,
})
self.write_charts(collection)
def write_charts(self, collection):
charts = ET.SubElement(collection, self.CHARTS_TAG_NAME)
for c in []:
self.write_chart(charts, c)
def write_chart(self, charts, chart):
ch = ET.SubElement(charts, self.CHART_TAG_NAME)

7
requests/classifier.py Normal file
View File

@@ -0,0 +1,7 @@
import graphql
CLASSIFIER_VERSION = '8.23'
class Classifier:
def get_classifier(self):

View File

@@ -2,6 +2,7 @@ from datetime import datetime, timedelta
from enumerations import ReplicationPackageStatusEnum from enumerations import ReplicationPackageStatusEnum
from models import CorrectingReplicationOutPackage, ReplicantInfo from models import CorrectingReplicationOutPackage, ReplicantInfo
from typing import List from typing import List
import xml.etree.ElementTree as ET
class CorrectingReplicationService: class CorrectingReplicationService:
@@ -58,4 +59,13 @@ class CorrectingReplicationService:
pkg.replication_timeout_date = date + delta pkg.replication_timeout_date = date + delta
return pkg return pkg
def get_package_xml(self) -> str: def get_package_xml(self, pkg: CorrectingReplicationOutPackage, request_uuid: str) -> str:
request_xml: ET.Element = xml_service.get_common_request_xml(request_uuid)
element = ET.SubElement(request_xml, self.PACKAGE_TAG_NAME, {
self.PACKAGE_ID_ATTRIBUTE_NAME: pkg.package_id,
self.SERVER_VERSION_ATTRIBUTE_NAME: '1.0.0',
self.SERVER_ID_ATTRIBUTE_NAME: 'ipd-server',
self.CLASSIFIER_VERSION_ATTRIBUTE_NAME: classifier.,
})
xml_service.set_request_uuid(request_xml, request_uuid)
return ET.tostring(request_xml, encoding='unicode', xml_declaration=True)

30
requests/graphql.py Normal file
View File

@@ -0,0 +1,30 @@
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
transport = AIOHTTPTransport(url="https://gql.ivazh.ru/graphql/")
def get_classifier():
client = Client(transport=transport, fetch_schema_from_transport=True)
query = gql(
"""
query getClassifier {
getClassifier(name: "ood")
}
"""
)
result = client.execute(query)
return result['getClassifier']
def get_catalog():
client = Client(transport=transport, fetch_schema_from_transport=True)
query = gql(
"""
query getCatalog {
getCatalog(name: "ood")
}
"""
)
result = client.execute(query)
return result['getClassifier']

View File

@@ -1,5 +1,6 @@
from enum import Enum from enum import Enum
class Messages(Enum): class Messages(Enum):
REQUEST_EXECUTED_PARTITIALY = 1 REQUEST_EXECUTED_PARTITIALY = 1
REQUEST_FAILED = 2 REQUEST_FAILED = 2

2
requirements.txt Normal file
View File

@@ -0,0 +1,2 @@
gql~=3.4.0
pygost~=5.11