mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 07:06:41 +00:00
Reverts #1126, #1125, #1124, #1123 and #1122. `go.mod` keeps the Go 1.26 line with toolchain `go1.26.9` (instead of the original `go1.26.6`) so the security fix from #1131 isn't lost. reopens #69 reopens #141 reopens #162 reopens #172 reopens #197 reopens #232 reopens #240 reopens #246 reopens #323 reopens #325 reopens #326 reopens #328 reopens #344 reopens #351 reopens #373 reopens #403 reopens #406 reopens #417 reopens #430 reopens #457 reopens #480 reopens #482 reopens #489 reopens #490 reopens #498 reopens #500 reopens #504 reopens #510 reopens #516 reopens #523 reopens #556 reopens #560 reopens #569 reopens #571 reopens #602 reopens #632 reopens #633 reopens #643 reopens #666 reopens #670 reopens #672 reopens #677 reopens #683 reopens #697 reopens #726 reopens #737 reopens #770 reopens #779 reopens #804 reopens #817 reopens #831 reopens #833 reopens #834 reopens #908 reopens #966 reopens #1023 reopens #1029 reopens #1032 reopens #1050 reopens #1052 reopens #1069 reopens #1073 reopens #1087 reopens #1091 reopens #1094 reopens #1110 reopens #1115 reopens #1116 reopens #1118 Reviewed-on: https://gitea.com/gitea/tea/pulls/1146 Reviewed-by: TheFox0x7 <95654+thefox0x7@noreply.gitea.com>
105 lines
2.6 KiB
Go
105 lines
2.6 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"
|
|
|
|
"gitea.dev/tea/modules/config"
|
|
"gitea.dev/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(&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
|
|
}
|