Skip to content

url_login

AIcrowd login via url

url_login(poll_interval=5)

Fetches the login URL and OTP Polls till user authenticates

Source code in aicrowd/auth/url_login.py
def url_login(poll_interval=5) -> str:
    """
    Fetches the login URL and OTP
    Polls till user authenticates
    """
    log = logging.getLogger()

    login_details = initiate_api_url_auth()

    if login_details is None:
        raise LoginException(
            "Couldn't contact AIcrowd servers", "Please contact AIcrowd administrators"
        )

    try:
        url, otp = login_details["login_url"], login_details["otp"]
    except KeyError as e:
        log.error("Couldn't extract expected keys login_url and otp from api response")
        raise LoginException(
            "Invalid response from AIcrowd servers",
            "Please contact AIcrowd administrators",
        ) from e

    click.echo(
        "Please login here: " + click.style(url, fg="blue", underline=True, bold=True)
    )
    click.launch(url)

    random_key = url.split("/")[-1]

    try:
        user_details = get_user_details(
            random_key,
            otp,
            max_retries=LoginConstants.URL_LOGIN_REQUEST_LIFETIME // poll_interval,
            poll_interval=poll_interval,
        )

        if user_details is None:
            raise LoginException(
                "Couldn't login. Max retries exceeded", "Please try logging in again"
            )

        return user_details["api_key"]
    except (JSONDecodeError, KeyError) as e:
        log.error("Couldn't extract API Key from response")
        raise LoginException(
            "Invalid response from AIcrowd servers",
            "Please contact AIcrowd administrators",
        ) from e