mirror of
https://gitea.com/gitea/tea.git
synced 2026-03-13 09:13:30 +01:00
## Summary - Introduce `github.com/go-authgate/sdk-go/credstore` to store OAuth tokens securely in the OS keyring (macOS Keychain / Linux Secret Service / Windows Credential Manager), with automatic fallback to an encrypted JSON file - Add `AuthMethod` field to `Login` struct; new OAuth logins are marked `auth_method: oauth` and no longer write `token`/`refresh_token`/`token_expiry` to `config.yml` - Add `GetAccessToken()` / `GetRefreshToken()` / `GetTokenExpiry()` accessors that transparently read from credstore for OAuth logins, with fallback to YAML fields for legacy logins - Update all token reference sites across the codebase to use the new accessors - Non-OAuth logins (token, SSH) are completely unaffected; no migration of existing tokens ## Key files | File | Role | |------|------| | `modules/config/credstore.go` | **New** — credstore wrapper (Load/Save/Delete) | | `modules/config/login.go` | Login struct, token accessors, refresh logic | | `modules/auth/oauth.go` | OAuth flow, token creation / re-authentication | | `modules/api/client.go`, `cmd/login/helper.go`, `cmd/login/oauth_refresh.go` | Token reference updates | | `modules/task/pull_*.go`, `modules/task/repo_clone.go` | Git operation token reference updates | ## Test plan - [x] `go build ./...` compiles successfully - [x] `go test ./...` all tests pass - [x] `tea login add --oauth` completes OAuth flow; verify config.yml has `auth_method: oauth` but no token/refresh_token/token_expiry - [x] `tea repos ls` API calls work (token read from credstore) - [x] `tea login delete <name>` credstore token is also removed - [x] Existing non-OAuth logins continue to work unchanged 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed-on: https://gitea.com/gitea/tea/pulls/926 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-committed-by: Bo-Yi Wu <appleboy.tw@gmail.com>
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package api
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"code.gitea.io/tea/modules/config"
|
|
"code.gitea.io/tea/modules/httputil"
|
|
)
|
|
|
|
// Client provides direct HTTP access to Gitea API
|
|
type Client struct {
|
|
baseURL string
|
|
token string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
// NewClient creates a new API client from a Login config
|
|
func NewClient(login *config.Login) *Client {
|
|
// Refresh OAuth token if expired or near expiry
|
|
if err := login.RefreshOAuthTokenIfNeeded(); err != nil {
|
|
log.Printf("Warning: failed to refresh OAuth token: %v", err)
|
|
}
|
|
|
|
httpClient := &http.Client{
|
|
Transport: httputil.WrapTransport(&http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: login.Insecure},
|
|
}),
|
|
}
|
|
|
|
return &Client{
|
|
baseURL: strings.TrimSuffix(login.URL, "/"),
|
|
token: login.GetAccessToken(),
|
|
httpClient: httpClient,
|
|
}
|
|
}
|
|
|
|
// Do executes an HTTP request with authentication headers
|
|
func (c *Client) Do(method, endpoint string, body io.Reader, headers map[string]string) (*http.Response, error) {
|
|
// Build the full URL
|
|
reqURL, err := c.buildURL(endpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err := http.NewRequest(method, reqURL, body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create request: %w", err)
|
|
}
|
|
|
|
// Set authentication header
|
|
if c.token != "" {
|
|
req.Header.Set("Authorization", "token "+c.token)
|
|
}
|
|
|
|
// Set default content type for requests with body
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
|
|
// Apply custom headers (can override defaults)
|
|
for key, value := range headers {
|
|
req.Header.Set(key, value)
|
|
}
|
|
|
|
return c.httpClient.Do(req)
|
|
}
|
|
|
|
// buildURL constructs the full URL from an endpoint
|
|
func (c *Client) buildURL(endpoint string) (string, error) {
|
|
// If endpoint is already a full URL, validate it matches the login's host
|
|
if strings.HasPrefix(endpoint, "http://") || strings.HasPrefix(endpoint, "https://") {
|
|
endpointURL, err := url.Parse(endpoint)
|
|
if err != nil {
|
|
return "", fmt.Errorf("invalid URL: %w", err)
|
|
}
|
|
baseURL, err := url.Parse(c.baseURL)
|
|
if err != nil {
|
|
return "", fmt.Errorf("invalid base URL: %w", err)
|
|
}
|
|
if endpointURL.Host != baseURL.Host {
|
|
return "", fmt.Errorf("URL host %q does not match login host %q (token would be sent to wrong server)", endpointURL.Host, baseURL.Host)
|
|
}
|
|
return endpoint, nil
|
|
}
|
|
|
|
// Ensure endpoint starts with /
|
|
if !strings.HasPrefix(endpoint, "/") {
|
|
endpoint = "/" + endpoint
|
|
}
|
|
|
|
// Auto-prefix /api/v1/ if not present
|
|
if !strings.HasPrefix(endpoint, "/api/") {
|
|
endpoint = "/api/v1" + endpoint
|
|
}
|
|
|
|
return c.baseURL + endpoint, nil
|
|
}
|