Projet

Général

Profil

0001-remove-authentic2_auth_oidc-from-plugin-system-44322.patch

Emmanuel Cazenave, 23 juin 2020 11:39

Télécharger (10,6 ko)

Voir les différences:

Subject: [PATCH] remove authentic2_auth_oidc from plugin system (#44322)

 setup.py                             |  1 -
 src/authentic2/settings.py           | 10 ++--
 src/authentic2/urls.py               |  8 +++-
 src/authentic2/utils/__init__.py     |  9 ++++
 src/authentic2_auth_oidc/__init__.py | 64 +------------------------
 src/authentic2_auth_oidc/apps.py     | 71 ++++++++++++++++++++++++++++
 6 files changed, 95 insertions(+), 68 deletions(-)
 create mode 100644 src/authentic2_auth_oidc/apps.py
setup.py
169 169
      },
170 170
      entry_points={
171 171
          'authentic2.plugin': [
172
              'authentic2-auth-oidc = authentic2_auth_oidc:Plugin',
173 172
              'authentic2-idp-cas = authentic2_idp_cas:Plugin',
174 173
              'authentic2-idp-oidc = authentic2_idp_oidc:Plugin',
175 174
              'authentic2-provisionning-ldap = authentic2_provisionning_ldap:Plugin',
src/authentic2/settings.py
130 130
    'django_tables2',
131 131
    'mellon',
132 132
    'authentic2_auth_saml',
133
    'authentic2_auth_oidc',
133 134
    'authentic2.nonce',
134 135
    'authentic2.saml',
135 136
    'authentic2.idp',
......
158 159
    'authentic2.backends.models_backend.DummyModelBackend',
159 160
    'django_rbac.backends.DjangoRBACBackend',
160 161
    'authentic2_auth_saml.backends.SAMLBackend',
162
    'authentic2_auth_oidc.backends.OIDCBackend',
161 163
)
162 164
AUTHENTICATION_BACKENDS = plugins.register_plugins_authentication_backends(AUTHENTICATION_BACKENDS)
163 165
CSRF_FAILURE_VIEW = 'authentic2.views.csrf_failure_view'
......
176 178
# Authentication settings
177 179
###########################
178 180
AUTH_USER_MODEL = 'custom_user.User'
179
AUTH_FRONTENDS = ('authentic2_auth_saml.authenticators.SAMLAuthenticator',) + \
180
    plugins.register_plugins_authenticators((
181
        'authentic2.authenticators.LoginPasswordAuthenticator',))
181
AUTH_FRONTENDS = (
182
    'authentic2_auth_saml.authenticators.SAMLAuthenticator',
183
    'authentic2_auth_oidc.authenticators.OIDCAuthenticator',
184
) + plugins.register_plugins_authenticators((
185
    'authentic2.authenticators.LoginPasswordAuthenticator',))
182 186

  
183 187
###########################
184 188
# RBAC settings
src/authentic2/urls.py
25 25

  
26 26
from . import plugins, views
27 27
from authentic2.decorators import setting_enabled, required, lasso_required
28
import authentic2_auth_oidc.urls
28 29
import authentic2_auth_saml.urls
29 30
import authentic2.idp.saml.app_settings
30 31

  
......
156 157
    ),
157 158
    [url(r'^idp/saml2/', include('authentic2.idp.saml.urls'))]
158 159
)
159
urlpatterns = authentic2_auth_saml.urls.urlpatterns + authentic2_idp_saml_urls + urlpatterns
160
urlpatterns = (
161
    authentic2_auth_oidc.urls.urlpatterns +
162
    authentic2_auth_saml.urls.urlpatterns +
163
    authentic2_idp_saml_urls +
164
    urlpatterns
165
)
src/authentic2/utils/__init__.py
28 28

  
29 29
from importlib import import_module
30 30

  
31
import django.apps
31 32
from django.conf import settings
32 33
from django.http import HttpResponseRedirect, HttpResponse
33 34
from django.core.exceptions import ImproperlyConfigured
......
143 144
            method = getattr(plugin, method_name)
144 145
            if callable(method):
145 146
                list += method(request, **kwargs)
147

  
148
    # now try app config
149
    for app_config in django.apps.apps.get_app_configs():
150
        if hasattr(app_config, method_name):
151
            method = getattr(app_config, method_name)
152
            if callable(method):
153
                list += method(request, **kwargs)
154

  
146 155
    return list
147 156

  
148 157

  
src/authentic2_auth_oidc/__init__.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17

  
18
class Plugin(object):
19
    def get_before_urls(self):
20
        from . import urls
21
        return urls.urlpatterns
22

  
23
    def get_apps(self):
24
        return [__name__]
25

  
26
    def get_authentication_backends(self):
27
        return ['authentic2_auth_oidc.backends.OIDCBackend']
28

  
29
    def get_authenticators(self):
30
        return ['authentic2_auth_oidc.authenticators.OIDCAuthenticator']
31

  
32
    def redirect_logout_list(self, request, next=None):
33
        from django.urls import reverse
34
        from authentic2.utils import make_url
35
        from .models import OIDCProvider
36

  
37
        tokens = request.session.get('auth_oidc', {}).get('tokens', [])
38
        urls = []
39
        if tokens:
40
            for token in tokens:
41
                provider = OIDCProvider.objects.get(pk=token['provider_pk'])
42
                # ignore providers wihtout SLO
43
                if not provider.end_session_endpoint:
44
                    continue
45
                params = {}
46
                if 'id_token' in token['token_response']:
47
                    params['id_token_hint'] = token['token_response']['id_token']
48
                if 'access_token' in token['token_response'] and provider.token_revocation_endpoint:
49
                    self.revoke_token(provider, token['token_response']['access_token'])
50
                params['post_logout_redirect_uri'] = request.build_absolute_uri(reverse('auth_logout'))
51
                urls.append(make_url(provider.end_session_endpoint, params=params))
52
        return urls
53

  
54
    def revoke_token(self, provider, access_token):
55
        import logging
56
        import requests
57

  
58
        logger = logging.getLogger(__name__)
59

  
60
        url = provider.token_revocation_endpoint
61
        try:
62
            response = requests.post(url, auth=(provider.client_id, provider.client_secret),
63
                                     data={'token': access_token, 'token_type': 'access_token'},
64
                                     timeout=10)
65
        except requests.RequestException as e:
66
            logger.warning(u'failed to revoke access token from OIDC provider %s: %s',
67
                           provider.issuer, e)
68
            return
69
        try:
70
            response.raise_for_status()
71
        except requests.RequestException as e:
72
            try:
73
                content = response.json()
74
            except ValueError:
75
                content = None
76
            logger.warning(u'failed to revoke access token from OIDC provider %s: %s, %s',
77
                           provider.issuer, e, content)
78
            return
79
        logger.info(u'revoked token from OIDC provider %s', provider.issuer)
17
default_app_config = 'authentic2_auth_oidc.apps.AppConfig'
src/authentic2_auth_oidc/apps.py
1
# authentic2 - versatile identity manager
2
# Copyright (C) 2010-2020 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import django.apps
18

  
19

  
20
class AppConfig(django.apps.AppConfig):
21

  
22
    name = 'authentic2_auth_oidc'
23

  
24
    def revoke_token(self, provider, access_token):
25
        import logging
26
        import requests
27

  
28
        logger = logging.getLogger(__name__)
29

  
30
        url = provider.token_revocation_endpoint
31
        try:
32
            response = requests.post(url, auth=(provider.client_id, provider.client_secret),
33
                                     data={'token': access_token, 'token_type': 'access_token'},
34
                                     timeout=10)
35
        except requests.RequestException as e:
36
            logger.warning(u'failed to revoke access token from OIDC provider %s: %s',
37
                           provider.issuer, e)
38
            return
39
        try:
40
            response.raise_for_status()
41
        except requests.RequestException as e:
42
            try:
43
                content = response.json()
44
            except ValueError:
45
                content = None
46
            logger.warning(u'failed to revoke access token from OIDC provider %s: %s, %s',
47
                           provider.issuer, e, content)
48
            return
49
        logger.info(u'revoked token from OIDC provider %s', provider.issuer)
50

  
51
    def redirect_logout_list(self, request, next=None):
52
        from django.urls import reverse
53
        from authentic2.utils import make_url
54
        from .models import OIDCProvider
55

  
56
        tokens = request.session.get('auth_oidc', {}).get('tokens', [])
57
        urls = []
58
        if tokens:
59
            for token in tokens:
60
                provider = OIDCProvider.objects.get(pk=token['provider_pk'])
61
                # ignore providers wihtout SLO
62
                if not provider.end_session_endpoint:
63
                    continue
64
                params = {}
65
                if 'id_token' in token['token_response']:
66
                    params['id_token_hint'] = token['token_response']['id_token']
67
                if 'access_token' in token['token_response'] and provider.token_revocation_endpoint:
68
                    self.revoke_token(provider, token['token_response']['access_token'])
69
                params['post_logout_redirect_uri'] = request.build_absolute_uri(reverse('auth_logout'))
70
                urls.append(make_url(provider.end_session_endpoint, params=params))
71
        return urls
0
-