Skip to content

Auth

py_moodle.auth

Authentication module for Moodle.

Handles session-based login (including support for CAS) and retrieves the session key required for further AJAX requests.

Classes:

Name Description
LoginError

Exception raised when authentication fails.

MoodleAuth

Authenticate a user against a Moodle site.

Functions:

Name Description
enable_webservice

Enables a webservice if it exists but is disabled (USE WITH CAUTION).

login

Authenticate a user and return an active session.

Classes

LoginError

Bases: Exception

Exception raised when authentication fails.

MoodleAuth

MoodleAuth(
    base_url: str,
    username: str,
    password: str,
    use_cas: bool = False,
    cas_url: Optional[str] = None,
    pre_configured_token: Optional[str] = None,
    debug: bool = False,
)

Authenticate a user against a Moodle site.

Initialize the authenticator.

Parameters:

Name Type Description Default
base_url str

Base URL of the Moodle instance.

required
username str

Username to authenticate with.

required
password str

Password for the user.

required
use_cas bool

Whether to use CAS authentication.

False
cas_url Optional[str]

URL of the CAS server (if use_cas is True).

None
pre_configured_token Optional[str]

Pre-created webservice token, if available.

None
debug bool

Enable verbose debugging output.

False

Methods:

Name Description
login

Authenticate the user and return a Moodle session.

Attributes:

Name Type Description
base_url
username
password
session
sesskey
use_cas
cas_url
pre_configured_token
debug
webservice_token

Attributes

base_url instance-attribute
base_url = rstrip('/')
username instance-attribute
username = username
password instance-attribute
password = password
session instance-attribute
session = Session()
sesskey instance-attribute
sesskey = None
use_cas instance-attribute
use_cas = use_cas
cas_url instance-attribute
cas_url = cas_url
pre_configured_token instance-attribute
pre_configured_token = pre_configured_token
debug instance-attribute
debug = debug
webservice_token instance-attribute
webservice_token = None

Functions

login
login() -> Session

Authenticate the user and return a Moodle session.

Returns:

Type Description
Session

requests.Session: Authenticated session with cookies.

Raises:

Type Description
LoginError

If authentication fails.

Functions

enable_webservice

enable_webservice(
    session: Session,
    base_url: str,
    sesskey: str,
    service_id: int = 1,
    debug: bool = True,
) -> bool

Enables a webservice if it exists but is disabled (USE WITH CAUTION).

Parameters:

Name Type Description Default
session Session

An authenticated requests.Session object.

required
base_url str

The base URL of the Moodle instance.

required
sesskey str

The session key for form submissions.

required
service_id int

The ID of the webservice to enable (default is 1 for 'Moodle mobile web service').

1
debug bool

If True, print debug information.

True

Returns:

Type Description
bool

True if the operation seems successful.

Raises:

Type Description
LoginError

If the operation fails.

login

login(
    url: str,
    username: str,
    password: str,
    use_cas: bool = False,
    cas_url: Optional[str] = None,
    pre_configured_token: Optional[str] = None,
    debug: bool = False,
) -> Session

Authenticate a user and return an active session.

Parameters:

Name Type Description Default
url str

Base URL of the Moodle instance.

required
username str

Username to authenticate.

required
password str

Password for the user.

required
use_cas bool

Whether to use CAS authentication.

False
cas_url Optional[str]

URL of the CAS server.

None
pre_configured_token Optional[str]

Optional pre-created webservice token.

None
debug bool

Enable verbose debugging output.

False

Returns:

Type Description
Session

An authenticated requests.Session instance.