Documentation
¶
Overview ¶
Package xai provides OAuth2 authentication helpers for xAI Grok.
Index ¶
- Constants
- func BuildAuthorizeURL(params AuthorizeURLParams) (string, error)
- func CredentialFileName(email, subject string) string
- func RefreshLead() time.Duration
- func ValidateOAuthEndpoint(rawURL string, field string) (string, error)
- type AuthBundle
- type AuthorizeURLParams
- type Discovery
- type PKCECodes
- type TokenData
- type TokenStorage
- type XAIAuth
- func (a *XAIAuth) CreateTokenStorage(bundle *AuthBundle) *TokenStorage
- func (a *XAIAuth) Discover(ctx context.Context) (*Discovery, error)
- func (a *XAIAuth) ExchangeCodeForTokens(ctx context.Context, code, redirectURI string, pkceCodes *PKCECodes, ...) (*AuthBundle, error)
- func (a *XAIAuth) RefreshTokens(ctx context.Context, refreshToken, tokenEndpoint string) (*TokenData, error)
Constants ¶
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 ¶
CredentialFileName returns the filename used for xAI credentials.
func RefreshLead ¶
RefreshLead returns the refresh lead time for xAI OAuth credentials.
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 ¶
PKCECodes holds the PKCE verifier/challenge pair.
func GeneratePKCECodes ¶
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 ¶
NewXAIAuth creates an xAI OAuth helper using config proxy settings.
func NewXAIAuthWithProxyURL ¶
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) 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.