camper/pkg
jordi fita mas 62b54961f4 Implement Redsys request signature in PostgreSQL
Every company need to have its own merchant code and encryption key,
thus it is not possible to use environment variables to keep that data,
and i have to store it in the database.

I do not want to give SELECT permission on the encryption key to guest,
because i am going to fuck it up sooner or later, and everyone would be
able to read that secret; i know it would.  Therefore, i need a security
definer function that takes the data to encrypt, use the key to encrypt
it, and returns the result; nobody else should have access to that key,
not even admins!

By the way, i found out that every merchant receives the same key, thus
it is not a problem to keep it in the repository.

Since i need that SQL function to encrypt the data, i thought that i may
go the whole nine yards and sign the request in PostgreSQL too, after
all the data to sign comes from there, and it has JSON functions to
create and base64-code an object.

Fortunately, pg_crypto has all the functions that i need, but i can no
longer keep that extension inside the auth schema, because it is used
from others, and the public schema, like every other extensions, seems
more appropriate.

Instead of having the list of currency and language codes that Redsys
uses as constants in the code, i moved that as field to the currency
and language relations, so i can simply pass the lang_tag to the
function and it can transform that tag to the correct code; the currency
is from the company’s relation, since it is the only currency used in
the whole application (for now).

As a consequence, i had to grant execute to currency and the parse_price
functions to guest, too.

To generate the test data used in the unit tests, i used a third-party
PHP implementation[0], but i only got from that the resulting base64-coded
JSON object and signature, using the same that as in the unit test, and
did not use any code from there.

PostgreSQL formats the JSON as text differently than most
implementations i have seen: it adds spaces between the key name and
the colons, and space between the value and the separating comma.  The
first implementation used replace() to format the JSON as exactly as
the PHP implementation, so that the result matches, and then tried to do
generate the form by hand using the output from PostgreSQL without the
replace(), to verify that Redsys would still accept my input.  Finally,
i adjusted the unit test to whatever pg_prove said it was getting from
the function.

I still have the form’s action hard-codded to the test environment, but
the idea is that administrators should be able to switch from test to
live themselves.  That means that i need that info in the redsys
relation as well.  I think this is one of the few use cases for SQL’s
types, because it is unlikely to change anytime soon, and i do not need
the actual labels.

Unfortunately, i could not use enumerations for the request’s
transaction type because i can not attach an arbitrary number to the
enum’s values.  Having a relation is overkill, because i would need
a constant in Go to refer to its primary key anyway, thus i kept the
same constant i had before for that.  Language and currency constant
went out, as this is in the corresponding relations.

In setup_redsys i must have a separate update if the encrypt_key is null
because PostgreSQL checks constraints before key conflict, and i do
not want to give a default value to the key if the row is not there yet.
The problem is that i want to use null to mean “keep the same password”,
because it is what i intend to do with the user-facing form: leave the
input empty to keep the same password.

As now Go needs to pass composite types back and forth with PostgreSQL,
i need to register these types, and i have to do it every time because
the only moment i have access to the non-pooled connection is in the
AfterConnect function, but at that point i have no idea whether the
user is going to request a payment.  I do not know how much the
performance degrades because of this.

[0]: https://github.com/ssheduardo/sermepa/blob/master/src/Sermepa/Tpv/Tpv.php
2023-10-27 01:52:04 +02:00
..
app Add the first draft of the booking and payment forms 2023-10-19 21:37:34 +02:00
auth Add the form to update company’s tax details 2023-08-15 22:35:21 +02:00
booking Implement Redsys request signature in PostgreSQL 2023-10-27 01:52:04 +02:00
campsite Add the slug to the form in the campsite type public page 2023-10-19 21:38:44 +02:00
carousel Add the carousel for campsite types 2023-10-12 17:43:23 +02:00
company Add the Catalonia’s Tourism Registry number to company 2023-10-14 21:59:36 +02:00
database Implement Redsys request signature in PostgreSQL 2023-10-27 01:52:04 +02:00
form Add the first draft of the booking and payment forms 2023-10-19 21:37:34 +02:00
hex Add cover media to campsite types 2023-09-10 03:04:18 +02:00
home Manage all media uploads in a single place 2023-09-21 01:56:44 +02:00
http Add the language switched to the public layout 2023-08-06 05:53:52 +02:00
locale Add the edit form for services 2023-09-25 20:10:33 +02:00
media Show the campsite type’s calendar in an “infinite scroll” carousel 2023-10-18 21:06:41 +02:00
redsys Implement Redsys request signature in PostgreSQL 2023-10-27 01:52:04 +02:00
season Show the campsite type’s calendar in an “infinite scroll” carousel 2023-10-18 21:06:41 +02:00
services Add function and HTTP handler to delete services 2023-09-26 17:29:49 +02:00
template Show the campsite type’s calendar in an “infinite scroll” carousel 2023-10-18 21:06:41 +02:00
uuid Add cover media to campsite types 2023-09-10 03:04:18 +02:00