qwen

package
v6.0.13 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: MIT Imports: 17 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

View Source
const (
	// QwenOAuthDeviceCodeEndpoint is the URL for initiating the OAuth 2.0 device authorization flow.
	QwenOAuthDeviceCodeEndpoint = "https://chat.qwen.ai/api/v1/oauth2/device/code"
	// QwenOAuthTokenEndpoint is the URL for exchanging device codes or refresh tokens for access tokens.
	QwenOAuthTokenEndpoint = "https://chat.qwen.ai/api/v1/oauth2/token"
	// QwenOAuthClientID is the client identifier for the Qwen OAuth 2.0 application.
	QwenOAuthClientID = "f0304373b74a44d2b584a3fb70ca9e56"
	// QwenOAuthScope defines the permissions requested by the application.
	QwenOAuthScope = "openid profile email model.completion"
	// QwenOAuthGrantType specifies the grant type for the device code flow.
	QwenOAuthGrantType = "urn:ietf:params:oauth:grant-type:device_code"
)

Variables

This section is empty.

Functions

This section is empty.

Types

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 QwenAuth

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

QwenAuth manages authentication and token handling for the Qwen API.

func NewQwenAuth

func NewQwenAuth(cfg *config.Config) *QwenAuth

NewQwenAuth creates a new QwenAuth instance with a proxy-configured HTTP client.

func (*QwenAuth) CreateTokenStorage

func (o *QwenAuth) CreateTokenStorage(tokenData *QwenTokenData) *QwenTokenStorage

CreateTokenStorage creates a QwenTokenStorage object from a QwenTokenData object.

func (*QwenAuth) InitiateDeviceFlow

func (qa *QwenAuth) InitiateDeviceFlow(ctx context.Context) (*DeviceFlow, error)

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

func (*QwenAuth) PollForToken

func (qa *QwenAuth) PollForToken(deviceCode, codeVerifier string) (*QwenTokenData, error)

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

func (*QwenAuth) RefreshTokens

func (qa *QwenAuth) RefreshTokens(ctx context.Context, refreshToken string) (*QwenTokenData, error)

RefreshTokens exchanges a refresh token for a new access token.

func (*QwenAuth) RefreshTokensWithRetry

func (o *QwenAuth) RefreshTokensWithRetry(ctx context.Context, refreshToken string, maxRetries int) (*QwenTokenData, error)

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

func (*QwenAuth) UpdateTokenStorage

func (o *QwenAuth) UpdateTokenStorage(storage *QwenTokenStorage, tokenData *QwenTokenData)

UpdateTokenStorage updates an existing token storage with new token data

type QwenTokenData

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

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

type QwenTokenResponse

type QwenTokenResponse struct {
	// AccessToken is the token used to access protected resources.
	AccessToken string `json:"access_token"`
	// RefreshToken is used to obtain a new access token.
	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"`
	// ExpiresIn is the time in seconds until the access token expires.
	ExpiresIn int `json:"expires_in"`
}

QwenTokenResponse represents the successful token response from the token endpoint.

type QwenTokenStorage

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

QwenTokenStorage 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 (*QwenTokenStorage) SaveTokenToFile

func (ts *QwenTokenStorage) 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.

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