476 lines
12 KiB
Go
476 lines
12 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: identities.sql
|
|
|
|
package postgresdb
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const claimIdentitySubject = `-- name: ClaimIdentitySubject :one
|
|
UPDATE identity_subjects SET subject = $1, bound_at = $2
|
|
WHERE issuer = $3 AND subject = $4
|
|
RETURNING identity_id
|
|
`
|
|
|
|
type ClaimIdentitySubjectParams struct {
|
|
Subject string
|
|
BoundAt time.Time
|
|
Issuer string
|
|
Placeholder string
|
|
}
|
|
|
|
// Moves a placeholder binding onto the subject the provider asserts, in one
|
|
// statement, so only the first claimant gets it.
|
|
func (q *Queries) ClaimIdentitySubject(ctx context.Context, arg ClaimIdentitySubjectParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, claimIdentitySubject,
|
|
arg.Subject,
|
|
arg.BoundAt,
|
|
arg.Issuer,
|
|
arg.Placeholder,
|
|
)
|
|
var identity_id string
|
|
err := row.Scan(&identity_id)
|
|
return identity_id, err
|
|
}
|
|
|
|
const deleteIdentitySubject = `-- name: DeleteIdentitySubject :execrows
|
|
DELETE FROM identity_subjects WHERE identity_id = $1 AND issuer = $2 AND subject = $3
|
|
`
|
|
|
|
type DeleteIdentitySubjectParams struct {
|
|
IdentityID string
|
|
Issuer string
|
|
Subject string
|
|
}
|
|
|
|
func (q *Queries) DeleteIdentitySubject(ctx context.Context, arg DeleteIdentitySubjectParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteIdentitySubject, arg.IdentityID, arg.Issuer, arg.Subject)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const getIdentity = `-- name: GetIdentity :one
|
|
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
|
FROM identities WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetIdentity(ctx context.Context, id string) (Identity, error) {
|
|
row := q.db.QueryRow(ctx, getIdentity, id)
|
|
var i Identity
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.DisplayName,
|
|
&i.Lifecycle,
|
|
&i.Version,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getIdentitySubjectUse = `-- name: GetIdentitySubjectUse :one
|
|
SELECT subject, bound_at, name, expires_at, last_used_at FROM identity_subjects
|
|
WHERE issuer = $1 AND subject = $2
|
|
`
|
|
|
|
type GetIdentitySubjectUseParams struct {
|
|
Issuer string
|
|
Subject string
|
|
}
|
|
|
|
type GetIdentitySubjectUseRow struct {
|
|
Subject string
|
|
BoundAt time.Time
|
|
Name string
|
|
ExpiresAt pgtype.Timestamptz
|
|
LastUsedAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) GetIdentitySubjectUse(ctx context.Context, arg GetIdentitySubjectUseParams) (GetIdentitySubjectUseRow, error) {
|
|
row := q.db.QueryRow(ctx, getIdentitySubjectUse, arg.Issuer, arg.Subject)
|
|
var i GetIdentitySubjectUseRow
|
|
err := row.Scan(
|
|
&i.Subject,
|
|
&i.BoundAt,
|
|
&i.Name,
|
|
&i.ExpiresAt,
|
|
&i.LastUsedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getVisibleIdentity = `-- name: GetVisibleIdentity :one
|
|
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
|
FROM identities WHERE id = $1 AND ($2::text IS NULL
|
|
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = identities.id AND m.org_id = $2)
|
|
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = identities.id AND a.org_id = $2))
|
|
`
|
|
|
|
type GetVisibleIdentityParams struct {
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) GetVisibleIdentity(ctx context.Context, arg GetVisibleIdentityParams) (Identity, error) {
|
|
row := q.db.QueryRow(ctx, getVisibleIdentity, arg.ID, arg.ScopeOrg)
|
|
var i Identity
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.DisplayName,
|
|
&i.Lifecycle,
|
|
&i.Version,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const identityAuditOrg = `-- name: IdentityAuditOrg :one
|
|
SELECT min(org_id)::text AS org_id
|
|
FROM (
|
|
SELECT m.org_id FROM memberships m WHERE m.identity_id = $1
|
|
UNION
|
|
SELECT a.org_id FROM org_agents a WHERE a.identity_id = $1
|
|
) belonging
|
|
HAVING count(DISTINCT org_id) = 1
|
|
`
|
|
|
|
// The identity's org only when it belongs to exactly one, across memberships
|
|
// and agent assignments; no row otherwise. One statement, so a concurrent
|
|
// membership add cannot make the count and the org disagree.
|
|
func (q *Queries) IdentityAuditOrg(ctx context.Context, identityID string) (string, error) {
|
|
row := q.db.QueryRow(ctx, identityAuditOrg, identityID)
|
|
var org_id string
|
|
err := row.Scan(&org_id)
|
|
return org_id, err
|
|
}
|
|
|
|
const insertIdentity = `-- name: InsertIdentity :exec
|
|
INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
`
|
|
|
|
type InsertIdentityParams struct {
|
|
ID string
|
|
Kind string
|
|
DisplayName string
|
|
Lifecycle string
|
|
Version int64
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) InsertIdentity(ctx context.Context, arg InsertIdentityParams) error {
|
|
_, err := q.db.Exec(ctx, insertIdentity,
|
|
arg.ID,
|
|
arg.Kind,
|
|
arg.DisplayName,
|
|
arg.Lifecycle,
|
|
arg.Version,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertIdentityAlias = `-- name: InsertIdentityAlias :exec
|
|
INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2)
|
|
`
|
|
|
|
type InsertIdentityAliasParams struct {
|
|
Alias string
|
|
IdentityID string
|
|
}
|
|
|
|
func (q *Queries) InsertIdentityAlias(ctx context.Context, arg InsertIdentityAliasParams) error {
|
|
_, err := q.db.Exec(ctx, insertIdentityAlias, arg.Alias, arg.IdentityID)
|
|
return err
|
|
}
|
|
|
|
const insertIdentityAliasIgnoreConflict = `-- name: InsertIdentityAliasIgnoreConflict :exec
|
|
INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2) ON CONFLICT DO NOTHING
|
|
`
|
|
|
|
type InsertIdentityAliasIgnoreConflictParams struct {
|
|
Alias string
|
|
IdentityID string
|
|
}
|
|
|
|
func (q *Queries) InsertIdentityAliasIgnoreConflict(ctx context.Context, arg InsertIdentityAliasIgnoreConflictParams) error {
|
|
_, err := q.db.Exec(ctx, insertIdentityAliasIgnoreConflict, arg.Alias, arg.IdentityID)
|
|
return err
|
|
}
|
|
|
|
const insertIdentityEvent = `-- name: InsertIdentityEvent :exec
|
|
INSERT INTO identity_events (identity_id, event_type, from_lifecycle, to_lifecycle, display_name, actor_id, source, request_id, at, org_id)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
|
`
|
|
|
|
type InsertIdentityEventParams struct {
|
|
IdentityID string
|
|
EventType string
|
|
FromLifecycle string
|
|
ToLifecycle string
|
|
DisplayName string
|
|
ActorID string
|
|
Source string
|
|
RequestID string
|
|
At time.Time
|
|
OrgID pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) InsertIdentityEvent(ctx context.Context, arg InsertIdentityEventParams) error {
|
|
_, err := q.db.Exec(ctx, insertIdentityEvent,
|
|
arg.IdentityID,
|
|
arg.EventType,
|
|
arg.FromLifecycle,
|
|
arg.ToLifecycle,
|
|
arg.DisplayName,
|
|
arg.ActorID,
|
|
arg.Source,
|
|
arg.RequestID,
|
|
arg.At,
|
|
arg.OrgID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const listIdentities = `-- name: ListIdentities :many
|
|
|
|
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
|
FROM identities
|
|
WHERE ($1::text IS NULL
|
|
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = identities.id AND m.org_id = $1)
|
|
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = identities.id AND a.org_id = $1))
|
|
ORDER BY created_at, id
|
|
`
|
|
|
|
// Identity persistence. The domain owns lifecycle semantics (internal/domain/
|
|
// identity); these queries are the durable half of that contract. Aliases are
|
|
// unique on lower(alias) rather than the case-sensitive PRIMARY KEY, matching
|
|
// the SQLite schema's COLLATE NOCASE.
|
|
// A scoped read sees the identities that are members of, or agents assigned
|
|
// to, its org.
|
|
func (q *Queries) ListIdentities(ctx context.Context, scopeOrg pgtype.Text) ([]Identity, error) {
|
|
rows, err := q.db.Query(ctx, listIdentities, scopeOrg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Identity
|
|
for rows.Next() {
|
|
var i Identity
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.DisplayName,
|
|
&i.Lifecycle,
|
|
&i.Version,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listIdentitySubjects = `-- name: ListIdentitySubjects :many
|
|
SELECT subject, bound_at, name, expires_at, last_used_at FROM identity_subjects
|
|
WHERE identity_id = $1 AND issuer = $2
|
|
ORDER BY bound_at DESC
|
|
`
|
|
|
|
type ListIdentitySubjectsParams struct {
|
|
IdentityID string
|
|
Issuer string
|
|
}
|
|
|
|
type ListIdentitySubjectsRow struct {
|
|
Subject string
|
|
BoundAt time.Time
|
|
Name string
|
|
ExpiresAt pgtype.Timestamptz
|
|
LastUsedAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) ListIdentitySubjects(ctx context.Context, arg ListIdentitySubjectsParams) ([]ListIdentitySubjectsRow, error) {
|
|
rows, err := q.db.Query(ctx, listIdentitySubjects, arg.IdentityID, arg.Issuer)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListIdentitySubjectsRow
|
|
for rows.Next() {
|
|
var i ListIdentitySubjectsRow
|
|
if err := rows.Scan(
|
|
&i.Subject,
|
|
&i.BoundAt,
|
|
&i.Name,
|
|
&i.ExpiresAt,
|
|
&i.LastUsedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const resolveIdentityAlias = `-- name: ResolveIdentityAlias :one
|
|
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
|
|
FROM identities i JOIN identity_aliases a ON a.identity_id = i.id
|
|
WHERE lower(a.alias) = lower($1) AND ($2::text IS NULL
|
|
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = i.id AND m.org_id = $2)
|
|
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = i.id AND a.org_id = $2))
|
|
`
|
|
|
|
type ResolveIdentityAliasParams struct {
|
|
Alias string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) ResolveIdentityAlias(ctx context.Context, arg ResolveIdentityAliasParams) (Identity, error) {
|
|
row := q.db.QueryRow(ctx, resolveIdentityAlias, arg.Alias, arg.ScopeOrg)
|
|
var i Identity
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.DisplayName,
|
|
&i.Lifecycle,
|
|
&i.Version,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const resolveIdentitySubject = `-- name: ResolveIdentitySubject :one
|
|
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
|
|
FROM identities i JOIN identity_subjects b ON b.identity_id = i.id
|
|
WHERE b.issuer = $1 AND b.subject = $2
|
|
`
|
|
|
|
type ResolveIdentitySubjectParams struct {
|
|
Issuer string
|
|
Subject string
|
|
}
|
|
|
|
func (q *Queries) ResolveIdentitySubject(ctx context.Context, arg ResolveIdentitySubjectParams) (Identity, error) {
|
|
row := q.db.QueryRow(ctx, resolveIdentitySubject, arg.Issuer, arg.Subject)
|
|
var i Identity
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Kind,
|
|
&i.DisplayName,
|
|
&i.Lifecycle,
|
|
&i.Version,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const touchIdentitySubject = `-- name: TouchIdentitySubject :exec
|
|
UPDATE identity_subjects SET last_used_at = $3 WHERE issuer = $1 AND subject = $2
|
|
`
|
|
|
|
type TouchIdentitySubjectParams struct {
|
|
Issuer string
|
|
Subject string
|
|
LastUsedAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) TouchIdentitySubject(ctx context.Context, arg TouchIdentitySubjectParams) error {
|
|
_, err := q.db.Exec(ctx, touchIdentitySubject, arg.Issuer, arg.Subject, arg.LastUsedAt)
|
|
return err
|
|
}
|
|
|
|
const updateIdentity = `-- name: UpdateIdentity :execrows
|
|
UPDATE identities SET display_name = $2, lifecycle = $3, version = $4, updated_at = $5
|
|
WHERE id = $1 AND version = $6
|
|
`
|
|
|
|
type UpdateIdentityParams struct {
|
|
ID string
|
|
DisplayName string
|
|
Lifecycle string
|
|
Version int64
|
|
UpdatedAt time.Time
|
|
Version_2 int64
|
|
}
|
|
|
|
func (q *Queries) UpdateIdentity(ctx context.Context, arg UpdateIdentityParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, updateIdentity,
|
|
arg.ID,
|
|
arg.DisplayName,
|
|
arg.Lifecycle,
|
|
arg.Version,
|
|
arg.UpdatedAt,
|
|
arg.Version_2,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const updateTaskIdentity = `-- name: UpdateTaskIdentity :exec
|
|
UPDATE tasks SET identity = $1 WHERE identity = $2
|
|
`
|
|
|
|
type UpdateTaskIdentityParams struct {
|
|
Identity string
|
|
Identity_2 string
|
|
}
|
|
|
|
func (q *Queries) UpdateTaskIdentity(ctx context.Context, arg UpdateTaskIdentityParams) error {
|
|
_, err := q.db.Exec(ctx, updateTaskIdentity, arg.Identity, arg.Identity_2)
|
|
return err
|
|
}
|
|
|
|
const upsertIdentitySubject = `-- name: UpsertIdentitySubject :exec
|
|
INSERT INTO identity_subjects (issuer, subject, identity_id, bound_at, name, expires_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
ON CONFLICT (issuer, subject) DO UPDATE SET identity_id = excluded.identity_id, bound_at = excluded.bound_at,
|
|
name = excluded.name, expires_at = excluded.expires_at
|
|
`
|
|
|
|
type UpsertIdentitySubjectParams struct {
|
|
Issuer string
|
|
Subject string
|
|
IdentityID string
|
|
BoundAt time.Time
|
|
Name string
|
|
ExpiresAt pgtype.Timestamptz
|
|
}
|
|
|
|
func (q *Queries) UpsertIdentitySubject(ctx context.Context, arg UpsertIdentitySubjectParams) error {
|
|
_, err := q.db.Exec(ctx, upsertIdentitySubject,
|
|
arg.Issuer,
|
|
arg.Subject,
|
|
arg.IdentityID,
|
|
arg.BoundAt,
|
|
arg.Name,
|
|
arg.ExpiresAt,
|
|
)
|
|
return err
|
|
}
|