|
1
|
|
|
# Copyright 2015 INFN |
|
2
|
|
|
# All Rights Reserved. |
|
3
|
|
|
# |
|
4
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
|
5
|
|
|
# not use this file except in compliance with the License. You may obtain |
|
6
|
|
|
# a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
12
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
13
|
|
|
# License for the specific language governing permissions and limitations |
|
14
|
|
|
# under the License. |
|
15
|
|
|
|
|
16
|
|
|
""" |
|
17
|
|
|
SSO Application tests |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
from flask import url_for |
|
22
|
|
|
import pytest |
|
23
|
|
|
from urlparse import urlparse |
|
24
|
|
|
from werkzeug.exceptions import BadRequest, Forbidden |
|
25
|
|
|
from discourseSSO import sso |
|
26
|
|
|
|
|
27
|
|
|
app = sso.app |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class Test_sso(): |
|
31
|
|
|
|
|
32
|
|
|
def test_payload_check(self): |
|
33
|
|
|
"""Test the payload is properly managed and the user is sent to the |
|
34
|
|
|
authentication page |
|
35
|
|
|
""" |
|
36
|
|
|
with app.test_request_context('/sso/login?sso=bm9uY2U9Y2I2ODI1MWVlZm' |
|
37
|
|
|
'I1MjExZTU4YzAwZmYxMzk1ZjBjMGI%3D%0A&' |
|
38
|
|
|
'sig=2828aa29899722b35a2f191d34ef9b3ce' |
|
39
|
|
|
'695e0e6eeec47deb46d588d70c7cb56', |
|
40
|
|
|
method='GET'): |
|
41
|
|
|
res = sso.payload_check() |
|
42
|
|
|
assert res.status_code == 302 |
|
43
|
|
|
assert urlparse(res.location).path == url_for('user_authz') |
|
44
|
|
|
|
|
45
|
|
|
def test_bad_payload_sig(self): |
|
46
|
|
|
"""Test the error code 400 is sent if the signature do not match |
|
47
|
|
|
the payload |
|
48
|
|
|
""" |
|
49
|
|
|
with app.test_request_context('/sso/login?sso=bm9uY2U9Y2I2ODI1MWVlZm' |
|
50
|
|
|
'I1MjExZTU4YzAwZmYxMzk1ZjBjMGI%3D%0A&' |
|
51
|
|
|
'sig=2828aa29899722b35a2f191d34ef9b3ce' |
|
52
|
|
|
'695e0e6eeec47deb46d588d70c7cb58', |
|
53
|
|
|
method='GET'): |
|
54
|
|
|
with pytest.raises(BadRequest): |
|
55
|
|
|
sso.payload_check() |
|
56
|
|
|
|
|
57
|
|
|
def test_no_payload(self): |
|
58
|
|
|
"""Test the error code 400 is sent if the sso field is not provided""" |
|
59
|
|
|
with app.test_request_context('/sso/login?sig=2828aa29899722b35a2f191' |
|
60
|
|
|
'd34ef9b3ce695e0e6eeec47deb46d588d70c7c' |
|
61
|
|
|
'b56', |
|
62
|
|
|
method='GET'): |
|
63
|
|
|
with pytest.raises(BadRequest): |
|
64
|
|
|
sso.payload_check() |
|
65
|
|
|
|
|
66
|
|
|
def test_no_hash(self): |
|
67
|
|
|
"""Test the error code 400 is sent if the sig field is not provided""" |
|
68
|
|
|
with app.test_request_context('/sso/login?sso=bm9uY2U9Y2I2ODI1MWVlZm' |
|
69
|
|
|
'I1MjExZTU4YzAwZmYxMzk1ZjBjMGI%3D%0A&', |
|
70
|
|
|
method='GET'): |
|
71
|
|
|
with pytest.raises(BadRequest): |
|
72
|
|
|
sso.payload_check() |
|
73
|
|
|
|
|
74
|
|
|
def test_authentication_no_shibboleth_attributes(self): |
|
75
|
|
|
"""Test the authentication when shibboleth do not provide attributes""" |
|
76
|
|
|
with app.test_request_context('/sso/auth', |
|
77
|
|
|
method='GET'): |
|
78
|
|
|
with pytest.raises(Forbidden): |
|
79
|
|
|
sso.user_authz() |
|
80
|
|
|
|
|
81
|
|
|
def test_authentication_no_previous_session(self): |
|
82
|
|
|
"""Test the authentication are properly send to Discourse""" |
|
83
|
|
|
with app.test_request_context('/sso/auth', |
|
84
|
|
|
method='GET', |
|
85
|
|
|
environ_base={ |
|
86
|
|
|
'givenName': 'sam', |
|
87
|
|
|
'sn': '', |
|
88
|
|
|
'username': 'samsam', |
|
89
|
|
|
'mail': '[email protected]', |
|
90
|
|
|
'eppn': 'hello123'} |
|
91
|
|
|
): |
|
92
|
|
|
with pytest.raises(Forbidden): |
|
93
|
|
|
sso.user_authz() |
|
94
|
|
|
|
|
95
|
|
|
def test_authentication_generation(self): |
|
96
|
|
|
"""Test the authentication are properly send to Discourse""" |
|
97
|
|
|
with app.test_request_context('/sso/auth', |
|
98
|
|
|
method='GET', |
|
99
|
|
|
environ_base={ |
|
100
|
|
|
'givenName': 'sam', |
|
101
|
|
|
'sn': '', |
|
102
|
|
|
'username': 'samsam', |
|
103
|
|
|
'mail': '[email protected]', |
|
104
|
|
|
'eppn': 'hello123'} |
|
105
|
|
|
) as req: |
|
106
|
|
|
req.session['nonce'] = 'nonce=cb68251eefb5211e58c00ff1395f0c0b' |
|
107
|
|
|
resp = sso.user_authz() |
|
108
|
|
|
assert resp.status_code == 302 |
|
109
|
|
|
# sso and sig are different from the one reported in |
|
110
|
|
|
# https://meta.discourse.org/t/official-single-sign-on-for- |
|
111
|
|
|
# discourse/13045 |
|
112
|
|
|
# This because ruby and python include new lines in different |
|
113
|
|
|
# positions during the base64 encoding (of course they do not |
|
114
|
|
|
# matter for the base64 but the following URLencoding and |
|
115
|
|
|
# signature are slightly different) |
|
116
|
|
|
assert resp.location == ('http://discuss.example.com/session/' |
|
117
|
|
|
'sso_login?sso=bm9uY2U9Y2I2ODI1MWVlZ' |
|
118
|
|
|
'mI1MjExZTU4YzAwZmYxMzk1ZjBjMGImbmFt' |
|
119
|
|
|
'ZT1zYW0mdXNlcm5hbWU9%0Ac2Ftc2FtJmVt' |
|
120
|
|
|
'YWlsPXRlc3QlNDB0ZXN0LmNvbSZleHRlcm5' |
|
121
|
|
|
'hbF9pZD1oZWxsbzEyMyZhZG1pbj1m%0AYWx' |
|
122
|
|
|
'zZQ%3D%3D%0A&sig=a8ad52d665ddf2d2d5' |
|
123
|
|
|
'5de5d08d745f46d44a503d0b51b0273dd95' |
|
124
|
|
|
'e1f2abe1cbd') |
|
125
|
|
|
|
|
126
|
|
|
def test_authentication_generation_with_full_name(self): |
|
127
|
|
|
"""Test the authentication are properly send to Discourse""" |
|
128
|
|
|
with app.test_request_context('/sso/auth', |
|
129
|
|
|
method='GET', |
|
130
|
|
|
environ_base={ |
|
131
|
|
|
'givenName': 'sam', |
|
132
|
|
|
'sn': 'big', |
|
133
|
|
|
'mail': '[email protected]', |
|
134
|
|
|
'eppn': 'hello123'} |
|
135
|
|
|
) as req: |
|
136
|
|
|
req.session['nonce'] = 'nonce=cb68251eefb5211e58c00ff1395f0c0b' |
|
137
|
|
|
resp = sso.user_authz() |
|
138
|
|
|
assert resp.status_code == 302 |
|
139
|
|
|
assert resp.location == ('http://discuss.example.com/session/' |
|
140
|
|
|
'sso_login?sso=bm9uY2U9Y2I2ODI1MWVlZ' |
|
141
|
|
|
'mI1MjExZTU4YzAwZmYxMzk1ZjBjMGImbmFt' |
|
142
|
|
|
'ZT1zYW0gYmlnJnVzZXJu%0AYW1lPXNhbWJp' |
|
143
|
|
|
'Z19iNjQyJmVtYWlsPXRlc3QlNDB0ZXN0LmN' |
|
144
|
|
|
'vbSZleHRlcm5hbF9pZD1oZWxsbzEy%0AMyZ' |
|
145
|
|
|
'hZG1pbj1mYWxzZQ%3D%3D%0A&sig=8177ae' |
|
146
|
|
|
'45c294212a96767cfb2208db867a14fa099' |
|
147
|
|
|
'0bf7efb2f36dcac41d563e8') |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
def test_authentication_generation_with_avatar_bio(self): |
|
151
|
|
|
"""Test the authentication are properly send to Discourse""" |
|
152
|
|
|
with app.test_request_context('/sso/auth', |
|
153
|
|
|
method='GET', |
|
154
|
|
|
environ_base={ |
|
155
|
|
|
'givenName': 'sam', |
|
156
|
|
|
'sn': '', |
|
157
|
|
|
'username': 'samsam', |
|
158
|
|
|
'mail': '[email protected]', |
|
159
|
|
|
'eppn': 'hello123', |
|
160
|
|
|
'avatar': 'http://myAvatarURL', |
|
161
|
|
|
'profile': 'http://myProfileURL'} |
|
162
|
|
|
) as req: |
|
163
|
|
|
req.session['nonce'] = 'nonce=cb68251eefb5211e58c00ff1395f0c0b' |
|
164
|
|
|
resp = sso.user_authz() |
|
165
|
|
|
assert resp.status_code == 302 |
|
166
|
|
|
# sso and sig are different from the one reported in |
|
167
|
|
|
# https://meta.discourse.org/t/official-single-sign-on-for- |
|
168
|
|
|
# discourse/13045 |
|
169
|
|
|
# This because ruby and python include new lines in different |
|
170
|
|
|
# positions during the base64 encoding (of course they do not |
|
171
|
|
|
# matter for the base64 but the following URLencoding and |
|
172
|
|
|
# signature are slightly different) |
|
173
|
|
|
assert resp.location == ('http://discuss.example.com/session/' |
|
174
|
|
|
'sso_login?sso=bm9uY2U9Y2I2ODI1MWVlZ' |
|
175
|
|
|
'mI1MjExZTU4YzAwZmYxMzk1ZjBjMGImbmFt' |
|
176
|
|
|
'ZT1zYW0mdXNlcm5hbWU9%0Ac2Ftc2FtJmVt' |
|
177
|
|
|
'YWlsPXRlc3QlNDB0ZXN0LmNvbSZleHRlcm5' |
|
178
|
|
|
'hbF9pZD1oZWxsbzEyMyZhdmF0YXJf%0AdXJ' |
|
179
|
|
|
'sPWh0dHAlM0EvL215QXZhdGFyVVJMJmJpbz' |
|
180
|
|
|
'1odHRwJTNBLy9teVByb2ZpbGVVUkwmYWRta' |
|
181
|
|
|
'W49%0AZmFsc2U%3D%0A&sig=61504842b6a' |
|
182
|
|
|
'130d0f2d6976de814313a8df539d5e95bd9' |
|
183
|
|
|
'32d693acbcf0b9df14') |
|
184
|
|
|
|
|
185
|
|
|
def test_authentication_generation_with_flags(self): |
|
186
|
|
|
"""Test the authentication are properly send to Discourse""" |
|
187
|
|
|
with app.test_request_context('/sso/auth', |
|
188
|
|
|
method='GET', |
|
189
|
|
|
environ_base={ |
|
190
|
|
|
'givenName': 'sam', |
|
191
|
|
|
'sn': '', |
|
192
|
|
|
'username': 'samsam', |
|
193
|
|
|
'mail': '[email protected]', |
|
194
|
|
|
'eppn': '[email protected]', |
|
195
|
|
|
'avatar': 'http://myAvatarURL', |
|
196
|
|
|
'profile': 'http://myProfileURL'} |
|
197
|
|
|
) as req: |
|
198
|
|
|
req.session['nonce'] = 'nonce=cb68251eefb5211e58c00ff1395f0c0b' |
|
199
|
|
|
resp = sso.user_authz() |
|
200
|
|
|
assert resp.status_code == 302 |
|
201
|
|
|
# sso and sig are different from the one reported in |
|
202
|
|
|
# https://meta.discourse.org/t/official-single-sign-on-for- |
|
203
|
|
|
# discourse/13045 |
|
204
|
|
|
# This because ruby and python include new lines in different |
|
205
|
|
|
# positions during the base64 encoding (of course they do not |
|
206
|
|
|
# matter for the base64 but the following URLencoding and |
|
207
|
|
|
# signature are slightly different) |
|
208
|
|
|
assert resp.location == ('http://discuss.example.com/session/' |
|
209
|
|
|
'sso_login?sso=bm9uY2U9Y2I2ODI1MWVlZ' |
|
210
|
|
|
'mI1MjExZTU4YzAwZmYxMzk1ZjBjMGImbmFt' |
|
211
|
|
|
'ZT1zYW0mdXNlcm5hbWU9%0Ac2Ftc2FtJmVt' |
|
212
|
|
|
'YWlsPXRlc3QlNDB0ZXN0LmNvbSZleHRlcm5' |
|
213
|
|
|
'hbF9pZD1teS5uYW1lJTQwbXkuaWRw%0AJmF' |
|
214
|
|
|
'2YXRhcl91cmw9aHR0cCUzQS8vbXlBdmF0YX' |
|
215
|
|
|
'JVUkwmYmlvPWh0dHAlM0EvL215UHJvZmlsZ' |
|
216
|
|
|
'VVS%0ATCZhZG1pbj1mYWxzZSZyZXF1aXJlX' |
|
217
|
|
|
'2FjdGl2YXRpb249ZmFsc2U%3D%0A&sig=26' |
|
218
|
|
|
'8beaa221824d9c5ec9df3cb85e0655e86e1' |
|
219
|
|
|
'ba49ce516155f3f2557d7340140') |
|
220
|
|
|
|
|
221
|
|
|
def test_error_page_403(self): |
|
222
|
|
|
"""Test the correct error code is propagated""" |
|
223
|
|
|
with app.test_request_context('/sso/auth', |
|
224
|
|
|
method='GET', |
|
225
|
|
|
environ_base={ |
|
226
|
|
|
'givenName': 'sam', |
|
227
|
|
|
'sn': '', |
|
228
|
|
|
'username': 'samsam', |
|
229
|
|
|
'mail': '[email protected]', |
|
230
|
|
|
'eppn': 'hello123'} |
|
231
|
|
|
): |
|
232
|
|
|
resp = sso.attribuete_not_provided(None) |
|
233
|
|
|
assert resp[1] == 403 |