auth

package
v0.2.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package auth provides browser-based authentication for the StackEye CLI.

This package handles the OAuth-like flow for CLI authentication:

  1. Start a local callback server on a random port
  2. Open the user's browser to the StackEye web UI /cli-auth page
  3. Wait for the callback with the API key
  4. Return the authentication result

The actual API key storage is handled by the stackeye-go-sdk/config package. This package only handles the browser flow mechanics.

Usage:

result, err := auth.BrowserLogin(auth.Options{
    APIURL:  "https://api.stackeye.io",
    Timeout: 5 * time.Minute,
})
if err != nil {
    return err
}
fmt.Printf("Logged in as org: %s\n", result.OrgName)

Package auth provides browser-based authentication for the StackEye CLI.

Example

Example shows basic usage of the auth package.

result, err := BrowserLogin(Options{
	APIURL:          "https://api.stackeye.io",
	Timeout:         5 * time.Minute,
	SkipBrowserOpen: true, // Skip browser in tests
	OnBrowserOpen: func(url string) {
		fmt.Printf("Please visit: %s\n", url)
	},
	OnWaiting: func() {
		fmt.Println("Waiting for authentication...")
	},
})
if err != nil {
	fmt.Printf("Login failed: %v\n", err)
	return
}

fmt.Printf("Logged in to org: %s\n", result.OrgName)

Index

Examples

Constants

View Source
const (
	// DefaultTimeout is the maximum time to wait for the browser callback.
	DefaultTimeout = 5 * time.Minute

	// DefaultAPIURL is the production API endpoint.
	DefaultAPIURL = "https://api.stackeye.io"
)

Default configuration values.

Variables

View Source
var (
	// ErrTimeout is returned when the browser callback times out.
	ErrTimeout = errors.New("auth: login timed out waiting for browser callback")

	// ErrCanceled is returned when the login is canceled.
	ErrCanceled = errors.New("auth: login canceled")

	// ErrInvalidAPIKey is returned when the received API key is invalid.
	ErrInvalidAPIKey = errors.New("auth: received invalid API key format")

	// ErrMissingAPIKey is returned when the callback is missing the API key.
	ErrMissingAPIKey = errors.New("auth: callback missing api_key parameter")

	// ErrForbidden is returned when a non-localhost request is received.
	ErrForbidden = errors.New("auth: request from non-localhost IP rejected")
)

Common errors returned by this package.

Functions

func APIURLToWebURL

func APIURLToWebURL(apiURL string) (string, error)

APIURLToWebURL converts an API URL to the corresponding web UI URL.

Transformations based on actual infrastructure:

  • api.stackeye.io -> app.stackeye.io (production)
  • api-dev.stackeye.io -> app-dev.stackeye.io
  • api-staging.stackeye.io -> app-staging.stackeye.io

For non-standard URLs (e.g., localhost, custom domains), returns unchanged.

func BuildWebUIURL

func BuildWebUIURL(apiURL, callbackURL string) (string, error)

BuildWebUIURL constructs the web UI authentication URL.

Parameters:

Returns the full web UI URL with the callback parameter.

func IsLocalhost

func IsLocalhost(ip string) bool

IsLocalhost checks if an IP address is a localhost address. Accepts both IPv4 (127.x.x.x) and IPv6 (::1) localhost addresses.

func OpenBrowser

func OpenBrowser(url string) error

OpenBrowser opens the specified URL in the default browser. Returns an error if the browser cannot be opened.

func SetDebug

func SetDebug(enabled bool)

SetDebug enables or disables debug logging for the auth package.

Types

type Options

type Options struct {
	// APIURL is the StackEye API URL to authenticate against.
	// If empty, defaults to DefaultAPIURL.
	APIURL string

	// Timeout is the maximum time to wait for the browser callback.
	// If zero, defaults to DefaultTimeout.
	Timeout time.Duration

	// OnBrowserOpen is called when the browser is about to be opened.
	// If nil, a default message is printed to stdout.
	OnBrowserOpen func(url string)

	// OnWaiting is called while waiting for the browser callback.
	// If nil, a default message is printed to stdout.
	OnWaiting func()

	// SkipBrowserOpen prevents the actual browser from opening.
	// Useful for testing - the OnBrowserOpen callback is still called.
	SkipBrowserOpen bool
}

Options configures the browser login flow.

type Result

type Result struct {
	// APIKey is the generated API key for CLI authentication.
	APIKey string

	// OrgID is the organization ID associated with the API key.
	OrgID string

	// OrgName is the organization name associated with the API key.
	OrgName string
}

Result holds the authentication result from a successful browser login.

func BrowserLogin

func BrowserLogin(opts Options) (*Result, error)

BrowserLogin performs browser-based authentication.

This function:

  1. Starts a local HTTP server on a random port
  2. Opens the browser to the StackEye web UI /cli-auth page
  3. Waits for the callback with the API key
  4. Returns the authentication result

The caller is responsible for storing the API key using the config package.

func BrowserLoginWithContext

func BrowserLoginWithContext(ctx context.Context, opts Options) (*Result, error)

BrowserLoginWithContext performs browser-based authentication with context support.

Jump to

Keyboard shortcuts

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