oauth2providertest

package
v2.39.5 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

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

func CleanupOAuth2App(t *testing.T, client *codersdk.Client, appID uuid.UUID)

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

func EnableDCR(t *testing.T, client *codersdk.Client)

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

func ExchangeCodeForToken(t *testing.T, baseURL string, params TokenExchangeParams) *oauth2.Token

ExchangeCodeForToken exchanges an authorization code for tokens

func FetchOAuth2Metadata

func FetchOAuth2Metadata(t *testing.T, baseURL string) map[string]any

FetchOAuth2Metadata fetches and returns OAuth2 authorization server metadata

func GenerateCodeChallenge

func GenerateCodeChallenge(verifier string) string

GenerateCodeChallenge creates an S256 code challenge from a verifier

func GeneratePKCE

func GeneratePKCE(t *testing.T) (verifier, challenge string)

GeneratePKCE generates a random PKCE code verifier and challenge

func GenerateState

func GenerateState(t *testing.T) string

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.

func RequireOAuth2Error

func RequireOAuth2Error(t *testing.T, resp *http.Response, expectedError string)

RequireOAuth2Error checks that the HTTP response contains an expected OAuth2 error

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

type TokenExchangeParams

type TokenExchangeParams struct {
	GrantType    string
	Code         string
	ClientID     string
	ClientSecret string
	CodeVerifier string
	RedirectURI  string
	RefreshToken string
	Resource     string
}

TokenExchangeParams contains parameters for token exchange

Jump to

Keyboard shortcuts

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