Coder Social home page Coder Social logo

Comments (6)

begriffs avatar begriffs commented on May 18, 2024

@deinspanjer any update on this?

from postgrest-docs.

deinspanjer avatar deinspanjer commented on May 18, 2024

Shoot, this slipped my mind cause I have been working on a different part of our stack, but I will try to dig up my notes and put an update in here early next week.

from postgrest-docs.

begriffs avatar begriffs commented on May 18, 2024

I believe this documentation, once written, will address the concerns in PostgREST/postgrest#809

from postgrest-docs.

begriffs avatar begriffs commented on May 18, 2024

Note to self: we also want to give an example of hiding the jwt functions themselves in a separate schema from the public api schema.

from postgrest-docs.

deinspanjer avatar deinspanjer commented on May 18, 2024

Here is some DDL that pertains to the management of the jwt_secret and generation of signed tokens without relying on a DATABASE level setting (which isn't possible in Amazon RDS):

CREATE ROLE auther;
CREATE SCHEMA private;

ALTER DEFAULT PRIVILEGES REVOKE ALL ON SCHEMA private FROM PUBLIC;
GRANT USAGE ON SCHEMA private TO auther;

CREATE TABLE private.jwt_secret (
  secret TEXT NOT NULL,
  modified_on TIMESTAMPTZ DEFAULT NOW()
);
CREATE UNIQUE INDEX single_row ON private.jwt_secret ((secret IS NOT NULL));

REVOKE ALL ON TABLE private.jwt_secret FROM PUBLIC;
GRANT SELECT ON TABLE private.jwt_secret TO auther;

INSERT INTO private.jwt_secret (secret) VALUES('notso');

-- Install pgjwt then:

CREATE TYPE private.jwt_claims AS (iss TEXT, exp INT, sub TEXT, aud TEXT, jti UUID, rol NAME, role NAME);
CREATE OR REPLACE FUNCTION public.create_jwt_token(
  iss    TEXT,
  dur    INTERVAL,
  sub    TEXT,
  rol    NAME,
  "role" NAME
)
  RETURNS TEXT
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
  exp INT;
  jti UUID;
  aud TEXT;
  claims private.jwt_claims;
  signed_token TEXT;
BEGIN

  aud := 'http://localhost:8080/';
  exp := extract(EPOCH FROM (NOW() + dur));
  jti := gen_random_uuid();

  PERFORM set_config('request.jwt.claim.sub', sub, true);
  PERFORM set_config('request.jwt.claim.rol', rol, true);

  SELECT
      iss    AS iss
    , exp    AS exp
    , sub    AS sub
    , aud    AS aud
    , jti    AS jti
    , rol    AS rol
    , "role" AS "role"
  INTO claims;

  SELECT public.sign(row_to_json(claims), (SELECT secret FROM private.jwt_secret))
  INTO signed_token;
  RETURN signed_token;
END
$$;

from postgrest-docs.

begriffs avatar begriffs commented on May 18, 2024

Thanks for that.

from postgrest-docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.