qwen

package
v6.10.9-aug.2 Latest Latest
Warning

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

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

Documentation

Overview

Package qwen provides authentication and token management functionality for Alibaba's Qwen AI services. It handles OAuth2 token storage, serialization, and retrieval for maintaining authenticated sessions with the Qwen API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

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

Auth manages authentication and token handling for the Qwen API.

func NewAuth

func NewAuth(cfg *config.Config) *Auth

NewAuth creates a new Auth instance with a proxy-configured HTTP client.

func (*Auth) CreateTokenStorage

func (a *Auth) CreateTokenStorage(tokenData *TokenData) *TokenStorage

CreateTokenStorage creates a TokenStorage object from a TokenData object.

func (*Auth) InitiateDeviceFlow

func (a *Auth) InitiateDeviceFlow(ctx context.Context) (*DeviceFlow, error)

InitiateDeviceFlow starts the OAuth 2.0 device authorization flow and returns the device flow details.

func (*Auth) PollForToken

func (a *Auth) PollForToken(deviceCode, codeVerifier string) (*TokenData, error)

PollForToken polls the token endpoint with the device code to obtain an access token.

func (*Auth) RefreshTokens

func (a *Auth) RefreshTokens(ctx context.Context, refreshToken string) (*TokenData, error)

RefreshTokens exchanges a refresh token for a new access token.

func (*Auth) RefreshTokensWithRetry

func (a *Auth) RefreshTokensWithRetry(ctx context.Context, refreshToken string, maxRetries int) (
	*TokenData,
	error,
)

RefreshTokensWithRetry attempts to refresh tokens with a specified number of retries upon failure.

func (*Auth) UpdateTokenStorage

func (a *Auth) UpdateTokenStorage(storage *TokenStorage, tokenData *TokenData)

UpdateTokenStorage updates an existing token storage with new token data

type DeviceFlow

type DeviceFlow struct {
	// DeviceCode is the code that the client uses to poll for an access token.
	DeviceCode string `json:"device_code"`
	// UserCode is the code that the user enters at the verification URI.
	UserCode string `json:"user_code"`
	// VerificationURI is the URL where the user can enter the user code to authorize the device.
	VerificationURI string `json:"verification_uri"`
	// VerificationURIComplete is a URI that includes the user_code, which can be used to automatically
	// fill in the code on the verification page.
	VerificationURIComplete string `json:"verification_uri_complete"`
	// ExpiresIn is the time in seconds until the device_code and user_code expire.
	ExpiresIn int `json:"expires_in"`
	// Interval is the minimum time in seconds that the client should wait between polling requests.
	Interval int `json:"interval"`
	// CodeVerifier is the cryptographically random string used in the PKCE flow.
	CodeVerifier string `json:"code_verifier"`
}

DeviceFlow represents the response from the device authorization endpoint.

type TokenData

type TokenData struct {
	AccessToken string `json:"access_token"`
	// RefreshToken is used to obtain a new access token when the current one expires.
	RefreshToken string `json:"refresh_token,omitempty"`
	// TokenType indicates the type of token, typically "Bearer".
	TokenType string `json:"token_type"`
	// ResourceURL specifies the base URL of the resource server.
	ResourceURL string `json:"resource_url,omitempty"`
	// Expire indicates the expiration date and time of the access token.
	Expire string `json:"expiry_date,omitempty"`
}

TokenData represents the OAuth credentials, including access and refresh tokens.

type TokenStorage

type TokenStorage struct {
	// AccessToken is the OAuth2 access token used for authenticating API requests.
	AccessToken string `json:"access_token"`
	// RefreshToken is used to obtain new access tokens when the current one expires.
	RefreshToken string `json:"refresh_token"`
	// LastRefresh is the timestamp of the last token refresh operation.
	LastRefresh string `json:"last_refresh"`
	// ResourceURL is the base URL for API requests.
	ResourceURL string `json:"resource_url"`
	// Email is the Qwen account email address associated with this token.
	Email string `json:"email"`
	// Type indicates the authentication provider type, always "qwen" for this storage.
	Type string `json:"type"`
	// Expire is the timestamp when the current access token expires.
	Expire string `json:"expired"`

	// 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 Alibaba Qwen API authentication. It maintains compatibility with the existing auth system while adding Qwen-specific fields for managing access tokens, refresh tokens, and user account information.

func (*TokenStorage) SaveTokenToFile

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

SaveTokenToFile serializes the Qwen 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. It merges any injected metadata into the top-level JSON object.

Parameters:

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

Returns:

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

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