2021-11-23 16:45:38 +07: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 13:29:55 +07:00
|
|
|
WHERE table_type = 'BASE TABLE' AND table_schema = $1;`
|
2021-11-23 16:45:38 +07:00
|
|
|
|
|
|
|
querySQLiteSelectExistingTables = `
|
|
|
|
SELECT name
|
|
|
|
FROM sqlite_master
|
|
|
|
WHERE type = 'table';`
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtSelectUserInfo = `
|
2022-03-03 18:20:43 +07: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 16:45:38 +07:00
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
|
|
|
queryFmtSelectPreferred2FAMethod = `
|
|
|
|
SELECT second_factor_method
|
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
|
|
|
queryFmtUpsertPreferred2FAMethod = `
|
|
|
|
REPLACE INTO %s (username, second_factor_method)
|
|
|
|
VALUES (?, ?);`
|
|
|
|
|
|
|
|
queryFmtPostgresUpsertPreferred2FAMethod = `
|
|
|
|
INSERT INTO %s (username, second_factor_method)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT (username)
|
|
|
|
DO UPDATE SET second_factor_method = $2;`
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-12-04 11:34:20 +07:00
|
|
|
queryFmtSelectIdentityVerification = `
|
|
|
|
SELECT id, jti, iat, issued_ip, exp, username, action, consumed, consumed_ip
|
|
|
|
FROM %s
|
|
|
|
WHERE jti = ?;`
|
2021-11-23 16:45:38 +07:00
|
|
|
|
|
|
|
queryFmtInsertIdentityVerification = `
|
2021-12-03 07:04:11 +07:00
|
|
|
INSERT INTO %s (jti, iat, issued_ip, exp, username, action)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?);`
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2021-12-03 07:04:11 +07:00
|
|
|
queryFmtConsumeIdentityVerification = `
|
2021-11-30 13:58:21 +07:00
|
|
|
UPDATE %s
|
2021-12-03 07:04:11 +07:00
|
|
|
SET consumed = CURRENT_TIMESTAMP, consumed_ip = ?
|
2021-11-30 13:58:21 +07:00
|
|
|
WHERE jti = ?;`
|
2021-11-23 16:45:38 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtSelectTOTPConfiguration = `
|
2021-12-02 08:24:10 +07:00
|
|
|
SELECT id, username, issuer, algorithm, digits, period, secret
|
2021-11-23 16:45:38 +07:00
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
queryFmtSelectTOTPConfigurations = `
|
2021-12-02 08:24:10 +07:00
|
|
|
SELECT id, username, issuer, algorithm, digits, period, secret
|
2021-11-25 08:56:58 +07:00
|
|
|
FROM %s
|
|
|
|
LIMIT ?
|
|
|
|
OFFSET ?;`
|
|
|
|
|
|
|
|
//nolint:gosec // These are not hardcoded credentials it's a query to obtain credentials.
|
|
|
|
queryFmtUpdateTOTPConfigurationSecret = `
|
|
|
|
UPDATE %s
|
|
|
|
SET secret = ?
|
|
|
|
WHERE id = ?;`
|
|
|
|
|
|
|
|
//nolint:gosec // These are not hardcoded credentials it's a query to obtain credentials.
|
|
|
|
queryFmtUpdateTOTPConfigurationSecretByUsername = `
|
|
|
|
UPDATE %s
|
|
|
|
SET secret = ?
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
queryFmtUpsertTOTPConfiguration = `
|
2022-03-03 18:20:43 +07:00
|
|
|
REPLACE INTO %s (created_at, last_used_at, username, issuer, algorithm, digits, period, secret)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`
|
2021-11-23 16:45:38 +07:00
|
|
|
|
|
|
|
queryFmtPostgresUpsertTOTPConfiguration = `
|
2022-03-03 18:20:43 +07: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 16:45:38 +07:00
|
|
|
ON CONFLICT (username)
|
2022-03-03 18:20:43 +07: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 16:45:38 +07:00
|
|
|
|
|
|
|
queryFmtDeleteTOTPConfiguration = `
|
|
|
|
DELETE FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-03 18:20:43 +07:00
|
|
|
queryFmtSelectWebauthnDevices = `
|
|
|
|
SELECT id, created_at, last_used_at, rpid, username, description, kid, public_key, attestation_type, transport, aaguid, sign_count, clone_warning
|
2021-11-23 16:45:38 +07:00
|
|
|
FROM %s
|
2021-12-03 07:04:11 +07:00
|
|
|
LIMIT ?
|
|
|
|
OFFSET ?;`
|
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
queryFmtSelectWebauthnDevicesByUsername = `
|
|
|
|
SELECT id, created_at, last_used_at, rpid, username, description, kid, public_key, attestation_type, transport, aaguid, sign_count, clone_warning
|
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
|
|
|
queryFmtUpdateWebauthnDevicePublicKey = `
|
2021-12-03 07:04:11 +07:00
|
|
|
UPDATE %s
|
|
|
|
SET public_key = ?
|
|
|
|
WHERE id = ?;`
|
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
queryFmtUpdateUpdateWebauthnDevicePublicKeyByUsername = `
|
2021-12-03 07:04:11 +07:00
|
|
|
UPDATE %s
|
|
|
|
SET public_key = ?
|
2022-03-03 18:20:43 +07:00
|
|
|
WHERE username = ? AND kid = ?;`
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
queryFmtUpdateWebauthnDeviceRecordSignIn = `
|
|
|
|
UPDATE %s
|
|
|
|
SET
|
|
|
|
rpid = ?, last_used_at = ?, sign_count = ?,
|
|
|
|
clone_warning = CASE clone_warning WHEN TRUE THEN TRUE ELSE ? END
|
|
|
|
WHERE id = ?;`
|
2021-11-23 16:45:38 +07:00
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
queryFmtUpdateWebauthnDeviceRecordSignInByUsername = `
|
|
|
|
UPDATE %s
|
|
|
|
SET
|
|
|
|
rpid = ?, last_used_at = ?, sign_count = ?,
|
|
|
|
clone_warning = CASE clone_warning WHEN TRUE THEN TRUE ELSE ? END
|
|
|
|
WHERE username = ? AND kid = ?;`
|
|
|
|
|
|
|
|
queryFmtUpsertWebauthnDevice = `
|
|
|
|
REPLACE INTO %s (created_at, last_used_at, rpid, username, description, kid, public_key, attestation_type, transport, aaguid, sign_count, clone_warning)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
|
|
|
|
|
|
|
|
queryFmtPostgresUpsertWebauthnDevice = `
|
|
|
|
INSERT INTO %s (created_at, last_used_at, rpid, username, description, kid, public_key, attestation_type, transport, aaguid, sign_count, clone_warning)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
2021-12-02 11:16:45 +07:00
|
|
|
ON CONFLICT (username, description)
|
2022-03-03 18:20:43 +07:00
|
|
|
DO UPDATE SET created_at = $1, last_used_at = $2, rpid = $3, kid = $6, public_key = $7, attestation_type = $8, transport = $9, aaguid = $10, sign_count = $11, clone_warning = $12;`
|
2021-11-23 16:45:38 +07:00
|
|
|
)
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
const (
|
|
|
|
queryFmtUpsertDuoDevice = `
|
|
|
|
REPLACE INTO %s (username, device, method)
|
|
|
|
VALUES (?, ?, ?);`
|
|
|
|
|
2021-12-02 11:16:45 +07:00
|
|
|
queryFmtPostgresUpsertDuoDevice = `
|
|
|
|
INSERT INTO %s (username, device, method)
|
|
|
|
VALUES ($1, $2, $3)
|
|
|
|
ON CONFLICT (username)
|
2022-03-03 18:20:43 +07:00
|
|
|
DO UPDATE SET device = $2, method = $3;`
|
2021-12-02 11:16:45 +07:00
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
queryFmtDeleteDuoDevice = `
|
|
|
|
DELETE
|
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
|
|
|
queryFmtSelectDuoDevice = `
|
|
|
|
SELECT id, username, device, method
|
|
|
|
FROM %s
|
|
|
|
WHERE username = ?
|
|
|
|
ORDER BY id;`
|
|
|
|
)
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
const (
|
|
|
|
queryFmtInsertAuthenticationLogEntry = `
|
2021-11-29 10:09:14 +07:00
|
|
|
INSERT INTO %s (time, successful, banned, username, auth_type, remote_ip, request_uri, request_method)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`
|
2021-11-23 16:45:38 +07:00
|
|
|
|
|
|
|
queryFmtSelect1FAAuthenticationLogEntryByUsername = `
|
|
|
|
SELECT time, successful, username
|
|
|
|
FROM %s
|
2022-01-12 21:42:41 +07:00
|
|
|
WHERE time > ? AND username = ? AND auth_type = '1FA' AND banned = FALSE
|
2021-11-23 16:45:38 +07:00
|
|
|
ORDER BY time DESC
|
|
|
|
LIMIT ?
|
|
|
|
OFFSET ?;`
|
|
|
|
)
|
2021-11-25 08:56:58 +07:00
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtSelectEncryptionValue = `
|
|
|
|
SELECT (value)
|
|
|
|
FROM %s
|
|
|
|
WHERE name = ?`
|
|
|
|
|
|
|
|
queryFmtUpsertEncryptionValue = `
|
|
|
|
REPLACE INTO %s (name, value)
|
|
|
|
VALUES (?, ?);`
|
|
|
|
|
|
|
|
queryFmtPostgresUpsertEncryptionValue = `
|
|
|
|
INSERT INTO %s (name, value)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT (name)
|
2022-03-03 18:20:43 +07:00
|
|
|
DO UPDATE SET value = $2;`
|
2021-11-25 08:56:58 +07:00
|
|
|
)
|