Skip to content

Commit b60149c

Browse files
ReeceReece
authored andcommitted
Don't parse html with xml parser, and instead get the data we need directly from pysaml2.
1 parent 2962d9b commit b60149c

File tree

2 files changed

+14
-31
lines changed

2 files changed

+14
-31
lines changed

djangosaml2/utils.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,3 @@ def get_location(http_info):
4646
header_name, header_value = headers[0]
4747
assert header_name == 'Location'
4848
return header_value
49-
50-
51-
def get_hidden_form_inputs(html):
52-
""" Extracts name/value pairs from hidden input tags in an html form."""
53-
pairs = dict()
54-
tree = ElementTree.fromstring(html.replace('&', '&'), forbid_dtd=True)
55-
# python 2.6 doesn't have iter
56-
if hasattr(tree, 'iter'):
57-
node_iter = tree.iter()
58-
else:
59-
node_iter = tree.getiterator()
60-
for node in node_iter:
61-
if node.tag == 'input':
62-
element = dict(node.items())
63-
if element['type'] == 'hidden':
64-
pairs[element['name']] = element['value']
65-
return pairs
66-

djangosaml2/views.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import base64
1617
import logging
1718

1819
try:
1920
from xml.etree import ElementTree
2021
except ImportError:
2122
from elementtree import ElementTree
22-
from defusedxml.common import (DTDForbidden, EntitiesForbidden,
23-
ExternalReferenceForbidden)
2423

2524
from django.conf import settings
2625
from django.contrib import auth
@@ -54,8 +53,7 @@ def csrf_exempt(view_func):
5453
from djangosaml2.cache import StateCache
5554
from djangosaml2.conf import get_config
5655
from djangosaml2.signals import post_authenticated
57-
from djangosaml2.utils import get_custom_setting, available_idps, get_location, \
58-
get_hidden_form_inputs
56+
from djangosaml2.utils import get_custom_setting, available_idps, get_location
5957

6058

6159
logger = logging.getLogger('djangosaml2')
@@ -177,17 +175,20 @@ def login(request,
177175
return HttpResponseRedirect(get_location(result))
178176
elif binding == BINDING_HTTP_POST:
179177
if not post_binding_form_template:
178+
# use the html provided by pysaml2
180179
return HttpResponse(result['data'])
181-
try:
182-
params = get_hidden_form_inputs(result['data'][3])
180+
else:
181+
# manually get request XML to build our own template
182+
request_id, request_xml = client.create_authn_request(
183+
client._sso_location(selected_idp, binding),
184+
binding=binding)
183185
return render(request, post_binding_form_template, {
184-
'target_url': result['url'],
185-
'params': params,
186-
})
187-
except (DTDForbidden, EntitiesForbidden, ExternalReferenceForbidden):
188-
raise PermissionDenied
189-
except TemplateDoesNotExist:
190-
return HttpResponse(result['data'])
186+
'target_url': result['url'],
187+
'params': {
188+
'SAMLRequest': base64.b64encode(request_xml),
189+
'RelayState': came_from,
190+
},
191+
})
191192
else:
192193
raise NotImplementedError('Unsupported binding: %s', binding)
193194

0 commit comments

Comments
 (0)