| 1 |
package op |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"log/slog" |
| 9 |
"net/http" |
| 10 |
"net/http/httptest" |
| 11 |
"net/url" |
| 12 |
"os" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/muhlemmer/gu" |
| 18 |
"github.com/stretchr/testify/assert" |
| 19 |
"github.com/stretchr/testify/require" |
| 20 |
httphelper "github.com/zitadel/oidc/v3/pkg/http" |
| 21 |
"github.com/zitadel/oidc/v3/pkg/oidc" |
| 22 |
"github.com/zitadel/schema" |
| 23 |
) |
| 24 |
|
| 25 |
func TestRegisterServer(t *testing.T) { |
| 26 |
previous := slog.Default() |
| 27 |
server := UnimplementedServer{} |
| 28 |
endpoints := Endpoints{ |
| 29 |
Authorization: &Endpoint{ |
| 30 |
path: "/auth", |
| 31 |
}, |
| 32 |
} |
| 33 |
decoder := schema.NewDecoder() |
| 34 |
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) |
| 35 |
|
| 36 |
h := RegisterServer(server, endpoints, |
| 37 |
WithDecoder(decoder), |
| 38 |
WithFallbackLogger(logger), |
| 39 |
WithFallbackLogger(nil), |
| 40 |
) |
| 41 |
got := h.(*webServer) |
| 42 |
assert.Equal(t, got.server, server) |
| 43 |
assert.Equal(t, got.endpoints, endpoints) |
| 44 |
assert.Equal(t, got.decoder, decoder) |
| 45 |
assert.Same(t, previous, slog.Default()) |
| 46 |
} |
| 47 |
|
| 48 |
type testClient struct { |
| 49 |
id string |
| 50 |
appType ApplicationType |
| 51 |
authMethod oidc.AuthMethod |
| 52 |
accessTokenType AccessTokenType |
| 53 |
responseTypes []oidc.ResponseType |
| 54 |
grantTypes []oidc.GrantType |
| 55 |
devMode bool |
| 56 |
} |
| 57 |
|
| 58 |
type clientType string |
| 59 |
|
| 60 |
const ( |
| 61 |
clientTypeWeb clientType = "web" |
| 62 |
clientTypeNative clientType = "native" |
| 63 |
clientTypeUserAgent clientType = "useragent" |
| 64 |
) |
| 65 |
|
| 66 |
func newClient(kind clientType) *testClient { |
| 67 |
client := &testClient{ |
| 68 |
id: string(kind), |
| 69 |
} |
| 70 |
|
| 71 |
switch kind { |
| 72 |
case clientTypeWeb: |
| 73 |
client.appType = ApplicationTypeWeb |
| 74 |
client.authMethod = oidc.AuthMethodBasic |
| 75 |
client.accessTokenType = AccessTokenTypeBearer |
| 76 |
client.responseTypes = []oidc.ResponseType{oidc.ResponseTypeCode} |
| 77 |
case clientTypeNative: |
| 78 |
client.appType = ApplicationTypeNative |
| 79 |
client.authMethod = oidc.AuthMethodNone |
| 80 |
client.accessTokenType = AccessTokenTypeBearer |
| 81 |
client.responseTypes = []oidc.ResponseType{oidc.ResponseTypeCode} |
| 82 |
case clientTypeUserAgent: |
| 83 |
client.appType = ApplicationTypeUserAgent |
| 84 |
client.authMethod = oidc.AuthMethodBasic |
| 85 |
client.accessTokenType = AccessTokenTypeJWT |
| 86 |
client.responseTypes = []oidc.ResponseType{oidc.ResponseTypeIDToken} |
| 87 |
default: |
| 88 |
panic(fmt.Errorf("invalid client type %s", kind)) |
| 89 |
} |
| 90 |
return client |
| 91 |
} |
| 92 |
|
| 93 |
func (c *testClient) RedirectURIs() []string { |
| 94 |
return []string{ |
| 95 |
"https://registered.com/callback", |
| 96 |
"http://registered.com/callback", |
| 97 |
"http://localhost:9999/callback", |
| 98 |
"custom://callback", |
| 99 |
// Survive one percent-decode unchanged, but not two. |
| 100 |
"https://registered.com/callback?p=a%2Fb", |
| 101 |
"https://registered.com/callback?a=b+c", |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func (c *testClient) PostLogoutRedirectURIs() []string { |
| 106 |
return []string{} |
| 107 |
} |
| 108 |
|
| 109 |
func (c *testClient) LoginURL(id string) string { |
| 110 |
return "login?id=" + id |
| 111 |
} |
| 112 |
|
| 113 |
func (c *testClient) ApplicationType() ApplicationType { |
| 114 |
return c.appType |
| 115 |
} |
| 116 |
|
| 117 |
func (c *testClient) AuthMethod() oidc.AuthMethod { |
| 118 |
return c.authMethod |
| 119 |
} |
| 120 |
|
| 121 |
func (c *testClient) GetID() string { |
| 122 |
return c.id |
| 123 |
} |
| 124 |
|
| 125 |
func (c *testClient) AccessTokenLifetime() time.Duration { |
| 126 |
return 5 * time.Minute |
| 127 |
} |
| 128 |
|
| 129 |
func (c *testClient) IDTokenLifetime() time.Duration { |
| 130 |
return 5 * time.Minute |
| 131 |
} |
| 132 |
|
| 133 |
func (c *testClient) AccessTokenType() AccessTokenType { |
| 134 |
return c.accessTokenType |
| 135 |
} |
| 136 |
|
| 137 |
func (c *testClient) ResponseTypes() []oidc.ResponseType { |
| 138 |
return c.responseTypes |
| 139 |
} |
| 140 |
|
| 141 |
func (c *testClient) GrantTypes() []oidc.GrantType { |
| 142 |
return c.grantTypes |
| 143 |
} |
| 144 |
|
| 145 |
func (c *testClient) DevMode() bool { |
| 146 |
return c.devMode |
| 147 |
} |
| 148 |
|
| 149 |
func (c *testClient) AllowedScopes() []string { |
| 150 |
return nil |
| 151 |
} |
| 152 |
|
| 153 |
func (c *testClient) RestrictAdditionalIdTokenScopes() func(scopes []string) []string { |
| 154 |
return func(scopes []string) []string { |
| 155 |
return scopes |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func (c *testClient) RestrictAdditionalAccessTokenScopes() func(scopes []string) []string { |
| 160 |
return func(scopes []string) []string { |
| 161 |
return scopes |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func (c *testClient) IsScopeAllowed(scope string) bool { |
| 166 |
return false |
| 167 |
} |
| 168 |
|
| 169 |
func (c *testClient) IDTokenUserinfoClaimsAssertion() bool { |
| 170 |
return false |
| 171 |
} |
| 172 |
|
| 173 |
func (c *testClient) ClockSkew() time.Duration { |
| 174 |
return 0 |
| 175 |
} |
| 176 |
|
| 177 |
type requestVerifier struct { |
| 178 |
UnimplementedServer |
| 179 |
client Client |
| 180 |
} |
| 181 |
|
| 182 |
func (s *requestVerifier) VerifyAuthRequest(ctx context.Context, r *Request[oidc.AuthRequest]) (*ClientRequest[oidc.AuthRequest], error) { |
| 183 |
if s.client == nil { |
| 184 |
return nil, oidc.ErrServerError() |
| 185 |
} |
| 186 |
return &ClientRequest[oidc.AuthRequest]{ |
| 187 |
Request: r, |
| 188 |
Client: s.client, |
| 189 |
}, nil |
| 190 |
} |
| 191 |
|
| 192 |
func (s *requestVerifier) VerifyClient(ctx context.Context, r *Request[ClientCredentials]) (Client, error) { |
| 193 |
if s.client == nil { |
| 194 |
return nil, oidc.ErrServerError() |
| 195 |
} |
| 196 |
return s.client, nil |
| 197 |
} |
| 198 |
|
| 199 |
// redirectingAuthorizer verifies like requestVerifier and then completes the |
| 200 |
// authorization request the way a real Server implementation would, so that a |
| 201 |
// test can tell successful validation apart from any error response. |
| 202 |
type redirectingAuthorizer struct { |
| 203 |
requestVerifier |
| 204 |
} |
| 205 |
|
| 206 |
func (s *redirectingAuthorizer) Authorize(ctx context.Context, r *ClientRequest[oidc.AuthRequest]) (*Redirect, error) { |
| 207 |
return NewRedirect(r.Client.LoginURL("authReqID")), nil |
| 208 |
} |
| 209 |
|
| 210 |
var testDecoder = func() *schema.Decoder { |
| 211 |
decoder := schema.NewDecoder() |
| 212 |
decoder.IgnoreUnknownKeys(true) |
| 213 |
return decoder |
| 214 |
}() |
| 215 |
|
| 216 |
type webServerResult struct { |
| 217 |
wantStatus int |
| 218 |
wantBody string |
| 219 |
} |
| 220 |
|
| 221 |
func runWebServerTest(t *testing.T, handler http.HandlerFunc, r *http.Request, want webServerResult) { |
| 222 |
t.Helper() |
| 223 |
if r.Method == http.MethodPost { |
| 224 |
r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 225 |
} |
| 226 |
w := httptest.NewRecorder() |
| 227 |
handler(w, r) |
| 228 |
res := w.Result() |
| 229 |
assert.Equal(t, want.wantStatus, res.StatusCode) |
| 230 |
body, err := io.ReadAll(res.Body) |
| 231 |
require.NoError(t, err) |
| 232 |
assert.JSONEq(t, want.wantBody, string(body)) |
| 233 |
} |
| 234 |
|
| 235 |
func Test_webServer_withClient(t *testing.T) { |
| 236 |
tests := []struct { |
| 237 |
name string |
| 238 |
r *http.Request |
| 239 |
want webServerResult |
| 240 |
}{ |
| 241 |
{ |
| 242 |
name: "parse error", |
| 243 |
r: httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(make([]byte, 11<<20))), |
| 244 |
want: webServerResult{ |
| 245 |
wantStatus: http.StatusBadRequest, |
| 246 |
wantBody: `{"error":"invalid_request", "error_description":"error parsing form"}`, |
| 247 |
}, |
| 248 |
}, |
| 249 |
{ |
| 250 |
name: "invalid grant type", |
| 251 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_id=native&grant_type=bad&foo=bar")), |
| 252 |
want: webServerResult{ |
| 253 |
wantStatus: http.StatusBadRequest, |
| 254 |
wantBody: `{"error":"unauthorized_client", "error_description":"grant_type \"bad\" not allowed"}`, |
| 255 |
}, |
| 256 |
}, |
| 257 |
{ |
| 258 |
name: "no grant type", |
| 259 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_id=native&foo=bar")), |
| 260 |
want: webServerResult{ |
| 261 |
wantStatus: http.StatusOK, |
| 262 |
wantBody: `{"foo":"bar"}`, |
| 263 |
}, |
| 264 |
}, |
| 265 |
} |
| 266 |
for _, tt := range tests { |
| 267 |
t.Run(tt.name, func(t *testing.T) { |
| 268 |
s := &webServer{ |
| 269 |
server: &requestVerifier{ |
| 270 |
client: newClient(clientTypeNative), |
| 271 |
}, |
| 272 |
decoder: testDecoder, |
| 273 |
} |
| 274 |
handler := func(w http.ResponseWriter, r *http.Request, client Client) { |
| 275 |
fmt.Fprintf(w, `{"foo":%q}`, r.FormValue("foo")) |
| 276 |
} |
| 277 |
runWebServerTest(t, s.withClient(handler), tt.r, tt.want) |
| 278 |
}) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
func Test_webServer_verifyRequestClient(t *testing.T) { |
| 283 |
tests := []struct { |
| 284 |
name string |
| 285 |
decoder httphelper.Decoder |
| 286 |
r *http.Request |
| 287 |
want Client |
| 288 |
wantErr error |
| 289 |
}{ |
| 290 |
{ |
| 291 |
name: "parse form error", |
| 292 |
decoder: testDecoder, |
| 293 |
r: httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(make([]byte, 11<<20))), |
| 294 |
wantErr: oidc.ErrInvalidRequest().WithDescription("error parsing form"), |
| 295 |
}, |
| 296 |
{ |
| 297 |
name: "decoder error", |
| 298 |
decoder: schema.NewDecoder(), |
| 299 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 300 |
wantErr: oidc.ErrInvalidRequest().WithDescription("error decoding form"), |
| 301 |
}, |
| 302 |
{ |
| 303 |
name: "basic auth, client_id error", |
| 304 |
decoder: testDecoder, |
| 305 |
r: func() *http.Request { |
| 306 |
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")) |
| 307 |
r.SetBasicAuth(`%%%`, "secret") |
| 308 |
return r |
| 309 |
}(), |
| 310 |
wantErr: oidc.ErrInvalidClient().WithDescription("invalid basic auth header"), |
| 311 |
}, |
| 312 |
{ |
| 313 |
name: "basic auth, client_secret error", |
| 314 |
decoder: testDecoder, |
| 315 |
r: func() *http.Request { |
| 316 |
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")) |
| 317 |
r.SetBasicAuth("web", `%%%`) |
| 318 |
return r |
| 319 |
}(), |
| 320 |
wantErr: oidc.ErrInvalidClient().WithDescription("invalid basic auth header"), |
| 321 |
}, |
| 322 |
{ |
| 323 |
name: "missing client id and assertion", |
| 324 |
decoder: testDecoder, |
| 325 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 326 |
wantErr: oidc.ErrInvalidRequest().WithDescription("client_id or client_assertion must be provided"), |
| 327 |
}, |
| 328 |
{ |
| 329 |
name: "wrong assertion type", |
| 330 |
decoder: testDecoder, |
| 331 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar&client_assertion=xxx&client_assertion_type=wrong")), |
| 332 |
wantErr: oidc.ErrInvalidRequest().WithDescription("invalid client_assertion_type wrong"), |
| 333 |
}, |
| 334 |
{ |
| 335 |
name: "unimplemented verify client called", |
| 336 |
decoder: testDecoder, |
| 337 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar&client_id=web")), |
| 338 |
wantErr: StatusError{ |
| 339 |
parent: oidc.ErrServerError().WithDescription("/ not implemented on this server"), |
| 340 |
statusCode: UnimplementedStatusCode, |
| 341 |
}, |
| 342 |
}, |
| 343 |
{ |
| 344 |
// RFC 6749 §2.3: The client MUST NOT use more than one authentication method in each request. |
| 345 |
name: "basic auth and client_assertion", |
| 346 |
decoder: testDecoder, |
| 347 |
r: func() *http.Request { |
| 348 |
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_assertion=xxx&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer")) |
| 349 |
r.SetBasicAuth("web", "secret") |
| 350 |
return r |
| 351 |
}(), |
| 352 |
wantErr: oidc.ErrInvalidRequest().WithDescription("client authentication must not use more than one method"), |
| 353 |
}, |
| 354 |
{ |
| 355 |
// RFC 6749 §2.3: The client MUST NOT use more than one authentication method in each request. |
| 356 |
name: "basic auth and client_secret in body", |
| 357 |
decoder: testDecoder, |
| 358 |
r: func() *http.Request { |
| 359 |
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_id=web&client_secret=secret")) |
| 360 |
r.SetBasicAuth("web", "secret") |
| 361 |
return r |
| 362 |
}(), |
| 363 |
wantErr: oidc.ErrInvalidRequest().WithDescription("client authentication must not use more than one method"), |
| 364 |
}, |
| 365 |
{ |
| 366 |
// RFC 6749 §2.3: The client MUST NOT use more than one authentication method in each request. |
| 367 |
name: "client_secret and client_assertion", |
| 368 |
decoder: testDecoder, |
| 369 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_id=web&client_secret=secret&client_assertion=xxx&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer")), |
| 370 |
wantErr: oidc.ErrInvalidRequest().WithDescription("client authentication must not use more than one method"), |
| 371 |
}, |
| 372 |
} |
| 373 |
for _, tt := range tests { |
| 374 |
t.Run(tt.name, func(t *testing.T) { |
| 375 |
s := &webServer{ |
| 376 |
server: UnimplementedServer{}, |
| 377 |
decoder: tt.decoder, |
| 378 |
} |
| 379 |
tt.r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 380 |
got, err := s.verifyRequestClient(tt.r) |
| 381 |
require.ErrorIs(t, err, tt.wantErr) |
| 382 |
assert.Equal(t, tt.want, got) |
| 383 |
}) |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
func Test_webServer_authorizeHandler(t *testing.T) { |
| 388 |
type fields struct { |
| 389 |
server Server |
| 390 |
decoder httphelper.Decoder |
| 391 |
} |
| 392 |
tests := []struct { |
| 393 |
name string |
| 394 |
fields fields |
| 395 |
r *http.Request |
| 396 |
want webServerResult |
| 397 |
}{ |
| 398 |
{ |
| 399 |
name: "decoder error", |
| 400 |
fields: fields{ |
| 401 |
server: &requestVerifier{}, |
| 402 |
decoder: schema.NewDecoder(), |
| 403 |
}, |
| 404 |
r: httptest.NewRequest(http.MethodPost, "/authorize", strings.NewReader("foo=bar")), |
| 405 |
want: webServerResult{ |
| 406 |
wantStatus: http.StatusBadRequest, |
| 407 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 408 |
}, |
| 409 |
}, |
| 410 |
{ |
| 411 |
name: "server error", |
| 412 |
fields: fields{ |
| 413 |
server: &requestVerifier{}, |
| 414 |
decoder: testDecoder, |
| 415 |
}, |
| 416 |
r: httptest.NewRequest(http.MethodPost, "/authorize", strings.NewReader("foo=bar")), |
| 417 |
want: webServerResult{ |
| 418 |
wantStatus: http.StatusInternalServerError, |
| 419 |
wantBody: `{"error":"server_error"}`, |
| 420 |
}, |
| 421 |
}, |
| 422 |
} |
| 423 |
for _, tt := range tests { |
| 424 |
t.Run(tt.name, func(t *testing.T) { |
| 425 |
s := &webServer{ |
| 426 |
server: tt.fields.server, |
| 427 |
decoder: tt.fields.decoder, |
| 428 |
} |
| 429 |
runWebServerTest(t, s.authorizeHandler, tt.r, tt.want) |
| 430 |
}) |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
// Test_webServer_authorizeHandler_encodedRedirectURI drives the authorization |
| 435 |
// endpoint over HTTP so that r.ParseForm takes part in the decode. That is the |
| 436 |
// only way to observe a redirect_uri being decoded a second time inside |
| 437 |
// ValidateAuthReqRedirectURI: a registered URI containing "+" or a percent |
| 438 |
// escape comes out of ParseForm exactly as registered, but a further decode |
| 439 |
// alters it and it no longer matches the client configuration. |
| 440 |
// Used to answer 400 invalid_request, see issue #968. |
| 441 |
func Test_webServer_authorizeHandler_encodedRedirectURI(t *testing.T) { |
| 442 |
client := newClient(clientTypeWeb) |
| 443 |
for _, redirectURI := range []string{ |
| 444 |
"https://registered.com/callback?p=a%2Fb", |
| 445 |
"https://registered.com/callback?a=b+c", |
| 446 |
} { |
| 447 |
t.Run(redirectURI, func(t *testing.T) { |
| 448 |
// url.Values.Encode is the encoding every OAuth client applies, |
| 449 |
// golang.org/x/oauth2 included: once, over the raw redirect_uri. |
| 450 |
query := url.Values{ |
| 451 |
"client_id": []string{client.GetID()}, |
| 452 |
"response_type": []string{string(oidc.ResponseTypeCode)}, |
| 453 |
"scope": []string{"openid"}, |
| 454 |
"redirect_uri": []string{redirectURI}, |
| 455 |
}.Encode() |
| 456 |
|
| 457 |
s := &webServer{ |
| 458 |
server: &redirectingAuthorizer{requestVerifier{client: client}}, |
| 459 |
decoder: testDecoder, |
| 460 |
} |
| 461 |
w := httptest.NewRecorder() |
| 462 |
s.authorizeHandler(w, httptest.NewRequest(http.MethodGet, "/authorize?"+query, nil)) |
| 463 |
|
| 464 |
res := w.Result() |
| 465 |
body, err := io.ReadAll(res.Body) |
| 466 |
require.NoError(t, err) |
| 467 |
require.Equal(t, http.StatusFound, res.StatusCode, "body: %s", body) |
| 468 |
// http.Redirect resolves the client's relative login URL |
| 469 |
// against the request path, "/authorize". |
| 470 |
assert.Equal(t, "/"+client.LoginURL("authReqID"), res.Header.Get("Location")) |
| 471 |
}) |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
func Test_webServer_authorize(t *testing.T) { |
| 476 |
type args struct { |
| 477 |
ctx context.Context |
| 478 |
r *Request[oidc.AuthRequest] |
| 479 |
} |
| 480 |
tests := []struct { |
| 481 |
name string |
| 482 |
server Server |
| 483 |
args args |
| 484 |
want *Redirect |
| 485 |
wantErr error |
| 486 |
}{ |
| 487 |
{ |
| 488 |
name: "verify error", |
| 489 |
server: &requestVerifier{}, |
| 490 |
args: args{ |
| 491 |
ctx: context.Background(), |
| 492 |
r: &Request[oidc.AuthRequest]{ |
| 493 |
Data: &oidc.AuthRequest{ |
| 494 |
Scopes: oidc.SpaceDelimitedArray{"openid"}, |
| 495 |
ResponseType: oidc.ResponseTypeCode, |
| 496 |
ClientID: "web", |
| 497 |
RedirectURI: "https://registered.com/callback", |
| 498 |
MaxAge: gu.Ptr[uint](300), |
| 499 |
}, |
| 500 |
}, |
| 501 |
}, |
| 502 |
wantErr: oidc.ErrServerError(), |
| 503 |
}, |
| 504 |
{ |
| 505 |
name: "missing redirect", |
| 506 |
server: &requestVerifier{ |
| 507 |
client: newClient(clientTypeWeb), |
| 508 |
}, |
| 509 |
args: args{ |
| 510 |
ctx: context.Background(), |
| 511 |
r: &Request[oidc.AuthRequest]{ |
| 512 |
Data: &oidc.AuthRequest{ |
| 513 |
Scopes: oidc.SpaceDelimitedArray{"openid"}, |
| 514 |
ResponseType: oidc.ResponseTypeCode, |
| 515 |
ClientID: "web", |
| 516 |
MaxAge: gu.Ptr[uint](300), |
| 517 |
}, |
| 518 |
}, |
| 519 |
}, |
| 520 |
wantErr: ErrAuthReqMissingRedirectURI, |
| 521 |
}, |
| 522 |
{ |
| 523 |
name: "invalid prompt", |
| 524 |
server: &requestVerifier{ |
| 525 |
client: newClient(clientTypeWeb), |
| 526 |
}, |
| 527 |
args: args{ |
| 528 |
ctx: context.Background(), |
| 529 |
r: &Request[oidc.AuthRequest]{ |
| 530 |
Data: &oidc.AuthRequest{ |
| 531 |
Scopes: oidc.SpaceDelimitedArray{"openid"}, |
| 532 |
ResponseType: oidc.ResponseTypeCode, |
| 533 |
ClientID: "web", |
| 534 |
RedirectURI: "https://registered.com/callback", |
| 535 |
MaxAge: gu.Ptr[uint](300), |
| 536 |
Prompt: []string{oidc.PromptNone, oidc.PromptLogin}, |
| 537 |
}, |
| 538 |
}, |
| 539 |
}, |
| 540 |
wantErr: oidc.ErrInvalidRequest().WithDescription("The prompt parameter `none` must only be used as a single value"), |
| 541 |
}, |
| 542 |
{ |
| 543 |
name: "missing scopes", |
| 544 |
server: &requestVerifier{ |
| 545 |
client: newClient(clientTypeWeb), |
| 546 |
}, |
| 547 |
args: args{ |
| 548 |
ctx: context.Background(), |
| 549 |
r: &Request[oidc.AuthRequest]{ |
| 550 |
Data: &oidc.AuthRequest{ |
| 551 |
ResponseType: oidc.ResponseTypeCode, |
| 552 |
ClientID: "web", |
| 553 |
RedirectURI: "https://registered.com/callback", |
| 554 |
MaxAge: gu.Ptr[uint](300), |
| 555 |
Prompt: []string{oidc.PromptNone}, |
| 556 |
}, |
| 557 |
}, |
| 558 |
}, |
| 559 |
wantErr: oidc.ErrInvalidRequest(). |
| 560 |
WithDescription("The scope of your request is missing. Please ensure some scopes are requested. " + |
| 561 |
"If you have any questions, you may contact the administrator of the application."), |
| 562 |
}, |
| 563 |
{ |
| 564 |
name: "invalid redirect", |
| 565 |
server: &requestVerifier{ |
| 566 |
client: newClient(clientTypeWeb), |
| 567 |
}, |
| 568 |
args: args{ |
| 569 |
ctx: context.Background(), |
| 570 |
r: &Request[oidc.AuthRequest]{ |
| 571 |
Data: &oidc.AuthRequest{ |
| 572 |
Scopes: oidc.SpaceDelimitedArray{"openid"}, |
| 573 |
ResponseType: oidc.ResponseTypeCode, |
| 574 |
ClientID: "web", |
| 575 |
RedirectURI: "https://example.com/callback", |
| 576 |
MaxAge: gu.Ptr[uint](300), |
| 577 |
Prompt: []string{oidc.PromptNone}, |
| 578 |
}, |
| 579 |
}, |
| 580 |
}, |
| 581 |
wantErr: oidc.ErrInvalidRequestRedirectURI(). |
| 582 |
WithDescription("The requested redirect_uri is missing in the client configuration. " + |
| 583 |
"If you have any questions, you may contact the administrator of the application."), |
| 584 |
}, |
| 585 |
{ |
| 586 |
name: "invalid response type", |
| 587 |
server: &requestVerifier{ |
| 588 |
client: newClient(clientTypeWeb), |
| 589 |
}, |
| 590 |
args: args{ |
| 591 |
ctx: context.Background(), |
| 592 |
r: &Request[oidc.AuthRequest]{ |
| 593 |
Data: &oidc.AuthRequest{ |
| 594 |
Scopes: oidc.SpaceDelimitedArray{"openid"}, |
| 595 |
ResponseType: oidc.ResponseTypeIDToken, |
| 596 |
ClientID: "web", |
| 597 |
RedirectURI: "https://registered.com/callback", |
| 598 |
MaxAge: gu.Ptr[uint](300), |
| 599 |
Prompt: []string{oidc.PromptNone}, |
| 600 |
}, |
| 601 |
}, |
| 602 |
}, |
| 603 |
wantErr: oidc.ErrUnauthorizedClient().WithDescription("The requested response type is missing in the client configuration. " + |
| 604 |
"If you have any questions, you may contact the administrator of the application."), |
| 605 |
}, |
| 606 |
{ |
| 607 |
name: "unimplemented Authorize called", |
| 608 |
server: &requestVerifier{ |
| 609 |
client: newClient(clientTypeWeb), |
| 610 |
}, |
| 611 |
args: args{ |
| 612 |
ctx: context.Background(), |
| 613 |
r: &Request[oidc.AuthRequest]{ |
| 614 |
URL: &url.URL{ |
| 615 |
Path: "/authorize", |
| 616 |
}, |
| 617 |
Data: &oidc.AuthRequest{ |
| 618 |
Scopes: oidc.SpaceDelimitedArray{"openid"}, |
| 619 |
ResponseType: oidc.ResponseTypeCode, |
| 620 |
ClientID: "web", |
| 621 |
RedirectURI: "https://registered.com/callback", |
| 622 |
MaxAge: gu.Ptr[uint](300), |
| 623 |
Prompt: []string{oidc.PromptNone}, |
| 624 |
}, |
| 625 |
}, |
| 626 |
}, |
| 627 |
wantErr: StatusError{ |
| 628 |
parent: oidc.ErrServerError().WithDescription("/authorize not implemented on this server"), |
| 629 |
statusCode: UnimplementedStatusCode, |
| 630 |
}, |
| 631 |
}, |
| 632 |
} |
| 633 |
for _, tt := range tests { |
| 634 |
t.Run(tt.name, func(t *testing.T) { |
| 635 |
s := &webServer{ |
| 636 |
server: tt.server, |
| 637 |
decoder: testDecoder, |
| 638 |
} |
| 639 |
got, err := s.authorize(tt.args.ctx, tt.args.r) |
| 640 |
require.ErrorIs(t, err, tt.wantErr) |
| 641 |
assert.Equal(t, tt.want, got) |
| 642 |
}) |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
func Test_webServer_deviceAuthorizationHandler(t *testing.T) { |
| 647 |
type fields struct { |
| 648 |
server Server |
| 649 |
decoder httphelper.Decoder |
| 650 |
} |
| 651 |
tests := []struct { |
| 652 |
name string |
| 653 |
fields fields |
| 654 |
r *http.Request |
| 655 |
want webServerResult |
| 656 |
}{ |
| 657 |
{ |
| 658 |
name: "decoder error", |
| 659 |
fields: fields{ |
| 660 |
server: &requestVerifier{}, |
| 661 |
decoder: schema.NewDecoder(), |
| 662 |
}, |
| 663 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 664 |
want: webServerResult{ |
| 665 |
wantStatus: http.StatusBadRequest, |
| 666 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 667 |
}, |
| 668 |
}, |
| 669 |
{ |
| 670 |
name: "unimplemented DeviceAuthorization called", |
| 671 |
fields: fields{ |
| 672 |
server: &requestVerifier{ |
| 673 |
client: newClient(clientTypeNative), |
| 674 |
}, |
| 675 |
decoder: testDecoder, |
| 676 |
}, |
| 677 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_id=native_client")), |
| 678 |
want: webServerResult{ |
| 679 |
wantStatus: UnimplementedStatusCode, |
| 680 |
wantBody: `{"error":"server_error", "error_description":"/ not implemented on this server"}`, |
| 681 |
}, |
| 682 |
}, |
| 683 |
} |
| 684 |
for _, tt := range tests { |
| 685 |
t.Run(tt.name, func(t *testing.T) { |
| 686 |
s := &webServer{ |
| 687 |
server: tt.fields.server, |
| 688 |
decoder: tt.fields.decoder, |
| 689 |
} |
| 690 |
client := newClient(clientTypeUserAgent) |
| 691 |
runWebServerClientTest(t, s.deviceAuthorizationHandler, tt.r, client, tt.want) |
| 692 |
}) |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
func Test_webServer_tokensHandler(t *testing.T) { |
| 697 |
tests := []struct { |
| 698 |
name string |
| 699 |
r *http.Request |
| 700 |
want webServerResult |
| 701 |
}{ |
| 702 |
{ |
| 703 |
name: "parse form error", |
| 704 |
r: httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(make([]byte, 11<<20))), |
| 705 |
want: webServerResult{ |
| 706 |
wantStatus: http.StatusBadRequest, |
| 707 |
wantBody: `{"error":"invalid_request", "error_description":"error parsing form"}`, |
| 708 |
}, |
| 709 |
}, |
| 710 |
{ |
| 711 |
name: "missing grant type", |
| 712 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 713 |
want: webServerResult{ |
| 714 |
wantStatus: http.StatusBadRequest, |
| 715 |
wantBody: `{"error":"invalid_request", "error_description":"grant_type missing"}`, |
| 716 |
}, |
| 717 |
}, |
| 718 |
{ |
| 719 |
name: "invalid grant type", |
| 720 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("grant_type=bar")), |
| 721 |
want: webServerResult{ |
| 722 |
wantStatus: http.StatusBadRequest, |
| 723 |
wantBody: `{"error":"unsupported_grant_type", "error_description":"bar not supported"}`, |
| 724 |
}, |
| 725 |
}, |
| 726 |
} |
| 727 |
for _, tt := range tests { |
| 728 |
t.Run(tt.name, func(t *testing.T) { |
| 729 |
s := &webServer{} |
| 730 |
runWebServerTest(t, s.tokensHandler, tt.r, tt.want) |
| 731 |
}) |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
func Test_webServer_jwtProfileHandler(t *testing.T) { |
| 736 |
tests := []struct { |
| 737 |
name string |
| 738 |
decoder httphelper.Decoder |
| 739 |
r *http.Request |
| 740 |
want webServerResult |
| 741 |
}{ |
| 742 |
{ |
| 743 |
name: "decoder error", |
| 744 |
decoder: schema.NewDecoder(), |
| 745 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 746 |
want: webServerResult{ |
| 747 |
wantStatus: http.StatusBadRequest, |
| 748 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 749 |
}, |
| 750 |
}, |
| 751 |
{ |
| 752 |
name: "assertion missing", |
| 753 |
decoder: testDecoder, |
| 754 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 755 |
want: webServerResult{ |
| 756 |
wantStatus: http.StatusBadRequest, |
| 757 |
wantBody: `{"error":"invalid_request", "error_description":"assertion missing"}`, |
| 758 |
}, |
| 759 |
}, |
| 760 |
{ |
| 761 |
name: "unimplemented JWTProfile called", |
| 762 |
decoder: testDecoder, |
| 763 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("assertion=bar")), |
| 764 |
want: webServerResult{ |
| 765 |
wantStatus: http.StatusBadRequest, |
| 766 |
wantBody: `{"error":"unsupported_grant_type", "error_description":"urn:ietf:params:oauth:grant-type:jwt-bearer not supported"}`, |
| 767 |
}, |
| 768 |
}, |
| 769 |
} |
| 770 |
for _, tt := range tests { |
| 771 |
t.Run(tt.name, func(t *testing.T) { |
| 772 |
s := &webServer{ |
| 773 |
server: UnimplementedServer{}, |
| 774 |
decoder: tt.decoder, |
| 775 |
} |
| 776 |
runWebServerTest(t, s.jwtProfileHandler, tt.r, tt.want) |
| 777 |
}) |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
func runWebServerClientTest(t *testing.T, handler func(http.ResponseWriter, *http.Request, Client), r *http.Request, client Client, want webServerResult) { |
| 782 |
t.Helper() |
| 783 |
runWebServerTest(t, func(client Client) http.HandlerFunc { |
| 784 |
return func(w http.ResponseWriter, r *http.Request) { |
| 785 |
handler(w, r, client) |
| 786 |
} |
| 787 |
}(client), r, want) |
| 788 |
} |
| 789 |
|
| 790 |
func Test_webServer_codeExchangeHandler(t *testing.T) { |
| 791 |
tests := []struct { |
| 792 |
name string |
| 793 |
decoder httphelper.Decoder |
| 794 |
r *http.Request |
| 795 |
want webServerResult |
| 796 |
}{ |
| 797 |
{ |
| 798 |
name: "decoder error", |
| 799 |
decoder: schema.NewDecoder(), |
| 800 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 801 |
want: webServerResult{ |
| 802 |
wantStatus: http.StatusBadRequest, |
| 803 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 804 |
}, |
| 805 |
}, |
| 806 |
{ |
| 807 |
name: "code missing", |
| 808 |
decoder: testDecoder, |
| 809 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 810 |
want: webServerResult{ |
| 811 |
wantStatus: http.StatusBadRequest, |
| 812 |
wantBody: `{"error":"invalid_request", "error_description":"code missing"}`, |
| 813 |
}, |
| 814 |
}, |
| 815 |
{ |
| 816 |
name: "redirect missing", |
| 817 |
decoder: testDecoder, |
| 818 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("code=123")), |
| 819 |
want: webServerResult{ |
| 820 |
wantStatus: http.StatusBadRequest, |
| 821 |
wantBody: `{"error":"invalid_request", "error_description":"redirect_uri missing"}`, |
| 822 |
}, |
| 823 |
}, |
| 824 |
{ |
| 825 |
name: "unimplemented CodeExchange called", |
| 826 |
decoder: testDecoder, |
| 827 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("code=123&redirect_uri=https://example.com/callback")), |
| 828 |
want: webServerResult{ |
| 829 |
wantStatus: http.StatusBadRequest, |
| 830 |
wantBody: `{"error":"unsupported_grant_type", "error_description":"authorization_code not supported"}`, |
| 831 |
}, |
| 832 |
}, |
| 833 |
} |
| 834 |
for _, tt := range tests { |
| 835 |
t.Run(tt.name, func(t *testing.T) { |
| 836 |
s := &webServer{ |
| 837 |
server: UnimplementedServer{}, |
| 838 |
decoder: tt.decoder, |
| 839 |
} |
| 840 |
client := newClient(clientTypeUserAgent) |
| 841 |
runWebServerClientTest(t, s.codeExchangeHandler, tt.r, client, tt.want) |
| 842 |
}) |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
func Test_webServer_refreshTokenHandler(t *testing.T) { |
| 847 |
tests := []struct { |
| 848 |
name string |
| 849 |
decoder httphelper.Decoder |
| 850 |
r *http.Request |
| 851 |
want webServerResult |
| 852 |
}{ |
| 853 |
{ |
| 854 |
name: "decoder error", |
| 855 |
decoder: schema.NewDecoder(), |
| 856 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 857 |
want: webServerResult{ |
| 858 |
wantStatus: http.StatusBadRequest, |
| 859 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 860 |
}, |
| 861 |
}, |
| 862 |
{ |
| 863 |
name: "refresh token missing", |
| 864 |
decoder: testDecoder, |
| 865 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 866 |
want: webServerResult{ |
| 867 |
wantStatus: http.StatusBadRequest, |
| 868 |
wantBody: `{"error":"invalid_request", "error_description":"refresh_token missing"}`, |
| 869 |
}, |
| 870 |
}, |
| 871 |
{ |
| 872 |
name: "unimplemented RefreshToken called", |
| 873 |
decoder: testDecoder, |
| 874 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("refresh_token=xxx")), |
| 875 |
want: webServerResult{ |
| 876 |
wantStatus: http.StatusBadRequest, |
| 877 |
wantBody: `{"error":"unsupported_grant_type", "error_description":"refresh_token not supported"}`, |
| 878 |
}, |
| 879 |
}, |
| 880 |
} |
| 881 |
for _, tt := range tests { |
| 882 |
t.Run(tt.name, func(t *testing.T) { |
| 883 |
s := &webServer{ |
| 884 |
server: UnimplementedServer{}, |
| 885 |
decoder: tt.decoder, |
| 886 |
} |
| 887 |
client := newClient(clientTypeUserAgent) |
| 888 |
runWebServerClientTest(t, s.refreshTokenHandler, tt.r, client, tt.want) |
| 889 |
}) |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
func Test_webServer_tokenExchangeHandler(t *testing.T) { |
| 894 |
tests := []struct { |
| 895 |
name string |
| 896 |
decoder httphelper.Decoder |
| 897 |
r *http.Request |
| 898 |
want webServerResult |
| 899 |
}{ |
| 900 |
{ |
| 901 |
name: "decoder error", |
| 902 |
decoder: schema.NewDecoder(), |
| 903 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 904 |
want: webServerResult{ |
| 905 |
wantStatus: http.StatusBadRequest, |
| 906 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 907 |
}, |
| 908 |
}, |
| 909 |
{ |
| 910 |
name: "subject token missing", |
| 911 |
decoder: testDecoder, |
| 912 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 913 |
want: webServerResult{ |
| 914 |
wantStatus: http.StatusBadRequest, |
| 915 |
wantBody: `{"error":"invalid_request", "error_description":"subject_token missing"}`, |
| 916 |
}, |
| 917 |
}, |
| 918 |
{ |
| 919 |
name: "subject token type missing", |
| 920 |
decoder: testDecoder, |
| 921 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("subject_token=xxx")), |
| 922 |
want: webServerResult{ |
| 923 |
wantStatus: http.StatusBadRequest, |
| 924 |
wantBody: `{"error":"invalid_request", "error_description":"subject_token_type missing"}`, |
| 925 |
}, |
| 926 |
}, |
| 927 |
{ |
| 928 |
name: "subject token type unsupported", |
| 929 |
decoder: testDecoder, |
| 930 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("subject_token=xxx&subject_token_type=foo")), |
| 931 |
want: webServerResult{ |
| 932 |
wantStatus: http.StatusBadRequest, |
| 933 |
wantBody: `{"error":"invalid_request", "error_description":"subject_token_type is not supported"}`, |
| 934 |
}, |
| 935 |
}, |
| 936 |
{ |
| 937 |
name: "unsupported requested token type", |
| 938 |
decoder: testDecoder, |
| 939 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("subject_token=xxx&subject_token_type=urn:ietf:params:oauth:token-type:access_token&requested_token_type=foo")), |
| 940 |
want: webServerResult{ |
| 941 |
wantStatus: http.StatusBadRequest, |
| 942 |
wantBody: `{"error":"invalid_request", "error_description":"requested_token_type is not supported"}`, |
| 943 |
}, |
| 944 |
}, |
| 945 |
{ |
| 946 |
name: "unsupported actor token type", |
| 947 |
decoder: testDecoder, |
| 948 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("subject_token=xxx&subject_token_type=urn:ietf:params:oauth:token-type:access_token&requested_token_type=urn:ietf:params:oauth:token-type:access_token&actor_token_type=foo")), |
| 949 |
want: webServerResult{ |
| 950 |
wantStatus: http.StatusBadRequest, |
| 951 |
wantBody: `{"error":"invalid_request", "error_description":"actor_token_type is not supported"}`, |
| 952 |
}, |
| 953 |
}, |
| 954 |
{ |
| 955 |
name: "unimplemented TokenExchange called", |
| 956 |
decoder: testDecoder, |
| 957 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("subject_token=xxx&subject_token_type=urn:ietf:params:oauth:token-type:access_token&requested_token_type=urn:ietf:params:oauth:token-type:access_token&actor_token_type=urn:ietf:params:oauth:token-type:access_token")), |
| 958 |
want: webServerResult{ |
| 959 |
wantStatus: http.StatusBadRequest, |
| 960 |
wantBody: `{"error":"unsupported_grant_type", "error_description":"urn:ietf:params:oauth:grant-type:token-exchange not supported"}`, |
| 961 |
}, |
| 962 |
}, |
| 963 |
} |
| 964 |
for _, tt := range tests { |
| 965 |
t.Run(tt.name, func(t *testing.T) { |
| 966 |
s := &webServer{ |
| 967 |
server: UnimplementedServer{}, |
| 968 |
decoder: tt.decoder, |
| 969 |
} |
| 970 |
client := newClient(clientTypeUserAgent) |
| 971 |
runWebServerClientTest(t, s.tokenExchangeHandler, tt.r, client, tt.want) |
| 972 |
}) |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
func Test_webServer_clientCredentialsHandler(t *testing.T) { |
| 977 |
tests := []struct { |
| 978 |
name string |
| 979 |
decoder httphelper.Decoder |
| 980 |
client Client |
| 981 |
r *http.Request |
| 982 |
want webServerResult |
| 983 |
}{ |
| 984 |
{ |
| 985 |
name: "decoder error", |
| 986 |
decoder: schema.NewDecoder(), |
| 987 |
client: newClient(clientTypeUserAgent), |
| 988 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 989 |
want: webServerResult{ |
| 990 |
wantStatus: http.StatusBadRequest, |
| 991 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 992 |
}, |
| 993 |
}, |
| 994 |
{ |
| 995 |
name: "public client", |
| 996 |
decoder: testDecoder, |
| 997 |
client: newClient(clientTypeNative), |
| 998 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 999 |
want: webServerResult{ |
| 1000 |
wantStatus: http.StatusBadRequest, |
| 1001 |
wantBody: `{"error":"invalid_client", "error_description":"client must be authenticated"}`, |
| 1002 |
}, |
| 1003 |
}, |
| 1004 |
{ |
| 1005 |
name: "unimplemented ClientCredentialsExchange called", |
| 1006 |
decoder: testDecoder, |
| 1007 |
client: newClient(clientTypeUserAgent), |
| 1008 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 1009 |
want: webServerResult{ |
| 1010 |
wantStatus: http.StatusBadRequest, |
| 1011 |
wantBody: `{"error":"unsupported_grant_type", "error_description":"client_credentials not supported"}`, |
| 1012 |
}, |
| 1013 |
}, |
| 1014 |
} |
| 1015 |
for _, tt := range tests { |
| 1016 |
t.Run(tt.name, func(t *testing.T) { |
| 1017 |
s := &webServer{ |
| 1018 |
server: UnimplementedServer{}, |
| 1019 |
decoder: tt.decoder, |
| 1020 |
} |
| 1021 |
runWebServerClientTest(t, s.clientCredentialsHandler, tt.r, tt.client, tt.want) |
| 1022 |
}) |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
func Test_webServer_deviceTokenHandler(t *testing.T) { |
| 1027 |
tests := []struct { |
| 1028 |
name string |
| 1029 |
decoder httphelper.Decoder |
| 1030 |
r *http.Request |
| 1031 |
want webServerResult |
| 1032 |
}{ |
| 1033 |
{ |
| 1034 |
name: "decoder error", |
| 1035 |
decoder: schema.NewDecoder(), |
| 1036 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 1037 |
want: webServerResult{ |
| 1038 |
wantStatus: http.StatusBadRequest, |
| 1039 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 1040 |
}, |
| 1041 |
}, |
| 1042 |
{ |
| 1043 |
name: "device code missing", |
| 1044 |
decoder: testDecoder, |
| 1045 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 1046 |
want: webServerResult{ |
| 1047 |
wantStatus: http.StatusBadRequest, |
| 1048 |
wantBody: `{"error":"invalid_request", "error_description":"device_code missing"}`, |
| 1049 |
}, |
| 1050 |
}, |
| 1051 |
{ |
| 1052 |
name: "unimplemented DeviceToken called", |
| 1053 |
decoder: testDecoder, |
| 1054 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("device_code=xxx")), |
| 1055 |
want: webServerResult{ |
| 1056 |
wantStatus: http.StatusBadRequest, |
| 1057 |
wantBody: `{"error":"unsupported_grant_type", "error_description":"urn:ietf:params:oauth:grant-type:device_code not supported"}`, |
| 1058 |
}, |
| 1059 |
}, |
| 1060 |
} |
| 1061 |
for _, tt := range tests { |
| 1062 |
t.Run(tt.name, func(t *testing.T) { |
| 1063 |
s := &webServer{ |
| 1064 |
server: UnimplementedServer{}, |
| 1065 |
decoder: tt.decoder, |
| 1066 |
} |
| 1067 |
client := newClient(clientTypeUserAgent) |
| 1068 |
runWebServerClientTest(t, s.deviceTokenHandler, tt.r, client, tt.want) |
| 1069 |
}) |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
func Test_webServer_introspectionHandler(t *testing.T) { |
| 1074 |
tests := []struct { |
| 1075 |
name string |
| 1076 |
decoder httphelper.Decoder |
| 1077 |
r *http.Request |
| 1078 |
want webServerResult |
| 1079 |
}{ |
| 1080 |
{ |
| 1081 |
name: "decoder error", |
| 1082 |
decoder: schema.NewDecoder(), |
| 1083 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 1084 |
want: webServerResult{ |
| 1085 |
wantStatus: http.StatusBadRequest, |
| 1086 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 1087 |
}, |
| 1088 |
}, |
| 1089 |
{ |
| 1090 |
name: "public client", |
| 1091 |
decoder: testDecoder, |
| 1092 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_id=123")), |
| 1093 |
want: webServerResult{ |
| 1094 |
wantStatus: http.StatusBadRequest, |
| 1095 |
wantBody: `{"error":"invalid_client", "error_description":"client must be authenticated"}`, |
| 1096 |
}, |
| 1097 |
}, |
| 1098 |
{ |
| 1099 |
name: "token missing", |
| 1100 |
decoder: testDecoder, |
| 1101 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_id=123&client_secret=SECRET")), |
| 1102 |
want: webServerResult{ |
| 1103 |
wantStatus: http.StatusBadRequest, |
| 1104 |
wantBody: `{"error":"invalid_request", "error_description":"token missing"}`, |
| 1105 |
}, |
| 1106 |
}, |
| 1107 |
{ |
| 1108 |
name: "unimplemented Introspect called", |
| 1109 |
decoder: testDecoder, |
| 1110 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("client_id=123&client_secret=SECRET&token=xxx")), |
| 1111 |
want: webServerResult{ |
| 1112 |
wantStatus: UnimplementedStatusCode, |
| 1113 |
wantBody: `{"error":"server_error", "error_description":"/ not implemented on this server"}`, |
| 1114 |
}, |
| 1115 |
}, |
| 1116 |
} |
| 1117 |
for _, tt := range tests { |
| 1118 |
t.Run(tt.name, func(t *testing.T) { |
| 1119 |
s := &webServer{ |
| 1120 |
server: UnimplementedServer{}, |
| 1121 |
decoder: tt.decoder, |
| 1122 |
} |
| 1123 |
runWebServerTest(t, s.introspectionHandler, tt.r, tt.want) |
| 1124 |
}) |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
func Test_webServer_userInfoHandler(t *testing.T) { |
| 1129 |
tests := []struct { |
| 1130 |
name string |
| 1131 |
decoder httphelper.Decoder |
| 1132 |
r *http.Request |
| 1133 |
want webServerResult |
| 1134 |
}{ |
| 1135 |
{ |
| 1136 |
name: "decoder error", |
| 1137 |
decoder: schema.NewDecoder(), |
| 1138 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 1139 |
want: webServerResult{ |
| 1140 |
wantStatus: http.StatusBadRequest, |
| 1141 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 1142 |
}, |
| 1143 |
}, |
| 1144 |
{ |
| 1145 |
name: "access token missing", |
| 1146 |
decoder: testDecoder, |
| 1147 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 1148 |
want: webServerResult{ |
| 1149 |
wantStatus: http.StatusUnauthorized, |
| 1150 |
wantBody: `{"error":"invalid_request", "error_description":"access token missing"}`, |
| 1151 |
}, |
| 1152 |
}, |
| 1153 |
{ |
| 1154 |
name: "unimplemented UserInfo called", |
| 1155 |
decoder: testDecoder, |
| 1156 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("access_token=xxx")), |
| 1157 |
want: webServerResult{ |
| 1158 |
wantStatus: UnimplementedStatusCode, |
| 1159 |
wantBody: `{"error":"server_error", "error_description":"/ not implemented on this server"}`, |
| 1160 |
}, |
| 1161 |
}, |
| 1162 |
{ |
| 1163 |
name: "bearer", |
| 1164 |
decoder: testDecoder, |
| 1165 |
r: func() *http.Request { |
| 1166 |
r := httptest.NewRequest(http.MethodGet, "/", nil) |
| 1167 |
r.Header.Set("authorization", strings.Join([]string{"Bearer", "xxx"}, " ")) |
| 1168 |
return r |
| 1169 |
}(), |
| 1170 |
want: webServerResult{ |
| 1171 |
wantStatus: UnimplementedStatusCode, |
| 1172 |
wantBody: `{"error":"server_error", "error_description":"/ not implemented on this server"}`, |
| 1173 |
}, |
| 1174 |
}, |
| 1175 |
} |
| 1176 |
for _, tt := range tests { |
| 1177 |
t.Run(tt.name, func(t *testing.T) { |
| 1178 |
s := &webServer{ |
| 1179 |
server: UnimplementedServer{}, |
| 1180 |
decoder: tt.decoder, |
| 1181 |
} |
| 1182 |
runWebServerTest(t, s.userInfoHandler, tt.r, tt.want) |
| 1183 |
}) |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
func Test_webServer_revocationHandler(t *testing.T) { |
| 1188 |
tests := []struct { |
| 1189 |
name string |
| 1190 |
decoder httphelper.Decoder |
| 1191 |
client Client |
| 1192 |
r *http.Request |
| 1193 |
want webServerResult |
| 1194 |
}{ |
| 1195 |
{ |
| 1196 |
name: "decoder error", |
| 1197 |
decoder: schema.NewDecoder(), |
| 1198 |
client: newClient(clientTypeWeb), |
| 1199 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 1200 |
want: webServerResult{ |
| 1201 |
wantStatus: http.StatusBadRequest, |
| 1202 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 1203 |
}, |
| 1204 |
}, |
| 1205 |
{ |
| 1206 |
name: "token missing", |
| 1207 |
decoder: testDecoder, |
| 1208 |
client: newClient(clientTypeWeb), |
| 1209 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 1210 |
want: webServerResult{ |
| 1211 |
wantStatus: http.StatusBadRequest, |
| 1212 |
wantBody: `{"error":"invalid_request", "error_description":"token missing"}`, |
| 1213 |
}, |
| 1214 |
}, |
| 1215 |
{ |
| 1216 |
name: "unimplemented Revocation called, confidential client", |
| 1217 |
decoder: testDecoder, |
| 1218 |
client: newClient(clientTypeWeb), |
| 1219 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("token=xxx")), |
| 1220 |
want: webServerResult{ |
| 1221 |
wantStatus: UnimplementedStatusCode, |
| 1222 |
wantBody: `{"error":"server_error", "error_description":"/ not implemented on this server"}`, |
| 1223 |
}, |
| 1224 |
}, |
| 1225 |
{ |
| 1226 |
name: "unimplemented Revocation called, public client", |
| 1227 |
decoder: testDecoder, |
| 1228 |
client: newClient(clientTypeNative), |
| 1229 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("token=xxx")), |
| 1230 |
want: webServerResult{ |
| 1231 |
wantStatus: UnimplementedStatusCode, |
| 1232 |
wantBody: `{"error":"server_error", "error_description":"/ not implemented on this server"}`, |
| 1233 |
}, |
| 1234 |
}, |
| 1235 |
} |
| 1236 |
for _, tt := range tests { |
| 1237 |
t.Run(tt.name, func(t *testing.T) { |
| 1238 |
s := &webServer{ |
| 1239 |
server: UnimplementedServer{}, |
| 1240 |
decoder: tt.decoder, |
| 1241 |
} |
| 1242 |
runWebServerClientTest(t, s.revocationHandler, tt.r, tt.client, tt.want) |
| 1243 |
}) |
| 1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
func Test_webServer_endSessionHandler(t *testing.T) { |
| 1248 |
tests := []struct { |
| 1249 |
name string |
| 1250 |
decoder httphelper.Decoder |
| 1251 |
r *http.Request |
| 1252 |
want webServerResult |
| 1253 |
}{ |
| 1254 |
{ |
| 1255 |
name: "decoder error", |
| 1256 |
decoder: schema.NewDecoder(), |
| 1257 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 1258 |
want: webServerResult{ |
| 1259 |
wantStatus: http.StatusBadRequest, |
| 1260 |
wantBody: `{"error":"invalid_request", "error_description":"error decoding form"}`, |
| 1261 |
}, |
| 1262 |
}, |
| 1263 |
{ |
| 1264 |
name: "unimplemented EndSession called", |
| 1265 |
decoder: testDecoder, |
| 1266 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("id_token_hint=xxx")), |
| 1267 |
want: webServerResult{ |
| 1268 |
wantStatus: UnimplementedStatusCode, |
| 1269 |
wantBody: `{"error":"server_error", "error_description":"/ not implemented on this server"}`, |
| 1270 |
}, |
| 1271 |
}, |
| 1272 |
} |
| 1273 |
for _, tt := range tests { |
| 1274 |
t.Run(tt.name, func(t *testing.T) { |
| 1275 |
s := &webServer{ |
| 1276 |
server: UnimplementedServer{}, |
| 1277 |
decoder: tt.decoder, |
| 1278 |
} |
| 1279 |
runWebServerTest(t, s.endSessionHandler, tt.r, tt.want) |
| 1280 |
}) |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
func Test_webServer_simpleHandler(t *testing.T) { |
| 1285 |
tests := []struct { |
| 1286 |
name string |
| 1287 |
decoder httphelper.Decoder |
| 1288 |
method func(context.Context, *Request[struct{}]) (*Response, error) |
| 1289 |
r *http.Request |
| 1290 |
want webServerResult |
| 1291 |
}{ |
| 1292 |
{ |
| 1293 |
name: "parse error", |
| 1294 |
decoder: schema.NewDecoder(), |
| 1295 |
r: httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(make([]byte, 11<<20))), |
| 1296 |
want: webServerResult{ |
| 1297 |
wantStatus: http.StatusBadRequest, |
| 1298 |
wantBody: `{"error":"invalid_request", "error_description":"error parsing form"}`, |
| 1299 |
}, |
| 1300 |
}, |
| 1301 |
{ |
| 1302 |
name: "method error", |
| 1303 |
decoder: schema.NewDecoder(), |
| 1304 |
method: func(ctx context.Context, r *Request[struct{}]) (*Response, error) { |
| 1305 |
return nil, io.ErrClosedPipe |
| 1306 |
}, |
| 1307 |
r: httptest.NewRequest(http.MethodGet, "/", bytes.NewReader(make([]byte, 11<<20))), |
| 1308 |
want: webServerResult{ |
| 1309 |
wantStatus: http.StatusInternalServerError, |
| 1310 |
wantBody: `{"error":"server_error", "error_description":"io: read/write on closed pipe"}`, |
| 1311 |
}, |
| 1312 |
}, |
| 1313 |
} |
| 1314 |
for _, tt := range tests { |
| 1315 |
t.Run(tt.name, func(t *testing.T) { |
| 1316 |
s := &webServer{ |
| 1317 |
server: UnimplementedServer{}, |
| 1318 |
decoder: tt.decoder, |
| 1319 |
} |
| 1320 |
runWebServerTest(t, simpleHandler(s, tt.method), tt.r, tt.want) |
| 1321 |
}) |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
func Test_decodeRequest(t *testing.T) { |
| 1326 |
type dst struct { |
| 1327 |
A string `schema:"a"` |
| 1328 |
B string `schema:"b"` |
| 1329 |
} |
| 1330 |
type args struct { |
| 1331 |
r *http.Request |
| 1332 |
postOnly bool |
| 1333 |
} |
| 1334 |
tests := []struct { |
| 1335 |
name string |
| 1336 |
args args |
| 1337 |
want *dst |
| 1338 |
wantErr error |
| 1339 |
}{ |
| 1340 |
{ |
| 1341 |
name: "parse error", |
| 1342 |
args: args{ |
| 1343 |
r: httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(make([]byte, 11<<20))), |
| 1344 |
}, |
| 1345 |
wantErr: oidc.ErrInvalidRequest().WithDescription("error parsing form"), |
| 1346 |
}, |
| 1347 |
{ |
| 1348 |
name: "decode error", |
| 1349 |
args: args{ |
| 1350 |
r: httptest.NewRequest(http.MethodPost, "/", strings.NewReader("foo=bar")), |
| 1351 |
}, |
| 1352 |
wantErr: oidc.ErrInvalidRequest().WithDescription("error decoding form"), |
| 1353 |
}, |
| 1354 |
{ |
| 1355 |
name: "success, get", |
| 1356 |
args: args{ |
| 1357 |
r: httptest.NewRequest(http.MethodGet, "/?a=b&b=a", nil), |
| 1358 |
}, |
| 1359 |
want: &dst{ |
| 1360 |
A: "b", |
| 1361 |
B: "a", |
| 1362 |
}, |
| 1363 |
}, |
| 1364 |
{ |
| 1365 |
name: "success, post only", |
| 1366 |
args: args{ |
| 1367 |
r: httptest.NewRequest(http.MethodPost, "/?b=a", strings.NewReader("a=b&")), |
| 1368 |
postOnly: true, |
| 1369 |
}, |
| 1370 |
want: &dst{ |
| 1371 |
A: "b", |
| 1372 |
}, |
| 1373 |
}, |
| 1374 |
{ |
| 1375 |
name: "success, post mixed", |
| 1376 |
args: args{ |
| 1377 |
r: httptest.NewRequest(http.MethodPost, "/?b=a", strings.NewReader("a=b&")), |
| 1378 |
postOnly: false, |
| 1379 |
}, |
| 1380 |
want: &dst{ |
| 1381 |
A: "b", |
| 1382 |
B: "a", |
| 1383 |
}, |
| 1384 |
}, |
| 1385 |
} |
| 1386 |
for _, tt := range tests { |
| 1387 |
t.Run(tt.name, func(t *testing.T) { |
| 1388 |
if tt.args.r.Method == http.MethodPost { |
| 1389 |
tt.args.r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 1390 |
} |
| 1391 |
got, err := decodeRequest[dst](schema.NewDecoder(), tt.args.r, tt.args.postOnly) |
| 1392 |
require.ErrorIs(t, err, tt.wantErr) |
| 1393 |
assert.Equal(t, tt.want, got) |
| 1394 |
}) |
| 1395 |
} |
| 1396 |
} |