auth

package
v0.0.0-...-5f651bb Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 18 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthClient

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

AuthClient is the main struct for the auth client

func NewClient

func NewClient(cfg AuthClientConfig) *AuthClient

NewClient creates a new AuthClient with the given configuration

func (*AuthClient) AuthorizeWithLocalServer

func (c *AuthClient) AuthorizeWithLocalServer(ctx context.Context) (string, error)

AuthorizeWithLocalServer starts a local HTTP server and opens a browser to the Microsoft authorization URL. It returns the authorization code, which can be used to exchange for tokens.

func (*AuthClient) ClearCachedToken

func (c *AuthClient) ClearCachedToken(_ context.Context) error

ClearCachedToken removes the stored refresh token for the current username. If username is not set, this returns an error.

func (*AuthClient) GetUsername

func (c *AuthClient) GetUsername() string

GetUsername returns the current username for this client.

func (*AuthClient) ListCachedAccounts

func (c *AuthClient) ListCachedAccounts() ([]string, error)

ListCachedAccounts returns a list of all usernames that have cached tokens.

func (*AuthClient) Login

func (c *AuthClient) Login(ctx context.Context) (LoginData, error)

Login performs a cached login. It attempts to load a refresh token from the configured TokenStore (or the default file-based store), refreshes it, and completes the Microsoft/XBL/XSTS/Minecraft authentication flow. If no cached token exists or the refresh fails, it falls back to interactive auth via a local HTTP callback and browser, then saves the new refresh token.

The username for caching is determined by: 1. The Username field in AuthClientConfig if set 2. The username from the LoginData response (after successful login)

func (*AuthClient) LoginWithRefreshToken

func (c *AuthClient) LoginWithRefreshToken(ctx context.Context, refreshToken string) (LoginData, error)

LoginWithRefreshToken performs a login with a refresh token.

func (*AuthClient) SetUsername

func (c *AuthClient) SetUsername(username string)

SetUsername updates the username for this client. This affects which cached token is loaded/saved.

type AuthClientConfig

type AuthClientConfig struct {
	ClientID         string
	RedirectPort     int
	Scopes           []string
	HTTPClient       *http.Client
	TokenStore       TokenStore
	TokenStoreConfig TokenStoreConfig
	// Username is the Minecraft username to use for caching.
	// If empty, username from login response will be used.
	Username string
}

AuthClientConfig is the configuration for the auth client

type LoginData

type LoginData struct {
	AccessToken  string
	RefreshToken string
	UUID         string
	Username     string
}

LoginData is the data returned from a login

type MojangCertificate

type MojangCertificate struct {
	ExpiresAt            string        `json:"expiresAt"`
	KeyPair              MojangKeyPair `json:"keyPair"`
	PublicKeySignature   string        `json:"publicKeySignature"`
	PublicKeySignatureV2 string        `json:"publicKeySignatureV2"`
	RefreshedAfter       string        `json:"refreshedAfter"`
}

type MojangCertificateData

type MojangCertificateData struct {
	Certificate    *MojangCertificate
	PrivateKey     *rsa.PrivateKey
	PublicKey      *rsa.PublicKey
	PublicKeyBytes []byte // SPKI DER format
	SignatureBytes []byte // Mojang signature V2 (512 bytes)
	ExpiryTime     time.Time
}

func FetchMojangCertificate

func FetchMojangCertificate(accessToken string) (*MojangCertificateData, error)

FetchMojangCertificate retrieves the Mojang signing certificate for chat messages

type MojangKeyPair

type MojangKeyPair struct {
	PrivateKey string `json:"privateKey"`
	PublicKey  string `json:"publicKey"`
}

type TokenStore

type TokenStore interface {
	Save(username, refreshToken string) error
	Load(username string) (string, error)
	Clear(username string) error
	ListAccounts() ([]string, error)
}

TokenStore defines an interface to persist and retrieve refresh tokens for multiple accounts.

func NewTokenStore

func NewTokenStore(config TokenStoreConfig) (TokenStore, error)

NewTokenStore creates a new TokenStore based on the provided configuration. If config.Path is nil, uses the default path: ~/.mclib/credentials_cache.json If config.Path points to empty string, returns an in-memory store (no persistence). If config.Path points to a path, uses that path.

type TokenStoreConfig

type TokenStoreConfig struct {
	// Path is the file path for the credentials cache.
	// If nil: uses default path "~/.mclib/credentials_cache.json"
	// If points to empty string: use in-memory store (no persistence)
	// If points to a path: use that path
	Path *string
}

TokenStoreConfig holds configuration for creating a TokenStore.

Jump to

Keyboard shortcuts

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