Files
c28383ed14 Worktree findings batch 2 (#17)
* Fix issues F-003, 006, 001, 022

* Fix issues F-025, 026, 010, 008

- Login tokens name their identity provider in the `provider` claim;
  SelectOrg and GetUserToken read it with pb.IDPFromString, which also
  takes the number older tokens carry, and AuthenticateAPIKey answers
  Provider Undefined.
- A login that fails after the callback decodes its state lands on the
  redirect URI of the client, in its response mode, with `error` and
  `error_description` (RFC 6749 section 4.1.2.1); a server-side failure
  is described by its status text only. The calls to an IdP take the
  request context and a client with a timeout, and the answer of the IdP
  stays in the server log.
- The platform bootstrap reads its ca-config with ca.LoadBootstrapConfig,
  which refuses an issuer URL template with `${ISSUER_ID}` or
  `$ISSUER_ID`: the loader expands them to an empty string. The proto,
  README and CLI help name `:ISSUER_ID`.
- CaDb.RegisterIssuer takes the trust anchor of a root issuer and writes
  both in one transaction; the backend service and the bootstrap pass it,
  and an anchor registered in another scope is AlreadyExists.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Address PR review comments

- Invite acceptance deletes the invite in its transaction
  (query.TakeInvite, DELETE ... RETURNING) and grants from the deleted
  row: a withdrawn or already accepted invite grants nothing, a re-issued
  one grants its current role, and an email mismatch, an expired invite or
  a failed grant rolls the delete back.
- CaDb.RegisterIssuer requires the anchor of a root issuer and refuses one
  for any other issuer; the service always builds it for a root.
- The OAuth response is merged into the query of a redirect URI that has
  one, and replaces its fragment in fragment mode.
- The codemap describes the transactions of RegisterOrg and the invite
  acceptance.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-23 19:59:28 -05:00

142 lines
4.5 KiB
Go

package query
import (
"github.com/effective-security/trustyca/api/pb"
"github.com/effective-security/trustyca/internal/db/schema"
"github.com/effective-security/xdb"
"github.com/effective-security/xdb/xsql"
)
// CreateInvite returns SQL query to create or update an invite at its scope
func CreateInvite(args ...any) (string, string) {
const key = "CreateInvite"
return xsql.Postgres.GetOrCreateQuery(key, func(name string) xsql.Builder {
q := schema.InviteTableInfo.
InsertInto().
Clause(`ON CONFLICT ON CONSTRAINT unique_invite_org_project_email DO UPDATE SET
inviter_id = EXCLUDED.inviter_id,
role = EXCLUDED.role,
expires_at = EXCLUDED.expires_at`).
Returning(schema.InviteTableInfo.AllColumns())
q.NewRow().
Set(schema.Invite.ID.Name, nil).
Set(schema.Invite.OrgID.Name, nil).
Set(schema.Invite.ProjectID.Name, nil).
Set(schema.Invite.InviterID.Name, nil).
Set(schema.Invite.Email.Name, nil).
Set(schema.Invite.Role.Name, nil).
SetExpr(schema.Invite.CreatedAt.Name, exprNow).
Set(schema.Invite.ExpiresAt.Name, nil)
return q
})
}
// GetInviteRequest defines request to get an invite by org, scope and email.
// ProjectID 0 is the Org scope invite.
type GetInviteRequest struct {
OrgID uint64
ProjectID uint64
Email string
}
// QueryParams returns the query params builder
func (r *GetInviteRequest) QueryParams() xdb.QueryParams {
b := xdb.NewQueryParams("GetInvite")
b.Set(schema.Invite.OrgID.Position, r.OrgID)
b.Set(schema.Invite.Email.Position, r.Email)
setProjectScope(b, schema.Invite.ProjectID.Position, r.ProjectID, pb.Scope_Org)
return b
}
// GetInvite returns SQL query to get an invite by org, scope and email
func GetInvite(args ...any) (string, string) {
p := xdb.GetQueryParams(args...)
key := p.Name()
return xsql.Postgres.GetOrCreateQuery(key, func(name string) xsql.Builder {
q := schema.InviteTableInfo.
Select().
Where(schema.Invite.OrgID.Name+" = ?", nil).
Where(schema.Invite.Email.Name+" = ?", nil)
whereProjectScope(q, p, schema.Invite.ProjectID.Position)
return q
})
}
// GetOrgInvites returns SQL query to list invites of all scopes for an org
func GetOrgInvites(args ...any) (string, string) {
const key = "GetOrgInvites"
return xsql.Postgres.GetOrCreateQuery(key, func(name string) xsql.Builder {
return schema.InviteTableInfo.
Select().
Where(schema.Invite.OrgID.Name+" = ?", nil)
})
}
// GetUserInvites returns SQL query to list the pending, not expired invites
// for a user's email
func GetUserInvites(args ...any) (string, string) {
const key = "GetUserInvites"
return xsql.Postgres.GetOrCreateQuery(key, func(name string) xsql.Builder {
return schema.InviteTableInfo.
Select().
Where(schema.Invite.Email.Name+" = ?", nil).
Where("(" + schema.Invite.ExpiresAt.Name + " IS NULL OR " + schema.Invite.ExpiresAt.Name + " > Now())")
})
}
// TakeInvite returns SQL query to delete an invite by ID and return the
// deleted row, so that an acceptance grants the invite it deletes
func TakeInvite(args ...any) (string, string) {
const key = "TakeInvite"
return xsql.Postgres.GetOrCreateQuery(key, func(name string) xsql.Builder {
return schema.InviteTableInfo.
DeleteFrom().
Where(schema.Invite.ID.Name+" = ?", nil).
Returning(schema.InviteTableInfo.AllColumns())
})
}
// DeleteInviteRequest defines request to delete invites.
// ProjectID deletes the Project scope invite of the project;
// Scope Org deletes the Org scope invite only;
// otherwise invites of all scopes matching OrgID and Email are deleted.
type DeleteInviteRequest struct {
OrgID uint64
ProjectID uint64
Email string
Scope pb.Scope_Enum
}
// QueryParams returns the query params builder
func (r *DeleteInviteRequest) QueryParams() xdb.QueryParams {
b := xdb.NewQueryParams("DeleteInvite")
if r.OrgID != 0 {
b.Set(schema.Invite.OrgID.Position, r.OrgID)
}
if r.Email != "" {
b.Set(schema.Invite.Email.Position, r.Email)
}
setProjectScope(b, schema.Invite.ProjectID.Position, r.ProjectID, r.Scope)
return b
}
// DeleteInvite returns SQL query
func DeleteInvite(args ...any) (string, string) {
p := xdb.GetQueryParams(args...)
key := p.Name()
return xsql.Postgres.GetOrCreateQuery(key, func(name string) xsql.Builder {
q := schema.InviteTableInfo.
DeleteFrom()
if p.IsSet(schema.Invite.OrgID.Position) {
q.Where(schema.Invite.OrgID.Name+" = ?", nil)
}
if p.IsSet(schema.Invite.Email.Position) {
q.Where(schema.Invite.Email.Name+" = ?", nil)
}
whereProjectScope(q, p, schema.Invite.ProjectID.Position)
return q
})
}