auth

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: Apache-2.0 Imports: 19 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

Index

Constants

View Source
const DefaultCallbackPort = config.DefaultOAuthCallbackPort

DefaultCallbackPort is the default port for the OAuth2 callback server.

Variables

This section is empty.

Functions

func GenerateState

func GenerateState() (string, error)

GenerateState generates a random state parameter for CSRF protection

func GetTokenFromConfig

func GetTokenFromConfig(cfg *config.Config) (string, bool)

GetTokenFromConfig checks for token in config file

func GetTokenFromEnv

func GetTokenFromEnv() (string, bool)

GetTokenFromEnv checks for token in environment variables Checks: NR_API_TOKEN, NODERINGS_API_TOKEN

func OpenBrowser

func OpenBrowser(url string) error

OpenBrowser opens the authorization URL in the default browser. On failure it returns a short error without embedding the URL; callers print the URL once.

Types

type OAuthClient

type OAuthClient struct {
	// contains filtered or unexported fields
}

OAuthClient handles OAuth2 authentication flows with PKCE

func NewOAuthClient

func NewOAuthClient(config *OAuthConfig) *OAuthClient

NewOAuthClient creates a new OAuth client

func (*OAuthClient) ExchangeCode

func (c *OAuthClient) ExchangeCode(ctx context.Context, code string) (*Token, error)

ExchangeCode exchanges authorization code for access token using PKCE

func (*OAuthClient) GetState

func (c *OAuthClient) GetState() string

GetState returns the current state value (for callback verification)

func (*OAuthClient) InitAuthFlow

func (c *OAuthClient) InitAuthFlow() error

InitAuthFlow initializes PKCE and state parameters (called before starting callback server)

func (*OAuthClient) RefreshToken

func (c *OAuthClient) RefreshToken(ctx context.Context, refreshToken string) (*Token, error)

RefreshToken exchanges refresh token for new access token

func (*OAuthClient) RevokeToken

func (c *OAuthClient) RevokeToken(ctx context.Context, token string, tokenTypeHint string) error

RevokeToken revokes an access or refresh token

func (*OAuthClient) StartAuthFlow

func (c *OAuthClient) StartAuthFlow(ctx context.Context) (string, error)

StartAuthFlow generates the authorization URL (called after callback server has updated redirect URL)

func (*OAuthClient) StartCallbackServer

func (c *OAuthClient) StartCallbackServer(ctx context.Context, expectedState string) (string, error)

StartCallbackServer starts a local HTTP server to receive the authorization callback Uses a fixed port (DefaultCallbackPort) for simplicity and consistency

type OAuthConfig

type OAuthConfig struct {
	ClientID    string
	AuthURL     string
	TokenURL    string
	RedirectURL string
	Scopes      []string
	APIBaseURL  string
	Debug       bool // Enable debug logging for troubleshooting
	TLSInsecure bool // Skip TLS certificate verification (development only)
}

OAuthConfig holds OAuth2 configuration

type PKCEParams

type PKCEParams struct {
	CodeVerifier  string
	CodeChallenge string
	Method        string
}

PKCEParams holds PKCE code verifier and challenge

func GeneratePKCE

func GeneratePKCE() (*PKCEParams, error)

GeneratePKCE generates PKCE code verifier and challenge

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage handles secure token storage

func NewStorage

func NewStorage(configDir string) (*Storage, error)

NewStorage creates a new token storage

func (*Storage) DeleteToken

func (s *Storage) DeleteToken() error

DeleteToken removes a token from storage

func (*Storage) LoadToken

func (s *Storage) LoadToken() (*Token, error)

LoadToken loads a token from secure storage

func (*Storage) SaveToken

func (s *Storage) SaveToken(token *Token) error

SaveToken saves a token securely

type Token

type Token struct {
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token"`
	ExpiresAt    time.Time `json:"expires_at"`
	TokenType    string    `json:"token_type"`
	Scope        string    `json:"scope"`
}

Token represents an authentication token

func (*Token) IsExpired

func (t *Token) IsExpired() bool

IsExpired checks if the token is expired

func (*Token) IsExpiringSoon

func (t *Token) IsExpiringSoon(threshold time.Duration) bool

IsExpiringSoon checks if the token is expiring soon (within threshold)

type TokenInfo

type TokenInfo struct {
	Token        string
	Source       TokenSource
	IsOAuthToken bool   // true if this is an OAuth token with refresh capability
	OAuthToken   *Token // Only set if IsOAuthToken is true
}

TokenInfo contains token information and its source

func LoadTokenInfo

func LoadTokenInfo(cfg *config.Config, configDir string) (*TokenInfo, error)

LoadTokenInfo loads token from various sources in priority order: 1. Environment variables (highest priority) - for service account tokens 2. Config file - for service account tokens 3. OAuth token storage - for OAuth tokens (via 'nr auth login')

Both OAuth and Service Account tokens are fully supported. The priority order determines which token is used when multiple are available.

type TokenSource

type TokenSource string

TokenSource represents where a token was loaded from

const (
	TokenSourceEnvVar     TokenSource = "environment_variable"
	TokenSourceConfigFile TokenSource = "config_file"
	TokenSourceOAuth      TokenSource = "oauth"
	TokenSourceUnknown    TokenSource = "unknown"
)

Jump to

Keyboard shortcuts

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