Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit d74afd7

Browse files
committed
Support RP initiated logout
1 parent 999ec0e commit d74afd7

2 files changed

Lines changed: 59 additions & 11 deletions

File tree

src/oidcrp/__init__.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
from importlib import import_module
66

77
from cryptojwt.utils import as_bytes
8-
from oidcmsg.oauth2 import is_error_message
98
from oidcmsg.oauth2 import ResponseMessage
9+
from oidcmsg.oauth2 import is_error_message
1010
from oidcmsg.oidc import AccessTokenResponse
1111
from oidcmsg.oidc import AuthorizationRequest
1212
from oidcmsg.oidc import AuthorizationResponse
1313
from oidcmsg.oidc import OpenIDSchema
1414
from oidcmsg.time_util import time_sans_frac
1515
from oidcservice import rndstr
1616
from oidcservice.exception import OidcServiceError
17-
from oidcservice.state_interface import StateInterface
1817
from oidcservice.state_interface import InMemoryStateDataBase
18+
from oidcservice.state_interface import StateInterface
1919

2020
from oidcrp import oauth2
2121
from oidcrp import oidc
@@ -329,7 +329,21 @@ def client_setup(self, iss_id='', user=''):
329329
return client
330330

331331
issuer = self.do_provider_info(client)
332-
self.do_client_registration(client, iss_id)
332+
_sc = client.service_context
333+
try:
334+
_fe = _sc.federation_entity
335+
except AttributeError:
336+
_fe = None
337+
registration_type = 'explicit'
338+
else:
339+
registration_type = _fe.registration_type
340+
341+
if registration_type == 'implicit':
342+
_sc.client_id = client.client_id = _fe.entity_id
343+
_sc.redirect_uris = _sc.behaviour['redirect_uris']
344+
else:
345+
self.do_client_registration(client, iss_id)
346+
333347
self.issuer2rp[issuer] = client
334348
return client
335349

@@ -805,6 +819,39 @@ def get_valid_access_token(self, state):
805819
else:
806820
raise OidcServiceError('No valid access token')
807821

822+
def logout(self, state, client=None, post_logout_redirect_uri=''):
823+
"""
824+
Does a RP initiated logout from an OP. After logout the user will be
825+
redirect by the OP to a URL of choice (post_logout_redirect_uri).
826+
827+
:param state: Key to an active session
828+
:param client: Which client to use
829+
:param post_logout_redirect_uri: If a special post_logout_redirect_uri
830+
should be used
831+
:return:
832+
"""
833+
if client is None:
834+
client = self.get_client_from_session_key(state)
835+
836+
try:
837+
srv = client.service['end_session']
838+
except KeyError:
839+
raise OidcServiceError("Does not know how to logout")
840+
841+
if post_logout_redirect_uri:
842+
request_args = {
843+
"post_logout_redirect_uri": post_logout_redirect_uri
844+
}
845+
else:
846+
request_args = {}
847+
848+
resp = client.do_request('end_session', state=state,
849+
request_args=request_args)
850+
if is_error_message(resp):
851+
raise OidcServiceError(resp['error'])
852+
853+
return resp
854+
808855

809856
def get_provider_specific_service(service_provider, service, **kwargs):
810857
"""

tests/test_04_http.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
from oidcrp.util import set_cookie
99

1010
_dirname = os.path.dirname(os.path.abspath(__file__))
11+
_keydir = os.path.join(_dirname, "data", "keys")
1112

12-
CLIENT_CERT = open(os.path.join(_dirname, "data", "keys",'cert.key')).read()
13-
CA_CERT = open(os.path.join(_dirname, "data", "keys",'cacert.pem')).read()
13+
# CLIENT_CERT = open(os.path.join(_keydir,'cert.key')).read()
14+
# CA_CERT = open(os.path.join(_keydir, 'cacert.pem')).read()
1415

1516

1617
@pytest.fixture
@@ -22,12 +23,12 @@ def __init__(self):
2223
return CookieDealer(DummyServer())
2324

2425

25-
def test_ca_cert():
26-
with pytest.raises(ValueError):
27-
HTTPLib(CA_CERT, False, CLIENT_CERT)
28-
29-
_h = HTTPLib(CA_CERT, True, CLIENT_CERT)
30-
assert _h.request_args["verify"] == CA_CERT
26+
# def test_ca_cert():
27+
# with pytest.raises(ValueError):
28+
# HTTPLib(CA_CERT, False, CLIENT_CERT)
29+
#
30+
# _h = HTTPLib(CA_CERT, True, CLIENT_CERT)
31+
# assert _h.request_args["verify"] == CA_CERT
3132

3233

3334
def test_cookie(cookie_dealer):

0 commit comments

Comments
 (0)