oauth

package
v6.2.12 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package oauth contains generic OAuth related functionality, variables and constants

Index

Constants

View Source
const (
	// AccessTokenRequestStatusPending is the status for a pending access token
	AccessTokenRequestStatusPending = "pending"
	// AccessTokenRequestStatusActive is the status for an active access token
	AccessTokenRequestStatusActive = "active"
)
View Source
const (
	// AuthzServerWellKnown is the well-known base path for the oauth authorization server metadata as defined in RFC8414
	AuthzServerWellKnown = "/.well-known/oauth-authorization-server"
	// ClientMetadataPath is the path to the client metadata relative to the complete did:web URL
	ClientMetadataPath = "/oauth-client"
	// OpenIdCredIssuerWellKnown is the well-known base path for the openID credential issuer metadata as defined in
	// OpenID4VCI specification
	OpenIdCredIssuerWellKnown = "/.well-known/openid-credential-issuer"
	// OpenIdConfigurationWellKnown is the well-known base path for the openID configuration metadata as defined in
	// OpenID4 federation specification
	OpenIdConfigurationWellKnown = "/.well-known/openid-configuration"
)

metadata endpoints

View Source
const (
	// AssertionParam is the parameter name for the assertion parameter. (RFC021)
	AssertionParam = "assertion"
	// AuthorizationDetailsParam is the parameter name for the authorization_details parameter. (RFC9396)
	AuthorizationDetailsParam = "authorization_details"
	// ClientIDParam is the parameter name for the client_id parameter. (RFC6749)
	ClientIDParam = "client_id"
	// ClientIDSchemeParam is the parameter name for the client_id_scheme parameter. (OpenID4VP)
	ClientIDSchemeParam = "client_id_scheme"
	// ClientMetadataParam is the parameter name for the client_metadata parameter. (OpenID4VP)
	ClientMetadataParam = "client_metadata"
	// ClientMetadataURIParam is the parameter name for the client_metadata_uri parameter. (OpenID4VP)
	ClientMetadataURIParam = "client_metadata_uri"
	// CNonceParam is the parameter name for the c_nonce parameter. (OpenID4VCI)
	CNonceParam = "c_nonce"
	// CodeParam is the parameter name for the code parameter. (RFC6749)
	CodeParam = CodeResponseType
	// CodeChallengeParam is the parameter name for the code_challenge parameter. (RFC7636)
	CodeChallengeParam = "code_challenge"
	// CodeChallengeMethodParam is the parameter name for the code_challenge_method parameter. (RFC7636)
	CodeChallengeMethodParam = "code_challenge_method"
	// CodeVerifierParam is the parameter name for the code_verifier parameter. (RFC7636)
	CodeVerifierParam = "code_verifier"
	// GrantTypeParam is the parameter name for the grant_type parameter. (RFC6749)
	GrantTypeParam = "grant_type"
	// NonceParam is the parameter name for the nonce parameter
	NonceParam = "nonce"
	// PresentationDefParam is the parameter name for the OpenID4VP presentation_definition parameter. (OpenID4VP)
	PresentationDefParam = "presentation_definition"
	// PresentationDefUriParam is the parameter name for the OpenID4VP presentation_definition_uri parameter. (OpenID4VP)
	PresentationDefUriParam = "presentation_definition_uri"
	// PresentationSubmissionParam is the parameter name for the presentation_submission parameter. (OpenID4VP)
	PresentationSubmissionParam = "presentation_submission"
	// RedirectURIParam is the parameter name for the redirect_uri parameter. (RFC6749)
	RedirectURIParam = "redirect_uri"
	// RequestParam is the parameter name for the request parameter.	(RFC9101)
	RequestParam = "request"
	// RequestURIParam is the parameter name for the request parameter. (RFC9101)
	RequestURIParam = "request_uri"
	// RequestURIMethodParam states what http method (get/post) should be used for RequestURIParam. (OpenID4VP)
	RequestURIMethodParam = "request_uri_method"
	// ResponseModeParam is the parameter name for the OAuth2 response_mode parameter.
	ResponseModeParam = "response_mode"
	// ResponseTypeParam is the parameter name for the response_type parameter. (RFC6749)
	ResponseTypeParam = "response_type"
	// ResponseURIParam is the parameter name for the OpenID4VP response_uri parameter.
	ResponseURIParam = "response_uri"
	// ScopeParam is the parameter name for the scope parameter. (RFC6749)
	ScopeParam = "scope"
	// StateParam is the parameter name for the state parameter. (RFC6749)
	StateParam = "state"
	// VpTokenParam is the parameter name for the vp_token parameter. (OpenID4VP)
	VpTokenParam = "vp_token"
	// WalletMetadataParam is used by the wallet to provide its metadata in an authorization request when RequestURIMethodParam is 'post'
	WalletMetadataParam = "wallet_metadata"
	// WalletNonceParam is a wallet generated nonce to prevent authorization request replay when RequestURIMethodParam is 'post'
	WalletNonceParam = "wallet_nonce"
)

oauth parameter keys

View Source
const (
	// AuthorizationCodeGrantType is the grant_type for the authorization_code grant type. (RFC6749)
	AuthorizationCodeGrantType = "authorization_code"
	// PreAuthorizedCodeGrantType is the grant_type for the pre-authorized_code grant type. (OpenID4VCI)
	PreAuthorizedCodeGrantType = "urn:ietf:params:oauth:grant-type:pre-authorized_code"
	// VpTokenGrantType is the grant_type for the vp_token-bearer grant type. (RFC021)
	VpTokenGrantType = "vp_token-bearer"
)

grant types

View Source
const (
	// CodeResponseType is the parameter name for the code parameter. (RFC6749)
	CodeResponseType = "code"
	// VPTokenResponseType is paramter name for the vp_token repsponse type. (OpenID4VP)
	VPTokenResponseType = "vp_token"
)

response types

View Source
const (
	// ErrorParam is the parameter name for the error parameter
	ErrorParam = "error"
	// ErrorDescriptionParam is the parameter name for the error_description parameter
	ErrorDescriptionParam = "error_description"
)
View Source
const CallbackPath = "callback"

CallbackPath is the node specific callback for an OAuth flow. The full callback URL is constructed as <node_url>/iam/{id}/callback

Variables

This section is empty.

Functions

func DefaultOpenIDSupportedFormats

func DefaultOpenIDSupportedFormats() map[string]map[string][]string

DefaultOpenIDSupportedFormats returns the OpenID formats supported by the Nuts node and is used in the

  • Authorization Server's metadata field `vp_formats_supported`
  • Client's metadata field `vp_formats`

TODO: spec is very unclear about this part. See https://github.com/nuts-foundation/nuts-node/issues/2447

func IssuerIdToWellKnown

func IssuerIdToWellKnown(issuer string, wellKnown string, strictmode bool) (*url.URL, error)

IssuerIdToWellKnown converts the OAuth2 Issuer identity to the specified well-known endpoint by inserting the well-known at the root of the path. It returns no url and an error when issuer is not a valid URL.

Types

type AuthorizationServerMetadata

type AuthorizationServerMetadata struct {
	// Issuer defines the authorization server's identifier, which is a URL that uses the "https" scheme and has no query or fragment components.
	Issuer string `json:"issuer,omitempty"`

	// AuthorizationEndpoint defines the URL of the authorization server's authorization endpoint [RFC6749]
	AuthorizationEndpoint string `json:"authorization_endpoint,omitempty"`

	// ResponseTypesSupported defines what response types a client can request
	ResponseTypesSupported []string `json:"response_types_supported,omitempty"`

	// ResponseModesSupported defines what response modes a client can request
	// Currently supports
	// - query for response_type=code
	// - direct_post for response_type=["vp_token", "vp_token id_token"]
	// TODO: is `form_post` something we want in the future?
	ResponseModesSupported []string `json:"response_modes_supported,omitempty"`

	// TokenEndpoint defines the URL of the authorization server's token endpoint [RFC6749].
	TokenEndpoint string `json:"token_endpoint,omitempty"`

	// GrantTypesSupported is a list of the OAuth 2.0 grant type values that this authorization server supports.
	GrantTypesSupported []string `json:"grant_types_supported,omitempty"`

	// PreAuthorizedGrantAnonymousAccessSupported indicates whether anonymous access (requests without client_id) for pre-authorized code grant flows.
	// See https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0.html#name-oauth-20-authorization-serv
	PreAuthorizedGrantAnonymousAccessSupported bool `json:"pre-authorized_grant_anonymous_access_supported,omitempty"`

	// PresentationDefinitionEndpoint defines the URL of the authorization server's presentation definition endpoint.
	// See https://nuts-foundation.gitbook.io/drafts/rfc/rfc021-vp_token-grant-type
	PresentationDefinitionEndpoint string `json:"presentation_definition_endpoint,omitempty"`

	// PresentationDefinitionUriSupported specifies whether the Wallet supports the transfer of presentation_definition by reference, with true indicating support.
	// If omitted, the default value is true. (hence pointer, or add custom unmarshalling)
	PresentationDefinitionUriSupported *bool `json:"presentation_definition_uri_supported,omitempty"`

	// VPFormatsSupported is an object containing a list of key value pairs, where the key is a string identifying a Credential format supported by the Wallet.
	VPFormatsSupported map[string]map[string][]string `json:"vp_formats_supported,omitempty"`

	// VPFormats is an object containing a list of key value pairs, where the key is a string identifying a Credential format supported by the Verifier.
	// TODO: Remove. VPFormatsSupported is the correct param, but the OpenID4VP spec is ambiguous so support both for now.
	VPFormats map[string]map[string][]string `json:"vp_formats,omitempty"`

	// ClientIdSchemesSupported defines the `client_id_schemes` currently supported.
	// If omitted, the default value is `pre-registered` (referring to the client), which is currently not supported.
	ClientIdSchemesSupported []string `json:"client_id_schemes_supported,omitempty"`

	// DIDMethodsSupported is a JSON array containing a list of the DID Methods (without scheme 'did:') that are supported by the Authorization Server.
	// Note: this is a custom parameter, not part of the OpenID4VC specifications.
	DIDMethodsSupported []string `json:"did_methods_supported,omitempty"`

	// DPoPSigningAlgValuesSupported is a JSON array containing a list of the DPoP proof JWS signing algorithms ("alg" values) supported by the token endpoint.
	DPoPSigningAlgValuesSupported []string `json:"dpop_signing_alg_values_supported,omitempty"`

	// RequireSignedRequestObject specifies if the authorization server requires the use of signed request objects.
	RequireSignedRequestObject bool `json:"require_signed_request_object,omitempty"`

	// RequestObjectSigningAlgValuesSupported is a JSON array containing a list of the JWS signing algorithms (alg values) supported by the OP for Request Objects, which are described in Section 6.1 of OpenID Connect Core 1.0 [OpenID.Core].
	// These algorithms are used both when the Request Object is passed by value (using the request parameter) and when it is passed by reference (using the request_uri parameter).
	RequestObjectSigningAlgValuesSupported []string `json:"request_object_signing_alg_values_supported,omitempty"`
}

AuthorizationServerMetadata defines the OAuth Authorization Server metadata. Specified by https://www.rfc-editor.org/rfc/rfc8414.txt

func (AuthorizationServerMetadata) SupportsClientIDScheme

func (m AuthorizationServerMetadata) SupportsClientIDScheme(scheme string) bool

SupportsClientIDScheme checks if the Authorization Server supports the given client ID scheme.

type EntityStatementMetadata

type EntityStatementMetadata struct {
	// OpenIDProvider: the metadata of the OpenID provider
	OpenIDProvider AuthorizationServerMetadata `json:"openid_provider"`
}

EntityStatementMetadata represents the metadata of an openID federation entity statement We only use the OpenID provider metadata

type ErrorCode

type ErrorCode string

ErrorCode specifies error codes as defined by the OAuth2 specifications. Codes and descriptions are taken from https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1

const (
	// InvalidGrant is returned when the authorization grant or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.
	InvalidGrant ErrorCode = "invalid_grant"
	// InvalidRequest is returned when the request is missing a required parameter, includes an invalid parameter value,
	// includes a parameter more than once, or is otherwise malformed.
	InvalidRequest ErrorCode = "invalid_request"
	// AccessDenied is returned wthen the resource owner or authorization server denied the
	// request.
	AccessDenied ErrorCode = "access_denied"
	// UnsupportedGrantType is returned when the authorization grant type is not supported by the authorization server.
	UnsupportedGrantType ErrorCode = "unsupported_grant_type"
	// UnsupportedResponseType is returned when the authorization server does not support obtaining an authorization code using this method.
	UnsupportedResponseType ErrorCode = "unsupported_response_type"
	// ServerError is returned when the Authorization Server encounters an unexpected condition that prevents it from fulfilling the request.
	ServerError ErrorCode = "server_error"
	// InvalidDPopProof is returned when the DPoP proof is invalid or missing.
	InvalidDPopProof ErrorCode = "invalid_dpop_proof"
	// InvalidScope is returned when the requested scope is invalid, unknown or malformed.
	InvalidScope ErrorCode = "invalid_scope"
	// InvalidPresentationDefinitionURI is returned when the requested presentation definition URI is invalid or can't be reached.
	InvalidPresentationDefinitionURI ErrorCode = "invalid_presentation_definition_uri"
	// InvalidRequestObject is returned when the JAR Request Object signature validation or decryption fails. (RFC9101)
	InvalidRequestObject ErrorCode = "invalid_request_object"
	// InvalidRequestURI is returned whn the request_uri in the authorization request returns an error or contains invalid data. (RFC9101)
	InvalidRequestURI ErrorCode = "invalid_request_uri"
	// InvalidRequestURIMethod is returned when the request_uri_method is not 'get' or 'post'. (OpenID4VP)
	InvalidRequestURIMethod ErrorCode = "invalid_request_uri_method"
)

type OAuth2Error

type OAuth2Error struct {
	// Code is the error code as defined by the OAuth2 specification.
	Code ErrorCode `json:"error"`
	// Description is a human-readable ASCII [USASCII] text providing additional information, used to assist the client developer in understanding the error that occurred.
	Description string `json:"error_description,omitempty"`
	// InternalError is the underlying error, may be omitted. It is not intended to be returned to the client, only to be logged.
	InternalError error `json:"-"`
	// RedirectURI is the redirect URI that should be used to redirect the client to, in case the user-agent is a browser.
	// It should not be set if the user-agent is not a browser, or there is no redirect_uri (because the request was malformed), this field is empty.
	// When the field is set, the user-agent is redirected to the specified URI with the error code and description as query parameters.
	// If it's not set, the error code and description are returned in the response body (plain text or JSON).
	RedirectURI *url.URL `json:"-"`
}

OAuth2Error is an OAuth2 error that signals the error was (probably) caused by the client (e.g. bad request), or that the client can recover from the error (e.g. retry).

func TestOAuthErrorCode

func TestOAuthErrorCode(responseBody []byte, code ErrorCode) (bool, OAuth2Error)

TestOAuthErrorCode tests if the response is an OAuth2 error with the given code. Also returns the unmarshalled OAuth2Error

func (OAuth2Error) Error

func (e OAuth2Error) Error() string

OAuth2Error returns the error message, which is either the underlying error or the code if there is no underlying error

func (OAuth2Error) StatusCode

func (e OAuth2Error) StatusCode() int

StatusCode returns the HTTP status code to be returned to the client, in case the user-agent can't be redirected with HTTP 302 - Found.

type OAuthClientMetadata

type OAuthClientMetadata struct {
	// RedirectURIs lists all URIs that the client may use in any redirect-based flow.
	// From https://www.rfc-editor.org/rfc/rfc7591.html
	RedirectURIs []string `json:"redirect_uris,omitempty"`

	// TODO: What do we use? Must provide a value if its not "client_secret_basic"
	// TokenEndpointAuthMethod indicator of the requested authentication method for the token endpoint.
	// If unspecified or omitted, the default is "client_secret_basic", denoting the HTTP Basic authentication scheme as specified in Section 2.3.1 of OAuth 2.0.
	// Examples are: none, client_secret_post, client_secret_basic, tls_client_auth.
	// From https://www.rfc-editor.org/rfc/rfc7591.html
	// TODO: Can "tls_client_auth" replace /n2n/ for pre-authorized_code flow? https://www.rfc-editor.org/rfc/rfc8705.html
	TokenEndpointAuthMethod string `json:"token_endpoint_auth_method,omitempty"`

	// GrantTypes lists all supported grant_types. Defaults to "authorization_code" if omitted.
	// From https://www.rfc-editor.org/rfc/rfc7591.html
	GrantTypes []string `json:"grant_types,omitempty"`

	// ResponseTypes lists all supported response_types. Defaults to "code". Must contain the values corresponding to listed GrantTypes.
	// From https://www.rfc-editor.org/rfc/rfc7591.html
	ResponseTypes []string `json:"response_types,omitempty"`

	// Scope contains a space-separated list of scopes the client can request.
	// From https://www.rfc-editor.org/rfc/rfc7591.html
	// TODO: I don't see the use for this. The idea is that an AS does not assign scopes to a client that it does not support (or wants to request at any time), but seems like unnecessary complexity for minimal safety.
	Scope string `json:"scope,omitempty"`

	// Contacts contains an array of strings representing ways to contact people responsible for this client, typically email addresses.
	// From https://www.rfc-editor.org/rfc/rfc7591.html
	// TODO: remove? Can plug DID docs contact info.
	Contacts []string `json:"contacts,omitempty"`

	// JwksURI URL string referencing the client's JSON Web Key (JWK) Set [RFC7517] document, which contains the client's public keys.
	// From https://www.rfc-editor.org/rfc/rfc7591.html
	// TODO: remove? Can list the DID's keys. Could be useful if authorization without DIDs/VCs is needed.
	// TODO: In EBSI it is a required field for the Service Wallet Metadata https://api-conformance.ebsi.eu/docs/ct/providers-and-wallets-metadata#service-wallet-metadata
	JwksURI string `json:"jwks_uri,omitempty"`
	// Jwks includes the JWK Set of a client. Mutually exclusive with JwksURI.
	// From https://www.rfc-editor.org/rfc/rfc7591.html
	Jwks any `json:"jwks,omitempty"`

	// SoftwareID is a unique identifier string (e.g., a Universally Unique Identifier (UUID)) assigned by the client developer.
	// From https://www.rfc-editor.org/rfc/rfc7591.html
	SoftwareID string `json:"software_id,omitempty"`
	// SoftwareVersion is a version identifier string for the client software identified by "software_id".
	// From https://www.rfc-editor.org/rfc/rfc7591.html
	// TODO: Including a software_id + software_version could provide us with some upgrade paths in the future.
	SoftwareVersion string `json:"software_version,omitempty"`

	// CredentialOfferEndpoint contains a URL where the pre-authorized_code flow offers a credential.
	// https://openid.bitbucket.io/connect/openid-4-verifiable-credential-issuance-1_0.html#name-client-metadata
	// TODO: openid4vci duplicate. Also defined on /.well-known/openid-credential-wallet to be /n2n/identity/{did}/openid4vci/credential_offer
	CredentialOfferEndpoint string `json:"credential_offer_endpoint,omitempty"`

	/*********** OpenID4VP ***********/
	// VPFormats lists the vp_formats supported by the client. See additional comments on vpFormatsSupported.
	// https://openid.bitbucket.io/connect/openid-4-verifiable-presentations-1_0.html#name-verifier-metadata-client-me
	VPFormats map[string]map[string][]string `json:"vp_formats,omitempty"`

	// ClientIdScheme is a string identifying the Client Identifier scheme. The value range defined by this specification is
	// pre-registered, redirect_uri, entity_id, did. If omitted, the default value is pre-registered.
	// https://openid.bitbucket.io/connect/openid-4-verifiable-presentations-1_0.html#name-verifier-metadata-client-me
	ClientIdScheme string `json:"client_id_scheme,omitempty"`
}

OAuthClientMetadata defines the OAuth Client metadata. Specified by https://www.rfc-editor.org/rfc/rfc7591.html and elsewhere.

type Oauth2ErrorWriter

type Oauth2ErrorWriter struct {
	HtmlPageTemplate *template.Template
}

Oauth2ErrorWriter is a HTTP response writer for OAuth errors

func (Oauth2ErrorWriter) Write

func (p Oauth2ErrorWriter) Write(echoContext echo.Context, _ int, _ string, err error) error

type OpenIDConfiguration

type OpenIDConfiguration struct {
	// Issuer: an url representing the issuer of the entity statement
	// for now we keep it teh same as the subject, eg the subject/tenant
	Issuer string `json:"iss"`
	// Subject: an url representing the subject of the entity statement
	Subject string `json:"sub"`
	// IssuedAt: the time the entity statement was issued
	IssuedAt int64 `json:"iat"`
	// Expiration: the time after which the entity statement may no longer be processed
	Expiration int64 `json:"exp"`
	// JWKs is the JSON Web Key Set of the entity statement. Contains keys of all DIDs for the subject
	JWKs jwk.Set `json:"jwks"`
	// Metadata: the metadata of the entity statement
	Metadata EntityStatementMetadata `json:"metadata"`
}

OpenIDConfiguration represents the OpenID configuration It contains the minimal information required for OpenID4VP, the required `jwks` is also omitted see https://openid.net/specs/openid-connect-federation-1_0-29.html#entity-statement

func (*OpenIDConfiguration) UnmarshalJSON

func (j *OpenIDConfiguration) UnmarshalJSON(bytes []byte) error

UnmarshalJSON parses the OpenIDConfiguration from JSON

type OpenIDCredentialIssuerMetadata

type OpenIDCredentialIssuerMetadata struct {
	// - CredentialIssuer: an url representing the credential issuer
	CredentialIssuer string `json:"credential_issuer"`
	// - CredentialEndpoint: an url representing the credential endpoint
	CredentialEndpoint string `json:"credential_endpoint"`
	// - AuthorizationServers: a slice of urls representing the authorization servers (optional)
	AuthorizationServers []string `json:"authorization_servers,omitempty"`
	// - Display: a slice of maps where each map represents the display information (optional)
	Display []map[string]string `json:"display,omitempty"`
}

OpenIDCredentialIssuerMetadata represents the metadata of an OpenID credential issuer

type Redirect

type Redirect struct {
	// RedirectURI is the URI to redirect the user-agent to.
	RedirectURI string `json:"redirect_uri"`
}

Redirect is the response from the verifier on the direct_post authorization response.

type TokenResponse

type TokenResponse struct {
	AccessToken string  `json:"access_token"`
	DPoPKid     *string `json:"dpop_kid,omitempty"`
	ExpiresAt   *int    `json:"expires_at,omitempty"`
	ExpiresIn   *int    `json:"expires_in,omitempty"`
	TokenType   string  `json:"token_type"`
	Scope       *string `json:"scope,omitempty"`
	// contains filtered or unexported fields
}

TokenResponse is the OAuth access token response. Through With() and Get() additional parameters (for OpenID4VCI, for instance) can be set and retrieved.

func (TokenResponse) Get

func (t TokenResponse) Get(key string) string

Get returns the value of the additional parameter with the given key as a string. If the key does not exist or the value is not a string, it returns an empty string. It should not be used to get any of the base parameters (access_token, expires_in, token_type, scope).

func (TokenResponse) MarshalJSON

func (t TokenResponse) MarshalJSON() ([]byte, error)

func (*TokenResponse) UnmarshalJSON

func (t *TokenResponse) UnmarshalJSON(data []byte) error

func (*TokenResponse) With

func (t *TokenResponse) With(key string, value interface{}) *TokenResponse

With adds a parameter to the token response. It's a builder-style function. It should not be used to set any of the base parameters (access_token, expires_in, token_type, scope).

Jump to

Keyboard shortcuts

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