Documentation
¶
Index ¶
- type AuthClient
- func (c *AuthClient) AuthorizeWithLocalServer(ctx context.Context) (string, error)
- func (c *AuthClient) ClearCachedToken(_ context.Context) error
- func (c *AuthClient) GetUsername() string
- func (c *AuthClient) ListCachedAccounts() ([]string, error)
- func (c *AuthClient) Login(ctx context.Context) (LoginData, error)
- func (c *AuthClient) LoginWithRefreshToken(ctx context.Context, refreshToken string) (LoginData, error)
- func (c *AuthClient) SetUsername(username string)
- type AuthClientConfig
- type CachedSession
- type LoginData
- type MojangCertificate
- type MojangCertificateData
- type MojangKeyPair
- type TokenStore
- type TokenStoreConfig
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 first checks for a valid cached session (with unexpired access token). If no valid session exists, it attempts to refresh using the stored refresh token. If that fails, it falls back to interactive auth via a local HTTP callback and browser.
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 string
}
AuthClientConfig is the configuration for the auth client
type CachedSession ¶
type CachedSession struct {
AccessToken string `json:"accessToken"`
RefreshToken string `json:"refreshToken"`
UUID string `json:"uuid"`
Username string `json:"username"`
ExpiresAt time.Time `json:"expiresAt"`
}
CachedSession represents a cached authentication session.
func (*CachedSession) IsValid ¶
func (s *CachedSession) IsValid() bool
IsValid returns true if the session has a valid access token that hasn't expired. Uses a 5-minute buffer to avoid edge cases.
type LoginData ¶
type LoginData struct {
AccessToken string
RefreshToken string
UUID string
Username string
ExpiresAt time.Time
}
LoginData is the data returned from a login
func FromSession ¶
func FromSession(s *CachedSession) LoginData
FromSession creates LoginData from a CachedSession.
func (LoginData) ToSession ¶
func (d LoginData) ToSession() *CachedSession
ToSession converts LoginData to a CachedSession for storage.
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 TokenStore ¶
type TokenStore interface {
SaveSession(session *CachedSession) error
LoadSession(username string) (*CachedSession, error)
Clear(username string) error
ListAccounts() ([]string, error)
}
TokenStore defines an interface to persist and retrieve sessions 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.