Documentation
¶
Overview ¶
Package oauth2providertest provides comprehensive testing utilities for OAuth2 identity provider functionality. It includes helpers for creating OAuth2 apps, performing authorization flows, token exchanges, PKCE challenge generation and verification, and testing error scenarios.
Index ¶
- Constants
- Variables
- func AuthorizeOAuth2App(t *testing.T, client *codersdk.Client, baseURL string, params AuthorizeParams) string
- func AuthorizeOAuth2AppExpectingRedirectError(t *testing.T, client *codersdk.Client, baseURL string, params AuthorizeParams, ...)
- func CleanupOAuth2App(t *testing.T, client *codersdk.Client, appID uuid.UUID)
- func CreateTestOAuth2App(t *testing.T, client *codersdk.Client) (*codersdk.OAuth2ProviderApp, string)
- func EnableDCR(t *testing.T, client *codersdk.Client)
- func ExchangeCodeForToken(t *testing.T, baseURL string, params TokenExchangeParams) *oauth2.Token
- func FetchOAuth2Metadata(t *testing.T, baseURL string) map[string]any
- func GenerateCodeChallenge(verifier string) string
- func GeneratePKCE(t *testing.T) (verifier, challenge string)
- func GenerateState(t *testing.T) string
- func PerformTokenExchangeExpectingError(t *testing.T, baseURL string, params TokenExchangeParams, expectedError string)
- func RegisterPublicClient(t *testing.T, client *codersdk.Client, name, redirectURI string) codersdk.OAuth2ClientRegistrationResponse
- func RegisterPublicClientWithRedirectURIs(t *testing.T, client *codersdk.Client, name string, redirectURIs ...string) codersdk.OAuth2ClientRegistrationResponse
- func RequireOAuth2Error(t *testing.T, resp *http.Response, expectedError string)
- type AuthorizeParams
- type OAuth2Error
- type TokenExchangeParams
Constants ¶
const ( // TestRedirectURI is the standard test redirect URI TestRedirectURI = "http://localhost:9876/callback" // TestResourceURI is used for testing resource parameter TestResourceURI = "https://api.example.com" // InvalidCodeVerifier is well-formed (43 characters, RFC 7636 §4.1's // unreserved set) but does not hash to any issued challenge, so it // exercises the PKCE comparison failure (invalid_grant) rather than the // length/charset check (invalid_request). InvalidCodeVerifier = "wrong-verifier-that-is-well-formed-43-chars" // MalformedCodeVerifier is below RFC 7636 §4.1's 43-character floor, so // it exercises the length/charset check (invalid_request) instead of the // PKCE comparison. MalformedCodeVerifier = "too-short" )
Test constants for OAuth2 testing
Variables ¶
var OAuth2ErrorTypes = struct { InvalidRequest string InvalidClient string InvalidGrant string UnauthorizedClient string UnsupportedGrantType string InvalidScope string }{ InvalidRequest: "invalid_request", InvalidClient: "invalid_client", InvalidGrant: "invalid_grant", UnauthorizedClient: "unauthorized_client", UnsupportedGrantType: "unsupported_grant_type", InvalidScope: "invalid_scope", }
OAuth2ErrorTypes contains standard OAuth2 error codes
Functions ¶
func AuthorizeOAuth2App ¶
func AuthorizeOAuth2App(t *testing.T, client *codersdk.Client, baseURL string, params AuthorizeParams) string
AuthorizeOAuth2App performs the OAuth2 authorization flow and returns the authorization code
func AuthorizeOAuth2AppExpectingRedirectError ¶
func AuthorizeOAuth2AppExpectingRedirectError(t *testing.T, client *codersdk.Client, baseURL string, params AuthorizeParams, expectedError codersdk.OAuth2ErrorCode, wantDescription string)
AuthorizeOAuth2AppExpectingRedirectError performs the OAuth2 authorization flow expecting a rejection, which RFC 6749 §4.1.2.1 delivers to the redirect URI the app registered rather than as a status code on this server.
wantDescription is asserted as a substring of error_description. Without it every caller reduces to the same four assertions, and one blanket invalid_request would satisfy all of them.
func CleanupOAuth2App ¶
CleanupOAuth2App deletes an OAuth2 app (helper for test cleanup)
func CreateTestOAuth2App ¶
func CreateTestOAuth2App(t *testing.T, client *codersdk.Client) (*codersdk.OAuth2ProviderApp, string)
CreateTestOAuth2App creates an OAuth2 app for testing and returns the app and client secret
func EnableDCR ¶ added in v2.37.0
EnableDCR turns on dynamic client registration for the deployment. DCR defaults to disabled, so any test that registers a client via POST /oauth2/register must call this first. The caller-provided client must have owner-level permissions.
func ExchangeCodeForToken ¶
ExchangeCodeForToken exchanges an authorization code for tokens
func FetchOAuth2Metadata ¶
FetchOAuth2Metadata fetches and returns OAuth2 authorization server metadata
func GenerateCodeChallenge ¶
GenerateCodeChallenge creates an S256 code challenge from a verifier
func GeneratePKCE ¶
GeneratePKCE generates a random PKCE code verifier and challenge
func GenerateState ¶
GenerateState generates a random state parameter
func PerformTokenExchangeExpectingError ¶
func PerformTokenExchangeExpectingError(t *testing.T, baseURL string, params TokenExchangeParams, expectedError string)
PerformTokenExchangeExpectingError performs a token exchange expecting an OAuth2 error
func RegisterPublicClient ¶ added in v2.37.0
func RegisterPublicClient(t *testing.T, client *codersdk.Client, name, redirectURI string) codersdk.OAuth2ClientRegistrationResponse
RegisterPublicClient registers a public (secretless, PKCE-only) OAuth2 client via RFC 7591 dynamic registration, the only way to create one. This is the public counterpart to CreateTestOAuth2App. The caller must call EnableDCR first, and needs owner-level permissions to do so.
func RegisterPublicClientWithRedirectURIs ¶
func RegisterPublicClientWithRedirectURIs(t *testing.T, client *codersdk.Client, name string, redirectURIs ...string) codersdk.OAuth2ClientRegistrationResponse
RegisterPublicClientWithRedirectURIs registers a public client with every redirect URI given. The first becomes the primary callback.
Types ¶
type AuthorizeParams ¶
type AuthorizeParams struct {
ClientID string
ResponseType string
RedirectURI string
State string
CodeChallenge string
CodeChallengeMethod string
Resource string
Scope string
}
AuthorizeParams contains parameters for OAuth2 authorization
type OAuth2Error ¶
type OAuth2Error struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description,omitempty"`
}
OAuth2Error represents an OAuth2 error response