ci: expand CI checks and make consumer sync opt-in
- Add Go, npm, Python, and Java validation to CI - Run Go tests against PostgreSQL - Record performance fixes in the report
This commit is contained in:
1 parent
199a5efaef
commit
22887fef03
100 files changed
+9740
-429
No files matched your search
@@ -68,7 +68,9 @@ type Client struct {
|
||||
// restrictions can be enforced. Empty means "don't identify" - the
|
||||
// caller is treated as unrestricted; see AuthServer's project-scoping
|
||||
// feature and WithProjectKey.
|
||||
projectKey string
|
||||
projectKey string
|
||||
validationMu sync.Mutex
|
||||
validationCache map[[32]byte]validationCacheEntry
|
||||
}
|
||||
|
||||
// ProjectKeyHeader is the header Client attaches to every request identifying
|
||||
@@ -112,9 +114,10 @@ func WithProjectKey(key string) Option {
|
||||
// is always left as-is, overriding the inference.
|
||||
func New(baseURL string, opts ...Option) *Client {
|
||||
c := &Client{
|
||||
baseURL: normalizeBaseURL(baseURL),
|
||||
httpClient: &http.Client{Timeout: 5 * time.Second},
|
||||
cookieName: DefaultCookieName,
|
||||
baseURL: normalizeBaseURL(baseURL),
|
||||
httpClient: &http.Client{Timeout: 5 * time.Second},
|
||||
cookieName: DefaultCookieName,
|
||||
validationCache: make(map[[32]byte]validationCacheEntry),
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
@@ -138,6 +141,7 @@ func (c *Client) SetBaseURL(baseURL string) {
|
||||
c.baseURLMu.Lock()
|
||||
c.baseURL = normalized
|
||||
c.baseURLMu.Unlock()
|
||||
c.clearValidationCache()
|
||||
}
|
||||
|
||||
func (c *Client) getBaseURL() string {
|
||||
@@ -155,6 +159,33 @@ func (c *Client) setProjectKeyHeader(req *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// ForwardedForHeader carries the end user's address to AuthServer on the
|
||||
// credential calls (see WithClientIP). AuthServer reads it only when the
|
||||
// calling service's own address is listed in its TRUSTED_PROXIES, so sending
|
||||
// it changes nothing for a deployment that has not opted in.
|
||||
const ForwardedForHeader = "X-Forwarded-For"
|
||||
|
||||
type clientIPKey struct{}
|
||||
|
||||
// WithClientIP returns a context that makes Login, Register, TOTPVerify,
|
||||
// TOTPDisable and the password-reset calls send ip as X-Forwarded-For, so
|
||||
// AuthServer's per-address limits see the end user instead of this service.
|
||||
// Pass an address the caller has already verified (for example
|
||||
// auth.Service's trusted-proxy-aware client address), never a raw request
|
||||
// header. Anything that is not an IP address is ignored.
|
||||
func WithClientIP(ctx context.Context, ip string) context.Context {
|
||||
return context.WithValue(ctx, clientIPKey{}, strings.TrimSpace(ip))
|
||||
}
|
||||
|
||||
// setClientIPHeader attaches ForwardedForHeader when ctx carries a valid
|
||||
// address from WithClientIP; a no-op otherwise.
|
||||
func setClientIPHeader(ctx context.Context, req *http.Request) {
|
||||
ip, _ := ctx.Value(clientIPKey{}).(string)
|
||||
if parsed := net.ParseIP(ip); parsed != nil {
|
||||
req.Header.Set(ForwardedForHeader, parsed.String())
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeBaseURL(baseURL string) string {
|
||||
trimmed := strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
||||
if trimmed == "" {
|
||||
@@ -211,6 +242,9 @@ func (c *Client) Validate(ctx context.Context, token string) (*User, error) {
|
||||
if token == "" {
|
||||
return nil, ErrInvalidSession
|
||||
}
|
||||
if user, cachedErr, ok := c.cachedValidation(token); ok {
|
||||
return user, cachedErr
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/api/session/validate", nil)
|
||||
if err != nil {
|
||||
@@ -233,15 +267,22 @@ func (c *Client) Validate(ctx context.Context, token string) (*User, error) {
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
c.cacheValidation(token, nil, false)
|
||||
return nil, ErrInvalidSession
|
||||
}
|
||||
return nil, fmt.Errorf("authclient: decode AuthServer response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK || !payload.Valid {
|
||||
if resp.StatusCode == http.StatusUnauthorized || (resp.StatusCode == http.StatusOK && !payload.Valid) {
|
||||
c.cacheValidation(token, nil, false)
|
||||
return nil, ErrInvalidSession
|
||||
}
|
||||
return &User{ID: payload.UserID, Username: payload.Username, Role: payload.Role}, nil
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("authclient: AuthServer returned HTTP %d", resp.StatusCode)
|
||||
}
|
||||
user := &User{ID: payload.UserID, Username: payload.Username, Role: payload.Role}
|
||||
c.cacheValidation(token, user, true)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// contextKey is unexported so only this package can set/read the resolved
|
||||
@@ -451,6 +492,7 @@ func (c *Client) call(ctx context.Context, method, path, token string, body, out
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
c.setProjectKeyHeader(req)
|
||||
setClientIPHeader(ctx, req)
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -535,6 +577,7 @@ func (c *Client) Logout(ctx context.Context, token string) ([]*http.Cookie, erro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.invalidateValidation(token)
|
||||
return resp.Cookies(), nil
|
||||
}
|
||||
|
||||
@@ -623,6 +666,9 @@ func (c *Client) CreateManagedUser(ctx context.Context, token string, input Mana
|
||||
// UpdateManagedUser calls PUT /api/admin/users/{id}.
|
||||
func (c *Client) UpdateManagedUser(ctx context.Context, token string, id int64, input ManagedUserInput) error {
|
||||
_, err := c.call(ctx, http.MethodPut, fmt.Sprintf("/api/admin/users/%d", id), token, input, nil)
|
||||
if err == nil {
|
||||
c.invalidateValidation(token)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user