This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add an admin endpoint to allow authorizing server to signal token revocations #16125
Merged
Merged
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9db3a90
add an expiring cache to `_introspect_token`
H-Shay e4dfba4
add some tests
H-Shay d3841eb
newsfragment
H-Shay 400c90d
requested changes
H-Shay f4d8665
add admin api for authorizing server to signal token revocations
H-Shay ea6029e
tests
H-Shay b928e90
newsfragment
H-Shay ed62b90
add functionality to stream token invalidations over replication
H-Shay 281212b
stream token cache invalidations when token is revoked or device is d…
H-Shay 7d16067
tests
H-Shay c26939d
Merge branch 'develop' into shay/revoke_token_api
H-Shay d2bf3a9
invalidate token cache when deleting device at store level
H-Shay bf29497
invlaidate cache when deleting in store
H-Shay 14d7c65
Merge branch 'shay/revoke_token_api' of https://github.com/matrix-org…
H-Shay becf6a6
fix stray extra space
H-Shay ef7d25a
Make invalidate take a single key
erikjohnston e03672e
Lint
erikjohnston 58d49b5
License
erikjohnston ec5f576
Fix test
erikjohnston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add an admin endpoint to allow authorizing server to signal token revocations. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2023 The Matrix.org Foundation C.I.C | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from http import HTTPStatus | ||
from typing import TYPE_CHECKING, Dict, Tuple | ||
|
||
from synapse.http.servlet import RestServlet | ||
from synapse.http.site import SynapseRequest | ||
from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin | ||
|
||
if TYPE_CHECKING: | ||
from synapse.server import HomeServer | ||
|
||
|
||
class OIDCTokenRevocationRestServlet(RestServlet): | ||
""" | ||
Delete a given token introspection response - identified by the `jti` field - from the | ||
introspection token cache when a token is revoked at the authorizing server | ||
""" | ||
|
||
PATTERNS = admin_patterns("/OIDC_token_revocation/(?P<token_id>[^/]*)") | ||
|
||
def __init__(self, hs: "HomeServer"): | ||
super().__init__() | ||
self.auth = hs.get_auth() | ||
self.store = hs.get_datastores().main | ||
|
||
async def on_DELETE( | ||
self, request: SynapseRequest, token_id: str | ||
) -> Tuple[HTTPStatus, Dict]: | ||
await assert_requester_is_admin(self.auth, request) | ||
|
||
# mypy ignore - this attribute is defined on MSC3861DelegatedAuth, which is loaded via a config flag | ||
# this endpoint will only be loaded if the same config flag is present | ||
self.auth._token_cache.invalidate([token_id]) # type: ignore[attr-defined] | ||
|
||
# make sure we invalidate the cache on any workers | ||
await self.store.stream_introspection_token_invalidation((token_id,)) | ||
|
||
return HTTPStatus.OK, {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clokep marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import Any, Dict | ||
|
||
import synapse.rest.admin._base | ||
|
||
from tests.replication._base import BaseMultiWorkerStreamTestCase | ||
|
||
|
||
class IntrospectionTokenCacheInvalidationTestCase(BaseMultiWorkerStreamTestCase): | ||
servlets = [synapse.rest.admin.register_servlets] | ||
|
||
def default_config(self) -> Dict[str, Any]: | ||
config = super().default_config() | ||
config["disable_registration"] = True | ||
config["experimental_features"] = { | ||
"msc3861": { | ||
"enabled": True, | ||
"issuer": "some_dude", | ||
"client_id": "ID", | ||
"client_auth_method": "client_secret_post", | ||
"client_secret": "secret", | ||
} | ||
} | ||
return config | ||
|
||
def test_stream_introspection_token_invalidation(self) -> None: | ||
worker_hs = self.make_worker_hs("synapse.app.generic_worker") | ||
auth = worker_hs.get_auth() | ||
store = self.hs.get_datastores().main | ||
|
||
# add a token to the cache on the worker | ||
auth._token_cache["open_sesame"] = "intro_token" # type: ignore[attr-defined] | ||
|
||
# stream the invalidation from the master | ||
self.get_success( | ||
store.stream_introspection_token_invalidation(("open_sesame",)) | ||
) | ||
|
||
# check that the cache on the worker was invalidated | ||
self.assertEqual(auth._token_cache.get("open_sesame"), None) # type: ignore[attr-defined] | ||
|
||
# test invalidating whole cache | ||
for i in range(0, 5): | ||
auth._token_cache[f"open_sesame_{i}"] = f"intro_token_{i}" # type: ignore[attr-defined] | ||
self.assertEqual(len(auth._token_cache), 5) # type: ignore[attr-defined] | ||
|
||
self.get_success(store.stream_introspection_token_invalidation((None,))) | ||
|
||
self.assertEqual(len(auth._token_cache), 0) # type: ignore[attr-defined] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
invalidate
on the other caches just invalidates a single key. Let's do the same thing here to avoid confusion