mirror of
https://github.com/coder/coder.git
synced 2026-10-31 02:43:21 +08:00
Before refreshing, the caller has to put a lease on the row to prevent any other replicas from trying to refresh using the same token. Callers unable to get a lease enter a holding pattern waiting for the results. Also use this same lease query to re-read the link. This more or less functions the same as the re-read we used to have after a refresh failure, except it also allows us to avoid making the failed request in the first place. The downside is that we have to refetch the link every time even if the caller just fetched it, but since we have to make a database call to set the lease anyway...
67 lines
1.7 KiB
SQL
67 lines
1.7 KiB
SQL
-- name: GetExternalAuthLink :one
|
|
SELECT * FROM external_auth_links WHERE provider_id = $1 AND user_id = $2;
|
|
|
|
-- name: DeleteExternalAuthLink :exec
|
|
DELETE FROM external_auth_links WHERE provider_id = $1 AND user_id = $2;
|
|
|
|
-- name: GetExternalAuthLinksByUserID :many
|
|
SELECT * FROM external_auth_links WHERE user_id = $1;
|
|
|
|
-- name: InsertExternalAuthLink :one
|
|
INSERT INTO external_auth_links (
|
|
provider_id,
|
|
user_id,
|
|
created_at,
|
|
updated_at,
|
|
oauth_access_token,
|
|
oauth_access_token_key_id,
|
|
oauth_refresh_token,
|
|
oauth_refresh_token_key_id,
|
|
oauth_expiry,
|
|
oauth_extra
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9,
|
|
$10
|
|
) RETURNING *;
|
|
|
|
-- name: UpdateExternalAuthLink :one
|
|
-- If a refresh lease is provided, the row is only updated if the lease matches.
|
|
UPDATE external_auth_links SET
|
|
updated_at = $4,
|
|
oauth_access_token = $5,
|
|
oauth_access_token_key_id = $6,
|
|
oauth_refresh_token = $7,
|
|
oauth_refresh_token_key_id = $8,
|
|
oauth_expiry = $9,
|
|
oauth_extra = $10,
|
|
oauth_refresh_failure_reason = $11
|
|
WHERE
|
|
provider_id = $1
|
|
AND user_id = $2
|
|
AND (refresh_lease_expires_at = $3 OR $3 IS NULL)
|
|
RETURNING *;
|
|
|
|
-- name: AcquireExternalAuthLinkRefreshLease :one
|
|
-- Set the lease to expire according to the provided timeout. If there is
|
|
-- already a lease, an exception is raised.
|
|
SELECT * from acquire_external_auth_link_refresh_lease(@provider_id, @user_id, @timeout_ms);
|
|
|
|
-- name: ReleaseExternalAuthLinkRefreshLease :exec
|
|
-- The lease is only removed if it is the current lease.
|
|
UPDATE
|
|
external_auth_links
|
|
SET
|
|
refresh_lease_expires_at = NULL
|
|
WHERE
|
|
provider_id = @provider_id
|
|
AND user_id = @user_id
|
|
AND refresh_lease_expires_at = @refresh_lease_expires_at;
|