Documentation
¶
Index ¶
- Constants
- func AcquireConfigLockForTesting(lockPath string, timeout time.Duration) (func() error, error)
- func AddLogin(login *Login) error
- func AddOAuthLogin(login *Login, accessToken, refreshToken string, expiresAt time.Time) error
- func DeleteLogin(name string) error
- func DeleteOAuthToken(loginName string) error
- func GetConfigPath() string
- func LoadOAuthToken(loginName string) (*credstore.Token, error)
- func SaveLoginTokens(login *Login) error
- func SaveOAuthToken(loginName, accessToken, refreshToken string, expiresAt time.Time) error
- func SaveOAuthTokenFromOAuth2(loginName string, token *oauth2.Token, login *Login) error
- func SetConfigForTesting(cfg LocalConfig)
- func SetConfigPathForTesting(path string)
- func SetDefaultLogin(name string) error
- type FlagDefaults
- type LocalConfig
- type Login
- func (l *Login) Client(options ...gitea.ClientOption) *gitea.Client
- func (l *Login) ClientWithoutRefresh(options ...gitea.ClientOption) *gitea.Client
- func (l *Login) GetAccessToken() string
- func (l *Login) GetRefreshToken() string
- func (l *Login) GetSSHHost() string
- func (l *Login) GetTokenExpiry() time.Time
- func (l *Login) IsOAuth() bool
- func (l *Login) RefreshOAuthToken() error
- func (l *Login) RefreshOAuthTokenIfNeeded() error
- type OAuthToken
- type Preferences
Constants ¶
const AuthMethodOAuth = "oauth"
AuthMethodOAuth marks a login as using OAuth with secure credential storage.
const DefaultClientID = "d57cb8c4-630c-4168-8324-ec79935e18d4"
DefaultClientID is the default OAuth2 client ID included in most Gitea instances
const ( // LockTimeout is the default timeout for acquiring the config file lock. LockTimeout = 5 * time.Second )
const TokenRefreshThreshold = 5 * time.Minute
TokenRefreshThreshold is how far before expiry we should refresh OAuth tokens. This is used by config.Login.Client() for automatic token refresh.
Variables ¶
This section is empty.
Functions ¶
func AcquireConfigLockForTesting ¶
AcquireConfigLockForTesting exposes the internal lock helper to integration tests.
func AddOAuthLogin ¶ added in v0.15.0
AddOAuthLogin saves the OAuth token and login profile as one operation. The profile is only written after secure token storage succeeds.
func DeleteLogin ¶
DeleteLogin delete a login by name from config
func DeleteOAuthToken ¶
DeleteOAuthToken removes tokens from the secure store.
func LoadOAuthToken ¶
LoadOAuthToken loads OAuth tokens from the secure store.
func SaveLoginTokens ¶
SaveLoginTokens updates the token fields for an existing login. This is used after browser-based re-authentication to save new tokens.
func SaveOAuthToken ¶
SaveOAuthToken saves OAuth tokens to the secure store.
func SaveOAuthTokenFromOAuth2 ¶
SaveOAuthTokenFromOAuth2 saves an oauth2.Token to credstore, falling back to the existing login's values for empty refresh token or zero expiry.
func SetConfigForTesting ¶
func SetConfigForTesting(cfg LocalConfig)
SetConfigForTesting replaces the in-memory config and marks it as loaded. This allows tests to inject config without relying on file-based loading.
func SetConfigPathForTesting ¶
func SetConfigPathForTesting(path string)
SetConfigPathForTesting overrides the config path used by helpers in tests.
func SetDefaultLogin ¶
SetDefaultLogin set the default login by name (case insensitive)
Types ¶
type FlagDefaults ¶
type FlagDefaults struct {
// Prefer a specific git remote to use for selecting a repository on gitea,
// instead of relying on the remote associated with main/master/trunk branch.
// The --remote flag still has precedence over this value.
Remote string `yaml:"remote"`
}
FlagDefaults defines all flags that can be overridden with a default value via the config file
type LocalConfig ¶
type LocalConfig struct {
Logins []Login `yaml:"logins"`
Prefs Preferences `yaml:"preferences"`
}
LocalConfig represents local configurations
type Login ¶
type Login struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Token string `yaml:"token,omitempty"`
Default bool `yaml:"default"`
SSHHost string `yaml:"ssh_host"`
// optional path to the private key
SSHKey string `yaml:"ssh_key"`
Insecure bool `yaml:"insecure"`
SSHCertPrincipal string `yaml:"ssh_certificate_principal"`
SSHAgent bool `yaml:"ssh_agent"`
SSHKeyFingerprint string `yaml:"ssh_key_agent_pub"`
SSHPassphrase string `yaml:"-"`
VersionCheck bool `yaml:"version_check"`
// User is username from gitea
User string `yaml:"user"`
// Created is auto created unix timestamp
Created int64 `yaml:"created"`
// AuthMethod indicates the authentication method ("oauth" for OAuth with credstore)
AuthMethod string `yaml:"auth_method,omitempty"`
// RefreshToken is used to renew the access token when it expires
RefreshToken string `yaml:"refresh_token,omitempty"`
// TokenExpiry is when the token expires (unix timestamp)
TokenExpiry int64 `yaml:"token_expiry,omitempty"`
}
Login represents a login to a gitea server, you even could add multiple logins for one gitea server
func GetDefaultLogin ¶
GetDefaultLogin return the default login
func GetLoginByHost ¶
GetLoginByHost finds a login by its server URL
func GetLoginByName ¶
GetLoginByName get login by name (case insensitive)
func GetLoginByToken ¶
GetLoginByToken get login by token
func GetLoginsByHost ¶
GetLoginsByHost returns all logins matching a host
func (*Login) Client ¶
func (l *Login) Client(options ...gitea.ClientOption) *gitea.Client
Client returns a client to operate Gitea API. You may provide additional modifiers for the client like gitea.SetBasicAuth() for customization
func (*Login) ClientWithoutRefresh ¶ added in v0.16.0
func (l *Login) ClientWithoutRefresh(options ...gitea.ClientOption) *gitea.Client
ClientWithoutRefresh returns a client to operate the Gitea API without attempting an automatic OAuth token refresh. Commands that need to handle token refresh errors themselves (such as 'tea login status') should use this instead of Client, which prints to stderr and exits on refresh failure.
func (*Login) GetAccessToken ¶
GetAccessToken returns the effective access token. For OAuth logins, reads from credstore. For others, returns l.Token directly.
func (*Login) GetRefreshToken ¶
GetRefreshToken returns the refresh token.
func (*Login) GetTokenExpiry ¶
GetTokenExpiry returns the token expiry time.
func (*Login) IsOAuth ¶
IsOAuth returns true if this login uses OAuth with secure credential storage.
func (*Login) RefreshOAuthToken ¶
RefreshOAuthToken refreshes the OAuth access token using the refresh token. It updates the login with new token information and saves it to config. Uses double-checked locking to avoid unnecessary refresh calls when multiple processes race to refresh the same token.
func (*Login) RefreshOAuthTokenIfNeeded ¶
RefreshOAuthTokenIfNeeded refreshes the OAuth token if it's expired or near expiry. Returns nil without doing anything if no refresh is needed.
type OAuthToken ¶
OAuthToken holds the token fields loaded from credstore.
type Preferences ¶
type Preferences struct {
// Prefer using an external text editor over inline multiline prompts
Editor bool `yaml:"editor"`
FlagDefaults FlagDefaults `yaml:"flag_defaults"`
}
Preferences that are stored in and read from the config file
func GetPreferences ¶
func GetPreferences() Preferences
GetPreferences returns preferences based on the config file