45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
from board_mate.auth import *
|
|
from flask import jsonify, request
|
|
from pydantic import StrictStr
|
|
|
|
from src.models.AuthData import AuthData
|
|
|
|
|
|
class AuthController:
|
|
|
|
_auth_data : AuthData = None
|
|
|
|
def __init__(self, app, auth_data, host):
|
|
self._register_routes(app)
|
|
self.config = Configuration(host=host)
|
|
self.auth_data = auth_data
|
|
self.config.verify_ssl = False
|
|
|
|
def _register_routes(self, app):
|
|
app.add_url_rule("/client/auth", view_func=self.auth, methods=['POST'])
|
|
|
|
def auth(self):
|
|
try:
|
|
received_data = request.get_json()
|
|
print(received_data)
|
|
with ApiClient(self.config) as client:
|
|
auth_api = AuthApi(client)
|
|
|
|
auth_request = AuthRequestDto(
|
|
username=StrictStr(received_data["username"]),
|
|
key=StrictStr(received_data["key"]),
|
|
)
|
|
|
|
response = auth_api.login(auth_request_dto=auth_request)
|
|
print("Data:", response.data)
|
|
print(response.data["authToken"])
|
|
print(response.data["clientId"])
|
|
|
|
self._auth_data.set_client_id(response.data["authToken"])
|
|
self._auth_data.set_token(response.data["clientId"])
|
|
return jsonify({"success" : response.success, "message" : response.message , "data" : response.data}), 200
|
|
except Exception as e:
|
|
print(f"Exception when calling AuthAPI->login: {e}")
|
|
return jsonify({"success" : False, "message" : e , "data" : None}), 500
|
|
|