Skip to content

Commit

Permalink
modeus init
Browse files Browse the repository at this point in the history
  • Loading branch information
depocoder committed Sep 23, 2024
1 parent 84efe6c commit 92c96bd
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 30 deletions.
48 changes: 24 additions & 24 deletions backend/app/controllers/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
from blacksheep.server.controllers import Controller, get


class Home(Controller):
"""Home Controller."""

@get()
def index(self) -> Response:
"""Index page."""
# Since the @get() decorator is used without arguments, the URL path
# is by default "/"

# Since the view function is called without parameters, the name is
# obtained from the calling request handler: 'index',
# -> /views/home/index.jinja
return self.view()

@get(None)
def example(self) -> Response:
"""Example view."""
# Since the @get() decorator is used explicitly with None, the URL path
# is obtained from the method name: "/example"

# Since the view function is called without parameters, the name is
# obtained from the calling request handler: 'example',
# -> /views/home/example.jinja
return self.view()
# class Home(Controller):
# """Home Controller."""
#
# @get()
# def index(self) -> Response:
# """Index page."""
# # Since the @get() decorator is used without arguments, the URL path
# # is by default "/"
#
# # Since the view function is called without parameters, the name is
# # obtained from the calling request handler: 'index',
# # -> /views/home/index.jinja
# return self.view()
#
# @get(None)
# def example(self) -> Response:
# """Example view."""
# # Since the @get() decorator is used explicitly with None, the URL path
# # is obtained from the method name: "/example"
#
# # Since the view function is called without parameters, the name is
# # obtained from the calling request handler: 'example',
# # -> /views/home/example.jinja
# return self.view()
47 changes: 47 additions & 0 deletions backend/app/controllers/modeus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Modeus API implemented using a controller.
"""

from typing import Optional

from blacksheep import Response
from blacksheep.server.bindings import FromJson
from blacksheep.server.controllers import Controller, post
from pydantic import BaseModel
from requests import RequestException

from integration import modeus
from integration.exceptions import ModeusError


class ModeusCreds(BaseModel):
"""Modeus creds."""

username: str
password: str


class NetologyController(Controller):
"""Controller for Modeus API."""

@classmethod
def route(cls) -> Optional[str]:
"""Get route."""
return "/api/modeus"

@classmethod
def class_name(cls) -> str:
"""Get class name."""
return "Modeus"

@post()
async def get_modeus_cookies(self, item: FromJson[ModeusCreds]) -> Response:
"""
Auth in Modeus and return cookies.
"""
try:
return self.json(
await modeus.login(item.value.username, item.value.password),
)
except (RequestException, ModeusError) as exception:
return self.json({"error": f"can't authenticate {exception}"}, status=400)
8 changes: 4 additions & 4 deletions backend/integration/modeus.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def get_post_url(session: AsyncClient, token_length: int = 16) -> URL:
"state": token_hex(token_length),
}
response = await session.get(auth_url, params=auth_data)
response.raise_for_status()
# response.raise_for_status()
post_url = response.url
if post_url is None:
raise CannotAuthenticateError
Expand All @@ -56,7 +56,7 @@ async def get_auth_form(session: AsyncClient, username: str, password: str) -> T
"AuthMethod": "FormsAuthentication",
}
response = await session.post(post_url, data=login_data)
response.raise_for_status()
# response.raise_for_status()
html_text = response.text

html = BeautifulSoup(html_text, "lxml")
Expand All @@ -70,7 +70,7 @@ async def get_auth_form(session: AsyncClient, username: str, password: str) -> T
return form


async def login(username: str, password: str, timeout: int = 15) -> Dict[str, Any]:
async def login(username: str, __password: str, timeout: int = 15) -> Dict[str, Any]:
"""
Log in Modeus.
Expand All @@ -83,7 +83,7 @@ async def login(username: str, password: str, timeout: int = 15) -> Dict[str, An
timeout=timeout,
)

form = await get_auth_form(session, username, password)
form = await get_auth_form(session, username, __password)
auth_data = {}
continue_auth_url = "https://auth.modeus.org/commonauth"
for input_html in form.find_all("input", type="hidden"):
Expand Down
Loading

0 comments on commit 92c96bd

Please sign in to comment.