iflow

package
v6.10.9-aug.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const CallbackPort = 11451

CallbackPort defines the local port used for OAuth callbacks.

View Source
const DefaultAPIBaseURL = "https://apis.iflow.cn/v1"

DefaultAPIBaseURL is the canonical chat completions endpoint.

Variables

This section is empty.

Functions

func CheckDuplicateBXAuth

func CheckDuplicateBXAuth(authDir, bxAuth string) (string, error)

CheckDuplicateBXAuth checks if the given BXAuth value already exists in any iflow auth file. Returns the path of the existing file if found, empty string otherwise.

func ExtractBXAuth

func ExtractBXAuth(cookie string) string

ExtractBXAuth extracts the BXAuth value from a cookie string.

func NormalizeCookie

func NormalizeCookie(raw string) (string, error)

NormalizeCookie normalizes raw cookie strings for iFlow authentication flows.

func SanitizeIFlowFileName

func SanitizeIFlowFileName(raw string) string

SanitizeIFlowFileName normalizes user identifiers for safe filename usage.

func ShouldRefreshAPIKey

func ShouldRefreshAPIKey(expireTime string) (bool, time.Duration, error)

ShouldRefreshAPIKey checks if the API key needs to be refreshed (within 2 days of expiry)

Types

type Auth

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

Auth encapsulates the HTTP client helpers for the OAuth flow.

func NewAuth

func NewAuth(cfg *config.Config) *Auth

NewAuth constructs a new Auth with proxy-aware transport.

func (*Auth) AuthenticateWithCookie

func (ia *Auth) AuthenticateWithCookie(ctx context.Context, cookie string) (*TokenData, error)

AuthenticateWithCookie performs authentication using browser cookies

func (*Auth) AuthorizationURL

func (ia *Auth) AuthorizationURL(state string, port int) (authURL, redirectURI string)

AuthorizationURL builds the authorization URL and matching redirect URI.

func (*Auth) CreateCookieTokenStorage

func (ia *Auth) CreateCookieTokenStorage(data *TokenData) *TokenStorage

CreateCookieTokenStorage converts cookie-based token data into persistence storage

func (*Auth) CreateTokenStorage

func (ia *Auth) CreateTokenStorage(data *TokenData) *TokenStorage

CreateTokenStorage converts token data into persistence storage.

func (*Auth) ExchangeCodeForTokens

func (ia *Auth) ExchangeCodeForTokens(ctx context.Context, code, redirectURI string) (*TokenData, error)

ExchangeCodeForTokens exchanges an authorization code for access and refresh tokens.

func (*Auth) RefreshAPIKey

func (ia *Auth) RefreshAPIKey(ctx context.Context, cookie, name string) (*KeyData, error)

RefreshAPIKey refreshes the API key using POST request

func (*Auth) RefreshTokens

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

RefreshTokens exchanges a refresh token for a new access token.

func (*Auth) UpdateCookieTokenStorage

func (ia *Auth) UpdateCookieTokenStorage(storage *TokenStorage, KeyData *KeyData)

UpdateCookieTokenStorage updates the persisted token storage with refreshed API key data.

func (*Auth) UpdateTokenStorage

func (ia *Auth) UpdateTokenStorage(storage *TokenStorage, data *TokenData)

UpdateTokenStorage updates the persisted token storage with latest token data.

type KeyData

type KeyData struct {
	HasExpired bool   `json:"hasExpired"`
	ExpireTime string `json:"expireTime"`
	Name       string `json:"name"`
	APIKey     string `json:"apiKey"`
	APIKeyMask string `json:"apiKeyMask"`
}

KeyData contains the API key information

type OAuthResult

type OAuthResult struct {
	Code  string
	State string
	Error string
}

OAuthResult captures the outcome of the local OAuth callback.

type OAuthServer

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

OAuthServer provides a minimal HTTP server for handling the iFlow OAuth callback.

func NewOAuthServer

func NewOAuthServer(port int) *OAuthServer

NewOAuthServer constructs a new OAuthServer bound to the provided port.

func (*OAuthServer) Start

func (s *OAuthServer) Start() error

Start launches the callback listener.

func (*OAuthServer) Stop

func (s *OAuthServer) Stop(ctx context.Context) error

Stop gracefully terminates the callback listener.

func (*OAuthServer) WaitForCallback

func (s *OAuthServer) WaitForCallback(timeout time.Duration) (*OAuthResult, error)

WaitForCallback blocks until a callback result, server error, or timeout occurs.

type TokenData

type TokenData struct {
	AccessToken  string
	RefreshToken string
	TokenType    string
	Scope        string
	Expire       string
	APIKey       string
	Email        string
	Cookie       string
}

TokenData captures processed token details.

type TokenStorage

type TokenStorage struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	LastRefresh  string `json:"last_refresh"`
	Expire       string `json:"expired"`
	APIKey       string `json:"api_key"`
	Email        string `json:"email"`
	TokenType    string `json:"token_type"`
	Scope        string `json:"scope"`
	Cookie       string `json:"cookie"`
	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 persists iFlow OAuth credentials alongside the derived API key.

func (*TokenStorage) SaveTokenToFile

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

SaveTokenToFile serialises the token storage to disk.

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