Skip to content

common.services.cookie

Find cookie in database. if not found or invalid, logins to get a new cookie str and stores it in the database.

Parameters:

Name Type Description Default
user LMSUser

LMS User instance

required

Returns:

Type Description
LMSCookie

An LMSCookie

Source code in src/common/services/cookie.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
async def get_cookie(user: LMSUser) -> LMSCookie:
    """Find cookie in database. if not found or invalid,
    logins to get a new cookie str and stores it in the database.

    Args:
        user (LMSUser): LMS User instance

    Returns:
        (LMSCookie): An LMSCookie
    """

    cookie_value = await LMSCookie.objects.filter(user=user).afirst()
    cookie_value = cookie_value.cookie if cookie_value else ""
    cookie = LMSCookie(user=user, cookie=cookie_value)
    is_valid = await is_cookie_valid(cookie=cookie)
    if is_valid:
        return cookie
    cookie = await login(user=user)
    await LMSCookie.objects.aupdate_or_create(
        user=user,
        defaults={"cookie": cookie.cookie},
    )
    return cookie

Send request to /members/home to see if cookie has expired. If response is 302, request is being redirected to the login page. So, the cookie was expired (or invalid). If not, it's reached /members/home.

Parameters:

Name Type Description Default
cookie LMSCookie

An LMSCookie

required

Returns:

Type Description
bool

True if we're not redirected to another page (login),

bool

False if redirected.

Source code in src/common/services/cookie.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
async def is_cookie_valid(cookie: LMSCookie) -> bool:
    """Send request to /members/home to see if cookie has expired.
    If response is 302, request is being redirected to the login page.
    So, the cookie was expired (or invalid). If not, it's reached /members/home.

    Args:
        cookie (LMSCookie): An LMSCookie

    Returns:
        (bool): True if we're not redirected to another page (login),
        False if redirected.
    """

    BASE_URL = constants.BASE_URL
    HOME_SUFFIX_URL = constants.HOME_SUFFIX_URL

    async with aiohttp.ClientSession(
        base_url=BASE_URL, cookies=cookie.as_dict
    ) as session:
        async with session.get(
            url=HOME_SUFFIX_URL, allow_redirects=False
        ) as response:
            if response.status == 302:
                return False
            return True

login(user) async

Login to LMS. BASE_URL and LOGIN_SUFFIX_URL are taken from constants.py. If status is 302, request is being redirected to the home page, so the login is successful. If not, session is still at the login page, so it was not successful.

Parameters:

Name Type Description Default
user LMSUser

LMSUser instance

required

Returns:

Type Description
LMSCookie

An LMSCookie

Source code in src/common/services/cookie.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
async def login(user: LMSUser) -> LMSCookie:
    """Login to LMS. `BASE_URL` and `LOGIN_SUFFIX_URL` are taken from
    constants.py. If status is 302, request is being redirected
    to the home page, so the login is successful. If not, session
    is still at the login page, so it was not successful.

    Args:
        user (LMSUser): LMSUser instance

    Returns:
        (LMSCookie): An LMSCookie
    """

    BASE_URL = constants.BASE_URL
    LOGIN_SUFFIX_URL = constants.LOGIN_SUFFIX_URL
    json_body = {"username": user.username, "password": user.decoded_password}

    async with aiohttp.ClientSession(base_url=BASE_URL) as session:
        async with session.post(
            url=LOGIN_SUFFIX_URL, data=json_body, allow_redirects=False
        ) as response:
            if response.status == 302 and response.cookies.get(
                constants.COOKIE_KEY_NAME, False
            ):
                return LMSCookie(
                    cookie=response.cookies[constants.COOKIE_KEY_NAME].value
                )
            return LMSCookie(cookie="")