• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / Products.PluggableAuthService / 5303493172

pending completion
5303493172

push

github

web-flow
Drop support for Python 2.7, 3.5, 3.6. (#116)

* Drop zserver extra in setup.py. Thus dropping FTP support.
* Drop support for Zope < 5.
Co-authored-by: Jens Vagelpohl <jens@plyp.com>

1288 of 1745 branches covered (73.81%)

Branch coverage included in aggregate %.

127 of 127 new or added lines in 30 files covered. (100.0%)

9619 of 10349 relevant lines covered (92.95%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.64
/src/Products/PluggableAuthService/plugins/ChallengeProtocolChooser.py
1
##############################################################################
2
#
3
# Copyright (c) 2001 Zope Foundation and Contributors
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this
7
# distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
""" Classes: ChallengeProtocolChooser
1✔
15
"""
16

17
from AccessControl import ClassSecurityInfo
1✔
18
from AccessControl.class_init import InitializeClass
1✔
19
from BTrees.OOBTree import OOBTree
1✔
20
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
1✔
21
from zope.interface import Interface
1✔
22

23
from ..interfaces.plugins import IChallengePlugin
1✔
24
from ..interfaces.plugins import IChallengeProtocolChooser
1✔
25
from ..interfaces.plugins import IRequestTypeSniffer
1✔
26
from ..interfaces.request import IBrowserRequest
1✔
27
from ..interfaces.request import IWebDAVRequest
1✔
28
from ..interfaces.request import IXMLRPCRequest
1✔
29
from ..plugins.BasePlugin import BasePlugin
1✔
30
from ..utils import classImplements
1✔
31

32

33
class IChallengeProtocolChooserPlugin(Interface):
1✔
34
    """ Marker interface.
35
    """
36

37

38
_request_types = ()
1✔
39
_request_type_bmap = {}
1✔
40

41

42
def registerRequestType(label, iface):
1✔
43
    global _request_types
44
    registry = list(_request_types)
1✔
45
    registry.append((label, iface))
1✔
46
    _request_types = tuple(registry)
1✔
47
    _request_type_bmap[iface] = label
1✔
48

49

50
def listRequestTypesLabels():
1✔
51
    return _request_type_bmap.values()
1✔
52

53

54
manage_addChallengeProtocolChooserForm = PageTemplateFile(
1✔
55
    'www/cpcAdd', globals(), __name__='manage_addChallengeProtocolChooserForm')
56

57

58
def addChallengeProtocolChooserPlugin(dispatcher, id, title=None,
1✔
59
                                      mapping=None, REQUEST=None):
60
    """ Add a ChallengeProtocolChooserPlugin to a Pluggable Auth Service. """
61

62
    cpc = ChallengeProtocolChooser(id, title=title, mapping=mapping)
1✔
63
    dispatcher._setObject(cpc.getId(), cpc)
1✔
64

65
    if REQUEST is not None:
1!
66
        REQUEST['RESPONSE'].redirect('%s/manage_workspace'
×
67
                                     '?manage_tabs_message='
68
                                     'ChallengeProtocolChooser+added.' %
69
                                     dispatcher.absolute_url())
70

71

72
class ChallengeProtocolChooser(BasePlugin):
1✔
73

74
    """ PAS plugin for choosing challenger protocol based on request
75
    """
76
    meta_type = 'Challenge Protocol Chooser Plugin'
1✔
77
    zmi_icon = 'fas fa-broadcast-tower'
1✔
78

79
    security = ClassSecurityInfo()
1✔
80

81
    manage_options = (({'label': 'Mapping',
1✔
82
                        'action': 'manage_editProtocolMapping'},)
83
                      + BasePlugin.manage_options)
84

85
    def __init__(self, id, title=None, mapping=None):
1✔
86
        self._id = self.id = id
1✔
87
        self.title = title
1✔
88
        self._map = OOBTree()
1✔
89
        if mapping is not None:
1✔
90
            self.manage_updateProtocolMapping(mapping=mapping)
1✔
91

92
    @security.private
1✔
93
    def chooseProtocols(self, request):
1✔
94
        pas_instance = self._getPAS()
1✔
95
        plugins = pas_instance._getOb('plugins')
1✔
96

97
        sniffers = plugins.listPlugins(IRequestTypeSniffer)
1✔
98

99
        for sniffer_id, sniffer in sniffers:
1!
100
            request_type = sniffer.sniffRequestType(request)
1✔
101
            if request_type is not None:
1!
102
                return self._getProtocolsFor(request_type)
1✔
103

104
    def _getProtocolsFor(self, request_type):
1✔
105
        label = _request_type_bmap.get(request_type, None)
1✔
106
        if label is None:
1!
107
            return
×
108
        return self._map.get(label, None)
1✔
109

110
    def _listProtocols(self):
1✔
111
        pas_instance = self._getPAS()
×
112
        plugins = pas_instance._getOb('plugins')
×
113

114
        challengers = plugins.listPlugins(IChallengePlugin)
×
115
        found = []
×
116

117
        for challenger_id, challenger in challengers:
×
118
            protocol = getattr(challenger, 'protocol', challenger_id)
×
119
            if protocol not in found:
×
120
                found.append(protocol)
×
121

122
        return found
×
123

124
    manage_editProtocolMappingForm = PageTemplateFile(
1✔
125
        'www/cpcEdit', globals(),
126
        __name__='manage_editProtocolMappingForm')
127

128
    def manage_editProtocolMapping(self, REQUEST=None):
1✔
129
        """ Edit Protocol Mapping
130
        """
131
        info = []
×
132
        available_protocols = self._listProtocols()
×
133

134
        request_types = listRequestTypesLabels()
×
135

136
        for label in sorted(request_types):
×
137
            settings = []
×
138
            select_any = False
×
139
            info.append({'label': label, 'settings': settings})
×
140
            protocols = self._map.get(label, None)
×
141
            if not protocols:
×
142
                select_any = True
×
143
            for protocol in available_protocols:
×
144
                selected = False
×
145
                if protocols and protocol in protocols:
×
146
                    selected = True
×
147
                settings.append({'label': protocol,
×
148
                                 'selected': selected,
149
                                 'value': protocol})
150

151
            settings.insert(0, {'label': '(any)',
×
152
                                'selected': select_any,
153
                                'value': ''})
154
        return self.manage_editProtocolMappingForm(info=info, REQUEST=REQUEST)
×
155

156
    def manage_updateProtocolMapping(self, mapping, REQUEST=None):
1✔
157
        """ Update mapping of Request Type to Protocols
158
        """
159
        for key, value in mapping.items():
1✔
160
            value = [_f for _f in value if _f]
1✔
161
            if not value:
1✔
162
                if key in self._map:
1!
163
                    del self._map[key]
×
164
            else:
165
                self._map[key] = value
1✔
166

167
        if REQUEST is not None:
1!
168
            REQUEST['RESPONSE'].redirect('%s/manage_editProtocolMapping'
×
169
                                         '?manage_tabs_message='
170
                                         'Protocol+Mappings+Changed.' %
171
                                         self.absolute_url())
172

173

174
classImplements(ChallengeProtocolChooser, IChallengeProtocolChooserPlugin,
1✔
175
                IChallengeProtocolChooser)
176

177
InitializeClass(ChallengeProtocolChooser)
1✔
178

179
for label, iface in (('Browser', IBrowserRequest),
1✔
180
                     ('WebDAV', IWebDAVRequest),
181
                     ('XML-RPC', IXMLRPCRequest)):
182
    registerRequestType(label, iface)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc