OPEN SWAG GO
Features

Auth Playground

pkg/auth provides a PlaygroundConfig describing the authentication options exposed to API consumers — which schemes are available, which is selected by default, whether credentials persist, and any prefilled values.

import "github.com/gopackx/open-swag-go/pkg/auth"

PlaygroundConfig

playground_config.go
type PlaygroundConfig struct {
	Enabled            bool              // Enable the playground
	DefaultScheme      string            // Pre-selected scheme name
	PersistCredentials bool              // Remember credentials across sessions
	Schemes            map[string]Scheme // Schemes keyed by name
	PrefilledValues    map[string]string // Prefilled values keyed by scheme name
}

Constructor and options

Build a *PlaygroundConfig with NewPlayground and functional options. NewPlayground defaults to Enabled: true and PersistCredentials: true.

func NewPlayground(opts ...PlaygroundOption) *PlaygroundConfig
 
func WithScheme(name string, scheme Scheme) PlaygroundOption
func WithDefaultScheme(name string) PlaygroundOption
func WithPersistence(enabled bool) PlaygroundOption
func WithPrefilledValue(key, value string) PlaygroundOption
func Disable() PlaygroundOption

Minimal setup

basic_playground.go
playground := auth.NewPlayground(
	auth.WithScheme("bearerAuth", auth.BearerAuth("JWT access token")),
	auth.WithDefaultScheme("bearerAuth"),
)

Available options

OptionDescription
WithScheme(name, scheme)Add a named scheme to the playground
WithDefaultScheme(name)Pre-select a scheme by name
WithPersistence(enabled)Toggle credential persistence
WithPrefilledValue(key, value)Prefill a value for a scheme
Disable()Disable the playground entirely

Credential persistence

Persistence is a boolean — toggle it with WithPersistence:

persistence.go
// Remember credentials across sessions (default)
auth.NewPlayground(
	auth.WithScheme("bearerAuth", auth.BearerAuth("JWT access token")),
	auth.WithPersistence(true),
)
 
// Do not persist — credentials cleared on reload
auth.NewPlayground(
	auth.WithScheme("bearerAuth", auth.BearerAuth("JWT access token")),
	auth.WithPersistence(false),
)

Multiple auth schemes

Add several schemes; users pick the one that matches their integration.

multi_scheme_playground.go
bearer := auth.BearerAuth("JWT access token")
apiKey := auth.APIKeyHeader("X-API-Key", "API key for machine-to-machine access")
 
playground := auth.NewPlayground(
	auth.WithScheme("bearerAuth", bearer),
	auth.WithScheme("apiKey", apiKey),
	auth.WithDefaultScheme("bearerAuth"),
	auth.WithPrefilledValue("apiKey", "demo-key-123"),
)

Full Example

full_example.go
package main
 
import (
	"fmt"
 
	"github.com/gopackx/open-swag-go/pkg/auth"
)
 
func main() {
	playground := auth.NewPlayground(
		auth.WithScheme("bearerAuth", auth.BearerAuth("JWT access token")),
		auth.WithDefaultScheme("bearerAuth"),
		auth.WithPersistence(true),
	)
 
	fmt.Printf("playground enabled: %v, schemes: %d\n",
		playground.Enabled, len(playground.Schemes))
}