-
Notifications
You must be signed in to change notification settings - Fork 4
/
authorize_write_test.go
228 lines (209 loc) · 11.2 KB
/
authorize_write_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oauth2_test
import (
"context"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
. "authelia.com/provider/oauth2"
"authelia.com/provider/oauth2/internal/consts"
"authelia.com/provider/oauth2/testing/mock"
)
func TestWriteAuthorizeResponse(t *testing.T) {
testCases := []struct {
name string
setup func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header)
expect func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header)
}{
{
name: "ShouldWriteResponseModeDefault",
setup: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
redir, _ := url.Parse("https://foobar.com/?foo=bar")
requester.EXPECT().GetRedirectURI().Return(redir)
requester.EXPECT().GetResponseMode().Return(ResponseModeDefault).Times(2)
responder.EXPECT().GetParameters().Return(url.Values{})
responder.EXPECT().GetHeader().Return(http.Header{})
rw.EXPECT().Header().Return(header).Times(2)
rw.EXPECT().WriteHeader(http.StatusSeeOther)
},
expect: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
assert.Equal(t, http.Header{
consts.HeaderLocation: []string{"https://foobar.com/?foo=bar"},
consts.HeaderCacheControl: []string{consts.CacheControlNoStore},
consts.HeaderPragma: []string{consts.PragmaNoCache},
}, header)
},
},
{
name: "ShouldWriteResponseModeFragment",
setup: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
redir, _ := url.Parse("https://foobar.com/?foo=bar")
requester.EXPECT().GetRedirectURI().Return(redir)
requester.EXPECT().GetResponseMode().Return(ResponseModeFragment).Times(2)
responder.EXPECT().GetParameters().Return(url.Values{"bar": {"baz"}})
responder.EXPECT().GetHeader().Return(http.Header{})
rw.EXPECT().Header().Return(header).Times(2)
rw.EXPECT().WriteHeader(http.StatusSeeOther)
},
expect: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
assert.Equal(t, http.Header{
consts.HeaderLocation: []string{"https://foobar.com/?foo=bar#bar=baz"},
consts.HeaderCacheControl: []string{consts.CacheControlNoStore},
consts.HeaderPragma: []string{consts.PragmaNoCache},
}, header)
},
},
{
name: "ShouldWriteResponseModeQuery",
setup: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
redir, _ := url.Parse("https://foobar.com/?foo=bar")
requester.EXPECT().GetRedirectURI().Return(redir)
requester.EXPECT().GetResponseMode().Return(ResponseModeQuery).Times(2)
responder.EXPECT().GetParameters().Return(url.Values{"bar": {"baz"}})
responder.EXPECT().GetHeader().Return(http.Header{})
rw.EXPECT().Header().Return(header).Times(2)
rw.EXPECT().WriteHeader(http.StatusSeeOther)
},
expect: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
expectedUrl, _ := url.Parse("https://foobar.com/?foo=bar&bar=baz")
actualUrl, err := url.Parse(header.Get(consts.HeaderLocation))
assert.Nil(t, err)
assert.Equal(t, expectedUrl.Query(), actualUrl.Query())
assert.Equal(t, consts.PragmaNoCache, header.Get(consts.HeaderPragma))
assert.Equal(t, consts.CacheControlNoStore, header.Get(consts.HeaderCacheControl))
},
},
{
name: "ShouldWriteResponseModeFragmentWithCustomHeaders",
setup: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
redir, _ := url.Parse("https://foobar.com/?foo=bar")
requester.EXPECT().GetRedirectURI().Return(redir)
requester.EXPECT().GetResponseMode().Return(ResponseModeFragment).Times(2)
responder.EXPECT().GetParameters().Return(url.Values{"bar": {"b+az ab"}})
responder.EXPECT().GetHeader().Return(http.Header{"X-Bar": {"baz"}})
rw.EXPECT().Header().Return(header).Times(2)
rw.EXPECT().WriteHeader(http.StatusSeeOther)
},
expect: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
assert.Equal(t, http.Header{
"X-Bar": {"baz"},
consts.HeaderLocation: {"https://foobar.com/?foo=bar#bar=b%2Baz+ab"},
consts.HeaderCacheControl: []string{consts.CacheControlNoStore},
consts.HeaderPragma: []string{consts.PragmaNoCache},
}, header)
},
},
{
name: "ShouldWriteResponseModeQueryWithCustomHeaders",
setup: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
redir, _ := url.Parse("https://foobar.com/?foo=bar")
requester.EXPECT().GetRedirectURI().Return(redir)
requester.EXPECT().GetResponseMode().Return(ResponseModeQuery).Times(2)
responder.EXPECT().GetParameters().Return(url.Values{"bar": {"b+az"}, consts.FormParameterScope: {"a b"}})
responder.EXPECT().GetHeader().Return(http.Header{"X-Bar": {"baz"}})
rw.EXPECT().Header().Return(header).Times(2)
rw.EXPECT().WriteHeader(http.StatusSeeOther)
},
expect: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
expectedUrl, err := url.Parse("https://foobar.com/?foo=bar&bar=b%2Baz&scope=a+b")
assert.Nil(t, err)
actualUrl, err := url.Parse(header.Get(consts.HeaderLocation))
assert.Nil(t, err)
assert.Equal(t, expectedUrl.Query(), actualUrl.Query())
assert.Equal(t, consts.PragmaNoCache, header.Get(consts.HeaderPragma))
assert.Equal(t, consts.CacheControlNoStore, header.Get(consts.HeaderCacheControl))
assert.Equal(t, "baz", header.Get("X-Bar"))
},
},
{
name: "ShouldWriteResponseModeFragmentWithCustomHeadersAndSpecialChars",
setup: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
redir, _ := url.Parse("https://foobar.com/?foo=bar")
requester.EXPECT().GetRedirectURI().Return(redir)
requester.EXPECT().GetResponseMode().Return(ResponseModeFragment).Times(2)
responder.EXPECT().GetParameters().Return(url.Values{consts.FormParameterScope: {"api:*"}})
responder.EXPECT().GetHeader().Return(http.Header{"X-Bar": {"baz"}})
rw.EXPECT().Header().Return(header).Times(2)
rw.EXPECT().WriteHeader(http.StatusSeeOther)
},
expect: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
assert.Equal(t, http.Header{
"X-Bar": {"baz"},
consts.HeaderLocation: {"https://foobar.com/?foo=bar#scope=api%3A%2A"},
consts.HeaderCacheControl: []string{consts.CacheControlNoStore},
consts.HeaderPragma: []string{consts.PragmaNoCache},
}, header)
},
},
{
name: "ShouldWriteResponseModeFragmentWithCustomParameters",
setup: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
redir, _ := url.Parse("https://foobar.com/?foo=bar#bar=baz")
requester.EXPECT().GetRedirectURI().Return(redir)
requester.EXPECT().GetResponseMode().Return(ResponseModeFragment).Times(2)
responder.EXPECT().GetParameters().Return(url.Values{"qux": {"quux"}})
responder.EXPECT().GetHeader().Return(http.Header{})
rw.EXPECT().Header().Return(header).Times(2)
rw.EXPECT().WriteHeader(http.StatusSeeOther)
},
expect: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
assert.Equal(t, http.Header{
consts.HeaderLocation: {"https://foobar.com/?foo=bar#qux=quux"},
consts.HeaderCacheControl: []string{consts.CacheControlNoStore},
consts.HeaderPragma: []string{consts.PragmaNoCache},
}, header)
},
},
{
name: "ShouldWriteResponseModeFragmentWithEncodedState",
setup: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
redir, _ := url.Parse("https://foobar.com/?foo=bar")
requester.EXPECT().GetRedirectURI().Return(redir)
requester.EXPECT().GetResponseMode().Return(ResponseModeFragment).Times(2)
responder.EXPECT().GetParameters().Return(url.Values{consts.FormParameterState: {"{\"a\":\"b=c&d=e\"}"}})
responder.EXPECT().GetHeader().Return(http.Header{})
rw.EXPECT().Header().Return(header).Times(2)
rw.EXPECT().WriteHeader(http.StatusSeeOther)
},
expect: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
assert.Equal(t, http.Header{
consts.HeaderLocation: {"https://foobar.com/?foo=bar#state=%7B%22a%22%3A%22b%3Dc%26d%3De%22%7D"},
consts.HeaderCacheControl: []string{consts.CacheControlNoStore},
consts.HeaderPragma: []string{consts.PragmaNoCache},
}, header)
},
},
{
name: "ShouldWriteResponseModeFormPostWithValues",
setup: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
redir, _ := url.Parse("https://foobar.com/?foo=bar")
requester.EXPECT().GetRedirectURI().Return(redir)
requester.EXPECT().GetResponseMode().Return(ResponseModeFormPost).Times(2)
responder.EXPECT().GetHeader().Return(http.Header{"X-Bar": {"baz"}})
responder.EXPECT().GetParameters().Return(url.Values{consts.FormParameterAuthorizationCode: {"poz65kqoneu"}, consts.FormParameterState: {"qm6dnsrn"}})
rw.EXPECT().Header().Return(header).AnyTimes()
rw.EXPECT().Write(gomock.Any()).AnyTimes()
},
expect: func(t *testing.T, rw *mock.MockResponseWriter, requester *mock.MockAuthorizeRequester, responder *mock.MockAuthorizeResponder, header http.Header) {
assert.Equal(t, consts.ContentTypeTextHTML, header.Get(consts.HeaderContentType))
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
provider := &Fosite{Config: new(Config)}
ctrl := gomock.NewController(t)
rw := mock.NewMockResponseWriter(ctrl)
requester := mock.NewMockAuthorizeRequester(ctrl)
responder := mock.NewMockAuthorizeResponder(ctrl)
defer ctrl.Finish()
header := http.Header{}
tc.setup(t, rw, requester, responder, header)
provider.WriteAuthorizeResponse(context.TODO(), rw, requester, responder)
tc.expect(t, rw, requester, responder, header)
})
}
}