oauth2

package
v0.46.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CLIClientId is the client_id of the knot CLI.
	CLIClientId = "knot-cli"

	GrantTypeDeviceCode = "urn:ietf:params:oauth:grant-type:device_code"
)
View Source
const (
	AuthCodeExpiry = 10 * time.Minute // OAuth2 spec recommends max 10 minutes
)
View Source
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

View Source
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

func BaseURL(r *http.Request) string

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

func MCPResource(r *http.Request) string

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

func MatchesMCPResource(r *http.Request, uri string) bool

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

func ProtectedResourceMetadataURL(r *http.Request) string

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

func ValidateClient(clientId, redirectURI string) (string, error)

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 AuthCode

type AuthCode struct {
	Code                string
	UserId              string
	ClientId            string
	RedirectURI         string
	Scope               string
	CodeChallenge       string
	CodeChallengeMethod string
	// Resource is the canonical resource (RFC 8707) the client asked for at
	// /authorize, or "" when it named none.
	Resource  string
	ExpiresAt time.Time
}

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 ErrorResponse struct {
	Error            string `json:"error" msgpack:"error"`
	ErrorDescription string `json:"error_description,omitempty" msgpack:"error_description,omitempty"`
}

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"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL