Repository navigation
Expand file tree
/
Copy patherroauth2callback_test.go
More file actions
246 lines (237 loc) · 6.48 KB
/
Copy patherroauth2callback_test.go
File metadata and controls
246 lines (237 loc) · 6.48 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package jawsauth
import (
"errors"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/linkdata/jaws"
"golang.org/x/oauth2"
)
func Test_oauth2CallbackError(t *testing.T) {
tests := []struct {
name string
url string
wantStatus int
wantErr string
}{
{
name: "none",
url: "http://example.com/oauth2/callback",
wantStatus: http.StatusTeapot,
},
{
name: "accessDenied",
url: "http://example.com/oauth2/callback?error=access_denied&error_description=User+cancelled&error_uri=https%3A%2F%2Fprovider.example%2Fdocs%2Ferrors%23access_denied",
wantStatus: http.StatusForbidden,
wantErr: "access_denied",
},
{
name: "otherError",
url: "http://example.com/oauth2/callback?error=server_error&error_description=boom",
wantStatus: http.StatusBadRequest,
wantErr: "server_error",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tc.url, nil)
statusCode, err := oauth2CallbackError(http.StatusTeapot, req)
if statusCode != tc.wantStatus {
t.Fatal(statusCode)
}
if tc.wantErr == "" {
if err != nil {
t.Fatal(err)
}
return
}
if err == nil {
t.Fatal("expected callback error")
}
if !errors.Is(err, ErrOAuth2Callback) {
t.Fatal(err)
}
var callbackErr *OAuth2CallbackError
if !errors.As(err, &callbackErr) {
t.Fatal(err)
}
if callbackErr.Code != tc.wantErr {
t.Fatal(callbackErr.Code)
}
})
}
req := httptest.NewRequest(
http.MethodGet,
"http://example.com/oauth2/callback?error=access_denied&error_description=User+cancelled&error_uri=https%3A%2F%2Fprovider.example%2Fdocs%2Ferrors%23access_denied",
nil,
)
_, err := oauth2CallbackError(http.StatusTeapot, req)
var callbackErr *OAuth2CallbackError
if !errors.As(err, &callbackErr) {
t.Fatal(err)
}
if callbackErr.Description != "User cancelled" {
t.Fatal(callbackErr.Description)
}
if callbackErr.URI != "https://provider.example/docs/errors#access_denied" {
t.Fatal(callbackErr.URI)
}
if !strings.Contains(err.Error(), "oauth2 callback error: access_denied") {
t.Fatal(err.Error())
}
}
func Test_oauth2CallbackErrorNilReceiver(t *testing.T) {
var nilErr *OAuth2CallbackError
if s := nilErr.Error(); s != ErrOAuth2Callback.Error() {
t.Fatal(s)
}
}
func Test_handleAuthResponseOAuthErrorCallback(t *testing.T) {
jw, err := jaws.New()
if err != nil {
t.Fatal(err)
}
defer jw.Close()
srv := &Server{
Jaws: jw,
HandledPaths: map[string]struct{}{},
oauth2cfg: &oauth2.Config{
ClientID: "client",
Endpoint: oauth2.Endpoint{TokenURL: "https://provider.example/token"},
RedirectURL: "https://example.com/oauth2/callback",
},
userinfoUrl: "https://provider.example/userinfo",
}
var callbackStatus int
var callbackErr error
srv.LoginFailed = func(hw http.ResponseWriter, hr *http.Request, httpCode int, callErr error, email string) bool {
callbackStatus = httpCode
callbackErr = callErr
if email != "" {
t.Fatal(email)
}
return false
}
rec := httptest.NewRecorder()
req := httptest.NewRequest(
http.MethodGet,
"http://example.com/oauth2/callback?state=state123&error=access_denied&error_description=User+cancelled",
nil,
)
sess := jw.NewSession(rec, req)
sess.Set(oauth2StateKey, "state123")
srv.HandleAuthResponse(rec, req)
if callbackStatus != http.StatusForbidden {
t.Fatal(callbackStatus)
}
if !errors.Is(callbackErr, ErrOAuth2Callback) {
t.Fatal(callbackErr)
}
var oauthErr *OAuth2CallbackError
if !errors.As(callbackErr, &oauthErr) {
t.Fatal(callbackErr)
}
if oauthErr.Code != "access_denied" {
t.Fatal(oauthErr.Code)
}
resp := rec.Result()
defer func() {
if err := resp.Body.Close(); err != nil {
t.Error(err)
}
}()
if resp.StatusCode != http.StatusForbidden {
t.Fatal(resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(body), "oauth2 callback error: access_denied") {
t.Fatal(string(body))
}
state, _ := sess.Get(oauth2StateKey).(string)
if state != "" {
t.Fatal(state)
}
}
func Test_handleAuthResponseOAuthErrorCallbackReceivesSessionEmail(t *testing.T) {
jw, err := jaws.New()
if err != nil {
t.Fatal(err)
}
defer jw.Close()
srv := &Server{
Jaws: jw,
SessionKey: "oauth2userinfo",
SessionTokenKey: "oauth2token",
SessionEmailKey: "email",
SessionEmailVerifiedKey: "email_verified",
HandledPaths: map[string]struct{}{},
oauth2cfg: &oauth2.Config{
ClientID: "client",
Endpoint: oauth2.Endpoint{TokenURL: "https://provider.example/token"},
RedirectURL: "https://example.com/oauth2/callback",
},
userinfoUrl: "https://provider.example/userinfo",
}
var callbackEmail string
srv.LoginFailed = func(hw http.ResponseWriter, hr *http.Request, httpCode int, callErr error, email string) bool {
_ = hw
_ = hr
_ = httpCode
if !errors.Is(callErr, ErrOAuth2Callback) {
t.Fatal(callErr)
}
callbackEmail = email
return false
}
rec := httptest.NewRecorder()
req := httptest.NewRequest(
http.MethodGet,
"http://example.com/oauth2/callback?state=state123&error=access_denied",
nil,
)
sess := jw.NewSession(rec, req)
sess.Set(oauth2StateKey, "state123")
sess.Set(srv.SessionKey, map[string]any{"email": "user@example.com"})
sess.Set(srv.SessionEmailKey, "user@example.com")
srv.HandleAuthResponse(rec, req)
resp := rec.Result()
defer func() {
if err := resp.Body.Close(); err != nil {
t.Error(err)
}
}()
if resp.StatusCode != http.StatusForbidden {
t.Fatal(resp.Status)
}
if callbackEmail != "user@example.com" {
t.Fatal(callbackEmail)
}
if email, _ := sess.Get(srv.SessionEmailKey).(string); email != "user@example.com" {
t.Fatal(email)
}
}
func TestCallbackParametersPreserveProviderText(t *testing.T) {
values := url.Values{
"error": {"server_error"},
"error_description": {" Åtkomst nekad\r\nTrace ID: 123\t "},
"error_uri": {"https://provider.example/help\x1b"},
}
req := httptest.NewRequest(http.MethodGet, "/callback?"+values.Encode(), nil)
_, err := oauth2CallbackError(http.StatusBadRequest, req)
var callbackErr *OAuth2CallbackError
if !errors.As(err, &callbackErr) {
t.Fatal(err)
}
if callbackErr.Description != "Åtkomst nekad\r\nTrace ID: 123" || callbackErr.URI != "https://provider.example/help\x1b" {
t.Fatal(callbackErr)
}
if !strings.Contains(err.Error(), "\r\nTrace ID: 123") {
t.Fatal(err)
}
}