2021-11-23 09:45:38 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtSelectMigrations = `
|
|
|
|
SELECT id, applied, version_before, version_after, application_version
|
|
|
|
FROM %s;`
|
|
|
|
|
|
|
|
queryFmtSelectLatestMigration = `
|
|
|
|
SELECT id, applied, version_before, version_after, application_version
|
|
|
|
FROM %s
|
|
|
|
ORDER BY id DESC
|
|
|
|
LIMIT 1;`
|
|
|
|
|
|
|
|
queryFmtInsertMigration = `
|
|
|
|
INSERT INTO %s (applied, version_before, version_after, application_version)
|
|
|
|
VALUES (?, ?, ?, ?);`
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
queryMySQLSelectExistingTables = `
|
|
|
|
SELECT table_name
|
|
|
|
FROM information_schema.tables
|
|
|
|
WHERE table_type = 'BASE TABLE' AND table_schema = database();`
|
|
|
|
|
|
|
|
queryPostgreSelectExistingTables = `
|
|
|
|
SELECT table_name
|
|
|
|
FROM information_schema.tables
|
2021-12-03 06:29:55 +00:00
|
|
|
WHERE table_type = 'BASE TABLE' AND table_schema = $1;`
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
querySQLiteSelectExistingTables = `
|
|
|
|
SELECT name
|
|
|
|
FROM sqlite_master
|
|
|
|
WHERE type = 'table';`
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtSelectUserInfo = `
|
2022-03-03 11:20:43 +00:00
|
|
|
SELECT second_factor_method, (SELECT EXISTS (SELECT id FROM %s WHERE username = ?)) AS has_totp, (SELECT EXISTS (SELECT id FROM %s WHERE username = ?)) AS has_webauthn, (SELECT EXISTS (SELECT id FROM %s WHERE username = ?)) AS has_duo
|
2021-11-23 09:45:38 +00:00
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
|
|
|
queryFmtSelectPreferred2FAMethod = `
|
|
|
|
SELECT second_factor_method
|
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
|
|
|
queryFmtUpsertPreferred2FAMethod = `
|
|
|
|
REPLACE INTO %s (username, second_factor_method)
|
|
|
|
VALUES (?, ?);`
|
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
queryFmtUpsertPreferred2FAMethodPostgreSQL = `
|
2021-11-23 09:45:38 +00:00
|
|
|
INSERT INTO %s (username, second_factor_method)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT (username)
|
|
|
|
DO UPDATE SET second_factor_method = $2;`
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-12-04 04:34:20 +00:00
|
|
|
queryFmtSelectIdentityVerification = `
|
|
|
|
SELECT id, jti, iat, issued_ip, exp, username, action, consumed, consumed_ip
|
|
|
|
FROM %s
|
|
|
|
WHERE jti = ?;`
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
queryFmtInsertIdentityVerification = `
|
2021-12-03 00:04:11 +00:00
|
|
|
INSERT INTO %s (jti, iat, issued_ip, exp, username, action)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?);`
|
2021-11-23 09:45:38 +00:00
|
|
|
|
2021-12-03 00:04:11 +00:00
|
|
|
queryFmtConsumeIdentityVerification = `
|
2021-11-30 06:58:21 +00:00
|
|
|
UPDATE %s
|
2021-12-03 00:04:11 +00:00
|
|
|
SET consumed = CURRENT_TIMESTAMP, consumed_ip = ?
|
2021-11-30 06:58:21 +00:00
|
|
|
WHERE jti = ?;`
|
2021-11-23 09:45:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtSelectTOTPConfiguration = `
|
2021-12-02 01:24:10 +00:00
|
|
|
SELECT id, username, issuer, algorithm, digits, period, secret
|
2021-11-23 09:45:38 +00:00
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
2021-11-25 01:56:58 +00:00
|
|
|
queryFmtSelectTOTPConfigurations = `
|
2021-12-02 01:24:10 +00:00
|
|
|
SELECT id, username, issuer, algorithm, digits, period, secret
|
2021-11-25 01:56:58 +00:00
|
|
|
FROM %s
|
|
|
|
LIMIT ?
|
|
|
|
OFFSET ?;`
|
|
|
|
|
2022-11-25 12:44:55 +00:00
|
|
|
queryFmtSelectTOTPConfigurationsEncryptedData = `
|
|
|
|
SELECT id, secret
|
|
|
|
FROM %s;`
|
|
|
|
|
2021-11-25 01:56:58 +00:00
|
|
|
//nolint:gosec // These are not hardcoded credentials it's a query to obtain credentials.
|
|
|
|
queryFmtUpdateTOTPConfigurationSecret = `
|
|
|
|
UPDATE %s
|
|
|
|
SET secret = ?
|
|
|
|
WHERE id = ?;`
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
queryFmtUpsertTOTPConfiguration = `
|
2022-03-03 11:20:43 +00:00
|
|
|
REPLACE INTO %s (created_at, last_used_at, username, issuer, algorithm, digits, period, secret)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`
|
2021-11-23 09:45:38 +00:00
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
queryFmtUpsertTOTPConfigurationPostgreSQL = `
|
2022-03-03 11:20:43 +00:00
|
|
|
INSERT INTO %s (created_at, last_used_at, username, issuer, algorithm, digits, period, secret)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
2021-11-23 09:45:38 +00:00
|
|
|
ON CONFLICT (username)
|
2022-03-03 11:20:43 +00:00
|
|
|
DO UPDATE SET created_at = $1, last_used_at = $2, issuer = $4, algorithm = $5, digits = $6, period = $7, secret = $8;`
|
|
|
|
|
|
|
|
queryFmtUpdateTOTPConfigRecordSignIn = `
|
|
|
|
UPDATE %s
|
|
|
|
SET last_used_at = ?
|
|
|
|
WHERE id = ?;`
|
|
|
|
|
|
|
|
queryFmtUpdateTOTPConfigRecordSignInByUsername = `
|
|
|
|
UPDATE %s
|
|
|
|
SET last_used_at = ?
|
|
|
|
WHERE username = ?;`
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
queryFmtDeleteTOTPConfiguration = `
|
|
|
|
DELETE FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-03 11:20:43 +00:00
|
|
|
queryFmtSelectWebauthnDevices = `
|
2023-02-16 19:40:40 +00:00
|
|
|
SELECT id, created_at, last_used_at, rpid, username, description, kid, aaguid, attestation_type, attachment, transport, sign_count, clone_warning, discoverable, present, verified, backup_eligible, backup_state, public_key
|
2021-11-23 09:45:38 +00:00
|
|
|
FROM %s
|
2021-12-03 00:04:11 +00:00
|
|
|
LIMIT ?
|
|
|
|
OFFSET ?;`
|
|
|
|
|
2022-03-03 11:20:43 +00:00
|
|
|
queryFmtSelectWebauthnDevicesByUsername = `
|
2023-02-16 19:40:40 +00:00
|
|
|
SELECT id, created_at, last_used_at, rpid, username, description, kid, aaguid, attestation_type, attachment, transport, sign_count, clone_warning, discoverable, present, verified, backup_eligible, backup_state, public_key
|
2022-03-03 11:20:43 +00:00
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
2023-02-11 15:47:03 +00:00
|
|
|
queryFmtSelectWebauthnDevicesByRPIDByUsername = `
|
2023-02-16 19:40:40 +00:00
|
|
|
SELECT id, created_at, last_used_at, rpid, username, description, kid, aaguid, attestation_type, attachment, transport, sign_count, clone_warning, discoverable, present, verified, backup_eligible, backup_state, public_key
|
2023-02-11 15:47:03 +00:00
|
|
|
FROM %s
|
|
|
|
WHERE rpid = ? AND username = ?;`
|
|
|
|
|
2022-12-31 07:27:43 +00:00
|
|
|
queryFmtSelectWebauthnDeviceByID = `
|
2023-02-16 19:40:40 +00:00
|
|
|
SELECT id, created_at, last_used_at, rpid, username, description, kid, aaguid, attestation_type, attachment, transport, sign_count, clone_warning, discoverable, present, verified, backup_eligible, backup_state, public_key
|
2022-12-31 07:27:43 +00:00
|
|
|
FROM %s
|
|
|
|
WHERE id = ?;`
|
|
|
|
|
2023-02-16 19:40:40 +00:00
|
|
|
queryFmtUpdateUpdateWebauthnDeviceDescriptionByUsernameAndID = `
|
2022-11-19 05:48:47 +00:00
|
|
|
UPDATE %s
|
2023-02-16 19:40:40 +00:00
|
|
|
SET description = ?
|
2022-11-19 05:48:47 +00:00
|
|
|
WHERE username = ? AND id = ?;`
|
|
|
|
|
2022-03-03 11:20:43 +00:00
|
|
|
queryFmtUpdateWebauthnDeviceRecordSignIn = `
|
|
|
|
UPDATE %s
|
2022-10-19 07:17:55 +00:00
|
|
|
SET
|
2023-02-14 02:53:57 +00:00
|
|
|
rpid = ?, last_used_at = ?, sign_count = ?, discoverable = ?, present = ?, verified = ?, backup_eligible = ?, backup_state = ?,
|
2022-03-03 11:20:43 +00:00
|
|
|
clone_warning = CASE clone_warning WHEN TRUE THEN TRUE ELSE ? END
|
|
|
|
WHERE id = ?;`
|
2021-11-23 09:45:38 +00:00
|
|
|
|
2023-02-16 19:40:40 +00:00
|
|
|
queryFmtInsertWebauthnDevice = `
|
|
|
|
INSERT INTO %s (created_at, last_used_at, rpid, username, description, kid, aaguid, attestation_type, attachment, transport, sign_count, clone_warning, discoverable, present, verified, backup_eligible, backup_state, public_key)
|
2023-02-14 02:53:57 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
|
2022-10-19 07:17:55 +00:00
|
|
|
|
|
|
|
queryFmtDeleteWebauthnDevice = `
|
|
|
|
DELETE FROM %s
|
|
|
|
WHERE kid = ?;`
|
|
|
|
|
|
|
|
queryFmtDeleteWebauthnDeviceByUsername = `
|
|
|
|
DELETE FROM %s
|
|
|
|
WHERE username = ?;`
|
2022-11-19 05:48:47 +00:00
|
|
|
|
2023-02-16 19:40:40 +00:00
|
|
|
queryFmtDeleteWebauthnDeviceByUsernameAndDescription = `
|
2022-10-19 07:17:55 +00:00
|
|
|
DELETE FROM %s
|
2023-02-16 19:40:40 +00:00
|
|
|
WHERE username = ? AND description = ?;`
|
2023-02-14 02:53:57 +00:00
|
|
|
|
|
|
|
queryFmtSelectWebauthnDevicesEncryptedData = `
|
|
|
|
SELECT id, public_key
|
|
|
|
FROM %s;`
|
|
|
|
|
|
|
|
queryFmtUpdateWebauthnDevicesEncryptedData = `
|
|
|
|
UPDATE %s
|
|
|
|
SET public_key = ?
|
|
|
|
WHERE id = ?;`
|
2021-11-23 09:45:38 +00:00
|
|
|
)
|
|
|
|
|
2023-02-16 19:40:40 +00:00
|
|
|
const (
|
|
|
|
queryFmtInsertWebauthnUser = `
|
|
|
|
INSERT INTO %s (rpid, username, userid)
|
|
|
|
VALUES (?, ?, ?);`
|
|
|
|
|
|
|
|
queryFmtSelectWebauthnUser = `
|
|
|
|
SELECT id, rpid, username, userid
|
|
|
|
FROM %s
|
|
|
|
WHERE rpid = ? AND username = ?;`
|
|
|
|
)
|
|
|
|
|
2021-12-01 03:32:58 +00:00
|
|
|
const (
|
|
|
|
queryFmtUpsertDuoDevice = `
|
|
|
|
REPLACE INTO %s (username, device, method)
|
|
|
|
VALUES (?, ?, ?);`
|
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
queryFmtUpsertDuoDevicePostgreSQL = `
|
2021-12-02 04:16:45 +00:00
|
|
|
INSERT INTO %s (username, device, method)
|
|
|
|
VALUES ($1, $2, $3)
|
|
|
|
ON CONFLICT (username)
|
2022-03-03 11:20:43 +00:00
|
|
|
DO UPDATE SET device = $2, method = $3;`
|
2021-12-02 04:16:45 +00:00
|
|
|
|
2021-12-01 03:32:58 +00:00
|
|
|
queryFmtDeleteDuoDevice = `
|
|
|
|
DELETE
|
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
|
|
|
queryFmtSelectDuoDevice = `
|
|
|
|
SELECT id, username, device, method
|
|
|
|
FROM %s
|
|
|
|
WHERE username = ?
|
|
|
|
ORDER BY id;`
|
|
|
|
)
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
const (
|
|
|
|
queryFmtInsertAuthenticationLogEntry = `
|
2021-11-29 03:09:14 +00:00
|
|
|
INSERT INTO %s (time, successful, banned, username, auth_type, remote_ip, request_uri, request_method)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
queryFmtSelect1FAAuthenticationLogEntryByUsername = `
|
|
|
|
SELECT time, successful, username
|
|
|
|
FROM %s
|
2022-01-12 14:42:41 +00:00
|
|
|
WHERE time > ? AND username = ? AND auth_type = '1FA' AND banned = FALSE
|
2021-11-23 09:45:38 +00:00
|
|
|
ORDER BY time DESC
|
|
|
|
LIMIT ?
|
|
|
|
OFFSET ?;`
|
|
|
|
)
|
2021-11-25 01:56:58 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtSelectEncryptionValue = `
|
|
|
|
SELECT (value)
|
|
|
|
FROM %s
|
|
|
|
WHERE name = ?`
|
|
|
|
|
|
|
|
queryFmtUpsertEncryptionValue = `
|
|
|
|
REPLACE INTO %s (name, value)
|
|
|
|
VALUES (?, ?);`
|
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
queryFmtUpsertEncryptionValuePostgreSQL = `
|
2021-11-25 01:56:58 +00:00
|
|
|
INSERT INTO %s (name, value)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT (name)
|
2022-03-03 11:20:43 +00:00
|
|
|
DO UPDATE SET value = $2;`
|
2021-11-25 01:56:58 +00:00
|
|
|
)
|
2022-04-07 05:33:53 +00:00
|
|
|
|
|
|
|
const (
|
2022-10-20 02:16:36 +00:00
|
|
|
queryFmtSelectOAuth2ConsentPreConfigurations = `
|
|
|
|
SELECT id, client_id, subject, created_at, expires_at, revoked, scopes, audience
|
2022-04-07 05:33:53 +00:00
|
|
|
FROM %s
|
2022-10-20 02:16:36 +00:00
|
|
|
WHERE client_id = ? AND subject = ? AND
|
|
|
|
revoked = FALSE AND (expires_at IS NULL OR expires_at >= CURRENT_TIMESTAMP);`
|
|
|
|
|
|
|
|
queryFmtInsertOAuth2ConsentPreConfiguration = `
|
|
|
|
INSERT INTO %s (client_id, subject, created_at, expires_at, revoked, scopes, audience)
|
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?);`
|
2022-04-07 05:33:53 +00:00
|
|
|
|
2022-10-20 02:16:36 +00:00
|
|
|
queryFmtInsertOAuth2ConsentPreConfigurationPostgreSQL = `
|
|
|
|
INSERT INTO %s (client_id, subject, created_at, expires_at, revoked, scopes, audience)
|
|
|
|
VALUES($1, $2, $3, $4, $5, $6, $7)
|
|
|
|
RETURNING id;`
|
|
|
|
|
|
|
|
queryFmtSelectOAuth2ConsentSessionByChallengeID = `
|
|
|
|
SELECT id, challenge_id, client_id, subject, authorized, granted, requested_at, responded_at,
|
|
|
|
form_data, requested_scopes, granted_scopes, requested_audience, granted_audience, preconfiguration
|
2022-04-07 05:33:53 +00:00
|
|
|
FROM %s
|
2022-10-20 02:16:36 +00:00
|
|
|
WHERE challenge_id = ?;`
|
2022-04-07 05:33:53 +00:00
|
|
|
|
|
|
|
queryFmtInsertOAuth2ConsentSession = `
|
2022-10-20 02:16:36 +00:00
|
|
|
INSERT INTO %s (challenge_id, client_id, subject, authorized, granted, requested_at, responded_at,
|
|
|
|
form_data, requested_scopes, granted_scopes, requested_audience, granted_audience, preconfiguration)
|
2022-04-07 05:33:53 +00:00
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
|
|
|
|
|
2022-04-25 00:31:05 +00:00
|
|
|
queryFmtUpdateOAuth2ConsentSessionSubject = `
|
|
|
|
UPDATE %s
|
|
|
|
SET subject = ?
|
|
|
|
WHERE id = ?;`
|
|
|
|
|
2022-11-25 12:44:55 +00:00
|
|
|
queryFmtUpdateOAuth2ConsentSessionSessionData = `
|
|
|
|
UPDATE %s
|
|
|
|
SET session_data = ?
|
|
|
|
WHERE id = ?;`
|
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
queryFmtUpdateOAuth2ConsentSessionResponse = `
|
|
|
|
UPDATE %s
|
2022-10-20 02:16:36 +00:00
|
|
|
SET authorized = ?, responded_at = CURRENT_TIMESTAMP, granted_scopes = ?, granted_audience = ?, preconfiguration = ?
|
2022-04-07 05:33:53 +00:00
|
|
|
WHERE id = ? AND responded_at IS NULL;`
|
|
|
|
|
|
|
|
queryFmtUpdateOAuth2ConsentSessionGranted = `
|
|
|
|
UPDATE %s
|
|
|
|
SET granted = TRUE
|
|
|
|
WHERE id = ? AND responded_at IS NOT NULL;`
|
|
|
|
|
|
|
|
queryFmtSelectOAuth2Session = `
|
|
|
|
SELECT id, challenge_id, request_id, client_id, signature, subject, requested_at,
|
|
|
|
requested_scopes, granted_scopes, requested_audience, granted_audience,
|
|
|
|
active, revoked, form_data, session_data
|
|
|
|
FROM %s
|
|
|
|
WHERE signature = ? AND revoked = FALSE;`
|
|
|
|
|
2022-11-25 12:44:55 +00:00
|
|
|
queryFmtSelectOAuth2SessionEncryptedData = `
|
|
|
|
SELECT id, session_data
|
|
|
|
FROM %s;`
|
|
|
|
|
2022-04-07 05:33:53 +00:00
|
|
|
queryFmtInsertOAuth2Session = `
|
2022-10-19 07:17:55 +00:00
|
|
|
INSERT INTO %s (challenge_id, request_id, client_id, signature, subject, requested_at,
|
|
|
|
requested_scopes, granted_scopes, requested_audience, granted_audience,
|
2022-04-07 05:33:53 +00:00
|
|
|
active, revoked, form_data, session_data)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
|
|
|
|
|
|
|
|
queryFmtRevokeOAuth2Session = `
|
|
|
|
UPDATE %s
|
|
|
|
SET revoked = TRUE
|
|
|
|
WHERE signature = ?;`
|
|
|
|
|
|
|
|
queryFmtRevokeOAuth2SessionByRequestID = `
|
|
|
|
UPDATE %s
|
|
|
|
SET revoked = TRUE
|
|
|
|
WHERE request_id = ?;`
|
|
|
|
|
|
|
|
queryFmtDeactivateOAuth2Session = `
|
|
|
|
UPDATE %s
|
|
|
|
SET active = FALSE
|
|
|
|
WHERE signature = ?;`
|
|
|
|
|
|
|
|
queryFmtDeactivateOAuth2SessionByRequestID = `
|
|
|
|
UPDATE %s
|
|
|
|
SET active = FALSE
|
2022-06-17 10:14:33 +00:00
|
|
|
WHERE request_id = ?;`
|
2022-04-07 05:33:53 +00:00
|
|
|
|
|
|
|
queryFmtSelectOAuth2BlacklistedJTI = `
|
|
|
|
SELECT id, signature, expires_at
|
|
|
|
FROM %s
|
|
|
|
WHERE signature = ?;`
|
|
|
|
|
|
|
|
queryFmtUpsertOAuth2BlacklistedJTI = `
|
|
|
|
REPLACE INTO %s (signature, expires_at)
|
|
|
|
VALUES(?, ?);`
|
|
|
|
|
|
|
|
queryFmtUpsertOAuth2BlacklistedJTIPostgreSQL = `
|
|
|
|
INSERT INTO %s (signature, expires_at)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT (signature)
|
|
|
|
DO UPDATE SET expires_at = $2;`
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtInsertUserOpaqueIdentifier = `
|
|
|
|
INSERT INTO %s (service, sector_id, username, identifier)
|
|
|
|
VALUES(?, ?, ?, ?);`
|
|
|
|
|
|
|
|
queryFmtSelectUserOpaqueIdentifier = `
|
2022-04-09 07:13:19 +00:00
|
|
|
SELECT id, service, sector_id, username, identifier
|
2022-04-07 05:33:53 +00:00
|
|
|
FROM %s
|
|
|
|
WHERE identifier = ?;`
|
|
|
|
|
|
|
|
queryFmtSelectUserOpaqueIdentifierBySignature = `
|
|
|
|
SELECT id, service, sector_id, username, identifier
|
|
|
|
FROM %s
|
|
|
|
WHERE service = ? AND sector_id = ? AND username = ?;`
|
2022-04-09 07:13:19 +00:00
|
|
|
|
|
|
|
queryFmtSelectUserOpaqueIdentifiers = `
|
|
|
|
SELECT id, service, sector_id, username, identifier
|
|
|
|
FROM %s;`
|
2022-04-07 05:33:53 +00:00
|
|
|
)
|