Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ version = "6.1.1"
[project.optional-dependencies]
all = [
"social-auth-core[azuread]",
"social-auth-core[cas]",
"social-auth-core[google-onetap]",
"social-auth-core[saml]",
"social-auth-core[shopify]"
]
allpy3 = [
"social-auth-core[all]"
]
cas = [
"python-cas>=1.7.2"
]
Comment on lines +80 to +82

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Record the new CAS backend in Unreleased

This adds a user-visible backend and installation extra, but CHANGELOG.md has no corresponding entry under Unreleased, so the next release would ship support without the repository's required release record. Add a concise Added entry before merging.

AGENTS.md reference: AGENTS.md:L80-L82

Useful? React with 👍 / 👎.

# This is present until pip implements supports for PEP 735
# see https://github.com/pypa/pip/issues/12963
dev = [
Expand Down Expand Up @@ -133,6 +137,7 @@ check_untyped_defs = true
disallow_untyped_defs = true
ignore_missing_imports = true
module = [
"cas",
"google.appengine.*",
"onelogin.*",
"openid.*",
Expand Down
82 changes: 82 additions & 0 deletions social_core/backends/cas_generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""
Generic CAS backend

Backend to authenticat with a generic CAS server.
"""

from __future__ import annotations

from cas import CASClient, CASClientV1, CASClientV2, CASClientV3, CASClientWithSAMLV1

from social_core.exceptions import AuthConfigurationError, AuthResponseError

from .base import BaseAuth


class CasAuth(BaseAuth):
name = "cas"
title = "Cas"

ID_KEY = "cas_user"
SERVER_URL: str | None = None

def auth_url(self) -> str:
client = self.get_cas_client()

url = client.get_login_url()
self.log_debug(f"Redirecting to CAS login: {url}")
return url

def get_cas_client(
self,
) -> CASClientV1 | CASClientV2 | CASClientV3 | CASClientWithSAMLV1:
"""
initializes the CASClient according to
the CAS_* settings
"""
server_url = self.setting("SERVER_URL", self.SERVER_URL)
service_url = self.redirect_uri

if not server_url:
raise AuthConfigurationError(
self,
"Server url is missing",
code="invalid_setting",
parameter="SERVER_URL",
)

version = self.setting("VERSION", 3)
kwargs = {
"service_url": service_url,
"server_url": server_url,
"extra_login_params": self.setting("EXTRA_LOGIN_PARAMS", []),
}

# ty checks don't like __new__ to return a different type
return CASClient.__new__(CASClient, version=version, **kwargs)

def auth_complete(self, *args, **kwargs):
client = self.get_cas_client()
ticket = self.strategy.request_data().get("ticket")
user, attributes, _pgtiou = client.verify_ticket(ticket)
Comment on lines +60 to +61

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Correlate CAS tickets with the initiating session

auth_complete accepts any ticket that validates for the shared static redirect_uri, without checking that this browser started the flow. An attacker can stop the redirect for a ticket issued to their own CAS account and send that callback URL to a victim; an anonymous victim is logged into the attacker's account, and an authenticated victim in an integration that passes the current user to do_complete can associate a fresh attacker CAS identity with the victim's local account. Put a session nonce in the CAS service URL and validate it before accepting the ticket.

AGENTS.md reference: AGENTS.md:L96-L98

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Route CAS ticket validation through the shared HTTP client

python-cas performs verify_ticket with its own requests call, so every callback bypasses BaseAuth.request: REQUESTS_TIMEOUT, PROXIES, VERIFY_SSL, the configured user agent, and structured HTTP error mapping are ignored. This makes proxy-only deployments fail and lets an unresponsive CAS validation endpoint tie up workers past the configured timeout; perform the validation via the backend's request helper while retaining the CAS response parsing.

AGENTS.md reference: AGENTS.md:L49-L51

Useful? React with 👍 / 👎.

if user is None or attributes is None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Accept successful CAS 1 responses without attributes

When SOCIAL_AUTH_CAS_VERSION is 1, python-cas's CASClientV1 returns (user, None, None) on successful validation because CAS 1 has no attributes, so this condition rejects every valid V1 ticket even though get_cas_client advertises and constructs that client. Treat a non-null user as success and normalize missing attributes to an empty mapping before merging.

Useful? React with 👍 / 👎.

raise AuthResponseError(
self,
"Token verification did not succeed",
code="authorization_code_rejected",
stage="token_validation",
)
kwargs.update(
{
"response": attributes | {"cas_user": user},
"backend": self,
}
)
return self.strategy.authenticate(*args, **kwargs)

def get_user_details(self, response):
return {
"username": response.get(self.id_key()),
"email": response.get(self.setting("EMAIL_FIELD", "email")),
"fullname": response.get(self.setting("NAME_FIELD", "name")),
}
73 changes: 73 additions & 0 deletions social_core/tests/backends/test_cas_generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from __future__ import annotations

import urllib.parse

import responses

from social_core.tests.strategy import TEST_URI

from .base import BaseBackendTest

VALIDATION_PAYLOAD = b"""
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationSuccess>
<cas:user>test</cas:user>
<cas:attributes>
<cas:authenticationDate>1970-01-01T00:00:00+00:00</cas:authenticationDate>
<cas:longTermAuthenticationRequestTokenUsed>false</cas:longTermAuthenticationRequestTokenUsed>
<cas:isFromNewLogin>true</cas:isFromNewLogin>
<cas:email>test@example.com</cas:email>
<cas:name>Firstname Lastname</cas:name>
</cas:attributes>
<cas:attribute name="authenticationDate" value="1970-00-00T00:00:00+00:00"/>
<cas:attribute name="longTermAuthenticationRequestTokenUsed" value="false"/>
<cas:attribute name="isFromNewLogin" value="true"/>
<cas:attribute name="email" value="test@example.com"/>
<cas:attribute name="name" value="Firstname Lastname"/>
</cas:authenticationSuccess>
</cas:serviceResponse>
"""


class CasAuthTest(BaseBackendTest):
backend_path = "social_core.backends.cas_generic.CasAuth"
expected_username = "test"
ticket = "ST-12345678"
server_url = "http://cas-server.com/"
service_url = TEST_URI
validate_endpoint = "p3/serviceValidate"

def setUp(self) -> None:
super().setUp()
params = [("ticket", self.ticket), ("service", self.service_url)]

self.strategy.set_settings(
{
"SOCIAL_AUTH_CAS_SERVER_URL": self.server_url,
}
)

url = (
urllib.parse.urljoin(self.server_url, self.validate_endpoint)
+ "?"
+ urllib.parse.urlencode(params)
)
responses.add(responses.GET, url, VALIDATION_PAYLOAD)

def do_start(self):
self.strategy.set_request_data({"ticket": self.ticket}, self.backend)
return self.backend.complete()

def test_login(self) -> None:
self.do_login()

def test_partial_pipeline(self) -> None:
self.do_partial_pipeline()

def test_auth_url(self) -> None:
self.assertEqual(
self.backend.auth_url(),
urllib.parse.urljoin(self.server_url, "login")
+ "?"
+ urllib.parse.urlencode({"service": self.service_url}),
)
Loading