all: stabilize client OAuth support (#861)
Also fix lint errors that were hidden because of the build tag.
This commit is contained in:
parent
c1a12cfeda
commit
638d404e0d
13 changed files with 387 additions and 915 deletions
|
|
@ -5,8 +5,6 @@
|
|||
// This file implements Authorization Server Metadata.
|
||||
// See https://www.rfc-editor.org/rfc/rfc8414.html.
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@
|
|||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@
|
|||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import "errors"
|
||||
|
|
|
|||
|
|
@ -5,8 +5,6 @@
|
|||
// This file implements Authorization Server Metadata.
|
||||
// See https://www.rfc-editor.org/rfc/rfc8414.html.
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@
|
|||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@
|
|||
|
||||
// Package oauthex implements extensions to OAuth2.
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@
|
|||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
|
|
|
|||
|
|
@ -5,8 +5,6 @@
|
|||
// This file implements Protected Resource Metadata.
|
||||
// See https://www.rfc-editor.org/rfc/rfc9728.html.
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
|
|
@ -14,81 +12,108 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/internal/util"
|
||||
)
|
||||
|
||||
const defaultProtectedResourceMetadataURI = "/.well-known/oauth-protected-resource"
|
||||
// ProtectedResourceMetadata is the metadata for an OAuth 2.0 protected resource,
|
||||
// as defined in section 2 of https://www.rfc-editor.org/rfc/rfc9728.html.
|
||||
//
|
||||
// The following features are not supported:
|
||||
// - additional keys (§2, last sentence)
|
||||
// - human-readable metadata (§2.1)
|
||||
// - signed metadata (§2.2)
|
||||
type ProtectedResourceMetadata struct {
|
||||
// Resource (resource) is the protected resource's resource identifier.
|
||||
// Required.
|
||||
Resource string `json:"resource"`
|
||||
|
||||
// GetProtectedResourceMetadataFromID issues a GET request to retrieve protected resource
|
||||
// metadata from a resource server by its ID.
|
||||
// The resource ID is an HTTPS URL, typically with a host:port and possibly a path.
|
||||
// For example:
|
||||
//
|
||||
// https://example.com/server
|
||||
//
|
||||
// This function, following the spec (§3), inserts the default well-known path into the
|
||||
// URL. In our example, the result would be
|
||||
//
|
||||
// https://example.com/.well-known/oauth-protected-resource/server
|
||||
//
|
||||
// It then retrieves the metadata at that location using the given client (or the
|
||||
// default client if nil) and validates its resource field against resourceID.
|
||||
//
|
||||
// Deprecated: Use [GetProtectedResourceMetadata] instead. This function will be removed in v1.5.0.
|
||||
func GetProtectedResourceMetadataFromID(ctx context.Context, resourceID string, c *http.Client) (_ *ProtectedResourceMetadata, err error) {
|
||||
defer util.Wrapf(&err, "GetProtectedResourceMetadataFromID(%q)", resourceID)
|
||||
// AuthorizationServers (authorization_servers) is an optional slice containing a list of
|
||||
// OAuth authorization server issuer identifiers (as defined in RFC 8414) that can be
|
||||
// used with this protected resource.
|
||||
AuthorizationServers []string `json:"authorization_servers,omitempty"`
|
||||
|
||||
u, err := url.Parse(resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Insert well-known URI into URL.
|
||||
u.Path = path.Join(defaultProtectedResourceMetadataURI, u.Path)
|
||||
return GetProtectedResourceMetadata(ctx, u.String(), resourceID, c)
|
||||
// JWKSURI (jwks_uri) is an optional URL of the protected resource's JSON Web Key (JWK) Set
|
||||
// document. This contains public keys belonging to the protected resource, such as
|
||||
// signing key(s) that the resource server uses to sign resource responses.
|
||||
JWKSURI string `json:"jwks_uri,omitempty"`
|
||||
|
||||
// ScopesSupported (scopes_supported) is a recommended slice containing a list of scope
|
||||
// values (as defined in RFC 6749) used in authorization requests to request access
|
||||
// to this protected resource.
|
||||
ScopesSupported []string `json:"scopes_supported,omitempty"`
|
||||
|
||||
// BearerMethodsSupported (bearer_methods_supported) is an optional slice containing
|
||||
// a list of the supported methods of sending an OAuth 2.0 bearer token to the
|
||||
// protected resource. Defined values are "header", "body", and "query".
|
||||
BearerMethodsSupported []string `json:"bearer_methods_supported,omitempty"`
|
||||
|
||||
// ResourceSigningAlgValuesSupported (resource_signing_alg_values_supported) is an optional
|
||||
// slice of JWS signing algorithms (alg values) supported by the protected
|
||||
// resource for signing resource responses.
|
||||
ResourceSigningAlgValuesSupported []string `json:"resource_signing_alg_values_supported,omitempty"`
|
||||
|
||||
// ResourceName (resource_name) is a human-readable name of the protected resource
|
||||
// intended for display to the end user. It is RECOMMENDED that this field be included.
|
||||
// This value may be internationalized.
|
||||
ResourceName string `json:"resource_name,omitempty"`
|
||||
|
||||
// ResourceDocumentation (resource_documentation) is an optional URL of a page containing
|
||||
// human-readable information for developers using the protected resource.
|
||||
// This value may be internationalized.
|
||||
ResourceDocumentation string `json:"resource_documentation,omitempty"`
|
||||
|
||||
// ResourcePolicyURI (resource_policy_uri) is an optional URL of a page containing
|
||||
// human-readable policy information on how a client can use the data provided.
|
||||
// This value may be internationalized.
|
||||
ResourcePolicyURI string `json:"resource_policy_uri,omitempty"`
|
||||
|
||||
// ResourceTOSURI (resource_tos_uri) is an optional URL of a page containing the protected
|
||||
// resource's human-readable terms of service. This value may be internationalized.
|
||||
ResourceTOSURI string `json:"resource_tos_uri,omitempty"`
|
||||
|
||||
// TLSClientCertificateBoundAccessTokens (tls_client_certificate_bound_access_tokens) is an
|
||||
// optional boolean indicating support for mutual-TLS client certificate-bound
|
||||
// access tokens (RFC 8705). Defaults to false if omitted.
|
||||
TLSClientCertificateBoundAccessTokens bool `json:"tls_client_certificate_bound_access_tokens,omitempty"`
|
||||
|
||||
// AuthorizationDetailsTypesSupported (authorization_details_types_supported) is an optional
|
||||
// slice of 'type' values supported by the resource server for the
|
||||
// 'authorization_details' parameter (RFC 9396).
|
||||
AuthorizationDetailsTypesSupported []string `json:"authorization_details_types_supported,omitempty"`
|
||||
|
||||
// DPOPSigningAlgValuesSupported (dpop_signing_alg_values_supported) is an optional
|
||||
// slice of JWS signing algorithms supported by the resource server for validating
|
||||
// DPoP proof JWTs (RFC 9449).
|
||||
DPOPSigningAlgValuesSupported []string `json:"dpop_signing_alg_values_supported,omitempty"`
|
||||
|
||||
// DPOPBoundAccessTokensRequired (dpop_bound_access_tokens_required) is an optional boolean
|
||||
// specifying whether the protected resource always requires the use of DPoP-bound
|
||||
// access tokens (RFC 9449). Defaults to false if omitted.
|
||||
DPOPBoundAccessTokensRequired bool `json:"dpop_bound_access_tokens_required,omitempty"`
|
||||
|
||||
// SignedMetadata (signed_metadata) is an optional JWT containing metadata parameters
|
||||
// about the protected resource as claims. If present, these values take precedence
|
||||
// over values conveyed in plain JSON.
|
||||
// TODO:implement.
|
||||
// Note that §2.2 says it's okay to ignore this.
|
||||
// SignedMetadata string `json:"signed_metadata,omitempty"`
|
||||
}
|
||||
|
||||
// GetProtectedResourceMetadataFromHeader retrieves protected resource metadata
|
||||
// using information in the given header, using the given client (or the default
|
||||
// client if nil).
|
||||
// It issues a GET request to a URL discovered by parsing the WWW-Authenticate headers in the given request.
|
||||
// Per RFC 9728 section 3.3, it validates that the resource field of the resulting metadata
|
||||
// matches the serverURL (the URL that the client used to make the original request to the resource server).
|
||||
// If there is no metadata URL in the header, it returns nil, nil.
|
||||
//
|
||||
// Deprecated: Use [GetProtectedResourceMetadata] instead. This function will be removed in v1.5.0.
|
||||
func GetProtectedResourceMetadataFromHeader(ctx context.Context, serverURL string, header http.Header, c *http.Client) (_ *ProtectedResourceMetadata, err error) {
|
||||
headers := header[http.CanonicalHeaderKey("WWW-Authenticate")]
|
||||
if len(headers) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
cs, err := ParseWWWAuthenticate(headers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
metadataURL := resourceMetadataURL(cs)
|
||||
if metadataURL == "" {
|
||||
return nil, nil
|
||||
}
|
||||
return GetProtectedResourceMetadata(ctx, metadataURL, serverURL, c)
|
||||
// Challenge represents a single authentication challenge from a WWW-Authenticate header.
|
||||
// As per RFC 9110, Section 11.6.1, a challenge consists of a scheme and optional parameters.
|
||||
type Challenge struct {
|
||||
// Scheme is the authentication scheme (e.g., "Bearer", "Basic").
|
||||
// It is case-insensitive. A parsed value will always be lower-case.
|
||||
Scheme string
|
||||
// Params is a map of authentication parameters.
|
||||
// Keys are case-insensitive. Parsed keys are always lower-case.
|
||||
Params map[string]string
|
||||
}
|
||||
|
||||
// resourceMetadataURL returns a resource metadata URL from the given "WWW-Authenticate" header challenges,
|
||||
// or the empty string if there is none.
|
||||
func resourceMetadataURL(cs []Challenge) string {
|
||||
for _, c := range cs {
|
||||
if u := c.Params["resource_metadata"]; u != "" {
|
||||
return u
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetProtectedResourceMetadataFromID issues a GET request to retrieve protected resource
|
||||
// GetProtectedResourceMetadata issues a GET request to retrieve protected resource
|
||||
// metadata from a resource server.
|
||||
// The metadataURL is typically a URL with a host:port and possibly a path.
|
||||
// The resourceURL is the resource URI the metadataURL is for.
|
||||
|
|
|
|||
|
|
@ -1,105 +0,0 @@
|
|||
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// This file implements Protected Resource Metadata.
|
||||
// See https://www.rfc-editor.org/rfc/rfc9728.html.
|
||||
|
||||
// This is a temporary file to expose the required objects to the main package.
|
||||
|
||||
package oauthex
|
||||
|
||||
// ProtectedResourceMetadata is the metadata for an OAuth 2.0 protected resource,
|
||||
// as defined in section 2 of https://www.rfc-editor.org/rfc/rfc9728.html.
|
||||
//
|
||||
// The following features are not supported:
|
||||
// - additional keys (§2, last sentence)
|
||||
// - human-readable metadata (§2.1)
|
||||
// - signed metadata (§2.2)
|
||||
type ProtectedResourceMetadata struct {
|
||||
// Resource (resource) is the protected resource's resource identifier.
|
||||
// Required.
|
||||
Resource string `json:"resource"`
|
||||
|
||||
// AuthorizationServers (authorization_servers) is an optional slice containing a list of
|
||||
// OAuth authorization server issuer identifiers (as defined in RFC 8414) that can be
|
||||
// used with this protected resource.
|
||||
AuthorizationServers []string `json:"authorization_servers,omitempty"`
|
||||
|
||||
// JWKSURI (jwks_uri) is an optional URL of the protected resource's JSON Web Key (JWK) Set
|
||||
// document. This contains public keys belonging to the protected resource, such as
|
||||
// signing key(s) that the resource server uses to sign resource responses.
|
||||
JWKSURI string `json:"jwks_uri,omitempty"`
|
||||
|
||||
// ScopesSupported (scopes_supported) is a recommended slice containing a list of scope
|
||||
// values (as defined in RFC 6749) used in authorization requests to request access
|
||||
// to this protected resource.
|
||||
ScopesSupported []string `json:"scopes_supported,omitempty"`
|
||||
|
||||
// BearerMethodsSupported (bearer_methods_supported) is an optional slice containing
|
||||
// a list of the supported methods of sending an OAuth 2.0 bearer token to the
|
||||
// protected resource. Defined values are "header", "body", and "query".
|
||||
BearerMethodsSupported []string `json:"bearer_methods_supported,omitempty"`
|
||||
|
||||
// ResourceSigningAlgValuesSupported (resource_signing_alg_values_supported) is an optional
|
||||
// slice of JWS signing algorithms (alg values) supported by the protected
|
||||
// resource for signing resource responses.
|
||||
ResourceSigningAlgValuesSupported []string `json:"resource_signing_alg_values_supported,omitempty"`
|
||||
|
||||
// ResourceName (resource_name) is a human-readable name of the protected resource
|
||||
// intended for display to the end user. It is RECOMMENDED that this field be included.
|
||||
// This value may be internationalized.
|
||||
ResourceName string `json:"resource_name,omitempty"`
|
||||
|
||||
// ResourceDocumentation (resource_documentation) is an optional URL of a page containing
|
||||
// human-readable information for developers using the protected resource.
|
||||
// This value may be internationalized.
|
||||
ResourceDocumentation string `json:"resource_documentation,omitempty"`
|
||||
|
||||
// ResourcePolicyURI (resource_policy_uri) is an optional URL of a page containing
|
||||
// human-readable policy information on how a client can use the data provided.
|
||||
// This value may be internationalized.
|
||||
ResourcePolicyURI string `json:"resource_policy_uri,omitempty"`
|
||||
|
||||
// ResourceTOSURI (resource_tos_uri) is an optional URL of a page containing the protected
|
||||
// resource's human-readable terms of service. This value may be internationalized.
|
||||
ResourceTOSURI string `json:"resource_tos_uri,omitempty"`
|
||||
|
||||
// TLSClientCertificateBoundAccessTokens (tls_client_certificate_bound_access_tokens) is an
|
||||
// optional boolean indicating support for mutual-TLS client certificate-bound
|
||||
// access tokens (RFC 8705). Defaults to false if omitted.
|
||||
TLSClientCertificateBoundAccessTokens bool `json:"tls_client_certificate_bound_access_tokens,omitempty"`
|
||||
|
||||
// AuthorizationDetailsTypesSupported (authorization_details_types_supported) is an optional
|
||||
// slice of 'type' values supported by the resource server for the
|
||||
// 'authorization_details' parameter (RFC 9396).
|
||||
AuthorizationDetailsTypesSupported []string `json:"authorization_details_types_supported,omitempty"`
|
||||
|
||||
// DPOPSigningAlgValuesSupported (dpop_signing_alg_values_supported) is an optional
|
||||
// slice of JWS signing algorithms supported by the resource server for validating
|
||||
// DPoP proof JWTs (RFC 9449).
|
||||
DPOPSigningAlgValuesSupported []string `json:"dpop_signing_alg_values_supported,omitempty"`
|
||||
|
||||
// DPOPBoundAccessTokensRequired (dpop_bound_access_tokens_required) is an optional boolean
|
||||
// specifying whether the protected resource always requires the use of DPoP-bound
|
||||
// access tokens (RFC 9449). Defaults to false if omitted.
|
||||
DPOPBoundAccessTokensRequired bool `json:"dpop_bound_access_tokens_required,omitempty"`
|
||||
|
||||
// SignedMetadata (signed_metadata) is an optional JWT containing metadata parameters
|
||||
// about the protected resource as claims. If present, these values take precedence
|
||||
// over values conveyed in plain JSON.
|
||||
// TODO:implement.
|
||||
// Note that §2.2 says it's okay to ignore this.
|
||||
// SignedMetadata string `json:"signed_metadata,omitempty"`
|
||||
}
|
||||
|
||||
// Challenge represents a single authentication challenge from a WWW-Authenticate header.
|
||||
// As per RFC 9110, Section 11.6.1, a challenge consists of a scheme and optional parameters.
|
||||
type Challenge struct {
|
||||
// Scheme is the authentication scheme (e.g., "Bearer", "Basic").
|
||||
// It is case-insensitive. A parsed value will always be lower-case.
|
||||
Scheme string
|
||||
// Params is a map of authentication parameters.
|
||||
// Keys are case-insensitive. Parsed keys are always lower-case.
|
||||
Params map[string]string
|
||||
}
|
||||
258
oauthex/resource_meta_test.go
Normal file
258
oauthex/resource_meta_test.go
Normal file
|
|
@ -0,0 +1,258 @@
|
|||
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSplitChallenges(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "single challenge no params",
|
||||
input: `Basic`,
|
||||
want: []string{`Basic`},
|
||||
},
|
||||
{
|
||||
name: "single challenge with params",
|
||||
input: `Bearer realm="example.com", error="invalid_token"`,
|
||||
want: []string{`Bearer realm="example.com", error="invalid_token"`},
|
||||
},
|
||||
{
|
||||
name: "single challenge with comma in quoted string",
|
||||
input: `Bearer realm="example, with comma"`,
|
||||
want: []string{`Bearer realm="example, with comma"`},
|
||||
},
|
||||
{
|
||||
name: "two challenges",
|
||||
input: `Basic, Bearer realm="example"`,
|
||||
want: []string{`Basic`, ` Bearer realm="example"`},
|
||||
},
|
||||
{
|
||||
name: "multiple challenges complex",
|
||||
input: `Newauth realm="apps", Basic, Bearer realm="example.com", error="invalid_token"`,
|
||||
want: []string{`Newauth realm="apps"`, ` Basic`, ` Bearer realm="example.com", error="invalid_token"`},
|
||||
},
|
||||
{
|
||||
name: "challenge with escaped quote",
|
||||
input: `Bearer realm="example \"quoted\""`,
|
||||
want: []string{`Bearer realm="example \"quoted\""`},
|
||||
},
|
||||
{
|
||||
name: "empty input",
|
||||
input: "",
|
||||
want: []string{""},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := splitChallenges(tt.input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("splitChallenges() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitChallengesError(t *testing.T) {
|
||||
if _, err := splitChallenges(`"Bearer"`); err == nil {
|
||||
t.Fatal("got nil, want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSingleChallenge(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want Challenge
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "scheme only",
|
||||
input: "Basic",
|
||||
want: Challenge{
|
||||
Scheme: "basic",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "scheme with one quoted param",
|
||||
input: `Bearer realm="example.com"`,
|
||||
want: Challenge{
|
||||
Scheme: "bearer",
|
||||
Params: map[string]string{"realm": "example.com"},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "scheme with one unquoted param",
|
||||
input: `Bearer realm=example.com`,
|
||||
want: Challenge{
|
||||
Scheme: "bearer",
|
||||
Params: map[string]string{"realm": "example.com"},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "scheme with multiple params",
|
||||
input: `Bearer realm="example", error="invalid_token", error_description="The token expired"`,
|
||||
want: Challenge{
|
||||
Scheme: "bearer",
|
||||
Params: map[string]string{
|
||||
"realm": "example",
|
||||
"error": "invalid_token",
|
||||
"error_description": "The token expired",
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "scheme with multiple unquoted params",
|
||||
input: `Bearer realm=example, error=invalid_token, error_description=The token expired`,
|
||||
want: Challenge{
|
||||
Scheme: "bearer",
|
||||
Params: map[string]string{
|
||||
"realm": "example",
|
||||
"error": "invalid_token",
|
||||
"error_description": "The token expired",
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "case-insensitive scheme and keys",
|
||||
input: `BEARER ReAlM="example"`,
|
||||
want: Challenge{
|
||||
Scheme: "bearer",
|
||||
Params: map[string]string{"realm": "example"},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "param with escaped quote",
|
||||
input: `Bearer realm="example \"foo\" bar"`,
|
||||
want: Challenge{
|
||||
Scheme: "bearer",
|
||||
Params: map[string]string{"realm": `example "foo" bar`},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "param without quotes (token)",
|
||||
input: "Bearer realm=example.com",
|
||||
want: Challenge{
|
||||
Scheme: "bearer",
|
||||
Params: map[string]string{"realm": "example.com"},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "malformed param - no value",
|
||||
input: "Bearer realm=",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed param - unterminated quote",
|
||||
input: `Bearer realm="example`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed param - missing comma",
|
||||
input: `Bearer realm="a" error="b"`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed param - initial equal",
|
||||
input: `Bearer ="a"`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty input",
|
||||
input: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := parseSingleChallenge(tt.input)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("parseSingleChallenge() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("parseSingleChallenge() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetProtectedResourceMetadata(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
h := &fakeResourceHandler{}
|
||||
server := httptest.NewTLSServer(h)
|
||||
h.installHandlers(server.URL)
|
||||
client := server.Client()
|
||||
metadataURL := server.URL + "/.well-known/oauth-protected-resource"
|
||||
prm, err := GetProtectedResourceMetadata(ctx, metadataURL, server.URL, client)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if prm == nil {
|
||||
t.Fatal("nil prm")
|
||||
}
|
||||
})
|
||||
t.Run("RejectsIncorrectResource", func(t *testing.T) {
|
||||
h := &fakeResourceHandler{resourceOverride: "https://attacker.com/evil"}
|
||||
server := httptest.NewTLSServer(h)
|
||||
h.installHandlers(server.URL)
|
||||
client := server.Client()
|
||||
metadataURL := server.URL + "/.well-known/oauth-protected-resource"
|
||||
prm, err := GetProtectedResourceMetadata(ctx, metadataURL, server.URL, client)
|
||||
if err == nil {
|
||||
t.Fatal("Expected validation error for mismatched resource, got nil")
|
||||
}
|
||||
if prm != nil {
|
||||
t.Fatal("Expected nil prm on validation failure")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type fakeResourceHandler struct {
|
||||
http.ServeMux
|
||||
resourceOverride string // If set, use this instead of correct resource (for testing validation)
|
||||
}
|
||||
|
||||
func (h *fakeResourceHandler) installHandlers(serverURL string) {
|
||||
path := "/.well-known/oauth-protected-resource"
|
||||
h.Handle("GET "+path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
// Per RFC 9728 section 3.3, the resource field should contain the actual resource identifier,
|
||||
// which is the URL the client uses to access the resource (serverURL + "/resource" for WWW-Authenticate case).
|
||||
// For the well-known URL test case, it's just the serverURL.
|
||||
resource := serverURL
|
||||
// Allow testing with custom resource values (e.g., impersonation attacks).
|
||||
if h.resourceOverride != "" {
|
||||
resource = h.resourceOverride
|
||||
}
|
||||
prm := &ProtectedResourceMetadata{Resource: resource}
|
||||
if err := json.NewEncoder(w).Encode(prm); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
|
@ -5,8 +5,6 @@
|
|||
// This file implements Token Exchange (RFC 8693) for Enterprise Managed Authorization.
|
||||
// See https://datatracker.ietf.org/doc/html/rfc8693
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@
|
|||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@
|
|||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
//go:build mcp_go_client_oauth
|
||||
|
||||
package oauthex
|
||||
|
||||
import (
|
||||
|
|
@ -240,7 +238,7 @@ func TestGetAuthServerMetaRejectsDangerousURLs(t *testing.T) {
|
|||
}
|
||||
|
||||
// TestGetProtectedResourceMetadataRejectsDangerousURLs tests that
|
||||
// GetProtectedResourceMetadataFromID rejects metadata with dangerous authorization server URLs.
|
||||
// GetProtectedResourceMetadata rejects metadata with dangerous authorization server URLs.
|
||||
func TestGetProtectedResourceMetadataRejectsDangerousURLs(t *testing.T) {
|
||||
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
serverURL := "https://" + r.Host
|
||||
|
|
@ -254,12 +252,13 @@ func TestGetProtectedResourceMetadataRejectsDangerousURLs(t *testing.T) {
|
|||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
_, err := GetProtectedResourceMetadataFromID(ctx, server.URL, server.Client())
|
||||
metadataURL := server.URL + "/.well-known/oauth-protected-resource"
|
||||
_, err := GetProtectedResourceMetadata(ctx, metadataURL, server.URL, server.Client())
|
||||
if err == nil {
|
||||
t.Fatal("GetProtectedResourceMetadataFromID(): got nil error, want error")
|
||||
t.Fatal("GetProtectedResourceMetadata(): got nil error, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "disallowed scheme") {
|
||||
t.Errorf("GetProtectedResourceMetadataFromID(): got error %v, want error containing \"disallowed scheme\"", err)
|
||||
t.Errorf("GetProtectedResourceMetadata(): got error %v, want error containing \"disallowed scheme\"", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue