feat(store): add per-organization sso providers and linked identities
Identities are keyed by provider and subject. Both tables are tenant scoped; org_id_by_slug resolves a slug before sign-in without opening a broader scope. Refs #4 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
c485d1a96f
commit
d1976ed57a
8 files changed
+544
No files matched your search
@@ -0,0 +1,70 @@
|
|||||||
|
-- +goose Up
|
||||||
|
CREATE TABLE sso_providers (
|
||||||
|
id uuid PRIMARY KEY,
|
||||||
|
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
|
||||||
|
name text NOT NULL CHECK (length(name) BETWEEN 1 AND 100),
|
||||||
|
issuer text NOT NULL CHECK (issuer ~ '^https?://[^\s]+$' AND length(issuer) <= 500),
|
||||||
|
client_id text NOT NULL CHECK (length(client_id) BETWEEN 1 AND 500),
|
||||||
|
-- Sealed with the application keyring, bound to the provider id.
|
||||||
|
client_secret bytea,
|
||||||
|
scopes text[] NOT NULL DEFAULT '{openid,email,profile}',
|
||||||
|
-- Lower-case domains; empty means any. Applies to auto-provisioning only.
|
||||||
|
allowed_domains text[] NOT NULL DEFAULT '{}',
|
||||||
|
auto_provision boolean NOT NULL DEFAULT false,
|
||||||
|
enabled boolean NOT NULL DEFAULT true,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
updated_at timestamptz NOT NULL DEFAULT now()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX sso_providers_org_idx ON sso_providers (org_id);
|
||||||
|
|
||||||
|
-- Accounts are linked by issuer subject, never by email: an org admin
|
||||||
|
-- controls their IdP and could otherwise assert any address.
|
||||||
|
CREATE TABLE user_identities (
|
||||||
|
provider_id uuid NOT NULL REFERENCES sso_providers (id) ON DELETE CASCADE,
|
||||||
|
subject text NOT NULL CHECK (length(subject) BETWEEN 1 AND 255),
|
||||||
|
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
|
||||||
|
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||||
|
email citext,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
last_login_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
PRIMARY KEY (provider_id, subject)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX user_identities_user_idx ON user_identities (user_id);
|
||||||
|
|
||||||
|
ALTER TABLE sso_providers ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE sso_providers FORCE ROW LEVEL SECURITY;
|
||||||
|
CREATE POLICY sso_providers_tenant ON sso_providers FOR ALL
|
||||||
|
USING (app_scope_system() OR org_id = app_org_id())
|
||||||
|
WITH CHECK (app_scope_system() OR org_id = app_org_id());
|
||||||
|
|
||||||
|
ALTER TABLE user_identities ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE user_identities FORCE ROW LEVEL SECURITY;
|
||||||
|
CREATE POLICY user_identities_tenant ON user_identities FOR ALL
|
||||||
|
USING (app_scope_system() OR org_id = app_org_id() OR user_id = app_user_id())
|
||||||
|
WITH CHECK (app_scope_system() OR org_id = app_org_id());
|
||||||
|
|
||||||
|
-- SSO starts before anyone is signed in. This reveals only whether a slug
|
||||||
|
-- exists, which the public /sso/<slug> URL does anyway. FORCE ROW LEVEL
|
||||||
|
-- SECURITY applies to the table owner too, so SECURITY DEFINER would not
|
||||||
|
-- help; the system scope is raised for this single lookup and restored.
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE FUNCTION org_id_by_slug(s citext) RETURNS uuid
|
||||||
|
LANGUAGE plpgsql VOLATILE AS $$
|
||||||
|
DECLARE
|
||||||
|
prev text := coalesce(current_setting('app.scope', true), '');
|
||||||
|
result uuid;
|
||||||
|
BEGIN
|
||||||
|
PERFORM set_config('app.scope', 'system', true);
|
||||||
|
SELECT id INTO result FROM orgs WHERE slug = s;
|
||||||
|
PERFORM set_config('app.scope', prev, true);
|
||||||
|
RETURN result;
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
DROP FUNCTION org_id_by_slug(citext);
|
||||||
|
DROP TABLE user_identities;
|
||||||
|
DROP TABLE sso_providers;
|
||||||
@@ -27,6 +27,27 @@ func (q *Queries) AddMembership(ctx context.Context, arg AddMembershipParams) er
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getMembership = `-- name: GetMembership :one
|
||||||
|
SELECT org_id, user_id, is_owner, created_at FROM memberships WHERE org_id = $1 AND user_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetMembershipParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
UserID uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetMembership(ctx context.Context, arg GetMembershipParams) (Membership, error) {
|
||||||
|
row := q.db.QueryRow(ctx, getMembership, arg.OrgID, arg.UserID)
|
||||||
|
var i Membership
|
||||||
|
err := row.Scan(
|
||||||
|
&i.OrgID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.IsOwner,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
const listOrgMembers = `-- name: ListOrgMembers :many
|
const listOrgMembers = `-- name: ListOrgMembers :many
|
||||||
SELECT u.id, u.email, u.display_name, m.is_owner, m.created_at AS joined_at
|
SELECT u.id, u.email, u.display_name, m.is_owner, m.created_at AS joined_at
|
||||||
FROM memberships m
|
FROM memberships m
|
||||||
|
|||||||
@@ -26,6 +26,21 @@ type Org struct {
|
|||||||
RequireTotp bool
|
RequireTotp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SsoProvider struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Name string
|
||||||
|
Issuer string
|
||||||
|
ClientID string
|
||||||
|
ClientSecret []byte
|
||||||
|
Scopes []string
|
||||||
|
AllowedDomains []string
|
||||||
|
AutoProvision bool
|
||||||
|
Enabled bool
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
Email string
|
Email string
|
||||||
@@ -38,3 +53,13 @@ type User struct {
|
|||||||
TotpEnabledAt *time.Time
|
TotpEnabledAt *time.Time
|
||||||
Locale *string
|
Locale *string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserIdentity struct {
|
||||||
|
ProviderID uuid.UUID
|
||||||
|
Subject string
|
||||||
|
OrgID uuid.UUID
|
||||||
|
UserID uuid.UUID
|
||||||
|
Email *string
|
||||||
|
CreatedAt time.Time
|
||||||
|
LastLoginAt time.Time
|
||||||
|
}
|
||||||
@@ -13,22 +13,35 @@ import (
|
|||||||
type Querier interface {
|
type Querier interface {
|
||||||
AddMembership(ctx context.Context, arg AddMembershipParams) error
|
AddMembership(ctx context.Context, arg AddMembershipParams) error
|
||||||
AnyUserExists(ctx context.Context) (bool, error)
|
AnyUserExists(ctx context.Context) (bool, error)
|
||||||
|
CreateIdentity(ctx context.Context, arg CreateIdentityParams) error
|
||||||
CreateOrg(ctx context.Context, arg CreateOrgParams) (Org, error)
|
CreateOrg(ctx context.Context, arg CreateOrgParams) (Org, error)
|
||||||
|
CreateSSOProvider(ctx context.Context, arg CreateSSOProviderParams) (SsoProvider, error)
|
||||||
CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
|
CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
|
||||||
DeleteOrg(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteOrg(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
|
DeleteSSOProvider(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteUser(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteUser(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DisableTOTP(ctx context.Context, id uuid.UUID) error
|
DisableTOTP(ctx context.Context, id uuid.UUID) error
|
||||||
EnableTOTP(ctx context.Context, arg EnableTOTPParams) error
|
EnableTOTP(ctx context.Context, arg EnableTOTPParams) error
|
||||||
|
GetIdentity(ctx context.Context, arg GetIdentityParams) (UserIdentity, error)
|
||||||
|
GetMembership(ctx context.Context, arg GetMembershipParams) (Membership, error)
|
||||||
GetOrg(ctx context.Context, id uuid.UUID) (Org, error)
|
GetOrg(ctx context.Context, id uuid.UUID) (Org, error)
|
||||||
GetOrgBySlug(ctx context.Context, slug string) (Org, error)
|
GetOrgBySlug(ctx context.Context, slug string) (Org, error)
|
||||||
|
GetSSOProvider(ctx context.Context, id uuid.UUID) (SsoProvider, error)
|
||||||
GetUser(ctx context.Context, id uuid.UUID) (User, error)
|
GetUser(ctx context.Context, id uuid.UUID) (User, error)
|
||||||
GetUserByEmail(ctx context.Context, email string) (User, error)
|
GetUserByEmail(ctx context.Context, email string) (User, error)
|
||||||
|
IsMember(ctx context.Context, arg IsMemberParams) (bool, error)
|
||||||
|
ListEnabledSSOProviders(ctx context.Context) ([]ListEnabledSSOProvidersRow, error)
|
||||||
ListOrgMembers(ctx context.Context, orgID uuid.UUID) ([]ListOrgMembersRow, error)
|
ListOrgMembers(ctx context.Context, orgID uuid.UUID) ([]ListOrgMembersRow, error)
|
||||||
ListOrgs(ctx context.Context) ([]Org, error)
|
ListOrgs(ctx context.Context) ([]Org, error)
|
||||||
|
ListSSOProviders(ctx context.Context) ([]SsoProvider, error)
|
||||||
|
ListUserIdentitiesInOrg(ctx context.Context, userID uuid.UUID) ([]ListUserIdentitiesInOrgRow, error)
|
||||||
ListUserOrgs(ctx context.Context, userID uuid.UUID) ([]ListUserOrgsRow, error)
|
ListUserOrgs(ctx context.Context, userID uuid.UUID) ([]ListUserOrgsRow, error)
|
||||||
|
OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error)
|
||||||
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
|
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
|
||||||
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
|
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
|
||||||
SetUserLocale(ctx context.Context, arg SetUserLocaleParams) error
|
SetUserLocale(ctx context.Context, arg SetUserLocaleParams) error
|
||||||
|
TouchIdentity(ctx context.Context, arg TouchIdentityParams) error
|
||||||
|
UpdateSSOProvider(ctx context.Context, arg UpdateSSOProviderParams) (SsoProvider, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Querier = (*Queries)(nil)
|
var _ Querier = (*Queries)(nil)
|
||||||
@@ -17,3 +17,6 @@ FROM memberships m
|
|||||||
JOIN orgs o ON o.id = m.org_id
|
JOIN orgs o ON o.id = m.org_id
|
||||||
WHERE m.user_id = $1
|
WHERE m.user_id = $1
|
||||||
ORDER BY o.name;
|
ORDER BY o.name;
|
||||||
|
|
||||||
|
-- name: GetMembership :one
|
||||||
|
SELECT * FROM memberships WHERE org_id = $1 AND user_id = $2;
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
-- name: OrgIDBySlug :one
|
||||||
|
SELECT coalesce(org_id_by_slug(sqlc.arg(slug)::citext), '00000000-0000-0000-0000-000000000000')::uuid AS id;
|
||||||
|
|
||||||
|
-- name: CreateSSOProvider :one
|
||||||
|
INSERT INTO sso_providers (id, org_id, name, issuer, client_id, client_secret, scopes, allowed_domains, auto_provision, enabled)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: UpdateSSOProvider :one
|
||||||
|
UPDATE sso_providers SET
|
||||||
|
name = $2,
|
||||||
|
issuer = $3,
|
||||||
|
client_id = $4,
|
||||||
|
client_secret = coalesce(sqlc.narg(client_secret), client_secret),
|
||||||
|
scopes = $5,
|
||||||
|
allowed_domains = $6,
|
||||||
|
auto_provision = $7,
|
||||||
|
enabled = $8,
|
||||||
|
updated_at = now()
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: DeleteSSOProvider :execrows
|
||||||
|
DELETE FROM sso_providers WHERE id = $1;
|
||||||
|
|
||||||
|
-- name: GetSSOProvider :one
|
||||||
|
SELECT * FROM sso_providers WHERE id = $1;
|
||||||
|
|
||||||
|
-- name: ListSSOProviders :many
|
||||||
|
SELECT * FROM sso_providers ORDER BY name;
|
||||||
|
|
||||||
|
-- name: ListEnabledSSOProviders :many
|
||||||
|
SELECT id, name FROM sso_providers WHERE enabled ORDER BY name;
|
||||||
|
|
||||||
|
-- name: GetIdentity :one
|
||||||
|
SELECT * FROM user_identities WHERE provider_id = $1 AND subject = $2;
|
||||||
|
|
||||||
|
-- name: CreateIdentity :exec
|
||||||
|
INSERT INTO user_identities (provider_id, subject, org_id, user_id, email)
|
||||||
|
VALUES ($1, $2, $3, $4, $5);
|
||||||
|
|
||||||
|
-- name: TouchIdentity :exec
|
||||||
|
UPDATE user_identities SET email = $3, last_login_at = now()
|
||||||
|
WHERE provider_id = $1 AND subject = $2;
|
||||||
|
|
||||||
|
-- name: IsMember :one
|
||||||
|
SELECT EXISTS (SELECT 1 FROM memberships WHERE org_id = $1 AND user_id = $2);
|
||||||
|
|
||||||
|
-- name: ListUserIdentitiesInOrg :many
|
||||||
|
SELECT p.name AS provider_name, i.email, i.created_at, i.last_login_at
|
||||||
|
FROM user_identities i
|
||||||
|
JOIN sso_providers p ON p.id = i.provider_id
|
||||||
|
WHERE i.user_id = $1
|
||||||
|
ORDER BY p.name;
|
||||||
@@ -0,0 +1,353 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
// source: sso.sql
|
||||||
|
|
||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
const createIdentity = `-- name: CreateIdentity :exec
|
||||||
|
INSERT INTO user_identities (provider_id, subject, org_id, user_id, email)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateIdentityParams struct {
|
||||||
|
ProviderID uuid.UUID
|
||||||
|
Subject string
|
||||||
|
OrgID uuid.UUID
|
||||||
|
UserID uuid.UUID
|
||||||
|
Email *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateIdentity(ctx context.Context, arg CreateIdentityParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, createIdentity,
|
||||||
|
arg.ProviderID,
|
||||||
|
arg.Subject,
|
||||||
|
arg.OrgID,
|
||||||
|
arg.UserID,
|
||||||
|
arg.Email,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const createSSOProvider = `-- name: CreateSSOProvider :one
|
||||||
|
INSERT INTO sso_providers (id, org_id, name, issuer, client_id, client_secret, scopes, allowed_domains, auto_provision, enabled)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||||
|
RETURNING id, org_id, name, issuer, client_id, client_secret, scopes, allowed_domains, auto_provision, enabled, created_at, updated_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateSSOProviderParams struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Name string
|
||||||
|
Issuer string
|
||||||
|
ClientID string
|
||||||
|
ClientSecret []byte
|
||||||
|
Scopes []string
|
||||||
|
AllowedDomains []string
|
||||||
|
AutoProvision bool
|
||||||
|
Enabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateSSOProvider(ctx context.Context, arg CreateSSOProviderParams) (SsoProvider, error) {
|
||||||
|
row := q.db.QueryRow(ctx, createSSOProvider,
|
||||||
|
arg.ID,
|
||||||
|
arg.OrgID,
|
||||||
|
arg.Name,
|
||||||
|
arg.Issuer,
|
||||||
|
arg.ClientID,
|
||||||
|
arg.ClientSecret,
|
||||||
|
arg.Scopes,
|
||||||
|
arg.AllowedDomains,
|
||||||
|
arg.AutoProvision,
|
||||||
|
arg.Enabled,
|
||||||
|
)
|
||||||
|
var i SsoProvider
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Issuer,
|
||||||
|
&i.ClientID,
|
||||||
|
&i.ClientSecret,
|
||||||
|
&i.Scopes,
|
||||||
|
&i.AllowedDomains,
|
||||||
|
&i.AutoProvision,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteSSOProvider = `-- name: DeleteSSOProvider :execrows
|
||||||
|
DELETE FROM sso_providers WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) DeleteSSOProvider(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||||
|
result, err := q.db.Exec(ctx, deleteSSOProvider, id)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return result.RowsAffected(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const getIdentity = `-- name: GetIdentity :one
|
||||||
|
SELECT provider_id, subject, org_id, user_id, email, created_at, last_login_at FROM user_identities WHERE provider_id = $1 AND subject = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type GetIdentityParams struct {
|
||||||
|
ProviderID uuid.UUID
|
||||||
|
Subject string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) GetIdentity(ctx context.Context, arg GetIdentityParams) (UserIdentity, error) {
|
||||||
|
row := q.db.QueryRow(ctx, getIdentity, arg.ProviderID, arg.Subject)
|
||||||
|
var i UserIdentity
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ProviderID,
|
||||||
|
&i.Subject,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.Email,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.LastLoginAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getSSOProvider = `-- name: GetSSOProvider :one
|
||||||
|
SELECT id, org_id, name, issuer, client_id, client_secret, scopes, allowed_domains, auto_provision, enabled, created_at, updated_at FROM sso_providers WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetSSOProvider(ctx context.Context, id uuid.UUID) (SsoProvider, error) {
|
||||||
|
row := q.db.QueryRow(ctx, getSSOProvider, id)
|
||||||
|
var i SsoProvider
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Issuer,
|
||||||
|
&i.ClientID,
|
||||||
|
&i.ClientSecret,
|
||||||
|
&i.Scopes,
|
||||||
|
&i.AllowedDomains,
|
||||||
|
&i.AutoProvision,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const isMember = `-- name: IsMember :one
|
||||||
|
SELECT EXISTS (SELECT 1 FROM memberships WHERE org_id = $1 AND user_id = $2)
|
||||||
|
`
|
||||||
|
|
||||||
|
type IsMemberParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
UserID uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) IsMember(ctx context.Context, arg IsMemberParams) (bool, error) {
|
||||||
|
row := q.db.QueryRow(ctx, isMember, arg.OrgID, arg.UserID)
|
||||||
|
var exists bool
|
||||||
|
err := row.Scan(&exists)
|
||||||
|
return exists, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listEnabledSSOProviders = `-- name: ListEnabledSSOProviders :many
|
||||||
|
SELECT id, name FROM sso_providers WHERE enabled ORDER BY name
|
||||||
|
`
|
||||||
|
|
||||||
|
type ListEnabledSSOProvidersRow struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) ListEnabledSSOProviders(ctx context.Context) ([]ListEnabledSSOProvidersRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, listEnabledSSOProviders)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []ListEnabledSSOProvidersRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i ListEnabledSSOProvidersRow
|
||||||
|
if err := rows.Scan(&i.ID, &i.Name); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const listSSOProviders = `-- name: ListSSOProviders :many
|
||||||
|
SELECT id, org_id, name, issuer, client_id, client_secret, scopes, allowed_domains, auto_provision, enabled, created_at, updated_at FROM sso_providers ORDER BY name
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListSSOProviders(ctx context.Context) ([]SsoProvider, error) {
|
||||||
|
rows, err := q.db.Query(ctx, listSSOProviders)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []SsoProvider{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i SsoProvider
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Issuer,
|
||||||
|
&i.ClientID,
|
||||||
|
&i.ClientSecret,
|
||||||
|
&i.Scopes,
|
||||||
|
&i.AllowedDomains,
|
||||||
|
&i.AutoProvision,
|
||||||
|
&i.Enabled,
|
||||||
|
&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 listUserIdentitiesInOrg = `-- name: ListUserIdentitiesInOrg :many
|
||||||
|
SELECT p.name AS provider_name, i.email, i.created_at, i.last_login_at
|
||||||
|
FROM user_identities i
|
||||||
|
JOIN sso_providers p ON p.id = i.provider_id
|
||||||
|
WHERE i.user_id = $1
|
||||||
|
ORDER BY p.name
|
||||||
|
`
|
||||||
|
|
||||||
|
type ListUserIdentitiesInOrgRow struct {
|
||||||
|
ProviderName string
|
||||||
|
Email *string
|
||||||
|
CreatedAt time.Time
|
||||||
|
LastLoginAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) ListUserIdentitiesInOrg(ctx context.Context, userID uuid.UUID) ([]ListUserIdentitiesInOrgRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, listUserIdentitiesInOrg, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []ListUserIdentitiesInOrgRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i ListUserIdentitiesInOrgRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ProviderName,
|
||||||
|
&i.Email,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.LastLoginAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const orgIDBySlug = `-- name: OrgIDBySlug :one
|
||||||
|
SELECT coalesce(org_id_by_slug($1::citext), '00000000-0000-0000-0000-000000000000')::uuid AS id
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error) {
|
||||||
|
row := q.db.QueryRow(ctx, orgIDBySlug, slug)
|
||||||
|
var id uuid.UUID
|
||||||
|
err := row.Scan(&id)
|
||||||
|
return id, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const touchIdentity = `-- name: TouchIdentity :exec
|
||||||
|
UPDATE user_identities SET email = $3, last_login_at = now()
|
||||||
|
WHERE provider_id = $1 AND subject = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type TouchIdentityParams struct {
|
||||||
|
ProviderID uuid.UUID
|
||||||
|
Subject string
|
||||||
|
Email *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) TouchIdentity(ctx context.Context, arg TouchIdentityParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, touchIdentity, arg.ProviderID, arg.Subject, arg.Email)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateSSOProvider = `-- name: UpdateSSOProvider :one
|
||||||
|
UPDATE sso_providers SET
|
||||||
|
name = $2,
|
||||||
|
issuer = $3,
|
||||||
|
client_id = $4,
|
||||||
|
client_secret = coalesce($9, client_secret),
|
||||||
|
scopes = $5,
|
||||||
|
allowed_domains = $6,
|
||||||
|
auto_provision = $7,
|
||||||
|
enabled = $8,
|
||||||
|
updated_at = now()
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING id, org_id, name, issuer, client_id, client_secret, scopes, allowed_domains, auto_provision, enabled, created_at, updated_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateSSOProviderParams struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
Name string
|
||||||
|
Issuer string
|
||||||
|
ClientID string
|
||||||
|
Scopes []string
|
||||||
|
AllowedDomains []string
|
||||||
|
AutoProvision bool
|
||||||
|
Enabled bool
|
||||||
|
ClientSecret []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateSSOProvider(ctx context.Context, arg UpdateSSOProviderParams) (SsoProvider, error) {
|
||||||
|
row := q.db.QueryRow(ctx, updateSSOProvider,
|
||||||
|
arg.ID,
|
||||||
|
arg.Name,
|
||||||
|
arg.Issuer,
|
||||||
|
arg.ClientID,
|
||||||
|
arg.Scopes,
|
||||||
|
arg.AllowedDomains,
|
||||||
|
arg.AutoProvision,
|
||||||
|
arg.Enabled,
|
||||||
|
arg.ClientSecret,
|
||||||
|
)
|
||||||
|
var i SsoProvider
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Issuer,
|
||||||
|
&i.ClientID,
|
||||||
|
&i.ClientSecret,
|
||||||
|
&i.Scopes,
|
||||||
|
&i.AllowedDomains,
|
||||||
|
&i.AutoProvision,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
@@ -16,6 +16,11 @@ sql:
|
|||||||
go_type: github.com/google/uuid.UUID
|
go_type: github.com/google/uuid.UUID
|
||||||
- db_type: citext
|
- db_type: citext
|
||||||
go_type: string
|
go_type: string
|
||||||
|
- db_type: citext
|
||||||
|
nullable: true
|
||||||
|
go_type:
|
||||||
|
type: string
|
||||||
|
pointer: true
|
||||||
- db_type: timestamptz
|
- db_type: timestamptz
|
||||||
go_type: time.Time
|
go_type: time.Time
|
||||||
- db_type: timestamptz
|
- db_type: timestamptz
|
||||||
|
|||||||
Reference in new issue
Block a user