Skip to content

Commit af878ef

Browse files
Fix some stuff
1 parent 8c6273f commit af878ef

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

cogs/get_token_authorisation.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import aiohttp
88
import bs4
99
import discord
10-
from bs4 import BeautifulSoup
1110
from discord.ext import tasks
1211

1312
from config import settings
@@ -52,7 +51,7 @@ class TokenStatus(Enum):
5251
Enum class that defines the status of the token.
5352
5453
INVALID: The token does not have access to a user, meaning it is invalid or expired.
55-
VALID: The token is a valid user, but not neccessarily to an organisation.
54+
VALID: The token is a valid user, but not neccessarily admin to an organisation.
5655
AUTHORISED: The token is a valid user and has access to an organisation.
5756
"""
5857

@@ -71,24 +70,16 @@ async def fetch_with_session(self, url: str) -> str:
7170
):
7271
return await http_response.text()
7372

74-
async def get_profile_page(self) -> bs4.BeautifulSoup:
75-
"""
76-
Definition of method to get the profile page.
77-
78-
This is done by requesting the user profile page and
79-
scraping the HTML for the list of groups.
80-
"""
81-
response_html: str = await self.fetch_with_session(PROFILE_URL)
82-
return BeautifulSoup(response_html, "html.parser")
83-
8473
async def get_token_status(self) -> TokenStatus:
8574
"""
8675
Definition of method to get the status of the token.
8776
8877
This is done by checking if the token is valid and if it is,
8978
checking if the token has access to the organisation.
9079
"""
91-
response_object: bs4.BeautifulSoup = await self.get_profile_page()
80+
response_object: bs4.BeautifulSoup = bs4.BeautifulSoup(
81+
await self.fetch_with_session(PROFILE_URL), "html.parser"
82+
)
9283
page_title: bs4.Tag | bs4.NavigableString | None = response_object.find("title")
9384
if not page_title or "Login" in str(page_title):
9485
logger.debug("Token is invalid or expired.")
@@ -97,11 +88,17 @@ async def get_token_status(self) -> TokenStatus:
9788
organisation_admin_url: str = f"{ORGANISATION_URL}/{settings['ORGANISATION_ID']}"
9889
response_html: str = await self.fetch_with_session(organisation_admin_url)
9990

100-
return (
101-
self.TokenStatus.AUTHORISED
102-
if "Admin Tools" in response_html
103-
else self.TokenStatus.VALID
104-
)
91+
if "admin tools" in response_html.lower():
92+
logger.debug(response_html)
93+
return self.TokenStatus.AUTHORISED
94+
95+
if "You do not have any permissions for this organisation" in response_html.lower():
96+
logger.debug(response_html)
97+
return self.TokenStatus.VALID
98+
99+
logger.warning("Unexpected response when checking token authorisation.")
100+
logger.debug(response_html)
101+
return self.TokenStatus.INVALID
105102

106103
async def get_token_groups(self, iterable: bool) -> "str | Iterable[str]": # noqa: FBT001
107104
"""
@@ -110,7 +107,9 @@ async def get_token_groups(self, iterable: bool) -> "str | Iterable[str]": # no
110107
This is done by requesting the user profile page and
111108
scraping the HTML for the list of groups.
112109
"""
113-
response_object: bs4.BeautifulSoup = await self.get_profile_page()
110+
response_object: bs4.BeautifulSoup = bs4.BeautifulSoup(
111+
await self.fetch_with_session(PROFILE_URL), "html.parser"
112+
)
114113

115114
page_title: bs4.Tag | bs4.NavigableString | None = response_object.find("title")
116115

0 commit comments

Comments
 (0)