Skip to content

Commit 24b4583

Browse files
Merge pull request #381 from IdentityPython/refactor-context-query-params
Enhance Context object properties
2 parents 7c82d89 + b50f70b commit 24b4583

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/satosa/context.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ class Context(object):
2323
def __init__(self):
2424
self._path = None
2525
self.request = None
26+
self.request_uri = None
27+
self.request_method = None
28+
self.qs_params = None
29+
self.server = None
30+
self.http_headers = None
31+
self.cookie = None
32+
self.request_authorization = None
2633
self.target_backend = None
2734
self.target_frontend = None
2835
self.target_micro_service = None
2936
# This dict is a data carrier between frontend and backend modules.
3037
self.internal_data = {}
31-
self.cookie = None
3238
self.state = None
3339

3440
@property

src/satosa/micro_services/idp_hinting.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ def process(self, context, data):
4141
:param data: the internal request
4242
"""
4343
target_entity_id = context.get_decoration(context.KEY_TARGET_ENTITYID)
44-
query_string = context.request
44+
qs_params = context.qs_params
4545

46-
an_issuer_is_already_selected = bool(target_entity_id)
47-
query_string_is_missing = not query_string
48-
if an_issuer_is_already_selected or query_string_is_missing:
46+
issuer_is_already_selected = bool(target_entity_id)
47+
query_string_is_missing = not qs_params
48+
if issuer_is_already_selected or query_string_is_missing:
4949
return super().process(context, data)
5050

5151
hints = (
5252
entity_id
53-
for param in self.idp_hint_param_names
54-
for entity_id in query_string.get(param, [])
55-
if entity_id
53+
for param_name in self.idp_hint_param_names
54+
for qs_param_name, entity_id in qs_params
55+
if param_name == qs_param_name
5656
)
5757
hint = next(hints, None)
5858

src/satosa/proxy_server.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import io
21
import json
32
import logging
43
import logging.config
54
import sys
5+
from io import BytesIO
66
from urllib.parse import parse_qsl as _parse_query_string
77

88
from cookies_samesite_compat import CookiesSameSiteCompatMiddleware
99

1010
import satosa
1111
import satosa.logging_util as lu
12+
1213
from .base import SATOSABase
1314
from .context import Context
1415
from .response import ServiceError, NotFound
@@ -68,14 +69,22 @@ def unpack_request(environ, content_length=0):
6869
return data
6970

7071

72+
def collect_server_headers(environ):
73+
headers = {
74+
header_name: header_value
75+
for header_name, header_value in environ.items()
76+
if header_name.startswith("SERVER_")
77+
}
78+
return headers
79+
80+
7181
def collect_http_headers(environ):
7282
headers = {
7383
header_name: header_value
7484
for header_name, header_value in environ.items()
7585
if (
7686
header_name.startswith("HTTP_")
7787
or header_name.startswith("REMOTE_")
78-
or header_name.startswith("SERVER_")
7988
)
8089
}
8190
return headers
@@ -119,19 +128,21 @@ def __call__(self, environ, start_response, debug=False):
119128
context.path = path
120129

121130
# copy wsgi.input stream to allow it to be re-read later by satosa plugins
122-
# see: http://stackoverflow.com/
123-
# questions/1783383/how-do-i-copy-wsgi-input-if-i-want-to-process-post-data-more-than-once
131+
# see: http://stackoverflow.com/questions/1783383/how-do-i-copy-wsgi-input-if-i-want-to-process-post-data-more-than-once
124132
content_length = int(environ.get('CONTENT_LENGTH', '0') or '0')
125-
body = io.BytesIO(environ['wsgi.input'].read(content_length))
133+
body = BytesIO(environ['wsgi.input'].read(content_length))
126134
environ['wsgi.input'] = body
135+
127136
context.request = unpack_request(environ, content_length)
128137
context.request_uri = environ.get("REQUEST_URI")
129138
context.request_method = environ.get("REQUEST_METHOD")
139+
context.qs_params = parse_query_string(environ.get("QUERY_STRING"))
140+
context.server = collect_server_headers(environ)
130141
context.http_headers = collect_http_headers(environ)
131-
environ['wsgi.input'].seek(0)
142+
context.cookie = context.http_headers.get("HTTP_COOKIE", "")
143+
context.request_authorization = context.http_headers.get("HTTP_AUTHORIZATION", "")
132144

133-
context.cookie = environ.get("HTTP_COOKIE", "")
134-
context.request_authorization = environ.get("HTTP_AUTHORIZATION", "")
145+
environ['wsgi.input'].seek(0)
135146

136147
try:
137148
resp = self.run(context)

0 commit comments

Comments
 (0)