Skip to content

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.

Attributes

logger module-attribute

logger = logging.getLogger(__name__)

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

Retained for backward compatibility with earlier releases that printed directly to stdout when this flag was set. All diagnostics now go through the stdlib logging module (see the module-level logger), so visibility is controlled by the caller's logging configuration instead of this flag: the py-moodle CLI's --verbose/--debug options configure it automatically (see :func:py_moodle.cli.output.configure_logging); direct (non-CLI) callers should call logging.basicConfig(level=logging.DEBUG) (or configure a handler on the "py_moodle" logger) to see this 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
compatibility
moodle_version

Attributes

base_url instance-attribute
base_url = base_url.rstrip('/')
username instance-attribute
username = username
password instance-attribute
password = password
session instance-attribute
session = requests.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
compatibility instance-attribute
compatibility = DEFAULT_COMPATIBILITY
moodle_version instance-attribute
moodle_version = None

Methods:

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

Retained for backward compatibility; see :class:MoodleAuth for how diagnostic visibility is now controlled via the stdlib logging module instead. The sesskey value is never logged, and response bodies are not dumped verbatim.

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

See :class:MoodleAuth; a convenience for direct (non-CLI) library use.

False

Returns:

Type Description
Session

An authenticated requests.Session instance.