All Projects → monacoremo → Postgrest Sessions Example

monacoremo / Postgrest Sessions Example

Licence: mit
An example on how to implement session-based authentication with PostgREST

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to Postgrest Sessions Example

Ktorm
A lightweight ORM framework for Kotlin with strong-typed SQL DSL and sequence APIs.
Stars: ✭ 843 (+2710%)
Mutual labels:  sql
Notes
ideas and planning on how to use IPFS together with Nix/OS
Stars: ✭ 15 (-50%)
Mutual labels:  nix
Spark
Apache Spark - A unified analytics engine for large-scale data processing
Stars: ✭ 31,618 (+105293.33%)
Mutual labels:  sql
System.linq.dynamic.core
The .NET Standard / .NET Core version from the System Linq Dynamic functionality.
Stars: ✭ 864 (+2780%)
Mutual labels:  sql
Alembic
A database migrations tool for SQLAlchemy.
Stars: ✭ 874 (+2813.33%)
Mutual labels:  sql
Express Knex Objection
A simple API system on a pg database, using knex and objection to simplify connection and management
Stars: ✭ 20 (-33.33%)
Mutual labels:  sql
Live2
SAP HANA Academy - Live2 project code samples for playlist https://www.youtube.com/playlist?list=PLkzo92owKnVyIXgkK__7Z1o_C7pyNc3SR
Stars: ✭ 8 (-73.33%)
Mutual labels:  sql
Fenix
Rust nightly toolchains and rust analyzer nightly for nix [[email protected]]
Stars: ✭ 29 (-3.33%)
Mutual labels:  nix
Jsqltuner
SQL tuner for improving Java applications ORM performance using NoSQL technologies.
Stars: ✭ 13 (-56.67%)
Mutual labels:  sql
Nix2aci
Machinery for creating and running ACIs on Linux systems where Nix(OS) and rkt are available
Stars: ✭ 21 (-30%)
Mutual labels:  nix
Phoenix
Mirror of Apache Phoenix
Stars: ✭ 867 (+2790%)
Mutual labels:  sql
Sinatra On Nix
An example Sinatra project using Nix to configure the development environment
Stars: ✭ 13 (-56.67%)
Mutual labels:  nix
Eosio sql plugin
EOSIO sql database plugin
Stars: ✭ 21 (-30%)
Mutual labels:  sql
Haskell Nix
Nix and Haskell in production
Stars: ✭ 860 (+2766.67%)
Mutual labels:  nix
Sql Composer
Standalone SQL composer DSL for Ruby
Stars: ✭ 26 (-13.33%)
Mutual labels:  sql
Pl Gapi
Stars: ✭ 8 (-73.33%)
Mutual labels:  sql
Nix Cabal Stack Skeleton
Skeleton project that builds with any of the three major build tools.
Stars: ✭ 15 (-50%)
Mutual labels:  nix
Nur Packages Template
A template for NUR repositories: [[email protected]]
Stars: ✭ 29 (-3.33%)
Mutual labels:  nix
Gorose
GoRose(go orm), a mini database ORM for golang, which inspired by the famous php framwork laravle's eloquent. It will be friendly for php developer and python or ruby developer. Currently provides six major database drivers: mysql,sqlite3,postgres,oracle,mssql, Clickhouse.
Stars: ✭ 947 (+3056.67%)
Mutual labels:  sql
Diff Table
Stars: ✭ 21 (-30%)
Mutual labels:  sql

PostgREST sessions example

Work in progress - feedback and contributions welcome!

This is an example of how session based authentication can be implemented with PostgREST.

The key file in this example is app.sql.md, which is a literate SQL file (like literal Haskell files). As a Markdown file, it explains how to set up an application with sessions. At the same time, it's also a full SQL script that defines the application when you filter for the SQL code blocks.

You can get the filtered SQL script by running sed -f deploy/md2sql.sed <app.sql.md >app.sql

How to implement session-based authentication with PostgREST

We leave the JWT-based authentication of PostgREST unused and implement our own authentication setup using the pre-request hook that PostgREST provides.

The application tracks sessions in its own sessions table.

The business logic for logging in, logging out etc. is defined using functions in the auth schema.

Functions that we expose as endpoints in the api schema wrap the business logic functions in auth to set cookies and raise errors to users where appropriate.

To tie everything together, we hook up the authenticate function as a pre-request in postgrest.conf, so that it runs before every request. It reads the session token from the request cookies and switches to the appropriate role and user_id based on the session.

Dependencies

On Linux, you'll need

  • PostgreSQL 9.5 or higher with the pgtap extension (I tested this with PostgreSQL 12.1, but older versions should also be fine as long as they have the Row Level Security feature).
  • PostgREST >= 6.0

If you have Nix (highly recommended, get it here: Getting Nix), running nix-shell in this directory will drop you in a shell where all dependencies are available, without any permanent changes to your environment. The environment is defined in shell.nix.

Running the application

Run deploy/deploy-local.sh and access the PostgREST API at http://localhost:3000/. The script will run PostgreSQL in a temporary directory and connect it to PostgREST via a Unix domain socket. The application is automatically loaded from the app.sql.md file.

You'll need to have the postgrest binary on your path. If you downloaded it into this directory, you should be able to run PATH=".:$PATH" deploy/deploy-local.sh. The Nix shell environment from above will also take care of this.

Press Ctrl-c to exit and clean up the directory where the temporary database was set up.

Development and testing

To quickly iterate on the database schema, you can run something like echo app.sql.md | entr -r deploy/deploy-local.sh. This will load the schema into a fresh database on every change, including the test suite defined within it.

The entr utility (which is also provided in the Nix environment) takes a list of files to watch on stdin and restarts the command if any of the given files is changed.

To run the integration tests in tests.py, you'll need Python 3 with py.test and requests. To run the full test-suite on each change, you can run, for example: ls | entr -r deploy/test-local.sh.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].