Files
certctl/internal/repository/postgres/oidc_bcl.go
T
shankar0123 21aeed4f4e legal: addlicense headers + normalize legacy variants (Phase 0 RED-4)
Phase 0 closure (Path B2, post-rewrite):

addlicense sweep — adds the canonical certctl LLC copyright + BUSL-1.1
SPDX header to every production Go file. Template:

  // Copyright 2026 certctl LLC. All rights reserved.
  // SPDX-License-Identifier: BUSL-1.1

Coverage: 338 / 338 production Go files (cmd/ + internal/, excluding
*_test.go and **/testdata/**). Pre-sweep coverage was 22 / 338 (6.5%);
post-sweep is 338 / 338 (100%).

Normalized 22 pre-existing legacy headers (`// Copyright (c) certctl`
+ `// SPDX-License-Identifier: BSL-1.1`) and 1 file using a
`Certctl Contributors` attribution. The legacy SPDX ID `BSL-1.1`
is non-standard; the official SPDX identifier for Business Source
License 1.1 is `BUSL-1.1` (capital U). All 338 files now share the
canonical form.

Generated via:
  addlicense -c "certctl LLC" -y 2026 \
    -f cowork/legal/copyright-header.tpl \
    -ignore '**/testdata/**' -ignore '**/*_test.go' \
    cmd/ internal/

Verification:
  find cmd internal -name '*.go' -not -name '*_test.go' \
    -not -path '*/testdata/*' \
    -exec grep -L '^// Copyright 2026 certctl LLC' {} \; | wc -l

  Returns: 0

gofmt clean. Header additions are comments only, no compile impact.

Closes: cowork/certctl-architecture-diligence-audit.html#fix-RED-4
2026-05-13 21:23:35 +00:00

60 lines
1.7 KiB
Go

// Copyright 2026 certctl LLC. All rights reserved.
// SPDX-License-Identifier: BUSL-1.1
package postgres
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/certctl-io/certctl/internal/repository"
)
// BCLReplayRepository is the postgres implementation of
// repository.BCLReplayRepository. Audit 2026-05-10 HIGH-3.
type BCLReplayRepository struct {
db *sql.DB
}
func NewBCLReplayRepository(db *sql.DB) *BCLReplayRepository {
return &BCLReplayRepository{db: db}
}
// ConsumeJTI atomically records that a (jti, issuer_url) pair has been
// consumed. INSERT...ON CONFLICT DO NOTHING RETURNING gives us
// single-use semantics in one round-trip: if zero rows return, the
// jti was already there.
func (r *BCLReplayRepository) ConsumeJTI(ctx context.Context, jti, issuerURL string, ttl time.Duration) error {
expiresAt := time.Now().UTC().Add(ttl)
var inserted bool
err := r.db.QueryRowContext(ctx, `
INSERT INTO oidc_bcl_consumed_jtis (jti, issuer_url, expires_at)
VALUES ($1, $2, $3)
ON CONFLICT (jti, issuer_url) DO NOTHING
RETURNING true`,
jti, issuerURL, expiresAt,
).Scan(&inserted)
if err != nil {
if err == sql.ErrNoRows {
// ON CONFLICT DO NOTHING returns zero rows = already consumed.
return repository.ErrBCLJTIAlreadyConsumed
}
return fmt.Errorf("bcl consume_jti: %w", err)
}
return nil
}
// SweepExpired removes rows whose expires_at is in the past.
func (r *BCLReplayRepository) SweepExpired(ctx context.Context, now time.Time) (int, error) {
res, err := r.db.ExecContext(ctx,
`DELETE FROM oidc_bcl_consumed_jtis WHERE expires_at < $1`,
now)
if err != nil {
return 0, fmt.Errorf("bcl sweep_expired: %w", err)
}
n, _ := res.RowsAffected()
return int(n), nil
}