Documentation
¶
Index ¶
- Constants
- Variables
- func BaseURL(r *http.Request) string
- func HandleAuthorizationServerMetadata(w http.ResponseWriter, r *http.Request)
- func HandleAuthorize(w http.ResponseWriter, r *http.Request)
- func HandleDeviceAuthorization(w http.ResponseWriter, r *http.Request)
- func HandleGrant(w http.ResponseWriter, r *http.Request)
- func HandleRegister(w http.ResponseWriter, r *http.Request)
- func HandleToken(w http.ResponseWriter, r *http.Request)
- func MCPResource(r *http.Request) string
- func MatchesMCPResource(r *http.Request, uri string) bool
- func ProtectedResourceMetadataURL(r *http.Request) string
- func ValidateClient(clientId, redirectURI string) (string, error)
- type AuthCode
- type AuthCodeStore
- type AuthorizationServerMetadata
- type DeviceAuthorizationResponse
- type ErrorResponse
- type OpenIDConfiguration
- type TokenResponse
Constants ¶
const ( // CLIClientId is the client_id of the knot CLI. CLIClientId = "knot-cli" GrantTypeDeviceCode = "urn:ietf:params:oauth:grant-type:device_code" )
const (
AuthCodeExpiry = 10 * time.Minute // OAuth2 spec recommends max 10 minutes
)
const ErrorInvalidTarget = "invalid_target"
ErrorInvalidTarget is the OAuth error for a resource the authorization server will not issue a token for (RFC 8707 section 2).
Variables ¶
var ResourceServer = &mcp.ResourceServer{ Resource: "/mcp", ResourceFunc: func(r *http.Request) string { return BaseURL(r) + "/mcp" }, AuthorizationServersFunc: func(r *http.Request) []string { return []string{BaseURL(r)} }, ScopesSupported: []string{model.ScopeMCP}, ResourceName: "Knot", MetadataURLFunc: ProtectedResourceMetadataURL, }
ResourceServer describes knot's /mcp endpoint as an OAuth 2.1 protected resource (RFC 9728): it serves the protected resource metadata and builds the spec-shaped 401/403 Bearer challenges the auth middleware answers /mcp refusals with. The resource and authorization server follow BaseURL, so they track the configured server URL or, without one, the request's host. Tokens are validated by the auth middleware, so ValidateToken is not set and Protect/Authenticate are not used.
Functions ¶
func BaseURL ¶ added in v0.38.1
BaseURL returns the server's public URL, from the configuration or, when that isn't set, from the request.
func HandleAuthorizationServerMetadata ¶
func HandleAuthorizationServerMetadata(w http.ResponseWriter, r *http.Request)
func HandleAuthorize ¶
func HandleAuthorize(w http.ResponseWriter, r *http.Request)
HandleAuthorize handles the OAuth2 authorization endpoint
func HandleDeviceAuthorization ¶ added in v0.41.0
func HandleDeviceAuthorization(w http.ResponseWriter, r *http.Request)
HandleDeviceAuthorization handles the device authorization endpoint (RFC 8628 section 3.1). Besides client_id it takes token_name, the name the API token is given once approved.
func HandleGrant ¶
func HandleGrant(w http.ResponseWriter, r *http.Request)
HandleGrant handles the OAuth2 grant approval
func HandleRegister ¶ added in v0.38.1
func HandleRegister(w http.ResponseWriter, r *http.Request)
HandleRegister handles the dynamic client registration endpoint.
func HandleToken ¶
func HandleToken(w http.ResponseWriter, r *http.Request)
HandleToken handles the OAuth2 token endpoint
func MCPResource ¶ added in v0.42.0
MCPResource returns the canonical resource identifier of knot's MCP endpoint, the same value the protected resource metadata advertises.
func MatchesMCPResource ¶ added in v0.42.0
MatchesMCPResource reports whether uri names this server's MCP endpoint.
The comparison is the one the protected resource (mcp.ResourceServer) uses for audiences: scheme and host compare case-insensitively, a trailing slash is ignored, and a query, fragment or userinfo never matches.
When server.url is configured the scheme must match too. Without it, BaseURL takes the scheme from the request, which behind a TLS-terminating proxy is http while the client saw https, so the scheme is not compared and either http or https is accepted: only host and path decide. Configure server.url on a server behind a proxy for the strict comparison.
func ProtectedResourceMetadataURL ¶ added in v0.38.1
ProtectedResourceMetadataURL returns the RFC 9728 metadata URL for the MCP endpoint, given to clients in the WWW-Authenticate challenge.
func ValidateClient ¶ added in v0.38.1
ValidateClient checks a client_id and redirect_uri pair. A registered client must use one of the redirect URIs it registered; other client IDs are accepted as unregistered public clients, as before registration was supported. It returns the client's registered name, if any.
Types ¶
type AuthCodeStore ¶
type AuthCodeStore struct {
// contains filtered or unexported fields
}
func GetAuthCodeStore ¶
func GetAuthCodeStore() *AuthCodeStore
func (*AuthCodeStore) ConsumeAuthCode ¶
func (s *AuthCodeStore) ConsumeAuthCode(code string) (*AuthCode, bool)
func (*AuthCodeStore) CreateAuthCode ¶
func (s *AuthCodeStore) CreateAuthCode(userId, clientId, redirectURI, scope, codeChallenge, codeChallengeMethod, resource string) (*AuthCode, error)
type AuthorizationServerMetadata ¶
type AuthorizationServerMetadata struct {
Issuer string `json:"issuer" msgpack:"issuer"`
AuthorizationEndpoint string `json:"authorization_endpoint" msgpack:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint" msgpack:"token_endpoint"`
RegistrationEndpoint string `json:"registration_endpoint" msgpack:"registration_endpoint"`
DeviceAuthorizationEndpoint string `json:"device_authorization_endpoint" msgpack:"device_authorization_endpoint"`
ResponseTypesSupported []string `json:"response_types_supported" msgpack:"response_types_supported"`
GrantTypesSupported []string `json:"grant_types_supported" msgpack:"grant_types_supported"`
TokenEndpointAuthMethodsSupported []string `json:"token_endpoint_auth_methods_supported" msgpack:"token_endpoint_auth_methods_supported"`
ScopesSupported []string `json:"scopes_supported,omitempty" msgpack:"scopes_supported,omitempty"`
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported,omitempty" msgpack:"code_challenge_methods_supported,omitempty"`
}
type DeviceAuthorizationResponse ¶ added in v0.41.0
type DeviceAuthorizationResponse struct {
DeviceCode string `json:"device_code" msgpack:"device_code"`
UserCode string `json:"user_code" msgpack:"user_code"`
VerificationURI string `json:"verification_uri" msgpack:"verification_uri"`
VerificationURIComplete string `json:"verification_uri_complete" msgpack:"verification_uri_complete"`
ExpiresIn int `json:"expires_in" msgpack:"expires_in"`
Interval int `json:"interval" msgpack:"interval"`
}
type ErrorResponse ¶
type OpenIDConfiguration ¶
type OpenIDConfiguration struct {
Issuer string `json:"issuer" msgpack:"issuer"`
AuthorizationEndpoint string `json:"authorization_endpoint" msgpack:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint" msgpack:"token_endpoint"`
UserinfoEndpoint string `json:"userinfo_endpoint" msgpack:"userinfo_endpoint"`
JwksURI string `json:"jwks_uri" msgpack:"jwks_uri"`
ResponseTypesSupported []string `json:"response_types_supported" msgpack:"response_types_supported"`
SubjectTypesSupported []string `json:"subject_types_supported" msgpack:"subject_types_supported"`
IDTokenSigningAlgValuesSupported []string `json:"id_token_signing_alg_values_supported" msgpack:"id_token_signing_alg_values_supported"`
ScopesSupported []string `json:"scopes_supported" msgpack:"scopes_supported"`
TokenEndpointAuthMethodsSupported []string `json:"token_endpoint_auth_methods_supported" msgpack:"token_endpoint_auth_methods_supported"`
ClaimsSupported []string `json:"claims_supported" msgpack:"claims_supported"`
}
type TokenResponse ¶
type TokenResponse struct {
AccessToken string `json:"access_token" msgpack:"access_token"`
TokenType string `json:"token_type" msgpack:"token_type"`
ExpiresIn int `json:"expires_in" msgpack:"expires_in"`
RefreshToken string `json:"refresh_token" msgpack:"refresh_token"`
Scope string `json:"scope" msgpack:"scope"`
}