"schemas/lib/imma1_nodt/code_tables/ICOADS.C7.SMV.json" did not exist on "c28e5b5e160cbbe58f476c39a9f72ec711a44ba3"
Commit 4408fd96 authored by Trishna Saeharaseelan's avatar Trishna Saeharaseelan
Browse files

feat(wrappers): add wrappers for rabbitmq

parent 1352b3a1
...@@ -51,7 +51,12 @@ pipenv run python api.py ...@@ -51,7 +51,12 @@ pipenv run python api.py
#### Create some clients #### Create some clients
`POST` to `http://localhost:3000/clients` `POST` to `http://localhost:3000/clients`. Example:
```
{
"client_id": "qwert123456789"
}
```
#### Event bus #### Event bus
......
...@@ -8,5 +8,5 @@ __all__ = [ ...@@ -8,5 +8,5 @@ __all__ = [
# "workers", # "workers",
"fixtures", "fixtures",
"tests", "tests",
"run", # "run",
] ]
...@@ -5,4 +5,4 @@ __all__ = [ ...@@ -5,4 +5,4 @@ __all__ = [
os.path.splitext(os.path.basename(x))[0] os.path.splitext(os.path.basename(x))[0]
for x in os.listdir(os.path.dirname(__file__)) for x in os.listdir(os.path.dirname(__file__))
if x.endswith(".py") and x != "__init__.py" if x.endswith(".py") and x != "__init__.py"
] ]
\ No newline at end of file
...@@ -2,24 +2,24 @@ import json ...@@ -2,24 +2,24 @@ import json
from flask_restful import Resource, abort from flask_restful import Resource, abort
from models.token import TokenModel from models.token import TokenModel
class AuthResource(Resource):
def __init__(self): class AuthResource(Resource):
def __init__(self):
self.token = TokenModel() self.token = TokenModel()
with open("clients.json", "r") as clients_file: with open("clients.json", "r") as clients_file:
self.clients = json.load(clients_file) self.clients = json.load(clients_file)
def auth(self, request): def auth(self, request):
allow = False allow = False
auth = request.headers.get('Authorization', False) auth = request.headers.get("Authorization", False)
if auth: if auth:
token = auth.split(' ').pop() token = auth.split(" ").pop()
parsed = self.token.validate(token) parsed = self.token.validate(token)
if parsed['valid']: if parsed["valid"]:
client = self.clients.get(parsed['client_id']) client = self.clients.get(parsed["client_id"])
if client: if client:
self.client = client self.client = client
allow = True allow = True
if not allow: if not allow:
abort(403, message="Invalid token") abort(403, message="Invalid token")
return allow return allow
\ No newline at end of file
...@@ -8,12 +8,12 @@ import string ...@@ -8,12 +8,12 @@ import string
class ClientSchema(Schema): class ClientSchema(Schema):
client_id = fields.Str(required=True) client_id = fields.Str(required=True)
client_name = fields.Str(required=True) client_name = fields.Str(required=True)
subscription = fields.Str(required=True) subscription = fields.Str(required=True)
class ClientsFile: class ClientsFile:
file = "clients.json" file = "fixtures.clients.json"
mtime = 0 mtime = 0
clients = {} clients = {}
parser = None parser = None
...@@ -27,7 +27,7 @@ class ClientsFile: ...@@ -27,7 +27,7 @@ class ClientsFile:
if mtime > self.mtime: if mtime > self.mtime:
with open(self.file, "r") as client_file: with open(self.file, "r") as client_file:
self.clients = json.load(client_file) self.clients = json.load(client_file)
except FileNotFoundError as error: except FileNotFoundError:
self.clients = {} self.clients = {}
self.save() self.save()
...@@ -42,7 +42,7 @@ class ClientsFile: ...@@ -42,7 +42,7 @@ class ClientsFile:
return client return client
def add(self, client): def add(self, client):
client['secret'] = self.secret() client["secret"] = self.secret()
self.clients[client["client_id"]] = client self.clients[client["client_id"]] = client
self.save() self.save()
return client return client
...@@ -70,28 +70,36 @@ class ClientsFile: ...@@ -70,28 +70,36 @@ class ClientsFile:
def secret(self, chars=36): def secret(self, chars=36):
res = "".join( res = "".join(
random.choices( random.choices(
string.ascii_lowercase + string.ascii_uppercase + string.digits, k=chars (
string.ascii_lowercase,
+string.ascii_uppercase,
+string.digits,
),
k=chars,
) )
) )
return str(res) return str(res)
clients_file = ClientsFile() clients_file = ClientsFile()
# Client # Client
class Client(Resource): class Client(Resource):
clients_file = None clients_file = None
def __init__(self):
def __init__(self):
self.schema = ClientSchema() self.schema = ClientSchema()
self.clients_file = ClientsFile() self.clients_file = ClientsFile()
def get(self, client_id): def get(self, client_id):
client = self.clients_file.find(client_id) client = self.clients_file.find(client_id)
del client['secret'] del client["secret"]
if not client: if not client:
abort(404, message="No client with id: {}".format(client_id)) abort(404, message="No client with id: {}".format(client_id))
return client return client
def delete(self, todo_id): def delete(self, client_id):
client = self.clients_file.find(client_id) client = self.clients_file.find(client_id)
if not client: if not client:
abort(404, message="No client with id: {}".format(client_id)) abort(404, message="No client with id: {}".format(client_id))
...@@ -102,9 +110,9 @@ class Client(Resource): ...@@ -102,9 +110,9 @@ class Client(Resource):
def put(self, client_id): def put(self, client_id):
args = request.get_json() args = request.get_json()
errors = self.schema.validate(args) errors = self.schema.validate(args)
if errors: if errors:
abort(400, message=str(errors)) abort(400, message=str(errors))
client = self.clients_file.find(client_id) client = self.clients_file.find(client_id)
if not client: if not client:
abort(404, message="No client with id: {}".format(client_id)) abort(404, message="No client with id: {}".format(client_id))
...@@ -115,7 +123,7 @@ class Client(Resource): ...@@ -115,7 +123,7 @@ class Client(Resource):
# ClientList # ClientList
class ClientList(Resource): class ClientList(Resource):
def __init__(self): def __init__(self):
self.schema = ClientSchema() self.schema = ClientSchema()
self.clients_file = ClientsFile() self.clients_file = ClientsFile()
...@@ -127,13 +135,13 @@ class ClientList(Resource): ...@@ -127,13 +135,13 @@ class ClientList(Resource):
def post(self): def post(self):
args = request.get_json() args = request.get_json()
print("Args is %s" % args)
errors = self.schema.validate(args) errors = self.schema.validate(args)
if errors: if errors:
abort(400, message=str(errors)) abort(400, message=str(errors))
print("errors is %s" % errors)
client = clients_file.find(args["client_id"]) client_id = clients_file.find(args["client_id"])
if client: if client_id:
abort(403, message="Duplicate client id: {}".format(client_id)) abort(403, message="Duplicate client id: {}".format(client_id))
else: else:
client = clients_file.add(args) client = clients_file.add(args)
......
...@@ -8,6 +8,7 @@ from api.auth_resource import AuthResource ...@@ -8,6 +8,7 @@ from api.auth_resource import AuthResource
class NotifySchema(Schema): class NotifySchema(Schema):
body = fields.Str(required=True) body = fields.Str(required=True)
class Notify(AuthResource): class Notify(AuthResource):
clients = None clients = None
schema = None schema = None
...@@ -15,7 +16,7 @@ class Notify(AuthResource): ...@@ -15,7 +16,7 @@ class Notify(AuthResource):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.schema = NotifySchema() self.schema = NotifySchema()
def post(self): def post(self):
args = request.get_json() args = request.get_json()
errors = self.schema.validate(args) errors = self.schema.validate(args)
...@@ -25,16 +26,20 @@ class Notify(AuthResource): ...@@ -25,16 +26,20 @@ class Notify(AuthResource):
allow = False allow = False
body = args.get("body") body = args.get("body")
message = { message = {
'topic': 'broadcast', "topic": "broadcast",
'message': body, "message": body,
} }
allow = self.auth(request) allow = self.auth(request)
if allow: if allow:
notify_queue = self.client['client_id'] + "-broadcast" notify_queue = self.client["client_id"] + "-broadcast"
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) connection = pika.BlockingConnection(
pika.ConnectionParameters(host="localhost")
)
channel = connection.channel() channel = connection.channel()
channel.queue_declare(queue=notify_queue, durable=True) channel.queue_declare(queue=notify_queue, durable=True)
channel.basic_publish(exchange="", routing_key=notify_queue, body=json.dumps(message)) channel.basic_publish(
exchange="", routing_key=notify_queue, body=json.dumps(message)
)
connection.close() connection.close()
...@@ -2,7 +2,8 @@ from flask_restful import request, abort ...@@ -2,7 +2,8 @@ from flask_restful import request, abort
from marshmallow import Schema, fields from marshmallow import Schema, fields
import pika import pika
import json import json
from models.token import TokenModel
# from models.token import TokenModel
from api.auth_resource import AuthResource from api.auth_resource import AuthResource
...@@ -25,11 +26,11 @@ class Receive(AuthResource): ...@@ -25,11 +26,11 @@ class Receive(AuthResource):
messages = [] messages = []
max_messages = request.args.get("max_messages", 10) max_messages = request.args.get("max_messages", 10)
allow = self.auth(request) allow = self.auth(request)
if allow: if allow:
inbox_queue = self.client['client_id'] + "-inbox" inbox_queue = self.client["client_id"] + "-inbox"
if allow: if allow:
connection = pika.BlockingConnection( connection = pika.BlockingConnection(
pika.ConnectionParameters(host="localhost") pika.ConnectionParameters(host="localhost")
...@@ -37,7 +38,9 @@ class Receive(AuthResource): ...@@ -37,7 +38,9 @@ class Receive(AuthResource):
channel = connection.channel() channel = connection.channel()
channel.queue_declare(queue=inbox_queue, durable=True) channel.queue_declare(queue=inbox_queue, durable=True)
while len(messages) < max_messages: while len(messages) < max_messages:
method_frame, header_frame, body = channel.basic_get(inbox_queue) method_frame, header_frame, body = channel.basic_get(
inbox_queue,
)
if method_frame: if method_frame:
print(method_frame, header_frame, body) print(method_frame, header_frame, body)
channel.basic_ack(method_frame.delivery_tag) channel.basic_ack(method_frame.delivery_tag)
......
...@@ -4,10 +4,12 @@ from marshmallow import Schema, fields ...@@ -4,10 +4,12 @@ from marshmallow import Schema, fields
import pika import pika
from api.auth_resource import AuthResource from api.auth_resource import AuthResource
class SendSchema(Schema): class SendSchema(Schema):
body = fields.Str(required=True) body = fields.Str(required=True)
topic = fields.Str(required=True) topic = fields.Str(required=True)
class Send(AuthResource): class Send(AuthResource):
clients = None clients = None
schema = None schema = None
...@@ -15,7 +17,7 @@ class Send(AuthResource): ...@@ -15,7 +17,7 @@ class Send(AuthResource):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.schema = SendSchema() self.schema = SendSchema()
def post(self): def post(self):
args = request.get_json() args = request.get_json()
errors = self.schema.validate(args) errors = self.schema.validate(args)
...@@ -23,18 +25,22 @@ class Send(AuthResource): ...@@ -23,18 +25,22 @@ class Send(AuthResource):
abort(400, message=str(errors)) abort(400, message=str(errors))
allow = self.auth(request) allow = self.auth(request)
if allow: if allow:
body = args.get("body") body = args.get("body")
topic = args.get("topic") topic = args.get("topic")
outbox_queue = self.client['client_id'] + "-outbox" outbox_queue = self.client["client_id"] + "-outbox"
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) connection = pika.BlockingConnection(
pika.ConnectionParameters(host="localhost")
)
channel = connection.channel() channel = connection.channel()
channel.queue_declare(queue=outbox_queue, durable=True) channel.queue_declare(queue=outbox_queue, durable=True)
message = { message = {
'topic': topic, "topic": topic,
'message': body, "message": body,
} }
channel.basic_publish(exchange="", routing_key=outbox_queue, body=json.dumps(message)) channel.basic_publish(
exchange="", routing_key=outbox_queue, body=json.dumps(message)
)
connection.close() connection.close()
import json import json
import pika
from flask_restful import Resource, request, abort from flask_restful import Resource, request, abort
from marshmallow import Schema, fields from marshmallow import Schema, fields
from models.token import TokenModel from models.token import TokenModel
...@@ -20,7 +19,7 @@ class Token(Resource): ...@@ -20,7 +19,7 @@ class Token(Resource):
self.model = TokenModel() self.model = TokenModel()
with open("clients.json", "r") as clients_file: with open("clients.json", "r") as clients_file:
self.clients = json.load(clients_file) self.clients = json.load(clients_file)
def get(self): def get(self):
errors = self.schema.validate(request.args) errors = self.schema.validate(request.args)
if errors: if errors:
...@@ -28,7 +27,7 @@ class Token(Resource): ...@@ -28,7 +27,7 @@ class Token(Resource):
token = None token = None
allow = False allow = False
max_messages = request.args.get("max_messages", 10) # max_messages = request.args.get("max_messages", 10)
client_id = request.args.get("client_id") client_id = request.args.get("client_id")
if client_id in self.clients: if client_id in self.clients:
client = self.clients.get(client_id) client = self.clients.get(client_id)
...@@ -40,4 +39,4 @@ class Token(Resource): ...@@ -40,4 +39,4 @@ class Token(Resource):
else: else:
abort(403, message="Invalid client credentials") abort(403, message="Invalid client credentials")
return token return token
\ No newline at end of file
import os import os
__all__ = [ __all__ = [
os.path.splitext(os.path.basename(x))[0] os.path.splitext(os.path.basename(x))[0]
for x in os.listdir(os.path.dirname(__file__)) for x in os.listdir(os.path.dirname(__file__))
if x.endswith(".py") and x != "__init__.py" if x.endswith(".py") and x != "__init__.py"
] ]
...@@ -6,7 +6,9 @@ import json ...@@ -6,7 +6,9 @@ import json
def main(): def main():
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) connection = pika.BlockingConnection(
pika.ConnectionParameters(host="localhost"),
)
channel = connection.channel() channel = connection.channel()
queue_name = sys.argv[1] queue_name = sys.argv[1]
...@@ -17,7 +19,11 @@ def main(): ...@@ -17,7 +19,11 @@ def main():
message = json.loads(body.decode()) message = json.loads(body.decode())
print(" [x] Received %r" % message) print(" [x] Received %r" % message)
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) channel.basic_consume(
queue=queue_name,
on_message_callback=callback,
auto_ack=True,
)
print(" [*] Waiting for messages. To exit press CTRL+C") print(" [*] Waiting for messages. To exit press CTRL+C")
channel.start_consuming() channel.start_consuming()
......
...@@ -4,7 +4,9 @@ import sys ...@@ -4,7 +4,9 @@ import sys
import json import json
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) connection = pika.BlockingConnection(
pika.ConnectionParameters(host="localhost"),
)
channel = connection.channel() channel = connection.channel()
queue_name = sys.argv[1] queue_name = sys.argv[1]
......
...@@ -21,13 +21,16 @@ def deliver(body, broadcast_exchange): ...@@ -21,13 +21,16 @@ def deliver(body, broadcast_exchange):
def listen(queue_name, broadcast_exchange): def listen(queue_name, broadcast_exchange):
def bcast_callback(ch, method, properties, body): def bcast_callback(ch, method, properties, body):
delivered = deliver(body, broadcast_exchange) # delivered = deliver(body, broadcast_exchange)
ch.basic_ack(delivery_tag=method.delivery_tag) ch.basic_ack(delivery_tag=method.delivery_tag)
listen_connection = get_connection() listen_connection = get_connection()
listen_channel = listen_connection.channel() listen_channel = listen_connection.channel()
listen_channel.queue_declare(queue=queue_name, durable=True) listen_channel.queue_declare(queue=queue_name, durable=True)
listen_channel.basic_consume(queue=queue_name, on_message_callback=bcast_callback) listen_channel.basic_consume(
queue=queue_name,
on_message_callback=bcast_callback,
)
listen_channel.start_consuming() listen_channel.start_consuming()
......
...@@ -4,10 +4,12 @@ ...@@ -4,10 +4,12 @@
# (per client) # (per client)
# [client_id]-inbox - receive subscriptions # [client_id]-inbox - receive subscriptions
# [client_id]-outbox - send messages to the bus # [client_id]-outbox - send messages to the bus
# [client-id]-broadcast - send message to all subscribers? - eg notify of downtime # [client-id]-broadcast - send message to all subscribers?
# - eg notify of downtime
# soar-publisher - fan-in from client-outboxes # soar-publisher - fan-in from client-outboxes
# soar-dlq - undeliverables # soar-dlq - undeliverables
# soar-broadcast - admin messages forwarded to all client-inboxes regardless of subscriptions # soar-broadcast - admin messages forwarded to all client-inboxes
# regardless of subscriptions
import concurrent.futures import concurrent.futures
from api.clients import ClientsFile from api.clients import ClientsFile
...@@ -29,7 +31,11 @@ def main(): ...@@ -29,7 +31,11 @@ def main():
with concurrent.futures.ProcessPoolExecutor() as executor: with concurrent.futures.ProcessPoolExecutor() as executor:
# publish # publish
thread = executor.submit(publish, "soar-publish", EXCHANGES.get("publish")) thread = executor.submit(
publish,
"soar-publish",
EXCHANGES.get("publish"),
)
THREADS.append(thread) THREADS.append(thread)
for (id, client) in clients.items(): for (id, client) in clients.items():
...@@ -51,8 +57,8 @@ def main(): ...@@ -51,8 +57,8 @@ def main():
) )
THREADS.append(thread) THREADS.append(thread)
# push # push
# TODO - add optional webhook target to client and post to webhook target # TODO - add optional webhook target to client and
# if present # post to webhook target if present
if __name__ == "__main__": if __name__ == "__main__":
......
#!/usr/bin/env python #!/usr/bin/env python
import pika import pika
...@@ -12,13 +11,17 @@ def deliver(body, queue_name): ...@@ -12,13 +11,17 @@ def deliver(body, queue_name):
deliver_connection = get_connection() deliver_connection = get_connection()
deliver_channel = deliver_connection.channel() deliver_channel = deliver_connection.channel()
deliver_channel.queue_declare(queue=queue_name, durable=True) deliver_channel.queue_declare(queue=queue_name, durable=True)
deliver_channel.basic_publish(exchange="", routing_key=queue_name, body=body) deliver_channel.basic_publish(
exchange="",
routing_key=queue_name,
body=body,
)
deliver_connection.close() deliver_connection.close()
def listen(from_queue_name, to_queue_name): def listen(from_queue_name, to_queue_name):
def fwd_callback(ch, method, properties, body): def fwd_callback(ch, method, properties, body):
delivered = deliver(body, to_queue_name) # delivered = deliver(body, to_queue_name)
ch.basic_ack(delivery_tag=method.delivery_tag) ch.basic_ack(delivery_tag=method.delivery_tag)
listen_connection = get_connection() listen_connection = get_connection()
......
...@@ -3,6 +3,8 @@ import pika ...@@ -3,6 +3,8 @@ import pika
import json import json
import sys import sys
# from wrappers.rabbit_mq import publish as publish_wrapper
def get_connection(): def get_connection():
return pika.BlockingConnection(pika.ConnectionParameters(host="localhost")) return pika.BlockingConnection(pika.ConnectionParameters(host="localhost"))
...@@ -12,7 +14,10 @@ def deliver(body, topic, publish_exchange): ...@@ -12,7 +14,10 @@ def deliver(body, topic, publish_exchange):
print("publish on topic: %s" % topic) print("publish on topic: %s" % topic)
deliver_connection = get_connection() deliver_connection = get_connection()
deliver_channel = deliver_connection.channel() deliver_channel = deliver_connection.channel()
deliver_channel.exchange_declare(exchange=publish_exchange, exchange_type="topic") deliver_channel.exchange_declare(
exchange=publish_exchange,
exchange_type="topic",
)
deliver_channel.basic_publish( deliver_channel.basic_publish(
exchange=publish_exchange, routing_key=topic, body=body exchange=publish_exchange, routing_key=topic, body=body
) )
...@@ -29,7 +34,10 @@ def listen(queue_name, publish_exchange): ...@@ -29,7 +34,10 @@ def listen(queue_name, publish_exchange):
listen_connection = get_connection() listen_connection = get_connection()
listen_channel = listen_connection.channel() listen_channel = listen_connection.channel()
listen_channel.queue_declare(queue=queue_name, durable=True) listen_channel.queue_declare(queue=queue_name, durable=True)
listen_channel.basic_consume(queue=queue_name, on_message_callback=pub_callback) listen_channel.basic_consume(
queue=queue_name,
on_message_callback=pub_callback,
)
listen_channel.start_consuming() listen_channel.start_consuming()
......
#!/usr/bin/env python
from wrappers.rabbit_mq import subscribe as subscribe_wrapper
def subscribe(
queue_name,
topics_list,
publish_exchange="soar_publish",
broadcast_exchange="soar_broadcast",
):
subscribe_wrapper(
"localhost", # TODO: Update
queue_name,
publish_exchange,
broadcast_exchange,
topics_list,
)
# Shifted to wrappers/
# def old_subscribe(
# queue_name,
# topic,
# publish_exchange="soar_publish",
# broadcast_exchange="soar_broadcast",
# ):
# adm_connection = get_connection()
# admin_channel = adm_connection.channel()
# admin_channel.exchange_declare(
# exchange=broadcast_exchange,
# exchange_type="fanout"
# )
# admin_channel.queue_bind(exchange=broadcast_exchange, queue=queue_name)
# sub_connection = get_connection()
# subscriber_channel = sub_connection.channel()
# subscriber_channel.exchange_declare(
# exchange=publish_exchange, exchange_type="topic"
# )
# subscriber_channel.queue_bind(
# exchange=publish_exchange, queue=queue_name, routing_key=topic
# )
from cryptography.fernet import Fernet,InvalidToken from cryptography.fernet import Fernet, InvalidToken
import datetime import datetime
import os import os
import json import json
...@@ -7,84 +7,75 @@ import json ...@@ -7,84 +7,75 @@ import json
TOKENS = {} TOKENS = {}
class TokenModel(): class TokenModel:
clients = None clients = None
schema = None schema = None
key = None key = None
fernet = None fernet = None
token_lifetime_hours = None token_lifetime_hours = None
env_lifetime = 'SOAR_TOKEN_LIFETIME' env_lifetime = "SOAR_TOKEN_LIFETIME"
env_secret = 'SOAR_TOKEN_SECRET' env_secret = "SOAR_TOKEN_SECRET"
def __init__(self): def __init__(self):
self.getFernet() self.getFernet()
self.token_lifetime_hours = os.getenv(self.env_lifetime, 24) self.token_lifetime_hours = os.getenv(self.env_lifetime, 24)
def getFernet(self):
self.fernet = Fernet(self.getKey().encode())
def getKey(self): def getFernet(self):
self.fernet = Fernet(self.getKey().encode())
def getKey(self):
key = os.getenv(self.env_secret) key = os.getenv(self.env_secret)
print(key) print(key)
if not key: if not key:
key = Fernet.generate_key().decode() key = Fernet.generate_key().decode()
os.environ[self.env_secret] = key os.environ[self.env_secret] = key
self.key = key self.key = key
return self.key return self.key
def setSecret(self): def setSecret(self):
if not os.getenv(self.env_secret): if not os.getenv(self.env_secret):
os.environ[self.env_secret] = self.getKey() os.environ[self.env_secret] = self.getKey()
def getExpiry(self): def getExpiry(self):
now = datetime.datetime.utcnow() now = datetime.datetime.utcnow()
expires = now + datetime.timedelta(hours=self.token_lifetime_hours) expires = now + datetime.timedelta(hours=self.token_lifetime_hours)
return expires.isoformat() return expires.isoformat()
def encrypt(self, client_id): def encrypt(self, client_id):
try: try:
expiry = self.getExpiry() expiry = self.getExpiry()
token_content = { token_content = {"client_id": client_id, "expiry": expiry}
'client_id': client_id, token = self.fernet.encrypt(
'expiry': expiry json.dumps(token_content).encode(),
} ).decode()
token = self.fernet.encrypt(json.dumps(token_content).encode()).decode() return {"token": token, "expiry": expiry}
return { except KeyError: # as e:
'token': token,
'expiry': expiry
}
except KeyError as e:
return None return None
def decrypt(self, token): def decrypt(self, token):
try: try:
content = json.loads(self.fernet.decrypt(token.encode()).decode()) content = json.loads(self.fernet.decrypt(token.encode()).decode())
return content return content
except (InvalidToken,KeyError) as e: except (InvalidToken, KeyError): # as e:
return None return None
def get(self, client_id): def get(self, client_id):
response = self.encrypt(client_id) response = self.encrypt(client_id)
TOKENS[response['token']] = client_id TOKENS[response["token"]] = client_id
return response return response
def validate(self, token): def validate(self, token):
response = { response = {"valid": False}
'valid': False
}
if token in TOKENS: if token in TOKENS:
content = self.decrypt(token) content = self.decrypt(token)
if content: if content:
now = datetime.datetime.utcnow() now = datetime.datetime.utcnow()
expires = datetime.datetime.fromisoformat(content['expiry']) expires = datetime.datetime.fromisoformat(content["expiry"])
response['valid'] = expires > now response["valid"] = expires > now
if response['valid']: if response["valid"]:
response.update(content) response.update(content)
else: else:
del TOKENS[token] del TOKENS[token]
else: else:
del TOKENS[token] del TOKENS[token]
return response return response
...@@ -24,4 +24,3 @@ api.add_resource(Token, "/token") ...@@ -24,4 +24,3 @@ api.add_resource(Token, "/token")
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True, port=8087) app.run(debug=True, port=8087)
...@@ -6,5 +6,5 @@ __all__ = [ ...@@ -6,5 +6,5 @@ __all__ = [
if x.endswith(".py") and x != "__init__.py" if x.endswith(".py") and x != "__init__.py"
] ]
__workers__ = ["core_forward", "core_subscribe", "client_read"] __workers__ = []
# ["core_bus", "core_forward", "core_subscribe", "client_read"]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment