-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.sql
375 lines (298 loc) · 7.88 KB
/
schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
--
-- PostgreSQL database dump
--
-- Dumped from database version 11.2
-- Dumped by pg_dump version 11.2
SET TIMEZONE='Asia/Kolkata';
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'SQL_ASCII';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: crl; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.crl (
crl jsonb NOT NULL
);
INSERT INTO public.crl VALUES('[]'::jsonb);
--
-- Name: groups; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.groups (
id character varying NOT NULL,
consumer character varying NOT NULL,
group_name character varying NOT NULL,
valid_till timestamp without time zone NOT NULL
);
CREATE INDEX idx_groups_id ON public.groups(id,group_name,valid_till);
--
-- Name: policy; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.policy (
id character varying PRIMARY KEY,
policy character varying(3145728) NOT NULL,
policy_in_json jsonb NOT NULL,
previous_policy character varying(3145728),
last_updated timestamp without time zone NOT NULL,
api_called_from character varying(512)
);
CREATE UNIQUE INDEX idx_policy_id ON public.policy(id);
--
-- Name: token; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.token (
id character varying NOT NULL,
token character varying NOT NULL,
expiry timestamp without time zone NOT NULL,
request jsonb NOT NULL,
cert_serial character varying NOT NULL,
cert_fingerprint character varying NOT NULL,
issued_at timestamp without time zone NOT NULL,
resource_ids jsonb NOT NULL,
introspected boolean NOT NULL,
revoked boolean NOT NULL,
cert_class integer NOT NULL,
cert_title character varying NOT NULL,
server_token jsonb NOT NULL,
providers jsonb NOT NULL,
geoip jsonb NOT NULL,
payment_info jsonb NOT NULL,
paid boolean NOT NULL,
paid_at timestamp without time zone ,
api_called_from character varying(512) ,
manual_authorization_array jsonb NOT NULL,
PRIMARY KEY (token)
);
CREATE UNIQUE INDEX idx_token_id ON public.token(id,token,issued_at);
CREATE TABLE public.credit (
id character varying NOT NULL,
cert_serial character varying NOT NULL,
cert_fingerprint character varying NOT NULL,
amount numeric NOT NULL CHECK (amount >= 0),
last_updated timestamp without time zone NOT NULL,
CONSTRAINT "credit_pkey" PRIMARY KEY (id, cert_serial, cert_fingerprint)
);
CREATE TABLE public.topup_transaction (
id character varying NOT NULL,
cert_serial character varying NOT NULL,
cert_fingerprint character varying NOT NULL,
amount numeric NOT NULL CHECK (amount >= 0),
time timestamp without time zone NOT NULL,
invoice_number character varying NOT NULL,
paid boolean NOT NULL,
payment_details jsonb NOT NULL
);
CREATE UNIQUE INDEX idx_topup_transaction ON public.topup_transaction (id,time);
CREATE TABLE public.telegram (
telegram_id character varying PRIMARY KEY,
chat_id character varying
);
--
-- Functions
--
CREATE OR REPLACE FUNCTION public.update_credit (
IN in_invoice_number character varying,
IN in_payment_details jsonb
) RETURNS SETOF jsonb AS
$$
DECLARE
my_id character varying;
my_cert_serial character varying;
my_cert_fingerprint character varying;
my_time timestamp without time zone;
my_amount numeric;
my_num_rows_affected int;
BEGIN
UPDATE public.topup_transaction
SET
paid = TRUE,
time = NOW(),
payment_details = in_payment_details
WHERE
invoice_number = in_invoice_number
AND
paid = FALSE
RETURNING
id,
cert_serial,
cert_fingerprint,
time,
amount
INTO
my_id,
my_cert_serial,
my_cert_fingerprint,
my_time,
my_amount
;
GET DIAGNOSTICS my_num_rows_affected = ROW_COUNT;
IF my_num_rows_affected = 0
THEN
RETURN QUERY
SELECT '{}'::jsonb;
RETURN;
END IF;
INSERT INTO public.credit (
id,
cert_serial,
cert_fingerprint,
amount,
last_updated
)
VALUES (
my_id,
my_cert_serial,
my_cert_fingerprint,
my_amount,
my_time
)
ON CONFLICT ON CONSTRAINT credit_pkey
DO UPDATE
SET
amount = credit.amount + my_amount,
last_updated = my_time
;
RETURN QUERY
SELECT json_build_object (
'id', id,
'amount', amount,
'time', time,
'cert_serial', cert_serial,
'cert_fingerprint', cert_fingerprint
) :: jsonb
FROM
topup_transaction
WHERE
invoice_number = in_invoice_number
;
END;
$$
LANGUAGE PLPGSQL STRICT;
CREATE OR REPLACE FUNCTION public.confirm_payment (
IN in_id character varying,
IN in_amount numeric,
IN in_cert_serial character varying,
IN in_cert_fingerprint character varying,
IN in_serial character varying,
IN in_fingerprint character varying
) RETURNS boolean AS
$$
DECLARE
my_num_rows_affected int;
BEGIN
UPDATE credit
SET
amount = amount - in_amount
WHERE
id = in_id
AND
cert_serial = in_serial
AND
cert_fingerprint = in_fingerprint
AND
(amount - in_amount) >= 0.0
;
GET DIAGNOSTICS my_num_rows_affected = ROW_COUNT;
IF my_num_rows_affected = 0
THEN
RETURN FALSE;
END IF;
UPDATE token
SET
paid = TRUE,
paid_at = NOW(),
expiry = (expiry - issued_at + NOW())
WHERE
id = in_id
AND
paid = FALSE
AND
cert_serial = in_cert_serial
AND
cert_fingerprint = in_cert_fingerprint
;
GET DIAGNOSTICS my_num_rows_affected = ROW_COUNT;
IF my_num_rows_affected = 0
THEN
RAISE EXCEPTION 'Invalid token';
END IF;
RETURN TRUE;
END;
$$
LANGUAGE PLPGSQL STRICT;
CREATE OR REPLACE FUNCTION public.transfer_credits (
IN in_id character varying,
IN in_amount numeric,
IN in_from character varying,
IN in_to character varying,
IN in_to_serial character varying
) RETURNS boolean AS
$$
DECLARE
my_num_rows_affected int;
BEGIN
UPDATE credit
SET
amount = amount - in_amount
WHERE
cert_fingerprint = in_from
AND
(amount - in_amount) > 0.0
;
GET DIAGNOSTICS my_num_rows_affected = ROW_COUNT;
IF my_num_rows_affected = 0
THEN
RAISE EXCEPTION 'Not enough balance';
END IF;
UPDATE credit
SET
amount = amount + in_amount
WHERE
cert_fingerprint = in_to
;
IF my_num_rows_affected = 0
THEN
INSERT INTO credit VALUES (
in_id,
in_to_serial,
in_to,
in_amount,
NOW()
);
END IF;
RETURN TRUE;
END;
$$
LANGUAGE PLPGSQL STRICT;
--
-- ACCESS CONTROLS
--
ALTER TABLE public.policy OWNER TO postgres;
ALTER TABLE public.groups OWNER TO postgres;
ALTER TABLE public.crl OWNER TO postgres;
ALTER TABLE public.token OWNER TO postgres;
ALTER TABLE public.credit OWNER TO postgres;
ALTER TABLE public.topup_transaction OWNER TO postgres;
ALTER TABLE public.telegram OWNER TO postgres;
ALTER FUNCTION public.update_credit OWNER TO postgres;
CREATE USER auth with PASSWORD 'XXX_auth';
CREATE USER bot with PASSWORD 'XXX_bot';
CREATE USER update_crl with PASSWORD 'XXX_update_crl';
GRANT SELECT ON TABLE public.crl TO auth;
GRANT SELECT,INSERT,UPDATE ON TABLE public.token TO auth;
GRANT SELECT,INSERT,UPDATE ON TABLE public.groups TO auth;
GRANT SELECT,INSERT,UPDATE ON TABLE public.policy TO auth;
GRANT SELECT,INSERT,UPDATE ON TABLE public.credit TO auth;
GRANT SELECT,INSERT,UPDATE ON TABLE public.topup_transaction TO auth;
GRANT SELECT,INSERT,UPDATE ON TABLE public.telegram TO auth;
GRANT SELECT,UPDATE ON TABLE public.token TO bot;
GRANT SELECT,INSERT,UPDATE ON TABLE public.telegram TO bot;
GRANT UPDATE ON TABLE public.crl TO update_crl;
GRANT EXECUTE ON FUNCTION public.update_credit(character varying,jsonb) TO auth;