2023-09-29 14:13:20 -05:00
|
|
|
-- name: GetExternalAuthLink :one
|
|
|
|
|
SELECT * FROM external_auth_links WHERE provider_id = $1 AND user_id = $2;
|
2022-10-24 19:46:24 -05:00
|
|
|
|
2023-12-05 14:03:44 -06:00
|
|
|
-- name: DeleteExternalAuthLink :exec
|
|
|
|
|
DELETE FROM external_auth_links WHERE provider_id = $1 AND user_id = $2;
|
|
|
|
|
|
2023-09-29 14:13:20 -05:00
|
|
|
-- name: GetExternalAuthLinksByUserID :many
|
|
|
|
|
SELECT * FROM external_auth_links WHERE user_id = $1;
|
2023-09-06 12:06:26 +01:00
|
|
|
|
2023-09-29 14:13:20 -05:00
|
|
|
-- name: InsertExternalAuthLink :one
|
|
|
|
|
INSERT INTO external_auth_links (
|
2022-10-24 19:46:24 -05:00
|
|
|
provider_id,
|
|
|
|
|
user_id,
|
|
|
|
|
created_at,
|
|
|
|
|
updated_at,
|
|
|
|
|
oauth_access_token,
|
2023-09-06 12:06:26 +01:00
|
|
|
oauth_access_token_key_id,
|
2022-10-24 19:46:24 -05:00
|
|
|
oauth_refresh_token,
|
2023-09-06 12:06:26 +01:00
|
|
|
oauth_refresh_token_key_id,
|
2023-10-09 18:49:30 -05:00
|
|
|
oauth_expiry,
|
|
|
|
|
oauth_extra
|
2022-10-24 19:46:24 -05:00
|
|
|
) VALUES (
|
|
|
|
|
$1,
|
|
|
|
|
$2,
|
|
|
|
|
$3,
|
|
|
|
|
$4,
|
|
|
|
|
$5,
|
|
|
|
|
$6,
|
2023-09-06 12:06:26 +01:00
|
|
|
$7,
|
|
|
|
|
$8,
|
2023-10-09 18:49:30 -05:00
|
|
|
$9,
|
|
|
|
|
$10
|
2022-10-24 19:46:24 -05:00
|
|
|
) RETURNING *;
|
|
|
|
|
|
2023-09-29 14:13:20 -05:00
|
|
|
-- name: UpdateExternalAuthLink :one
|
|
|
|
|
UPDATE external_auth_links SET
|
2022-10-24 19:46:24 -05:00
|
|
|
updated_at = $3,
|
|
|
|
|
oauth_access_token = $4,
|
2023-09-06 12:06:26 +01:00
|
|
|
oauth_access_token_key_id = $5,
|
|
|
|
|
oauth_refresh_token = $6,
|
|
|
|
|
oauth_refresh_token_key_id = $7,
|
2023-10-09 18:49:30 -05:00
|
|
|
oauth_expiry = $8,
|
2025-08-14 09:50:31 -05:00
|
|
|
oauth_extra = $9,
|
|
|
|
|
-- Only 'UpdateExternalAuthLinkRefreshToken' supports updating the oauth_refresh_failure_reason.
|
|
|
|
|
-- Any updates to the external auth link, will be assumed to change the state and clear
|
|
|
|
|
-- any cached errors.
|
|
|
|
|
oauth_refresh_failure_reason = ''
|
2023-02-27 10:18:19 -06:00
|
|
|
WHERE provider_id = $1 AND user_id = $2 RETURNING *;
|
2024-11-20 20:13:07 -06:00
|
|
|
|
2024-12-03 19:26:31 +00:00
|
|
|
-- name: UpdateExternalAuthLinkRefreshToken :exec
|
2026-03-10 10:52:55 -07:00
|
|
|
-- Optimistic lock: only update the row if the refresh token in the database
|
|
|
|
|
-- still matches the one we read before attempting the refresh. This prevents
|
|
|
|
|
-- a concurrent caller that lost a token-refresh race from overwriting a valid
|
|
|
|
|
-- token stored by the winner.
|
2024-11-20 20:13:07 -06:00
|
|
|
UPDATE
|
|
|
|
|
external_auth_links
|
|
|
|
|
SET
|
2025-08-14 09:50:31 -05:00
|
|
|
-- oauth_refresh_failure_reason can be set to cache the failure reason
|
|
|
|
|
-- for subsequent refresh attempts.
|
|
|
|
|
oauth_refresh_failure_reason = @oauth_refresh_failure_reason,
|
2024-12-03 19:26:31 +00:00
|
|
|
oauth_refresh_token = @oauth_refresh_token,
|
2024-11-20 20:13:07 -06:00
|
|
|
updated_at = @updated_at
|
2024-12-03 19:26:31 +00:00
|
|
|
WHERE
|
|
|
|
|
provider_id = @provider_id
|
|
|
|
|
AND
|
|
|
|
|
user_id = @user_id
|
2026-03-10 10:52:55 -07:00
|
|
|
AND
|
|
|
|
|
oauth_refresh_token = @old_oauth_refresh_token
|
2024-12-03 19:26:31 +00:00
|
|
|
AND
|
|
|
|
|
-- Required for sqlc to generate a parameter for the oauth_refresh_token_key_id
|
|
|
|
|
@oauth_refresh_token_key_id :: text = @oauth_refresh_token_key_id :: text;
|