kimi

package
v6.10.9-aug.3 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 15 Imported by: 0

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

View Source
const (

	// APIBaseURL is the base URL for Kimi API requests.
	APIBaseURL = "https://api.kimi.com/coding"
)

Variables

This section is empty.

Functions

func BuildMetadata

func BuildMetadata(bundle *AuthBundle) map[string]any

BuildMetadata constructs the standard metadata map for a Kimi auth bundle.

Types

type Auth

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

Auth handles Kimi authentication flow.

func NewAuth

func NewAuth(cfg *config.Config) *Auth

NewAuth creates a new Auth service instance.

func (*Auth) CreateTokenStorage

func (k *Auth) CreateTokenStorage(bundle *AuthBundle) *TokenStorage

CreateTokenStorage creates a new TokenStorage from auth bundle.

func (*Auth) StartDeviceFlow

func (k *Auth) StartDeviceFlow(ctx context.Context) (*DeviceCodeResponse, error)

StartDeviceFlow initiates the device flow authentication.

func (*Auth) WaitForAuthorization

func (k *Auth) WaitForAuthorization(ctx context.Context, deviceCode *DeviceCodeResponse) (*AuthBundle, error)

WaitForAuthorization polls for user authorization and returns the auth bundle.

type AuthBundle

type AuthBundle struct {
	// TokenData contains the OAuth token information.
	TokenData *TokenData
	// DeviceID is the device identifier used during OAuth device flow.
	DeviceID string
}

AuthBundle bundles authentication data for storage.

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 NewDeviceFlowClientWithDeviceID

func NewDeviceFlowClientWithDeviceID(cfg *config.Config, deviceID string) *DeviceFlowClient

NewDeviceFlowClientWithDeviceID creates a new device flow client with the specified device ID.

func (*DeviceFlowClient) RefreshToken

func (c *DeviceFlowClient) RefreshToken(ctx context.Context, refreshToken string) (*TokenData, error)

RefreshToken exchanges a refresh token for a new access token.

type TokenData

type TokenData 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"`
}

TokenData holds the raw OAuth token response from Kimi.

type TokenStorage

type TokenStorage 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, always "kimi" for this storage.
	Type string `json:"type"`

	// 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:"-"`
}

TokenStorage stores OAuth2 token information for Kimi API authentication.

func (*TokenStorage) IsExpired

func (ts *TokenStorage) IsExpired() bool

IsExpired checks if the token has expired.

func (*TokenStorage) NeedsRefresh

func (ts *TokenStorage) NeedsRefresh() bool

NeedsRefresh checks if the token should be refreshed.

func (*TokenStorage) SaveTokenToFile

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

SaveTokenToFile serializes the token storage to a JSON file.

func (*TokenStorage) SetMetadata

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

SetMetadata allows external callers to inject metadata into the storage before saving.

Jump to

Keyboard shortcuts

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