Skip to content

Commit 88d9279

Browse files
authored
Merge pull request #467 from yetanalytics/csv-export-auth
[SQL-283] Authentication for CSV export
2 parents 15a392a + 2fdb8bd commit 88d9279

File tree

27 files changed

+418
-53
lines changed

27 files changed

+418
-53
lines changed

src/db/postgres/lrsql/postgres/record.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080
(add-statement-to-actor-cascading-delete! tx))
8181
(when (some? (query-varchar-exists tx))
8282
(convert-varchars-to-text! tx))
83-
(create-blocked-jwt-table! tx))
83+
(create-blocked-jwt-table! tx)
84+
(alter-blocked-jwt-add-one-time-id! tx))
8485

8586
bp/BackendUtil
8687
(-txn-retry? [_ ex]
@@ -210,10 +211,16 @@
210211
bp/JWTBlocklistBackend
211212
(-insert-blocked-jwt! [_ tx input]
212213
(insert-blocked-jwt! tx input))
214+
(-insert-one-time-jwt! [_ tx input]
215+
(insert-one-time-jwt! tx input))
216+
(-update-one-time-jwt! [_ tx input]
217+
(update-one-time-jwt! tx input))
213218
(-delete-blocked-jwt-by-time! [_ tx input]
214219
(delete-blocked-jwt-by-time! tx input))
215220
(-query-blocked-jwt [_ tx input]
216221
(query-blocked-jwt-exists tx input))
222+
(-query-one-time-jwt [_ tx input]
223+
(query-one-time-jwt-exists tx input))
217224

218225
bp/CredentialBackend
219226
(-insert-credential! [_ tx input]

src/db/postgres/lrsql/postgres/sql/ddl.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,10 @@ CREATE TABLE IF NOT EXISTS blocked_jwt (
499499
evict_time TIMESTAMP WITH TIME ZONE
500500
);
501501
CREATE INDEX IF NOT EXISTS blocked_jwt_evict_time_idx ON blocked_jwt(evict_time);
502+
503+
/* Migration 2025-03-05 - Add One-Time ID to Blocklist Table */
504+
505+
-- :name alter-blocked-jwt-add-one-time-id!
506+
-- :command :execute
507+
-- :doc Add the column `blocked_jwt.one_time_id` for one-time JWTs; JWTs with one-time IDs are not considered blocked yet.
508+
ALTER TABLE IF EXISTS blocked_jwt ADD COLUMN IF NOT EXISTS one_time_id UUID UNIQUE;

src/db/postgres/lrsql/postgres/sql/insert.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,13 @@ INSERT INTO blocked_jwt (
171171
) VALUES (
172172
:jwt, :eviction-time
173173
);
174+
175+
-- :name insert-one-time-jwt!
176+
-- :command :insert
177+
-- :result :affected
178+
-- :doc Insert a `:jwt` and a `:eviction-time` with `:one-time-id` into the blocklist.
179+
INSERT INTO blocked_jwt (
180+
jwt, evict_time, one_time_id
181+
) VALUES (
182+
:jwt, :eviction-time, :one-time-id
183+
);

src/db/postgres/lrsql/postgres/sql/query.sql

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,15 @@ WHERE reaction_id IS NOT NULL;
433433
-- :name query-blocked-jwt-exists
434434
-- :command :query
435435
-- :result :one
436-
-- :doc Query that `:jwt` is in the blocklist.
436+
-- :doc Query that `:jwt` is in the blocklist. Excludes JWTs where `one_time_id` is not null.
437437
SELECT 1 FROM blocked_jwt
438-
WHERE jwt = :jwt;
438+
WHERE jwt = :jwt
439+
AND one_time_id IS NULL;
440+
441+
-- :name query-one-time-jwt-exists
442+
-- :command :query
443+
-- :result :one
444+
-- :doc Query that `:jwt` with `:one-time-id` exists.
445+
SELECT 1 FROM blocked_jwt
446+
WHERE jwt = :jwt
447+
AND one_time_id = :one-time-id;

src/db/postgres/lrsql/postgres/sql/update.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ SET
7777
passhash = :new-passhash
7878
WHERE id = :account-id;
7979

80+
-- :name update-one-time-jwt!
81+
-- :command :execute
82+
-- :result :affected
83+
-- :doc Update `blocked_jwt.one_time_id` to be null, thus blocking the JWT.
84+
UPDATE blocked_jwt
85+
SET
86+
one_time_id = NULL
87+
WHERE one_time_id = :one-time-id;
88+
8089
-- :name update-reaction!
8190
-- :command :execute
8291
-- :result :affected

src/db/sqlite/lrsql/sqlite/record.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
(update-schema-simple! tx alter-statement-to-actor-add-cascade-delete!))
118118
(create-blocked-jwt-table! tx)
119119
(create-blocked-jwt-evict-time-idx! tx)
120+
(when-not (some? (query-blocked-jwt-one-time-id-exists tx))
121+
(alter-blocked-jwt-add-one-time-id! tx)
122+
(alter-blocked-jwt-add-one-time-id-idx! tx))
120123
(log/infof "sqlite schema_version: %d"
121124
(:schema_version (query-schema-version tx))))
122125

@@ -247,10 +250,16 @@
247250
bp/JWTBlocklistBackend
248251
(-insert-blocked-jwt! [_ tx input]
249252
(insert-blocked-jwt! tx input))
253+
(-insert-one-time-jwt! [_ tx input]
254+
(insert-one-time-jwt! tx input))
255+
(-update-one-time-jwt! [_ tx input]
256+
(update-one-time-jwt! tx input))
250257
(-delete-blocked-jwt-by-time! [_ tx input]
251258
(delete-blocked-jwt-by-time! tx input))
252259
(-query-blocked-jwt [_ tx input]
253260
(query-blocked-jwt-exists tx input))
261+
(-query-one-time-jwt [_ tx input]
262+
(query-one-time-jwt-exists tx input))
254263

255264
bp/CredentialBackend
256265
(-insert-credential! [_ tx input]

src/db/sqlite/lrsql/sqlite/sql/ddl.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,23 @@ CREATE TABLE IF NOT EXISTS blocked_jwt (
538538
-- :command :execute
539539
-- :doc Create the `blocked_jwt_evict_time_idx` table if it does not exist yet.
540540
CREATE INDEX IF NOT EXISTS blocked_jwt_evict_time_idx ON blocked_jwt(evict_time);
541+
542+
/* Migration 2025-03-05 - Add One-Time ID to Blocklist Table */
543+
544+
-- :name query-blocked-jwt-one-time-id-exists
545+
-- :command :query
546+
-- :result :one
547+
-- :doc Query to see if `blocked_jwt.one_time_id` exists.
548+
SELECT 1 FROM pragma_table_info('blocked_jwt') WHERE name = 'one_time_id';
549+
550+
-- :name alter-blocked-jwt-add-one-time-id!
551+
-- :command :execute
552+
-- :result :one
553+
-- :doc Add the column `blocked_jwt.one_time_id` for one-time JWTs; JWTs with one-time IDs are not considered blocked yet.
554+
ALTER TABLE blocked_jwt ADD COLUMN one_time_id TEXT;
555+
556+
-- :name alter-blocked-jwt-add-one-time-id-idx!
557+
-- :command :execute
558+
-- :result :one
559+
-- :doc Add a unique index on `blocked_jwt.one_time_id` (since SQLite does not allow directly adding unique columns).
560+
CREATE UNIQUE INDEX IF NOT EXISTS blocked_jwt_one_time_id_idx ON blocked_jwt(one_time_id);

src/db/sqlite/lrsql/sqlite/sql/insert.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,13 @@ INSERT INTO blocked_jwt (
171171
) VALUES (
172172
:jwt, :eviction-time
173173
);
174+
175+
-- :name insert-one-time-jwt!
176+
-- :command :insert
177+
-- :result :affected
178+
-- :doc Insert a `:jwt` and a `:eviction-time` with `:one-time-id` into the blocklist.
179+
INSERT INTO blocked_jwt (
180+
jwt, evict_time, one_time_id
181+
) VALUES (
182+
:jwt, :eviction-time, :one-time-id
183+
);

src/db/sqlite/lrsql/sqlite/sql/query.sql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,15 @@ WHERE reaction_id IS NOT NULL;
400400
-- :name query-blocked-jwt-exists
401401
-- :command :query
402402
-- :result :one
403-
-- :doc Query that `:jwt` is in the blocklist.
403+
-- :doc Query that `:jwt` is in the blocklist. Excludes JWTs where `one_time_id` is not null.
404404
SELECT 1 FROM blocked_jwt
405405
WHERE jwt = :jwt
406+
AND one_time_id IS NULL;
407+
408+
-- :name query-one-time-jwt-exists
409+
-- :command :query
410+
-- :result :one
411+
-- :doc Query that `:jwt` with `:one-time-id` exists.
412+
SELECT 1 FROM blocked_jwt
413+
WHERE jwt = :jwt
414+
AND one_time_id = :one-time-id;

src/db/sqlite/lrsql/sqlite/sql/update.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ SET
7272
passhash = :new-passhash
7373
WHERE id = :account-id
7474

75+
-- :name update-one-time-jwt!
76+
-- :command :execute
77+
-- :result :affected
78+
-- :doc Update `blocked_jwt.one_time_id` to be null, thus blocking the JWT.
79+
UPDATE blocked_jwt
80+
SET
81+
one_time_id = NULL
82+
WHERE jwt = :jwt
83+
AND one_time_id = :one-time-id;
84+
7585
-- :name update-reaction!
7686
-- :command :execute
7787
-- :result :affected

0 commit comments

Comments
 (0)