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>
354 lines
7.1 KiB
Go
354 lines
7.1 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"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"go.gearno.de/kit/pg"
|
|
"go.probo.inc/probo/pkg/gid"
|
|
)
|
|
|
|
type (
|
|
OAuth2RefreshToken struct {
|
|
ID gid.GID `db:"id"`
|
|
HashedValue []byte `db:"hashed_value"`
|
|
ClientID gid.GID `db:"client_id"`
|
|
IdentityID gid.GID `db:"identity_id"`
|
|
Scopes OAuth2Scopes `db:"scopes"`
|
|
AccessTokenID gid.GID `db:"access_token_id"`
|
|
CreatedAt time.Time `db:"created_at"`
|
|
ExpiresAt time.Time `db:"expires_at"`
|
|
RevokedAt *time.Time `db:"revoked_at"`
|
|
}
|
|
)
|
|
|
|
func (t *OAuth2RefreshToken) Insert(ctx context.Context, conn pg.Tx) error {
|
|
q := `
|
|
INSERT INTO iam_oauth2_refresh_tokens (
|
|
id,
|
|
hashed_value,
|
|
client_id,
|
|
identity_id,
|
|
scopes,
|
|
access_token_id,
|
|
created_at,
|
|
expires_at,
|
|
revoked_at
|
|
) VALUES (
|
|
@id,
|
|
@hashed_value,
|
|
@client_id,
|
|
@identity_id,
|
|
@scopes,
|
|
@access_token_id,
|
|
@created_at,
|
|
@expires_at,
|
|
@revoked_at
|
|
)
|
|
`
|
|
|
|
args := pgx.StrictNamedArgs{
|
|
"id": t.ID,
|
|
"hashed_value": t.HashedValue,
|
|
"client_id": t.ClientID,
|
|
"identity_id": t.IdentityID,
|
|
"scopes": t.Scopes,
|
|
"access_token_id": t.AccessTokenID,
|
|
"created_at": t.CreatedAt,
|
|
"expires_at": t.ExpiresAt,
|
|
"revoked_at": t.RevokedAt,
|
|
}
|
|
|
|
if _, err := conn.Exec(ctx, q, args); err != nil {
|
|
return fmt.Errorf("cannot insert oauth2_refresh_token: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *OAuth2RefreshToken) LoadByHashedValue(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
hashedValue []byte,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
hashed_value,
|
|
client_id,
|
|
identity_id,
|
|
scopes,
|
|
access_token_id,
|
|
created_at,
|
|
expires_at,
|
|
revoked_at
|
|
FROM
|
|
iam_oauth2_refresh_tokens
|
|
WHERE
|
|
hashed_value = @hashed_value
|
|
LIMIT 1;
|
|
`
|
|
|
|
rows, err := conn.Query(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{"hashed_value": hashedValue},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query oauth2_refresh_token: %w", err)
|
|
}
|
|
|
|
token, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[OAuth2RefreshToken])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect oauth2_refresh_token: %w", err)
|
|
}
|
|
|
|
*t = token
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *OAuth2RefreshToken) LoadByHashedValueAndClientID(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
hashedValue []byte,
|
|
clientID gid.GID,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
hashed_value,
|
|
client_id,
|
|
identity_id,
|
|
scopes,
|
|
access_token_id,
|
|
created_at,
|
|
expires_at,
|
|
revoked_at
|
|
FROM
|
|
iam_oauth2_refresh_tokens
|
|
WHERE
|
|
hashed_value = @hashed_value
|
|
AND client_id = @client_id
|
|
LIMIT 1;
|
|
`
|
|
|
|
rows, err := conn.Query(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{
|
|
"hashed_value": hashedValue,
|
|
"client_id": clientID,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query oauth2_refresh_token: %w", err)
|
|
}
|
|
|
|
token, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[OAuth2RefreshToken])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect oauth2_refresh_token: %w", err)
|
|
}
|
|
|
|
*t = token
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *OAuth2RefreshToken) LoadByHashedValueForUpdate(
|
|
ctx context.Context,
|
|
conn pg.Tx,
|
|
hashedValue []byte,
|
|
clientID gid.GID,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
hashed_value,
|
|
client_id,
|
|
identity_id,
|
|
scopes,
|
|
access_token_id,
|
|
created_at,
|
|
expires_at,
|
|
revoked_at
|
|
FROM
|
|
iam_oauth2_refresh_tokens
|
|
WHERE
|
|
hashed_value = @hashed_value
|
|
AND client_id = @client_id
|
|
FOR UPDATE;
|
|
`
|
|
|
|
rows, err := conn.Query(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{
|
|
"hashed_value": hashedValue,
|
|
"client_id": clientID,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query oauth2_refresh_token: %w", err)
|
|
}
|
|
|
|
token, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[OAuth2RefreshToken])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect oauth2_refresh_token: %w", err)
|
|
}
|
|
|
|
*t = token
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *OAuth2RefreshToken) Revoke(
|
|
ctx context.Context,
|
|
conn pg.Tx,
|
|
now time.Time,
|
|
) error {
|
|
q := `
|
|
UPDATE iam_oauth2_refresh_tokens
|
|
SET
|
|
revoked_at = @revoked_at
|
|
WHERE
|
|
id = @id
|
|
`
|
|
|
|
args := pgx.StrictNamedArgs{
|
|
"id": t.ID,
|
|
"revoked_at": now,
|
|
}
|
|
|
|
if _, err := conn.Exec(ctx, q, args); err != nil {
|
|
return fmt.Errorf("cannot revoke oauth2_refresh_token: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *OAuth2RefreshToken) RevokeByClientAndIdentity(
|
|
ctx context.Context,
|
|
conn pg.Tx,
|
|
clientID gid.GID,
|
|
identityID gid.GID,
|
|
now time.Time,
|
|
) (int64, error) {
|
|
q := `
|
|
UPDATE iam_oauth2_refresh_tokens
|
|
SET
|
|
revoked_at = @revoked_at
|
|
WHERE
|
|
client_id = @client_id
|
|
AND identity_id = @identity_id
|
|
AND revoked_at IS NULL
|
|
`
|
|
|
|
result, err := conn.Exec(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{
|
|
"client_id": clientID,
|
|
"identity_id": identityID,
|
|
"revoked_at": now,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("cannot revoke oauth2_refresh_tokens by client and identity: %w", err)
|
|
}
|
|
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
func (t *OAuth2RefreshToken) RevokeByAccessTokenID(
|
|
ctx context.Context,
|
|
conn pg.Tx,
|
|
accessTokenID gid.GID,
|
|
now time.Time,
|
|
) (int64, error) {
|
|
q := `
|
|
UPDATE iam_oauth2_refresh_tokens
|
|
SET
|
|
revoked_at = @revoked_at
|
|
WHERE
|
|
access_token_id = @access_token_id
|
|
AND revoked_at IS NULL
|
|
`
|
|
|
|
result, err := conn.Exec(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{
|
|
"access_token_id": accessTokenID,
|
|
"revoked_at": now,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("cannot revoke oauth2_refresh_tokens by access_token_id: %w", err)
|
|
}
|
|
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
func (t *OAuth2RefreshToken) DeleteExpired(
|
|
ctx context.Context,
|
|
conn pg.Tx,
|
|
now time.Time,
|
|
) (int64, error) {
|
|
q := `
|
|
DELETE FROM iam_oauth2_refresh_tokens
|
|
WHERE
|
|
expires_at < @now
|
|
OR (revoked_at IS NOT NULL AND revoked_at < @revoked_cutoff)
|
|
`
|
|
|
|
result, err := conn.Exec(
|
|
ctx,
|
|
q,
|
|
pgx.StrictNamedArgs{
|
|
"now": now,
|
|
"revoked_cutoff": now.Add(-7 * 24 * time.Hour),
|
|
},
|
|
)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("cannot delete expired oauth2_refresh_tokens: %w", err)
|
|
}
|
|
|
|
return result.RowsAffected(), nil
|
|
}
|