xai

package
v7.1.13 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package xai provides OAuth2 authentication helpers for xAI Grok.

Index

Constants

View Source
const (
	// DefaultAPIBaseURL is the default xAI Responses API base URL.
	DefaultAPIBaseURL = "https://api.x.ai/v1"
	// Issuer is xAI's OAuth issuer.
	Issuer = "https://auth.x.ai"
	// DiscoveryURL is the OIDC discovery endpoint used to resolve OAuth endpoints.
	DiscoveryURL = Issuer + "/.well-known/openid-configuration"
	// ClientID is the public xAI Grok CLI OAuth client ID.
	ClientID = "b1a00492-073a-47ea-816f-4c329264a828"
	// Scope is the OAuth scope set required for xAI API access.
	Scope = "openid profile email offline_access grok-cli:access api:access"
	// RedirectHost is the loopback host used by xAI OAuth.
	RedirectHost = "127.0.0.1"
	// CallbackPort is the preferred loopback callback port.
	CallbackPort = 56121
	// RedirectPath is the loopback callback path registered by the xAI client.
	RedirectPath = "/callback"
)

Variables

This section is empty.

Functions

func BuildAuthorizeURL

func BuildAuthorizeURL(params AuthorizeURLParams) (string, error)

BuildAuthorizeURL builds the browser URL for xAI OAuth.

func CredentialFileName

func CredentialFileName(email, subject string) string

CredentialFileName returns the filename used for xAI credentials.

func RefreshLead

func RefreshLead() time.Duration

RefreshLead returns the refresh lead time for xAI OAuth credentials.

func ValidateOAuthEndpoint

func ValidateOAuthEndpoint(rawURL string, field string) (string, error)

ValidateOAuthEndpoint validates an endpoint returned by xAI discovery.

Types

type AuthBundle

type AuthBundle struct {
	TokenData     TokenData
	LastRefresh   string
	BaseURL       string
	RedirectURI   string
	TokenEndpoint string
}

AuthBundle aggregates token data and OAuth metadata for persistence.

type AuthorizeURLParams

type AuthorizeURLParams struct {
	AuthorizationEndpoint string
	RedirectURI           string
	CodeChallenge         string
	State                 string
	Nonce                 string
}

AuthorizeURLParams contains the values used to build the xAI OAuth URL.

type Discovery

type Discovery struct {
	AuthorizationEndpoint string `json:"authorization_endpoint"`
	TokenEndpoint         string `json:"token_endpoint"`
}

Discovery contains OAuth endpoints resolved from xAI OIDC discovery.

type PKCECodes

type PKCECodes struct {
	CodeVerifier  string
	CodeChallenge string
}

PKCECodes holds the PKCE verifier/challenge pair.

func GeneratePKCECodes

func GeneratePKCECodes() (*PKCECodes, error)

GeneratePKCECodes creates a verifier/challenge pair for the OAuth flow.

type TokenData

type TokenData struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	IDToken      string `json:"id_token,omitempty"`
	TokenType    string `json:"token_type,omitempty"`
	ExpiresIn    int    `json:"expires_in,omitempty"`
	Expire       string `json:"expired,omitempty"`
	Email        string `json:"email,omitempty"`
	Subject      string `json:"sub,omitempty"`
}

TokenData holds xAI OAuth token data.

type TokenStorage

type TokenStorage struct {
	Type          string `json:"type"`
	AccessToken   string `json:"access_token"`
	RefreshToken  string `json:"refresh_token"`
	IDToken       string `json:"id_token,omitempty"`
	TokenType     string `json:"token_type,omitempty"`
	ExpiresIn     int    `json:"expires_in,omitempty"`
	Expire        string `json:"expired,omitempty"`
	LastRefresh   string `json:"last_refresh,omitempty"`
	Email         string `json:"email,omitempty"`
	Subject       string `json:"sub,omitempty"`
	BaseURL       string `json:"base_url,omitempty"`
	RedirectURI   string `json:"redirect_uri,omitempty"`
	TokenEndpoint string `json:"token_endpoint,omitempty"`
	AuthKind      string `json:"auth_kind,omitempty"`

	Metadata map[string]any `json:"-"`
}

TokenStorage stores xAI OAuth credentials on disk.

func (*TokenStorage) SaveTokenToFile

func (ts *TokenStorage) SaveTokenToFile(authFilePath string) error

SaveTokenToFile writes xAI credentials to a JSON auth file.

func (*TokenStorage) SetMetadata

func (ts *TokenStorage) SetMetadata(meta map[string]any)

SetMetadata allows the token store to merge status fields before saving.

type XAIAuth

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

XAIAuth performs xAI OAuth discovery, token exchange, and refresh.

func NewXAIAuth

func NewXAIAuth(cfg *config.Config) *XAIAuth

NewXAIAuth creates an xAI OAuth helper using config proxy settings.

func NewXAIAuthWithProxyURL

func NewXAIAuthWithProxyURL(cfg *config.Config, proxyURL string) *XAIAuth

NewXAIAuthWithProxyURL creates an xAI OAuth helper with an explicit proxy URL.

func (*XAIAuth) CreateTokenStorage

func (a *XAIAuth) CreateTokenStorage(bundle *AuthBundle) *TokenStorage

CreateTokenStorage converts an auth bundle into persistable storage.

func (*XAIAuth) Discover

func (a *XAIAuth) Discover(ctx context.Context) (*Discovery, error)

Discover resolves xAI OAuth endpoints through OIDC discovery.

func (*XAIAuth) ExchangeCodeForTokens

func (a *XAIAuth) ExchangeCodeForTokens(ctx context.Context, code, redirectURI string, pkceCodes *PKCECodes, tokenEndpoint string) (*AuthBundle, error)

ExchangeCodeForTokens exchanges an authorization code for xAI OAuth tokens.

func (*XAIAuth) RefreshTokens

func (a *XAIAuth) RefreshTokens(ctx context.Context, refreshToken, tokenEndpoint string) (*TokenData, error)

RefreshTokens refreshes an xAI access token.

Jump to

Keyboard shortcuts

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