michelp / pgjwt

PostgreSQL implementation of JWT (JSON Web Tokens)
MIT License
368 stars 60 forks source link

Failure when pgcrypto is loaded into a schema. #6

Closed daurnimator closed 5 years ago

daurnimator commented 7 years ago

When pgcrypto is loaded into a schema then pgjwt fails to find pgcrypto functions:

postgres=# create schema pgcrypto;
CREATE SCHEMA
postgres=# create extension pgcrypto with schema pgcrypto;
CREATE EXTENSION
postgres=# create schema pgjwt;
CREATE SCHEMA
postgres=# create extension pgjwt with schema pgjwt;
CREATE EXTENSION
postgres=# select pgjwt.sign('{}', 'foo');
ERROR:  function hmac(text, text, text) does not exist
LINE 9: SELECT pgjwt.url_encode(hmac(signables, secret, alg.id)) FRO...
                                ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
QUERY:  
WITH
  alg AS (
    SELECT CASE
      WHEN algorithm = 'HS256' THEN 'sha256'
      WHEN algorithm = 'HS384' THEN 'sha384'
      WHEN algorithm = 'HS512' THEN 'sha512'
      ELSE '' END AS id)  -- hmac throws error
SELECT pgjwt.url_encode(hmac(signables, secret, alg.id)) FROM alg;

CONTEXT:  SQL function "algorithm_sign" during inlining
SQL function "sign" during startup
guiuprado commented 6 years ago

I have the same issue and to adjust to

was: SELECT pgjwt.url_encode(hmac(signables, secret, alg.id)) FROM alg;

are: SELECT pgjwt.url_encode(public.hmac(signables, secret, alg.id)) FROM alg;

I'm using pg10 with a other schema with Postgrest 0.5

michelp commented 6 years ago

This is likely due to a recent change made to postgrest that sets the search path for a request to only the api schema https://github.com/PostgREST/postgrest/issues/1125

I'll release a fix for this that puts public back into the function paths, and add a note to the readme warning about having pgcrypto in another schema requires altering the functions.

daurnimator commented 6 years ago

This is likely due to a recent change made to postgrest

Note that the original issue here is not postgrest related.