Re: Using Token (JWT) authentication mechanism in Postgres

From: Alexander Kukushkin <cyberdemn(at)gmail(dot)com>
To: Julio Cesar Tenganan Daza <ctenganand(at)psl(dot)com(dot)co>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Using Token (JWT) authentication mechanism in Postgres
Date: 2018-01-25 08:43:19
Message-ID: CAFh8B==3ef_L2F7uCOo61W9wrBo2uTzmYNLK4fx-+zm_Stfm3w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

2018-01-24 22:27 GMT+01:00 Julio Cesar Tenganan Daza <ctenganand(at)psl(dot)com(dot)co>
:

> Hello,
>
>
>
> I would like to know if is possible to use Token (JWT) authentication
> mechanism in Postgres? In order to authenticate users and also authorize
> access to specific tables, This is in a multi-tenant application context
> where users can create their own tables and share it if they want.
>
>
>
> Is it possible this authentication mechanism or is there any plugin to
> achieve it?
>

Postgres can use pam for authentication.
I am not sure that such plugin already exists, but it shouldn't be very
hard to implement it.

There are a few problems though:
1. JWT token already contains information about username, but you still
have to provide it (username) when opening connection.
2. Token has to be send as a connection password. Therefor connection must
be encrypted.
3. Usually JWT tokens are quite big in size, but for example when psql is
asking you for a password, it thinks that password can't be longer than 100
characters. And this value is hard-coded. It's possible to overcome this
issue if you specify your token in PGPASSWORD env variable.

We at Zalando are using JWT tokens to authenticate employees when they are
accessing postgres databases, but we are not dealing with JWT directly.
We have some OAuth infrastructure in-place, which can validate JWT tokens.
At the end it boiled down to sending http request to tokeninfo service and
validating its answer.

Source code of PAM module is here: https://github.com/CyberDem0n/pam-oauth2

Basically you can do something similar. Either take pam-oauth2 as a
reference and add possibility to validate JWT tokens or implement your
tokeninfo service.

>
>
> Thank You for your help!
>
>
>
> Regards,
>
>
>
> Cesar
>

Regards,
--
Alexander Kukushkin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Durumdara 2018-01-25 09:38:54 Re: PGSQL 10, many Random named DB
Previous Message Azimuddin Mohammed 2018-01-25 07:36:49 Re: Postgres for hadoop