Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ authors = [{name="Yaal Coop", email="contact@yaal.coop"}]
maintainers = [{name="Éloi Rivard", email="eloi@yaal.coop"}]

dependencies = [
"authlib>=1.9.1",
"authlib>=1.10.0b1",
"babel>=2.14.0",
"cachelib>=0.10.0",
"flask>=3.1.2",
Expand Down
12 changes: 2 additions & 10 deletions src/auth_playground/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,9 @@ def setup_oauth(app):
client_secret = config.get("client_secret")
client_auth_methods = [ClientSecretJWT()]
if config["token_endpoint_auth_method"] == KEY_METHOD:
client_key = config["client_key"]
# Authlib 1.9.4 reads a key given as text as an RSA key only.
# TODO: with Authlib 1.10, pass client_key itself.
client_secret = load_key(client_key)
client_secret = load_key(config["client_key"])
client_auth_methods.append(
PrivateKeyJWT(
alg=config["token_endpoint_auth_signing_alg"],
# Authlib 1.9.4 does not put the kid of the key in the header.
# TODO: remove with Authlib 1.10, which does.
headers={"kid": client_key["kid"]},
)
PrivateKeyJWT(alg=config["token_endpoint_auth_signing_alg"])
)

registry.register(
Expand Down
44 changes: 41 additions & 3 deletions src/auth_playground/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,35 @@ def _get_metadata_document(url: str, timeout: int) -> dict:
return metadata


class IssuerMismatchError(Exception):
"""The issuer of the server metadata is not the issuer URL entered.

The issuer is ``None`` when the metadata has none.
"""

def __init__(self, issuer):
super().__init__(issuer)
self.issuer = issuer


def fetch_server_metadata(issuer_url: str, timeout: int = 10) -> tuple[dict, str]:
"""Fetch server metadata from well-known OIDC or OAuth2 endpoints."""
"""Fetch server metadata from well-known OIDC or OAuth2 endpoints.

Per OIDC Discovery §4.3 and RFC 8414 §3.3, the issuer of the metadata must
be the issuer URL the well-known URL is built from. ``issuer_url`` has no
trailing slash, and a trailing slash in the issuer is accepted.
"""
metadata, server_type = _fetch_metadata_document(issuer_url, timeout)
issuer = metadata.get("issuer")
if not isinstance(issuer, str):
raise IssuerMismatchError(None)
if issuer.rstrip("/") != issuer_url:
raise IssuerMismatchError(issuer)
return metadata, server_type


def _fetch_metadata_document(issuer_url: str, timeout: int) -> tuple[dict, str]:
"""Fetch the OIDC metadata document, or else the OAuth2 one."""
oidc_url = f"{issuer_url}/.well-known/openid-configuration"
try:
return _get_metadata_document(oidc_url, timeout), "oidc"
Expand All @@ -54,7 +81,17 @@ def fetch_server_metadata(issuer_url: str, timeout: int = 10) -> tuple[dict, str

def _flash_metadata_error(exception: Exception) -> None:
"""Flash user-friendly error messages for metadata loading errors."""
if isinstance(exception, requests.exceptions.ConnectionError):
if isinstance(exception, IssuerMismatchError) and exception.issuer is None:
flash(_("The server metadata has no issuer."), "error")
elif isinstance(exception, IssuerMismatchError):
flash(
_(
"The issuer of the server metadata is {issuer}, not the URL "
"you entered. A server must not use the issuer of another one."
).format(issuer=exception.issuer),
"error",
)
elif isinstance(exception, requests.exceptions.ConnectionError):
flash(_("Cannot connect to the server. Please check the URL."), "error")
elif isinstance(exception, requests.exceptions.Timeout):
flash(_("Connection timeout. The server is not responding."), "error")
Expand Down Expand Up @@ -108,6 +145,7 @@ def load_server_metadata(
if not issuer_url:
return None

issuer_url = issuer_url.rstrip("/")
try:
metadata, server_type = fetch_server_metadata(issuer_url)
except Exception as e:
Expand All @@ -117,7 +155,7 @@ def load_server_metadata(
if update_config and g.server_config:
g.server_config.metadata = metadata
g.server_config.server_type = server_type
g.server_config.issuer_url = issuer_url.rstrip("/")
g.server_config.issuer_url = issuer_url
# The client was set up before the metadata, which can change its
# signing algorithm.
auth_playground.setup_oauth(current_app)
Expand Down
226 changes: 220 additions & 6 deletions src/auth_playground/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from flask import current_app
from flask import g
from flask import render_template
from flask import session
from flask import url_for
from flask_babel import lazy_gettext as _
from flask_wtforms_htmx import HtmxFlaskForm
Expand All @@ -34,6 +35,7 @@
from wtforms.validators import Optional
from wtforms.widgets import CheckboxInput
from wtforms.widgets import ListWidget
from wtforms.widgets import RadioInput
from wtforms.widgets import html_params

import auth_playground
Expand All @@ -58,7 +60,19 @@
from auth_playground.client_key import preferred_algorithm
from auth_playground.client_key import publish_key
from auth_playground.grants import GRANTS
from auth_playground.grants import JWT_BEARER
from auth_playground.grants import advertised_grant_types
from auth_playground.jwt_bearer import DEFAULT_LIFETIME
from auth_playground.jwt_bearer import FORM_CLAIMS
from auth_playground.jwt_bearer import KEY_SOURCE
from auth_playground.jwt_bearer import PASTED_SOURCE
from auth_playground.jwt_bearer import SECRET_SOURCE
from auth_playground.jwt_bearer import assertion_alg
from auth_playground.jwt_bearer import available_sources
from auth_playground.jwt_bearer import build_assertion_preview
from auth_playground.jwt_bearer import claims_object
from auth_playground.jwt_bearer import jwt_bearer_preview
from auth_playground.jwt_bearer import pasted_assertion
from auth_playground.session import server_metadata
from auth_playground.token_request import TOKEN_RESERVED_PARAMS
from auth_playground.token_request import client_credentials_preview
Expand All @@ -69,6 +83,7 @@
"refresh_token",
"client_credentials",
"password",
JWT_BEARER,
)

DEFAULT_REGISTERED_GRANT_TYPES = ["authorization_code", "refresh_token"]
Expand Down Expand Up @@ -654,25 +669,28 @@ def get_pkce_default():
return ""


class RequestPreviewWidget:
"""Render the request that the form builds."""
class PreviewWidget:
"""Render what the form builds, with ``template``."""

def __init__(self, template):
self.template = template

def __call__(self, field, **kwargs):
kwargs.setdefault("id", field.id)
return Markup(
render_template(
"_request_preview.html",
self.template,
attributes=Markup(html_params(**kwargs)),
label=field.label.text,
request=field.build(field._form),
preview=field.build(field._form),
)
)


class RequestPreviewField(Field):
"""A read-only preview of the request that ``build(form)`` returns."""

widget = RequestPreviewWidget()
widget = PreviewWidget("_request_preview.html")

def __init__(self, label=None, build=None, **kwargs):
super().__init__(label, **kwargs)
Expand All @@ -694,6 +712,12 @@ def scopes_field(default=None):
)


class AssertionPreviewField(RequestPreviewField):
"""A read-only preview of the assertion that ``build(form)`` returns."""

widget = PreviewWidget("_assertion_preview.html")


class RequestParamsForm(BaseForm):
"""Parameters common to the requests that obtain tokens.

Expand Down Expand Up @@ -835,6 +859,196 @@ class PasswordForm(RequestParamsForm):
request_tokens = ButtonField(_("Request tokens"), render_kw={"class": "primary"})


class ChoiceRadioInput(RadioInput):
"""A radio button that applies the render_kw of its choice."""

# WTForms 3.3.2 applies the render_kw of the choices to the options of a
# select, but not to radio buttons.
def __call__(self, field, **kwargs):
return super().__call__(field, **{**field.choice.render_kw, **kwargs})


def assertion_source_choices():
"""Offer the sources of the assertion, and disable those the client lacks."""
oauth_config = auth_playground.get_oauth_config(current_app)
available = available_sources(oauth_config)
if KEY_SOURCE in available:
key_label = _("Sign with the client key: {alg}").format(
alg=assertion_alg(KEY_SOURCE, oauth_config)
)
else:
key_label = _("Sign with the client key: this client has none.")
if SECRET_SOURCE in available:
secret_label = _("Sign with the client secret: {alg}").format(
alg=assertion_alg(SECRET_SOURCE, oauth_config)
)
else:
secret_label = _("Sign with the client secret: this client has none.")
return [
SelectChoice(
KEY_SOURCE, key_label, render_kw={"disabled": KEY_SOURCE not in available}
),
SelectChoice(
SECRET_SOURCE,
secret_label,
render_kw={"disabled": SECRET_SOURCE not in available},
),
SelectChoice(
PASTED_SOURCE,
_("Paste an assertion issued elsewhere"),
),
]


def assertion_source_default():
"""Return the first source of the assertion the client can use."""
return available_sources(auth_playground.get_oauth_config(current_app))[0]


def signed_in_subject():
"""Return the sub of the signed-in user, or an empty string."""
return (session.get("user") or {}).get("sub", "")


def configured_client_id():
"""Return the ID of the configured client, or an empty string."""
return auth_playground.get_oauth_config(current_app).get("client_id", "")


def audience_choices():
"""Offer the issuer and the token endpoint as the audience."""
metadata = server_metadata()
return [
SelectChoice(
"issuer", _("{url} (issuer)").format(url=metadata.get("issuer", ""))
),
SelectChoice(
"token_endpoint",
_("{url} (token endpoint)").format(url=metadata.get("token_endpoint", "")),
),
]


class JwtBearerForm(RequestParamsForm):
"""Form to request tokens with a JWT that says who they are for (RFC 7523 §2.1)."""

endpoint = "oauth.jwt_bearer"
reserved_params = TOKEN_RESERVED_PARAMS | {"assertion"}

source = RadioField(
_("Assertion:"),
choices=assertion_source_choices,
default=assertion_source_default,
option_widget=ChoiceRadioInput(),
description=_(
"A client gets a key with private_key_jwt, and a secret with the "
"other confidential methods."
),
)
assertion = TextAreaField(
_("Assertion (JWT):"),
description=_(
"Sent as is. The playground only decodes it, it does not check "
"the signature."
),
render_kw={"rows": 5, "autocomplete": "off", "spellcheck": "false"},
).triggered_by("source", on="change")
subject = StringField(
_("Subject (sub):"),
default=signed_in_subject,
description=_(
"Who the tokens are for. Filled with the sub of the signed-in user."
),
render_kw={"autocomplete": "off"},
).triggered_by("source", on="change")
issuer = StringField(
_("Issuer (iss):"),
default=configured_client_id,
description=_("The client ID, unless another party issues the assertion."),
).triggered_by("source", on="change")
audience = SelectField(
_("Audience (aud):"),
choices=audience_choices,
default="issuer",
description=_("Per RFC 10070 §4, both are allowed for a grant."),
).triggered_by("source", on="change")
lifetime = IntegerField(
_("Lifetime (seconds):"),
default=DEFAULT_LIFETIME,
description=_("From now to exp."),
render_kw={"min": 1},
).triggered_by("source", on="change")
extra_claims = TextAreaField(
_("Extra claims:"),
description=_("A JSON object, added to the claims above."),
render_kw={"placeholder": '{"act": {"sub": "admin"}}'},
).triggered_by("source", on="change")
scopes = scopes_field(default=get_scopes_default)
preview = RequestPreviewField(
_("Token request"), build=jwt_bearer_preview, wrapper=False
).triggered_by("*")
assertion_preview = AssertionPreviewField(
_("Assertion"), build=build_assertion_preview, wrapper=False
).triggered_by("*")
request_tokens = ButtonField(_("Request tokens"), render_kw={"class": "primary"})

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
signing = self.signing()
show_field(self.assertion, not signing)
for field in (
self.subject,
self.issuer,
self.audience,
self.lifetime,
self.extra_claims,
):
show_field(field, signing)

def signing(self):
"""Tell whether the playground signs the assertion."""
return self.source.data != PASTED_SOURCE

def validate_source(self, field):
oauth_config = auth_playground.get_oauth_config(current_app)
if field.data not in available_sources(oauth_config):
raise ValidationError(_("This client cannot sign the assertion this way."))

def validate_assertion(self, field):
if not self.signing() and not pasted_assertion(self):
raise ValidationError(_("Paste the assertion."))

def validate_subject(self, field):
if self.signing() and not field.data:
raise ValidationError(_("Enter who the tokens are for."))

def validate_issuer(self, field):
if self.signing() and not field.data:
raise ValidationError(_("Enter who issues the assertion."))

def validate_lifetime(self, field):
if not self.signing():
# Like Optional, forget that the hidden value is not a number.
field.errors[:] = []
return
if field.data is not None and field.data < 1:
raise ValidationError(_("Enter a number of seconds, 1 or more."))

def validate_extra_claims(self, field):
if not self.signing() or not field.data:
return

claims = claims_object(field.data)
if claims is None:
raise ValidationError(_("The extra claims must be a JSON object."))
if form_claims := [name for name in FORM_CLAIMS if name in claims]:
raise ValidationError(
_("The form already sets these claims: {claims}.").format(
claims=", ".join(form_claims)
)
)


def grant_label(grant):
"""Mark the label of a deprecated grant type, or one the server does not advertise."""
marks = []
Expand All @@ -852,7 +1066,7 @@ def grant_label(grant):
def get_grant_choices():
"""Group the grant types, and label the ones the server does not advertise."""
return [
SelectChoice(grant.value, grant_label(grant), optgroup=grant.group)
SelectChoice(grant.name, grant_label(grant), optgroup=grant.group)
for grant in GRANTS
]

Expand Down
Loading
Loading