The source headers, LICENSE files, and license metadata had drifted apart. Align the entire project to MIT: - Convert every source-file header to the MIT text across all comment styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including SPDX-License-Identifier tags - Set the root and cookie-banner LICENSE files to the MIT text with a "MIT License" title line - Switch the package.json license fields, Docker image label, and cookie-banner README to MIT - Update docs and the genmodels header generator accordingly - Normalize copyright lines to a single format (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the hello@getprobo.com and hello@probo.inc emails to hello@probo.com and the comma-separated years to a hyphenated range Genuine third-party references are intentionally left untouched: the Lucide icon attributions (Lucide is ISC) and the trivy dependency license allowlist. Signed-off-by: Sacha Al Himdani <sacha@probo.com>
532 lines
11 KiB
Go
532 lines
11 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
package coredata
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"go.gearno.de/kit/pg"
|
|
"go.probo.inc/probo/pkg/gid"
|
|
"go.probo.inc/probo/pkg/iam/policy"
|
|
"go.probo.inc/probo/pkg/page"
|
|
"go.probo.inc/probo/pkg/uri"
|
|
)
|
|
|
|
type (
|
|
OAuth2Consent struct {
|
|
ID gid.GID `db:"id"`
|
|
IdentityID gid.GID `db:"identity_id"`
|
|
SessionID gid.GID `db:"session_id"`
|
|
ClientID gid.GID `db:"client_id"`
|
|
Scopes OAuth2Scopes `db:"scopes"`
|
|
RedirectURI *uri.URI `db:"redirect_uri"`
|
|
CodeChallenge string `db:"code_challenge"`
|
|
CodeChallengeMethod OAuth2CodeChallengeMethod `db:"code_challenge_method"`
|
|
Nonce string `db:"nonce"`
|
|
State string `db:"state"`
|
|
DeviceCodeID *gid.GID `db:"device_code_id"`
|
|
Approved bool `db:"approved"`
|
|
CreatedAt time.Time `db:"created_at"`
|
|
UpdatedAt time.Time `db:"updated_at"`
|
|
}
|
|
|
|
OAuth2Consents []*OAuth2Consent
|
|
)
|
|
|
|
func (c *OAuth2Consent) CursorKey(orderBy OAuth2ConsentOrderField) page.CursorKey {
|
|
switch orderBy {
|
|
case OAuth2ConsentOrderFieldCreatedAt:
|
|
return page.NewCursorKey(c.ID, c.CreatedAt)
|
|
}
|
|
|
|
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
|
}
|
|
|
|
func (c *OAuth2Consent) AuthorizationAttributes(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
resourceIDs []gid.GID,
|
|
) (policy.AttributesByID, error) {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
identity_id,
|
|
session_id
|
|
FROM
|
|
iam_oauth2_consents
|
|
WHERE
|
|
id = ANY(@resource_ids::text[])
|
|
`
|
|
|
|
args := pgx.StrictNamedArgs{
|
|
"resource_ids": resourceIDs,
|
|
}
|
|
|
|
rows, err := conn.Query(ctx, q, args)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot query oauth2 consent authorization attributes: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
attrsByID := make(policy.AttributesByID, len(resourceIDs))
|
|
|
|
for rows.Next() {
|
|
var (
|
|
id gid.GID
|
|
identityID gid.GID
|
|
sessionID gid.GID
|
|
)
|
|
|
|
err = rows.Scan(&id, &identityID, &sessionID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot scan oauth2 consent authorization attributes: %w", err)
|
|
}
|
|
|
|
attrsByID[id] = policy.Attributes{
|
|
"identity_id": identityID.String(),
|
|
"session_id": sessionID.String(),
|
|
}
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("cannot iterate oauth2 consent authorization attributes: %w", err)
|
|
}
|
|
|
|
return attrsByID, nil
|
|
}
|
|
|
|
func (c *OAuth2Consent) LoadByID(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
id gid.GID,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
identity_id,
|
|
session_id,
|
|
client_id,
|
|
scopes,
|
|
redirect_uri,
|
|
code_challenge,
|
|
code_challenge_method,
|
|
nonce,
|
|
state,
|
|
device_code_id,
|
|
approved,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
iam_oauth2_consents
|
|
WHERE
|
|
id = @id
|
|
LIMIT 1;
|
|
`
|
|
|
|
rows, err := conn.Query(ctx, q, pgx.StrictNamedArgs{"id": id})
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query oauth2_consent: %w", err)
|
|
}
|
|
|
|
consent, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[OAuth2Consent])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect oauth2_consent: %w", err)
|
|
}
|
|
|
|
*c = consent
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *OAuth2Consent) LoadByIDForSession(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
id gid.GID,
|
|
identityID gid.GID,
|
|
sessionID gid.GID,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
identity_id,
|
|
session_id,
|
|
client_id,
|
|
scopes,
|
|
redirect_uri,
|
|
code_challenge,
|
|
code_challenge_method,
|
|
nonce,
|
|
state,
|
|
device_code_id,
|
|
approved,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
iam_oauth2_consents
|
|
WHERE
|
|
id = @id
|
|
AND identity_id = @identity_id
|
|
AND session_id = @session_id
|
|
LIMIT 1;
|
|
`
|
|
|
|
rows, err := conn.Query(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{
|
|
"id": id,
|
|
"identity_id": identityID,
|
|
"session_id": sessionID,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query oauth2_consent: %w", err)
|
|
}
|
|
|
|
consent, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[OAuth2Consent])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect oauth2_consent: %w", err)
|
|
}
|
|
|
|
*c = consent
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *OAuth2Consent) LoadByIDForSessionForUpdate(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
id gid.GID,
|
|
identityID gid.GID,
|
|
sessionID gid.GID,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
identity_id,
|
|
session_id,
|
|
client_id,
|
|
scopes,
|
|
redirect_uri,
|
|
code_challenge,
|
|
code_challenge_method,
|
|
nonce,
|
|
state,
|
|
device_code_id,
|
|
approved,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
iam_oauth2_consents
|
|
WHERE
|
|
id = @id
|
|
AND identity_id = @identity_id
|
|
AND session_id = @session_id
|
|
LIMIT 1
|
|
FOR UPDATE;
|
|
`
|
|
|
|
rows, err := conn.Query(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{
|
|
"id": id,
|
|
"identity_id": identityID,
|
|
"session_id": sessionID,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query oauth2_consent: %w", err)
|
|
}
|
|
|
|
consent, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[OAuth2Consent])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect oauth2_consent: %w", err)
|
|
}
|
|
|
|
*c = consent
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *OAuth2Consent) LoadMatchingConsent(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
identityID gid.GID,
|
|
clientID gid.GID,
|
|
scopes OAuth2Scopes,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
identity_id,
|
|
session_id,
|
|
client_id,
|
|
scopes,
|
|
redirect_uri,
|
|
code_challenge,
|
|
code_challenge_method,
|
|
nonce,
|
|
state,
|
|
device_code_id,
|
|
approved,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
iam_oauth2_consents
|
|
WHERE
|
|
identity_id = @identity_id
|
|
AND client_id = @client_id
|
|
AND approved = TRUE
|
|
AND scopes @> @scopes
|
|
AND scopes <@ @scopes
|
|
LIMIT 1;
|
|
`
|
|
|
|
rows, err := conn.Query(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{
|
|
"identity_id": identityID,
|
|
"client_id": clientID,
|
|
"scopes": scopes,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query oauth2_consent: %w", err)
|
|
}
|
|
|
|
consent, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[OAuth2Consent])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect oauth2_consent: %w", err)
|
|
}
|
|
|
|
*c = consent
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *OAuth2Consent) Insert(ctx context.Context, conn pg.Tx) error {
|
|
q := `
|
|
INSERT INTO iam_oauth2_consents (
|
|
id,
|
|
identity_id,
|
|
session_id,
|
|
client_id,
|
|
scopes,
|
|
redirect_uri,
|
|
code_challenge,
|
|
code_challenge_method,
|
|
nonce,
|
|
state,
|
|
device_code_id,
|
|
approved,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
@id,
|
|
@identity_id,
|
|
@session_id,
|
|
@client_id,
|
|
@scopes,
|
|
@redirect_uri,
|
|
@code_challenge,
|
|
@code_challenge_method,
|
|
@nonce,
|
|
@state,
|
|
@device_code_id,
|
|
@approved,
|
|
@created_at,
|
|
@updated_at
|
|
)
|
|
`
|
|
|
|
args := pgx.StrictNamedArgs{
|
|
"id": c.ID,
|
|
"identity_id": c.IdentityID,
|
|
"session_id": c.SessionID,
|
|
"client_id": c.ClientID,
|
|
"scopes": c.Scopes,
|
|
"redirect_uri": c.RedirectURI,
|
|
"code_challenge": c.CodeChallenge,
|
|
"code_challenge_method": c.CodeChallengeMethod,
|
|
"nonce": c.Nonce,
|
|
"state": c.State,
|
|
"device_code_id": c.DeviceCodeID,
|
|
"approved": c.Approved,
|
|
"created_at": c.CreatedAt,
|
|
"updated_at": c.UpdatedAt,
|
|
}
|
|
|
|
_, err := conn.Exec(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot insert oauth2_consent: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *OAuth2Consent) Update(ctx context.Context, conn pg.Tx) error {
|
|
q := `
|
|
UPDATE iam_oauth2_consents
|
|
SET
|
|
scopes = @scopes,
|
|
approved = @approved,
|
|
updated_at = @updated_at
|
|
WHERE
|
|
id = @id
|
|
`
|
|
|
|
_, err := conn.Exec(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{
|
|
"id": c.ID,
|
|
"scopes": c.Scopes,
|
|
"approved": c.Approved,
|
|
"updated_at": c.UpdatedAt,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot update oauth2_consent: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *OAuth2Consent) Delete(ctx context.Context, conn pg.Tx) error {
|
|
q := `
|
|
DELETE FROM iam_oauth2_consents
|
|
WHERE
|
|
id = @id
|
|
`
|
|
|
|
_, err := conn.Exec(ctx, q, pgx.StrictNamedArgs{"id": c.ID})
|
|
if err != nil {
|
|
return fmt.Errorf("cannot delete oauth2_consent: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *OAuth2Consents) LoadByIdentityID(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
identityID gid.GID,
|
|
cursor *page.Cursor[OAuth2ConsentOrderField],
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
identity_id,
|
|
session_id,
|
|
client_id,
|
|
scopes,
|
|
redirect_uri,
|
|
code_challenge,
|
|
code_challenge_method,
|
|
nonce,
|
|
state,
|
|
device_code_id,
|
|
approved,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
iam_oauth2_consents
|
|
WHERE
|
|
identity_id = @identity_id
|
|
AND approved = TRUE
|
|
AND %s
|
|
`
|
|
|
|
q = fmt.Sprintf(
|
|
q,
|
|
cursor.SQLFragment(),
|
|
)
|
|
|
|
args := pgx.StrictNamedArgs{"identity_id": identityID}
|
|
maps.Copy(args, cursor.SQLArguments())
|
|
|
|
rows, err := conn.Query(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query oauth2_consents: %w", err)
|
|
}
|
|
|
|
consents, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[OAuth2Consent])
|
|
if err != nil {
|
|
return fmt.Errorf("cannot collect oauth2_consents: %w", err)
|
|
}
|
|
|
|
*c = consents
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *OAuth2Consents) CountByIdentityID(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
identityID gid.GID,
|
|
) (int, error) {
|
|
q := `
|
|
SELECT
|
|
COUNT(id)
|
|
FROM
|
|
iam_oauth2_consents
|
|
WHERE
|
|
identity_id = @identity_id
|
|
AND approved = TRUE;
|
|
`
|
|
|
|
var count int
|
|
|
|
err := conn.QueryRow(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{"identity_id": identityID},
|
|
).Scan(&count)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("cannot count oauth2_consents: %w", err)
|
|
}
|
|
|
|
return count, nil
|
|
}
|