Documentation
¶
Overview ¶
Package kimi provides authentication and token management for Kimi (Moonshot AI) API. It handles the RFC 8628 OAuth2 Device Authorization Grant flow for secure authentication.
Package kimi provides authentication and token management functionality for Kimi (Moonshot AI) services. It handles OAuth2 device flow token storage, serialization, and retrieval for maintaining authenticated sessions with the Kimi API.
Index ¶
- Constants
- func IsKimiAIAuth(auth *cliproxyauth.Auth) bool
- func IsKimiAIDomain(domain string) bool
- func IsKimiComDomain(domain string) bool
- func NormalizeKimiDomain(domain string) string
- func ResolveKimiAPIBaseURL(domain string) string
- func ResolveKimiDomainFromAuth(auth *cliproxyauth.Auth) string
- func ResolveKimiOAuthHost(domain string) string
- type DeviceCodeResponse
- type DeviceFlowClient
- func NewDeviceFlowClient(cfg *config.Config) *DeviceFlowClient
- func NewDeviceFlowClientWithDeviceID(cfg *config.Config, deviceID string) *DeviceFlowClient
- func NewDeviceFlowClientWithDeviceIDAndProxyURL(cfg *config.Config, deviceID string, proxyURL string) *DeviceFlowClient
- func NewDeviceFlowClientWithDomain(cfg *config.Config, domain string) *DeviceFlowClient
- func NewDeviceFlowClientWithDomainDeviceIDAndProxyURL(cfg *config.Config, domain string, deviceID string, proxyURL string) *DeviceFlowClient
- func (c *DeviceFlowClient) PollForToken(ctx context.Context, deviceCode *DeviceCodeResponse) (*KimiTokenData, error)
- func (c *DeviceFlowClient) RefreshToken(ctx context.Context, refreshToken string) (*KimiTokenData, error)
- func (c *DeviceFlowClient) RequestDeviceCode(ctx context.Context) (*DeviceCodeResponse, error)
- func (c *DeviceFlowClient) SetHTTPClient(client *http.Client)
- type KimiAuth
- type KimiAuthBundle
- type KimiTokenData
- type KimiTokenStorage
Constants ¶
const ( // KimiDefaultDomain is the default domain for Kimi (.com). KimiDefaultDomain = "kimi.com" // KimiAIDomain is the domain for Kimi.ai. KimiAIDomain = "kimi.ai" // KimiOAuthHost is the OAuth server endpoint for Kimi (.com). KimiOAuthHost = "https://auth.kimi.com" // KimiAIOAuthHost is the OAuth server endpoint for Kimi.ai. KimiAIOAuthHost = "https://auth.kimi.ai" // KimiAPIBaseURL is the base URL for Kimi (.com) API requests. KimiAPIBaseURL = "https://api.kimi.com/coding" // KimiAIAPIBaseURL is the base URL for Kimi.ai API requests. KimiAIAPIBaseURL = "https://api.kimi.ai/coding" )
Variables ¶
This section is empty.
Functions ¶
func IsKimiAIAuth ¶
func IsKimiAIAuth(auth *cliproxyauth.Auth) bool
IsKimiAIAuth reports whether the provided auth targets kimi.ai.
func IsKimiAIDomain ¶
IsKimiAIDomain checks whether a domain or provider string represents kimi.ai.
func IsKimiComDomain ¶
IsKimiComDomain checks whether a domain or provider string represents kimi.com.
func NormalizeKimiDomain ¶
NormalizeKimiDomain canonicalizes a domain string to "kimi.ai" or "kimi.com".
func ResolveKimiAPIBaseURL ¶
ResolveKimiAPIBaseURL returns the API base URL for the given domain.
func ResolveKimiDomainFromAuth ¶
func ResolveKimiDomainFromAuth(auth *cliproxyauth.Auth) string
ResolveKimiDomainFromAuth detects whether an Auth represents kimi.ai or kimi.com. Explicit configurations (domain, base_url, type) take precedence over file names.
func ResolveKimiOAuthHost ¶
ResolveKimiOAuthHost returns the OAuth host for the given domain.
Types ¶
type DeviceCodeResponse ¶
type DeviceCodeResponse struct {
// DeviceCode is the device verification code.
DeviceCode string `json:"device_code"`
// UserCode is the code the user must enter at the verification URI.
UserCode string `json:"user_code"`
// VerificationURI is the URL where the user should enter the code.
VerificationURI string `json:"verification_uri,omitempty"`
// VerificationURIComplete is the URL with the code pre-filled.
VerificationURIComplete string `json:"verification_uri_complete"`
// ExpiresIn is the number of seconds until the device code expires.
ExpiresIn int `json:"expires_in"`
// Interval is the minimum number of seconds to wait between polling requests.
Interval int `json:"interval"`
}
DeviceCodeResponse represents Kimi's device code response.
type DeviceFlowClient ¶
type DeviceFlowClient struct {
// contains filtered or unexported fields
}
DeviceFlowClient handles the OAuth2 device flow for Kimi.
func NewDeviceFlowClient ¶
func NewDeviceFlowClient(cfg *config.Config) *DeviceFlowClient
NewDeviceFlowClient creates a new device flow client.
func NewDeviceFlowClientWithDeviceID ¶
func NewDeviceFlowClientWithDeviceID(cfg *config.Config, deviceID string) *DeviceFlowClient
NewDeviceFlowClientWithDeviceID creates a new device flow client with the specified device ID.
func NewDeviceFlowClientWithDeviceIDAndProxyURL ¶
func NewDeviceFlowClientWithDeviceIDAndProxyURL(cfg *config.Config, deviceID string, proxyURL string) *DeviceFlowClient
NewDeviceFlowClientWithDeviceIDAndProxyURL creates a new device flow client with a proxy override. proxyURL takes precedence over cfg.ProxyURL when non-empty.
func NewDeviceFlowClientWithDomain ¶
func NewDeviceFlowClientWithDomain(cfg *config.Config, domain string) *DeviceFlowClient
NewDeviceFlowClientWithDomain creates a new device flow client for the specified domain.
func NewDeviceFlowClientWithDomainDeviceIDAndProxyURL ¶
func NewDeviceFlowClientWithDomainDeviceIDAndProxyURL(cfg *config.Config, domain string, deviceID string, proxyURL string) *DeviceFlowClient
NewDeviceFlowClientWithDomainDeviceIDAndProxyURL creates a new device flow client with domain, device ID, and proxy override.
func (*DeviceFlowClient) PollForToken ¶
func (c *DeviceFlowClient) PollForToken(ctx context.Context, deviceCode *DeviceCodeResponse) (*KimiTokenData, error)
PollForToken polls the token endpoint until the user authorizes or the device code expires.
func (*DeviceFlowClient) RefreshToken ¶
func (c *DeviceFlowClient) RefreshToken(ctx context.Context, refreshToken string) (*KimiTokenData, error)
RefreshToken exchanges a refresh token for a new access token.
func (*DeviceFlowClient) RequestDeviceCode ¶
func (c *DeviceFlowClient) RequestDeviceCode(ctx context.Context) (*DeviceCodeResponse, error)
RequestDeviceCode initiates the device flow by requesting a device code from Kimi.
func (*DeviceFlowClient) SetHTTPClient ¶
func (c *DeviceFlowClient) SetHTTPClient(client *http.Client)
SetHTTPClient overrides the internal HTTP client.
type KimiAuth ¶
type KimiAuth struct {
// contains filtered or unexported fields
}
KimiAuth handles Kimi authentication flow.
func NewKimiAIAuth ¶
NewKimiAIAuth creates a new KimiAuth service instance for kimi.ai.
func NewKimiAuth ¶
NewKimiAuth creates a new KimiAuth service instance for kimi.com.
func NewKimiAuthWithDomain ¶
NewKimiAuthWithDomain creates a new KimiAuth service instance for the specified domain.
func (*KimiAuth) CreateTokenStorage ¶
func (k *KimiAuth) CreateTokenStorage(bundle *KimiAuthBundle) *KimiTokenStorage
CreateTokenStorage creates a new KimiTokenStorage from auth bundle.
func (*KimiAuth) StartDeviceFlow ¶
func (k *KimiAuth) StartDeviceFlow(ctx context.Context) (*DeviceCodeResponse, error)
StartDeviceFlow initiates the device flow authentication.
func (*KimiAuth) WaitForAuthorization ¶
func (k *KimiAuth) WaitForAuthorization(ctx context.Context, deviceCode *DeviceCodeResponse) (*KimiAuthBundle, error)
WaitForAuthorization polls for user authorization and returns the auth bundle.
type KimiAuthBundle ¶
type KimiAuthBundle struct {
// TokenData contains the OAuth token information.
TokenData *KimiTokenData
// DeviceID is the device identifier used during OAuth device flow.
DeviceID string
}
KimiAuthBundle bundles authentication data for storage.
type KimiTokenData ¶
type KimiTokenData struct {
// AccessToken is the OAuth2 access token.
AccessToken string `json:"access_token"`
// RefreshToken is the OAuth2 refresh token.
RefreshToken string `json:"refresh_token"`
// TokenType is the type of token, typically "Bearer".
TokenType string `json:"token_type"`
// ExpiresAt is the Unix timestamp when the token expires.
ExpiresAt int64 `json:"expires_at"`
// Scope is the OAuth2 scope granted to the token.
Scope string `json:"scope"`
}
KimiTokenData holds the raw OAuth token response from Kimi.
type KimiTokenStorage ¶
type KimiTokenStorage struct {
// AccessToken is the OAuth2 access token used for authenticating API requests.
AccessToken string `json:"access_token"`
// RefreshToken is the OAuth2 refresh token used to obtain new access tokens.
RefreshToken string `json:"refresh_token"`
// TokenType is the type of token, typically "Bearer".
TokenType string `json:"token_type"`
// Scope is the OAuth2 scope granted to the token.
Scope string `json:"scope,omitempty"`
// DeviceID is the OAuth device flow identifier used for Kimi requests.
DeviceID string `json:"device_id,omitempty"`
// Expired is the RFC3339 timestamp when the access token expires.
Expired string `json:"expired,omitempty"`
// Type indicates the authentication provider type ("kimi" or "kimi-ai").
Type string `json:"type"`
// Domain indicates the Kimi domain (e.g. "kimi.com" or "kimi.ai").
Domain string `json:"domain,omitempty"`
// BaseURL is the base URL for API requests.
BaseURL string `json:"base_url,omitempty"`
// Metadata holds arbitrary key-value pairs injected via hooks.
// It is not exported to JSON directly to allow flattening during serialization.
Metadata map[string]any `json:"-"`
}
KimiTokenStorage stores OAuth2 token information for Kimi API authentication.
func (*KimiTokenStorage) IsExpired ¶
func (ts *KimiTokenStorage) IsExpired() bool
IsExpired checks if the token has expired.
func (*KimiTokenStorage) NeedsRefresh ¶
func (ts *KimiTokenStorage) NeedsRefresh() bool
NeedsRefresh checks if the token should be refreshed.
func (*KimiTokenStorage) SaveTokenToFile ¶
func (ts *KimiTokenStorage) SaveTokenToFile(authFilePath string) error
SaveTokenToFile serializes the Kimi token storage to a JSON file.
func (*KimiTokenStorage) SetMetadata ¶
func (ts *KimiTokenStorage) SetMetadata(meta map[string]any)
SetMetadata allows external callers to inject metadata into the storage before saving.