initial commit
[squeep-indie-auther] / src / db / postgres / sql / redeem-code.sql
1 --
2 INSERT INTO token (
3 created,
4 code_id,
5 is_token,
6 client_id,
7 profile_id,
8 duration,
9 expires,
10 refresh_duration,
11 refresh_expires,
12 resource,
13 profile_data
14 ) SELECT
15 $(created)::timestamptz,
16 $(codeId),
17 $(isToken),
18 $(clientId),
19 p.profile_id,
20 $(lifespanSeconds)::text::interval,
21 CASE WHEN $(lifespanSeconds) IS NULL THEN NULL ELSE $(created)::timestamptz + $(lifespanSeconds)::text::interval END,
22 $(refreshLifespanSeconds)::text::interval,
23 CASE WHEN $(refreshLifespanSeconds) IS NULL THEN NULL ELSE $(created)::timestamptz + $(refreshLifespanSeconds)::text::interval END,
24 $(resource),
25 $(profileData)
26 FROM profile p INNER JOIN authentication a USING (identifier_id)
27 WHERE p.profile = $(profile) AND a.identifier = $(identifier)
28 ON CONFLICT (code_id) DO UPDATE -- repeated redemption attempt invalidates existing token
29 SET is_revoked = true
30 RETURNING *