kimi

package
v6.8.13 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 16 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 (

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

Variables

This section is empty.

Functions

This section is empty.

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 (*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.

type KimiAuth

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

KimiAuth handles Kimi authentication flow.

func NewKimiAuth

func NewKimiAuth(cfg *config.Config) *KimiAuth

NewKimiAuth creates a new KimiAuth service instance.

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

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.

Jump to

Keyboard shortcuts

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