crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Handling refresh token in jwt (multiple secret keys and tokens handling)

Open sspsinha opened this issue 6 years ago • 7 comments

  • [ ] question Does current postgraphile library handle multiple secret keys and tokens. Say I have to use access token and refresh token in one single sign in request.

Current behavior:

jwtSecret: accessSecret, jwtPgTypeIdentifier: pgIdentifier,

Expected behavior:

jwtSecret: [accessSecret, refreshSecret] jwtPgTypeIdentifier: [pgIdentifier1, pgIdentifier2]

sspsinha avatar Aug 05 '20 06:08 sspsinha

Yes, you can achieve this with pgSettings: https://www.graphile.org/postgraphile/usage-library/#pgsettings-function

This approach is recommended as soon as you need anything more than trivially simple JWT support, it gives you the full flexibility to use whatever authentication rules you want.

benjie avatar Aug 05 '20 08:08 benjie

HI @benjie : Could you please let me know how to sign following two tokens there in database with two different secret key secret1 & secret2. And what to do in TODO section for pgsettings inside postgraphile options

CREATE TYPE DB1.jwt_token AS
(
	role text,
	user_profile_id uuid,	
	exp integer
);
CREATE TYPE DB1.jwt_refresh AS
(
	ipaddress text
	user_profile_id uuid,	
	exp integer
);

with following postgraphile option:

const postgraphileOptions = { subscriptions: true, retryOnInitFail: true, dynamicJson: true, setofFunctionsContainNulls: false, ignoreRBAC: false, ignoreIndexes: false, extendedErrors: ["errcode"], appendPlugins: [require("@graphile-contrib/pg-simplify-inflector")], graphiql: false, enableQueryBatching: true, disableQueryLog: true, // our default logging has performance issues, but do make sure you have a logging system in place! legacyRelations: "omit", pgSettings(req) { /* TODO */ }, };

sspsinha avatar Aug 05 '20 17:08 sspsinha

You'd need to duplicate the JWT plugin, modify it to support your additional identifier:

https://github.com/graphile/graphile-engine/blob/9081501d946364ee49d50fa62071bc115f89eb02/packages/graphile-build-pg/src/plugins/PgJWTPlugin.js

benjie avatar Aug 11 '20 09:08 benjie

Not to complicate. Could you really provide the nodejs sample to access jwt.claims.user_profile_id through pgSettings.

SELECT current_setting('jwt.claims.user_profile_id', true)::uuid;

Tried following https://github.com/graphile/postgraphile/issues/1049

My approach: import express from "express"; import session from "express-session"; import passport from "passport";

const app = express(); app.use(session({ secret: "keyboard cat", cookie: { maxAge: 60000 } })); app.use(passport.initialize()); app.use(passport.session());

Here is my pgSettings pgSettings: (req) => ( { role: process.env.POSTGRES_DEFAULT_ROLE, "jwt.claims.user_profile_id": req.session?.user_profile_id, }, ),

sspsinha avatar Aug 11 '20 10:08 sspsinha

That looks correct if you're using sessions rather than JWTs. If you need more help on this, I suggest you ask the community over chat: https://discord.gg/graphile

benjie avatar Aug 11 '20 11:08 benjie

@benjie : I have posted on discord. But I dont get any response

sspsinha avatar Aug 13 '20 04:08 sspsinha

If anyone arrives here in search of how to support refresh tokens like I did, there is nice project: https://github.com/onpaws/refresh-token-postgraphile. It fully implements and example frontend + backend with the correct configuration for the Apollo Client.

bn3t avatar Jan 08 '22 14:01 bn3t