Skip to content

Commit

Permalink
Fix ruff and black errors
Browse files Browse the repository at this point in the history
  • Loading branch information
iburakov committed Sep 22, 2023
1 parent 1084c53 commit 5e92af8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
39 changes: 21 additions & 18 deletions src/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,46 @@

import requests

# inspired by https://www.stefaanlippens.net/oauth-code-flow-pkce.html
from utils import timed_cache

# inspired by https://www.stefaanlippens.net/oauth-code-flow-pkce.html
_CLIENT_ID = "student-personal-cabinet"
_REDIRECT_URI = "https://my.itmo.ru/login/callback"
_PROVIDER = "https://id.itmo.ru/auth/realms/itmo"


def generate_code_verifier():
code_verifier = urlsafe_b64encode(os.urandom(40)).decode('utf-8')
return re.sub('[^a-zA-Z0-9]+', '', code_verifier)
code_verifier = urlsafe_b64encode(os.urandom(40)).decode("utf-8")
return re.sub("[^a-zA-Z0-9]+", "", code_verifier)


def get_code_challenge(code_verifier: str):
code_challenge = sha256(code_verifier.encode('utf-8')).digest()
code_challenge = urlsafe_b64encode(code_challenge).decode('utf-8')
return code_challenge.replace('=', '') # remove base64 padding
code_challenge = sha256(code_verifier.encode("utf-8")).digest()
code_challenge = urlsafe_b64encode(code_challenge).decode("utf-8")
return code_challenge.replace("=", "") # remove base64 padding


@timed_cache(minutes=55)
def get_access_token(username: str, password: str):
code_verifier = generate_code_verifier()
code_challenge = get_code_challenge(code_verifier)

auth_resp = requests.get(_PROVIDER + "/protocol/openid-connect/auth", params=dict(
protocol="oauth2",
response_type="code",
client_id=_CLIENT_ID,
redirect_uri=_REDIRECT_URI,
scope="openid",
state="im_not_a_browser",
code_challenge_method="S256",
code_challenge=code_challenge,
))
auth_resp = requests.get(
_PROVIDER + "/protocol/openid-connect/auth",
params=dict(
protocol="oauth2",
response_type="code",
client_id=_CLIENT_ID,
redirect_uri=_REDIRECT_URI,
scope="openid",
state="im_not_a_browser",
code_challenge_method="S256",
code_challenge=code_challenge,
),
)
auth_resp.raise_for_status()

form_action = html.unescape(re.search('<form\s+.*?\s+action="(.*?)"', auth_resp.text, re.DOTALL).group(1))
form_action = html.unescape(re.search(r'<form\s+.*?\s+action="(.*?)"', auth_resp.text, re.DOTALL).group(1))

form_resp = requests.post(
url=form_action,
Expand All @@ -57,7 +60,7 @@ def get_access_token(username: str, password: str):
url_redirected_to = form_resp.headers["Location"]
query = urllib.parse.urlparse(url_redirected_to).query
redirect_params = urllib.parse.parse_qs(query)
auth_code = redirect_params['code'][0]
auth_code = redirect_params["code"][0]

token_resp = requests.post(
url=_PROVIDER + "/protocol/openid-connect/token",
Expand Down
9 changes: 5 additions & 4 deletions src/calendar_processing.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from datetime import datetime, timedelta
from typing import List

import requests
from dateutil.parser import isoparse
from flask import current_app
from ics import Event, Calendar
from ics import Calendar, Event

_event_type_to_tag_map = {
"Лекции": "Лек",
Expand All @@ -24,7 +25,7 @@
_CALENDAR_CREATOR_VALUE = "my-itmo-ru-to-ical"


def get_raw_events(auth_token: str) -> List[dict]:
def get_raw_events(auth_token: str) -> list[dict]:
resp = requests.get(
"https://my.itmo.ru/api/schedule/schedule/personal",
params=dict(
Expand Down Expand Up @@ -72,7 +73,7 @@ def _raw_event_to_location(re: dict):
return result if result else None


def raw_events_to_calendar(raw_events: List[dict]):
def raw_events_to_calendar(raw_events: list[dict]):
calendar = Calendar()
calendar.creator = _CALENDAR_CREATOR_VALUE
for raw_event in raw_events:
Expand Down

0 comments on commit 5e92af8

Please sign in to comment.