oauth2providertest

package
v2.25.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2025 License: AGPL-3.0 Imports: 15 Imported by: 0

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"

	// Invalid PKCE verifier for negative testing
	InvalidCodeVerifier = "wrong-verifier"
)

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