codebuddy

package
v6.9.4-2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package codebuddy provides authentication and token management functionality for CodeBuddy AI services. It handles OAuth2 token storage, serialization, and retrieval for maintaining authenticated sessions with the CodeBuddy API.

Index

Constants

View Source
const (
	BaseURL       = "https://copilot.tencent.com"
	DefaultDomain = "www.codebuddy.cn"
	UserAgent     = "CLI/2.63.2 CodeBuddy/2.63.2"
)

Variables

View Source
var (
	ErrPollingTimeout   = errors.New("codebuddy: polling timeout, user did not authorize in time")
	ErrAccessDenied     = errors.New("codebuddy: access denied by user")
	ErrTokenFetchFailed = errors.New("codebuddy: failed to fetch token from server")
	ErrJWTDecodeFailed  = errors.New("codebuddy: failed to decode JWT token")
)

Functions

func GetUserFriendlyMessage

func GetUserFriendlyMessage(err error) string

Types

type AuthState

type AuthState struct {
	State   string
	AuthURL string
}

AuthState holds the state and auth URL returned by the auth state API.

type CodeBuddyAuth

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

func NewCodeBuddyAuth

func NewCodeBuddyAuth(cfg *config.Config) *CodeBuddyAuth

func (*CodeBuddyAuth) DecodeUserID

func (a *CodeBuddyAuth) DecodeUserID(accessToken string) (string, error)

DecodeUserID decodes the sub field from a JWT access token as the user ID.

func (*CodeBuddyAuth) FetchAuthState

func (a *CodeBuddyAuth) FetchAuthState(ctx context.Context) (*AuthState, error)

FetchAuthState calls POST /v2/plugin/auth/state?platform=CLI to get the state and login URL.

func (*CodeBuddyAuth) PollForToken

func (a *CodeBuddyAuth) PollForToken(ctx context.Context, state string) (*CodeBuddyTokenStorage, error)

PollForToken polls until the user completes browser authorization and returns auth data.

func (*CodeBuddyAuth) RefreshToken

func (a *CodeBuddyAuth) RefreshToken(ctx context.Context, accessToken, refreshToken, userID, domain string) (*CodeBuddyTokenStorage, error)

RefreshToken exchanges a refresh token for a new access token. It calls POST /v2/plugin/auth/token/refresh with the required headers.

type CodeBuddyTokenStorage

type CodeBuddyTokenStorage 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"`
	// ExpiresIn is the number of seconds until the access token expires.
	ExpiresIn int64 `json:"expires_in"`
	// RefreshExpiresIn is the number of seconds until the refresh token expires.
	RefreshExpiresIn int64 `json:"refresh_expires_in,omitempty"`
	// TokenType is the type of token, typically "bearer".
	TokenType string `json:"token_type"`
	// Domain is the CodeBuddy service domain/region.
	Domain string `json:"domain"`
	// UserID is the user ID associated with this token.
	UserID string `json:"user_id"`
	// Type indicates the authentication provider type, always "codebuddy" for this storage.
	Type string `json:"type"`
}

CodeBuddyTokenStorage stores OAuth token information for CodeBuddy API authentication. It maintains compatibility with the existing auth system while adding CodeBuddy-specific fields for managing access tokens and user account information.

func (*CodeBuddyTokenStorage) SaveTokenToFile

func (s *CodeBuddyTokenStorage) SaveTokenToFile(authFilePath string) error

SaveTokenToFile serializes the CodeBuddy token storage to a JSON file. This method creates the necessary directory structure and writes the token data in JSON format to the specified file path for persistent storage.

Parameters:

  • authFilePath: The full path where the token file should be saved

Returns:

  • error: An error if the operation fails, nil otherwise

Jump to

Keyboard shortcuts

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