pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Using Token (JWT) authentication mechanism in Postgres
5+ messages / 3 participants
[nested] [flat]

* Using Token (JWT) authentication mechanism in Postgres
@ 2018-01-24 21:27  Julio Cesar Tenganan Daza <ctenganand@psl.com.co>
  0 siblings, 2 replies; 5+ messages in thread

From: Julio Cesar Tenganan Daza @ 2018-01-24 21:27 UTC (permalink / raw)
  To: pgsql-general

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?

Thank You for your help!

Regards,

Cesar

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Using Token (JWT) authentication mechanism in Postgres
@ 2018-01-24 22:07  James Keener <jim@jimkeener.com>
  parent: Julio Cesar Tenganan Daza <ctenganand@psl.com.co>
  1 sibling, 0 replies; 5+ messages in thread

From: James Keener @ 2018-01-24 22:07 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org; Julio Cesar Tenganan Daza <ctenganand@psl.com.co>; pgsql-general

I believe postgrest can, and you can always use jwt in your application, but postgresql doesn't natively support them.

On January 24, 2018 4:27:23 PM EST, Julio Cesar Tenganan Daza <ctenganand@psl.com.co> wrote:
>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?
>
>Thank You for your help!
>
>Regards,
>
>Cesar

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Using Token (JWT) authentication mechanism in Postgres
@ 2018-01-25 08:43  Alexander Kukushkin <cyberdemn@gmail.com>
  parent: Julio Cesar Tenganan Daza <ctenganand@psl.com.co>
  1 sibling, 1 reply; 5+ messages in thread

From: Alexander Kukushkin @ 2018-01-25 08:43 UTC (permalink / raw)
  To: Julio Cesar Tenganan Daza <ctenganand@psl.com.co>; +Cc: pgsql-general

Hi,


2018-01-24 22:27 GMT+01:00 Julio Cesar Tenganan Daza <ctenganand@psl.com.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

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* RE: Using Token (JWT) authentication mechanism in Postgres
@ 2018-01-26 14:54  Julio Cesar Tenganan Daza <ctenganand@psl.com.co>
  parent: Alexander Kukushkin <cyberdemn@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Julio Cesar Tenganan Daza @ 2018-01-26 14:54 UTC (permalink / raw)
  To: Alexander Kukushkin <cyberdemn@gmail.com>; +Cc: pgsql-general

Hi Alexander,

I think we can use PAM authentication with something similar as you did is a good mechanism.

And also I have an additional question, can the implemented PAM authentication module be used from JDBC connections? Or they works totally apart?

Thank you so much!


Regards,


Cesar

From: Alexander Kukushkin [mailto:cyberdemn@gmail.com]
Sent: Thursday, January 25, 2018 3:43 AM
To: Julio Cesar Tenganan Daza <ctenganand@psl.com.co>
Cc: pgsql-general@postgresql.org
Subject: Re: Using Token (JWT) authentication mechanism in Postgres

Hi,


2018-01-24 22:27 GMT+01:00 Julio Cesar Tenganan Daza <ctenganand@psl.com.co<mailto:ctenganand@psl.com.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


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Using Token (JWT) authentication mechanism in Postgres
@ 2018-01-26 15:21  Alexander Kukushkin <cyberdemn@gmail.com>
  parent: Julio Cesar Tenganan Daza <ctenganand@psl.com.co>
  0 siblings, 0 replies; 5+ messages in thread

From: Alexander Kukushkin @ 2018-01-26 15:21 UTC (permalink / raw)
  To: Julio Cesar Tenganan Daza <ctenganand@psl.com.co>; +Cc: pgsql-general

Hi,


>
> And also I have an additional question, can the implemented PAM
> authentication module be used from JDBC connections? Or they works totally
> apart?
>

I think it should work from JDBC without any additional effort. And
basically pam module itself doesn't even know that it is used from postgres.

Regards,
--
Alexander Kukushkin

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2018-01-26 15:21 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-01-24 21:27 Using Token (JWT) authentication mechanism in Postgres Julio Cesar Tenganan Daza <ctenganand@psl.com.co>
2018-01-24 22:07 ` James Keener <jim@jimkeener.com>
2018-01-25 08:43 ` Alexander Kukushkin <cyberdemn@gmail.com>
2018-01-26 14:54   ` Julio Cesar Tenganan Daza <ctenganand@psl.com.co>
2018-01-26 15:21     ` Alexander Kukushkin <cyberdemn@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox