Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
Closed
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
1 change: 1 addition & 0 deletions changelog.d/9493.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update JWT login type to support JSON Web Key Sets (JWKS), custom sub claim, and option to encode unsupported characters in user ID.
5 changes: 3 additions & 2 deletions docs/jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ will be removed in a future version of Synapse.

The `token` field should include the JSON web token with the following claims:

* The `sub` (subject) claim is required and should encode the local part of the
user ID.
* A claim that encodes the local part of the user ID is required. By default,
the `sub` (subject) claim is used, or a custom claim can be set in the
configuration file.
* The expiration time (`exp`), not before time (`nbf`), and issued at (`iat`)
claims are optional, but validated if present.
* The issuer (`iss`) claim is optional, but required and validated if configured.
Expand Down
22 changes: 21 additions & 1 deletion docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2147,10 +2147,30 @@ sso:
# This is either the private shared secret or the public key used to
# decode the contents of the JSON web token.
#
# Required if 'enabled' is true.
# Either 'secret' or 'jwks_uri' is required if 'enabled' is true.
#
#secret: "provided-by-your-issuer"

# URI where to fetch the JWKS containing the public keys that
# should be used to verify the signature of the JSON web token.
# Only used if 'secret' is not provided.
#
# Either 'secret' or 'jwks_uri' is required if 'enabled' is true.
#
#jwks_uri: "provided-by-your-issuer"

# Name of the claim containing a unique identifier for the user.
#
# Optional, defaults to `sub`.
#
#subject_claim: "sub"

# Perform normalisation of the user ID and encode unsupported characters.
#
# Optional, defaults to false.
#
#normalize_user_id: true

# The algorithm used to sign the JSON web token.
#
# Supported algorithms are listed at
Expand Down
36 changes: 33 additions & 3 deletions synapse/config/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from ._base import Config, ConfigError

MISSING_SECRET = "Either 'secret' or 'jwks_uri' is required in jwt_config."
MISSING_JWT = """Missing jwt library. This is required for jwt login.

Install by running:
Expand All @@ -28,14 +29,20 @@ def read_config(self, config, **kwargs):
jwt_config = config.get("jwt_config", None)
if jwt_config:
self.jwt_enabled = jwt_config.get("enabled", False)
self.jwt_secret = jwt_config["secret"]
self.jwt_algorithm = jwt_config["algorithm"]

# The issuer and audiences are optional, if provided, it is asserted
# that the claims exist on the JWT.
self.jwt_secret = jwt_config.get("secret")
self.jwt_jwks_uri = jwt_config.get("jwks_uri")
self.jwt_subject_claim = jwt_config.get("subject_claim", "sub")
self.jwt_normalize_user_id = jwt_config.get("normalize_user_id", False)
self.jwt_issuer = jwt_config.get("issuer")
self.jwt_audiences = jwt_config.get("audiences")

if not self.jwt_secret and not self.jwt_jwks_uri:
raise ConfigError(MISSING_SECRET)

try:
import jwt

Expand All @@ -45,9 +52,12 @@ def read_config(self, config, **kwargs):
else:
self.jwt_enabled = False
self.jwt_secret = None
self.jwt_algorithm = None
self.jwt_issuer = None
self.jwt_audiences = None
self.jwt_jwks_uri = None
self.jwt_subject_claim = None
self.jwt_normalize_user_id = False
self.jwt_algorithm = None

def generate_config_section(self, **kwargs):
return """\
Expand Down Expand Up @@ -75,10 +85,30 @@ def generate_config_section(self, **kwargs):
# This is either the private shared secret or the public key used to
# decode the contents of the JSON web token.
#
# Required if 'enabled' is true.
# Either 'secret' or 'jwks_uri' is required if 'enabled' is true.
#
#secret: "provided-by-your-issuer"

# URI where to fetch the JWKS containing the public keys that
# should be used to verify the signature of the JSON web token.
# Only used if 'secret' is not provided.
#
# Either 'secret' or 'jwks_uri' is required if 'enabled' is true.
#
#jwks_uri: "provided-by-your-issuer"

# Name of the claim containing a unique identifier for the user.
#
# Optional, defaults to `sub`.
#
#subject_claim: "sub"

# Perform normalisation of the user ID and encode unsupported characters.
#
# Optional, defaults to false.
#
#normalize_user_id: true

# The algorithm used to sign the JSON web token.
#
# Supported algorithms are listed at
Expand Down
2 changes: 1 addition & 1 deletion synapse/python_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"url_preview": ["lxml>=3.5.0"],
"sentry": ["sentry-sdk>=0.7.2"],
"opentracing": ["jaeger-client>=4.0.0", "opentracing>=2.2.0"],
"jwt": ["pyjwt>=1.6.4"],
"jwt": ["pyjwt>=2.1.0"],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I asked the packages whether this change would be OK. Can you remind me why we're bumping this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like supported versions of Debian and Fedora are on 1.7.1 still. Would it be possible to use that?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

PyJWKClient was added in v2.0.0 and caching of signing keys is supported since v2.1.0: https://github.com/jpadilla/pyjwt/blob/master/CHANGELOG.rst#v210

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not really sure what to do about this, although I do wonder if we should be using PyJWKClient since it bypasses the reactor / current methods for communicating with external resources.

# hiredis is not a *strict* dependency, but it makes things much faster.
# (if it is not installed, we fall back to slow code.)
"redis": ["txredisapi>=1.4.7", "hiredis"],
Expand Down
21 changes: 18 additions & 3 deletions synapse/rest/client/v1/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from synapse.http.site import SynapseRequest
from synapse.rest.client.v2_alpha._base import client_patterns
from synapse.rest.well_known import WellKnownBuilder
from synapse.types import JsonDict, UserID
from synapse.types import JsonDict, UserID, map_username_to_mxid_localpart

if TYPE_CHECKING:
from synapse.server import HomeServer
Expand All @@ -56,6 +56,9 @@ def __init__(self, hs: "HomeServer"):
# JWT configuration variables.
self.jwt_enabled = hs.config.jwt_enabled
self.jwt_secret = hs.config.jwt_secret
self.jwt_jwks_uri = hs.config.jwt_jwks_uri
self.jwt_subject_claim = hs.config.jwt_subject_claim
self.jwt_normalize_user_id = hs.config.jwt_normalize_user_id
self.jwt_algorithm = hs.config.jwt_algorithm
self.jwt_issuer = hs.config.jwt_issuer
self.jwt_audiences = hs.config.jwt_audiences
Expand Down Expand Up @@ -319,11 +322,19 @@ async def _do_jwt_login(self, login_submission: JsonDict) -> Dict[str, str]:
)

import jwt
from jwt import PyJWKClient

key = self.jwt_secret

if not key and self.jwt_jwks_uri:
jwks_client = PyJWKClient(self.jwt_jwks_uri)
signing_key = jwks_client.get_signing_key_from_jwt(token)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this make a synchronous HTTP call? If so we ideally would do this via a SimpleHttpClient or push this into the background.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, seems like it makes a synchronous HTTP call. It is also possible to implement the loading and parsing of JWKS without PyJWT, like in OidcHandler, then we have more control over the request and caching.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fetching this for every login seems quite inefficient. I wonder if we should do something on start-up (like OIDC). It looks like PyJWKClient is quite simple!

key = signing_key.key
Comment thread
boti7 marked this conversation as resolved.

try:
payload = jwt.decode(
token,
self.jwt_secret,
key,
algorithms=[self.jwt_algorithm],
issuer=self.jwt_issuer,
audience=self.jwt_audiences,
Expand All @@ -336,10 +347,14 @@ async def _do_jwt_login(self, login_submission: JsonDict) -> Dict[str, str]:
errcode=Codes.FORBIDDEN,
)

user = payload.get("sub", None)
subject_claim = self.jwt_subject_claim or "sub"
user = payload.get(subject_claim, None)
Comment on lines +350 to +351
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We already did the fallback to sub in the config code, no need to do it again.

Suggested change
subject_claim = self.jwt_subject_claim or "sub"
user = payload.get(subject_claim, None)
user = payload.get(self.jwt_subject_claim, None)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The issue is that tests don't use the config code but set hs.config directly, so without specifying a fallback here, many tests in JWTTestCase would fail. What do you suggest to solve this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The config code should get run during tests, see:

synapse/tests/unittest.py

Lines 469 to 472 in fe604a0

# Parse the config from a config dict into a HomeServerConfig
config_obj = HomeServerConfig()
config_obj.parse_config_dict(config, "", "")
kwargs["config"] = config_obj

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Were you able to try this again? It should run fine during tests.

if user is None:
raise LoginError(403, "Invalid JWT", errcode=Codes.FORBIDDEN)

if self.jwt_normalize_user_id:
user = map_username_to_mxid_localpart(user)

user_id = UserID(user, self.hs.hostname).to_string()
result = await self._complete_login(
user_id, login_submission, create_non_existent_users=True
Expand Down
48 changes: 48 additions & 0 deletions tests/rest/client/v1/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,54 @@ def test_login_aud_no_config(self):
channel.json_body["error"], "JWT validation failed: Invalid audience"
)

def test_login_default_sub(self):
"""Test reading user ID from the default subject claim."""
channel = self.jwt_login({"sub": "kermit"})
self.assertEqual(channel.result["code"], b"200", channel.result)
self.assertEqual(channel.json_body["user_id"], "@kermit:test")

@override_config(
{
"jwt_config": {
"jwt_enabled": True,
"secret": jwt_secret,
"algorithm": jwt_algorithm,
"subject_claim": "username",
}
}
)
def test_login_custom_sub(self):
"""Test reading user ID from a custom subject claim."""
channel = self.jwt_login({"username": "frog"})
self.assertEqual(channel.result["code"], b"200", channel.result)
self.assertEqual(channel.json_body["user_id"], "@frog:test")

def test_login_no_normalize_id(self):
"""Test mapping user ID to Matrix ID without normalization"""
channel = self.jwt_login({"sub": "#kermit"})
self.assertEqual(channel.result["code"], b"400", channel.result)
self.assertEqual(channel.json_body["errcode"], "M_INVALID_USERNAME")
self.assertEqual(
channel.json_body["error"],
"User ID can only contain characters a-z, 0-9, or '=_-./'",
)

@override_config(
{
"jwt_config": {
"jwt_enabled": True,
"secret": jwt_secret,
"algorithm": jwt_algorithm,
"normalize_user_id": True,
}
}
)
def test_login_normalize_id(self):
"""Test mapping user ID to Matrix ID with normalization"""
channel = self.jwt_login({"sub": "#kermit"})
self.assertEqual(channel.result["code"], b"200", channel.result)
self.assertEqual(channel.json_body["user_id"], "@=23kermit:test")

def test_login_no_token(self):
params = {"type": "org.matrix.login.jwt"}
channel = self.make_request(b"POST", LOGIN_URL, params)
Expand Down