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
|
|
|
|
WHERE table_type = 'BASE TABLE' AND table_schema = 'public';`
|
|
|
|
|
|
|
|
querySQLiteSelectExistingTables = `
|
|
|
|
SELECT name
|
|
|
|
FROM sqlite_master
|
|
|
|
WHERE type = 'table';`
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtSelectUserInfo = `
|
2021-12-01 10:32:58 +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_u2f, (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 (
|
|
|
|
queryFmtSelectExistsIdentityVerification = `
|
|
|
|
SELECT EXISTS (
|
|
|
|
SELECT id
|
|
|
|
FROM %s
|
2021-11-30 13:58:21 +07:00
|
|
|
WHERE jti = ? AND exp > CURRENT_TIMESTAMP AND used IS NULL
|
2021-11-23 16:45:38 +07:00
|
|
|
);`
|
|
|
|
|
|
|
|
queryFmtInsertIdentityVerification = `
|
2021-11-30 13:58:21 +07:00
|
|
|
INSERT INTO %s (jti, iat, exp, username, action)
|
|
|
|
VALUES (?, ?, ?, ?, ?);`
|
2021-11-23 16:45:38 +07:00
|
|
|
|
|
|
|
queryFmtDeleteIdentityVerification = `
|
2021-11-30 13:58:21 +07:00
|
|
|
UPDATE %s
|
|
|
|
SET used = CURRENT_TIMESTAMP
|
|
|
|
WHERE jti = ?;`
|
2021-11-23 16:45:38 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtSelectTOTPConfiguration = `
|
|
|
|
SELECT id, username, algorithm, digits, totp_period, secret
|
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
2021-11-25 08:56:58 +07:00
|
|
|
queryFmtSelectTOTPConfigurations = `
|
|
|
|
SELECT id, username, algorithm, digits, totp_period, secret
|
|
|
|
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 = `
|
|
|
|
REPLACE INTO %s (username, algorithm, digits, totp_period, secret)
|
|
|
|
VALUES (?, ?, ?, ?, ?);`
|
|
|
|
|
|
|
|
queryFmtPostgresUpsertTOTPConfiguration = `
|
|
|
|
INSERT INTO %s (username, algorithm, digits, totp_period, secret)
|
|
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
|
|
ON CONFLICT (username)
|
|
|
|
DO UPDATE SET algorithm = $2, digits = $3, totp_period = $4, secret = $5;`
|
|
|
|
|
|
|
|
queryFmtDeleteTOTPConfiguration = `
|
|
|
|
DELETE FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
queryFmtSelectU2FDevice = `
|
|
|
|
SELECT key_handle, public_key
|
|
|
|
FROM %s
|
|
|
|
WHERE username = ?;`
|
|
|
|
|
|
|
|
queryFmtUpsertU2FDevice = `
|
|
|
|
REPLACE INTO %s (username, key_handle, public_key)
|
|
|
|
VALUES (?, ?, ?);`
|
|
|
|
|
|
|
|
queryFmtPostgresUpsertU2FDevice = `
|
|
|
|
INSERT INTO %s (username, key_handle, public_key)
|
|
|
|
VALUES ($1, $2, $3)
|
|
|
|
ON CONFLICT (username)
|
|
|
|
DO UPDATE SET key_handle=$2, public_key=$3;`
|
|
|
|
)
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
const (
|
|
|
|
queryFmtUpsertDuoDevice = `
|
|
|
|
REPLACE INTO %s (username, device, method)
|
|
|
|
VALUES (?, ?, ?);`
|
|
|
|
|
|
|
|
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
|
2021-11-29 10:09:14 +07:00
|
|
|
WHERE time > ? AND username = ? AND auth_type = '1FA' AND banned = 0
|
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)
|
|
|
|
DO UPDATE SET value=$2;`
|
|
|
|
)
|