Expose ext::pgcrypto::gen_salt and ext::pgcrypto::crypt (#6264)

Fixes: #6075
This commit is contained in:
Elvis Pranskevichus 2023-10-10 15:36:07 -07:00 committed by GitHub
parent b74e7133da
commit 007ab4b935
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 118 additions and 1 deletions

View file

@ -44,7 +44,7 @@ from edb.common import verutils
# Increment this whenever the database layout or stdlib changes.
EDGEDB_CATALOG_VERSION = 2023_10_02_00_03
EDGEDB_CATALOG_VERSION = 2023_10_10_00_00
EDGEDB_MAJOR_VERSION = 4

View file

@ -56,4 +56,33 @@ create extension package pgcrypto version '1.3' {
set volatility := 'Immutable';
using sql function 'edgedb.hmac';
};
create function ext::pgcrypto::gen_salt(
) -> std::str {
set volatility := 'Volatile';
using sql "SELECT edgedb.gen_salt('bf')";
};
create function ext::pgcrypto::gen_salt(
type: std::str,
) -> std::str {
set volatility := 'Volatile';
using sql 'SELECT edgedb.gen_salt("type")';
};
create function ext::pgcrypto::gen_salt(
type: std::str,
iter_count: std::int64,
) -> std::str {
set volatility := 'Volatile';
using sql 'SELECT edgedb.gen_salt("type", "iter_count"::integer)';
};
create function ext::pgcrypto::crypt(
password: std::str,
salt: std::str,
) -> std::str {
set volatility := 'Immutable';
using sql function 'edgedb.crypt';
};
};

View file

@ -103,3 +103,91 @@ class TestEdgeQLExtPgCrypto(tb.QueryTestCase):
},
json_only=True,
)
async def test_edgeql_ext_pgcrypto_gen_salt(self):
CASES = [
("bf", 5),
("xdes", 801),
("md5", None),
("des", None),
]
await self.assert_query_result(
"""
select {
salt := ext::pgcrypto::gen_salt()
}
""",
[{}],
json_only=True,
)
for hash_type, iter_count in CASES:
with self.subTest(hash_type):
await self.assert_query_result(
"""
select {
salt := ext::pgcrypto::gen_salt(<str>$type)
}
""",
[{}],
variables={
"type": hash_type,
},
json_only=True,
)
if iter_count is not None:
await self.assert_query_result(
"""
select {
salt := ext::pgcrypto::gen_salt(
<str>$type,
<int64>$iter_count,
)
}
""",
[{}],
variables={
"type": hash_type,
"iter_count": iter_count,
},
json_only=True,
)
async def test_edgeql_ext_pgcrypto_crypt(self):
CASES = [
(
"bf",
"$2a$06$1qmwsi8lj0HKQnokkCkZSe",
"$2a$06$1qmwsi8lj0HKQnokkCkZSenubuHv5CGJ2ICxcOPjOr6xOKDBY..Eu",
),
(
"md5",
"$1$ePFh8A9K",
"$1$ePFh8A9K$xN/KWq.qDWTW9HYvx8/VP/",
),
(
"xdes",
"_J9..3OQ/",
"_J9..3OQ/ylnZ6cP2muw",
),
(
"des",
"JU",
"JUJ5Ovy43JsfM",
),
]
for hash_type, salt, result in CASES:
with self.subTest(hash_type):
await self.assert_query_result(
"""
select ext::pgcrypto::crypt("foo", <str>$salt)
""",
[result],
variables={
"salt": salt,
},
json_only=True,
)