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

87 lines
2.5 KiB
Go

package query_test
import (
"testing"
"github.com/effective-security/trustyca/api/pb"
"github.com/effective-security/trustyca/internal/db/pgsql/query"
"github.com/effective-security/trustyca/internal/db/schema"
)
func Test_InviteQueries(t *testing.T) {
t.Parallel()
check(t, []tcase{
{
query.CreateInvite, nil,
`INSERT INTO trustyca.invite
( id, org_id, project_id, inviter_id, email, role, created_at, expires_at
) VALUES ( $1, $2, $3, $4, $5, $6, Now(), $7
)
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(),
},
{
query.GetInvite, []any{&query.GetInviteRequest{OrgID: 1, Email: "[email protected]"}},
`SELECT ` + schema.InviteTableInfo.AllColumns() + `
FROM trustyca.invite
WHERE org_id = $1 AND email = $2 AND project_id IS NULL`,
},
{
query.GetInvite, []any{&query.GetInviteRequest{OrgID: 1, ProjectID: 3, Email: "[email protected]"}},
`SELECT ` + schema.InviteTableInfo.AllColumns() + `
FROM trustyca.invite
WHERE org_id = $1 AND email = $2 AND project_id = $3`,
},
{
query.GetOrgInvites, nil,
`SELECT ` + schema.InviteTableInfo.AllColumns() + `
FROM trustyca.invite
WHERE org_id = $1`,
},
{
query.GetUserInvites, nil,
`SELECT ` + schema.InviteTableInfo.AllColumns() + `
FROM trustyca.invite
WHERE email = $1 AND (expires_at IS NULL OR expires_at > Now())`,
},
{
query.TakeInvite, nil,
`DELETE FROM trustyca.invite
WHERE id = $1
RETURNING ` + schema.InviteTableInfo.AllColumns(),
},
{
// all scopes
query.DeleteInvite, []any{&query.DeleteInviteRequest{OrgID: 1, Email: "[email protected]"}},
`DELETE FROM trustyca.invite
WHERE org_id = $1 AND email = $2`,
},
{
// Org scope only
query.DeleteInvite, []any{&query.DeleteInviteRequest{OrgID: 1, Email: "[email protected]", Scope: pb.Scope_Org}},
`DELETE FROM trustyca.invite
WHERE org_id = $1 AND email = $2 AND project_id IS NULL`,
},
{
// Project scope of a project
query.DeleteInvite, []any{&query.DeleteInviteRequest{OrgID: 1, ProjectID: 3, Email: "[email protected]"}},
`DELETE FROM trustyca.invite
WHERE org_id = $1 AND email = $2 AND project_id = $3`,
},
{
query.DeleteInvite, []any{&query.DeleteInviteRequest{OrgID: 1}},
`DELETE FROM trustyca.invite
WHERE org_id = $1`,
},
{
query.DeleteInvite, []any{&query.DeleteInviteRequest{Email: "[email protected]"}},
`DELETE FROM trustyca.invite
WHERE email = $1`,
},
})
}