Files
yetano-clients/atlassian/atlassian.go
T

151 lines
4.8 KiB
Go
Raw Permalink Normal View History

// Package atlassian holds what the Jira and Confluence clients share:
// connection options, authentication, deployment kind and the error
// types every request can return. The HTTP machinery itself (retries,
// rate-limit throttling, pagination, downloads) lives in
// atlassian/internal/transport and is reached only through
// atlassian/jira and atlassian/confluence.
package atlassian
import (
"log/slog"
"net/http"
"time"
)
// Deployment selects the API flavour: Atlassian Cloud or a self-hosted
// Server/Data Center instance. The zero value is Cloud.
type Deployment string
const (
// Cloud is Atlassian Cloud (Jira REST API v3, ADF bodies,
// Confluence under /wiki).
Cloud Deployment = "cloud"
// DataCenter is Jira/Confluence Server or Data Center (Jira REST API
// v2, plain-text/wiki-markup bodies, Confluence at the site root).
DataCenter Deployment = "datacenter"
)
// IsCloud reports whether d is Cloud (including the zero value).
func (d Deployment) IsCloud() bool { return d != DataCenter }
// AuthType selects how Auth is applied to a request.
type AuthType string
const (
// AuthAuto picks from the fields that are set: Username+Password as
// basic auth, else Token as bearer, else Cookie.
AuthAuto AuthType = ""
// AuthAPIToken is HTTP basic auth with the account e-mail as
// Username and the API token as Password (Atlassian Cloud).
AuthAPIToken AuthType = "api_token"
// AuthBasic is HTTP basic auth with username and password.
AuthBasic AuthType = "basic"
// AuthBearer sends "Authorization: Bearer <Token>" (Data Center
// personal access tokens, OAuth bearer tokens).
AuthBearer AuthType = "bearer"
// AuthPAT is AuthBearer under the name Data Center uses.
AuthPAT AuthType = "pat"
// AuthCookie sends Cookie verbatim as the Cookie header (a browser
// session, for sites where no token can be issued).
AuthCookie AuthType = "cookie"
)
// Auth is the resolved authentication material for one site. It lives
// in memory only and is never logged or put into an error.
type Auth struct {
Type AuthType
Username string // e-mail for AuthAPIToken
Password string // API token for AuthAPIToken
Token string // AuthBearer/AuthPAT
Cookie string // AuthCookie: the raw Cookie header value
}
// Apply sets the credentials on req. A type whose fields are empty sends
// nothing rather than an empty Authorization header.
func (a Auth) Apply(req *http.Request) {
switch a.effectiveType() {
case AuthAPIToken, AuthBasic:
if a.Username != "" && a.Password != "" {
req.SetBasicAuth(a.Username, a.Password)
}
case AuthBearer, AuthPAT:
if a.Token != "" {
req.Header.Set("Authorization", "Bearer "+a.Token)
}
case AuthCookie:
if a.Cookie != "" {
req.Header.Set("Cookie", a.Cookie)
}
}
}
// IsSet reports whether Apply would send any credentials.
func (a Auth) IsSet() bool {
switch a.effectiveType() {
case AuthAPIToken, AuthBasic:
return a.Username != "" && a.Password != ""
case AuthBearer, AuthPAT:
return a.Token != ""
case AuthCookie:
return a.Cookie != ""
}
return false
}
func (a Auth) effectiveType() AuthType {
if a.Type != AuthAuto {
return a.Type
}
switch {
case a.Username != "" && a.Password != "":
return AuthBasic
case a.Token != "":
return AuthBearer
case a.Cookie != "":
return AuthCookie
}
return AuthAuto
}
// TLS configures certificate verification.
type TLS struct {
// CAFile is a PEM bundle used instead of the system roots.
CAFile string
// InsecureSkipVerify turns verification off; an explicit opt-in.
InsecureSkipVerify bool
}
// Options configures a Jira or Confluence client.
type Options struct {
// BaseURL is the site root ("https://example.atlassian.net"), without
// /rest/... or /wiki; a trailing slash is ignored.
BaseURL string
Deployment Deployment
Auth Auth
TLS TLS
// ProxyURL overrides the proxy; empty uses HTTP(S)_PROXY from the
// environment.
ProxyURL string
// Timeout bounds one request, 0 means none.
Timeout time.Duration
// DownloadTimeout bounds one file download, 0 means none.
DownloadTimeout time.Duration
// MaxRetries is the number of attempts for 5xx answers and connection
// errors (values below 1 mean 1, i.e. no retry). Backoff is
// exponential, 2s, 4s, ... capped at 30s.
MaxRetries int
// DisableRateLimitRetry returns a 429 as an *APIError right away.
// Without it a 429 pauses every request of the client until
// Retry-After and is retried (up to 10 times) without using up
// MaxRetries.
DisableRateLimitRetry bool
// APIDelay is a fixed pause after every response.
APIDelay time.Duration
// MaxResponseBytes caps a response body read into memory; 0 means no
// cap. A larger body fails with ErrResponseTooLarge.
MaxResponseBytes int64
// Logger receives retry and throttling messages (never headers or
// credentials). Nil means slog.Default().
Logger *slog.Logger
}