All Projects → platyplus → Authentication Server

platyplus / Authentication Server

A simple authentication service to deliver JWT with Hasura claims, based on users with multiples roles stored in a Postgres database.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Authentication Server

Express Mongodb Rest Api Boilerplate
A boilerplate for Node.js apps / Rest API / Authentication from scratch - express, mongodb (mongoose).
Stars: ✭ 153 (+218.75%)
Mutual labels:  authentication, jwt, jwt-authentication, boilerplate
Express Graphql Boilerplate
Express GraphQL API with JWT Authentication and support for sqlite, mysql, and postgresql
Stars: ✭ 201 (+318.75%)
Mutual labels:  postgresql, authentication, jwt, boilerplate
Express Rest Api Boilerplate
Express REST API with JWT Authentication and support for sqlite, mysql, and postgresql
Stars: ✭ 384 (+700%)
Mutual labels:  postgresql, authentication, jwt, boilerplate
Barong
Barong auth server
Stars: ✭ 100 (+108.33%)
Mutual labels:  authentication, jwt, jwt-authentication
Node Express Mongoose Passport Jwt Rest Api Auth
Node, express, mongoose, passport and JWT REST API authentication example
Stars: ✭ 146 (+204.17%)
Mutual labels:  jwt, jwt-authentication, passportjs
Mern Boilerplate
Fullstack boilerplate with React, Redux, Express, Mongoose, Passport Local, JWT, Facebook and Google OAuth out of the box.
Stars: ✭ 112 (+133.33%)
Mutual labels:  authentication, jwt, boilerplate
Security.identity
.NET DevPack Identity is a set of common implementations to help you implementing Identity, Jwt, claims validation and another facilities
Stars: ✭ 165 (+243.75%)
Mutual labels:  authentication, jwt, roles
Go Postgres Jwt React Starter
A go, gin, and postgres API with jwt auth, complete with a react frontend
Stars: ✭ 115 (+139.58%)
Mutual labels:  authentication, jwt, boilerplate
Fullstack Apollo Express Postgresql Boilerplate
💥 A sophisticated GraphQL with Apollo, Express and PostgreSQL boilerplate project.
Stars: ✭ 1,079 (+2147.92%)
Mutual labels:  postgresql, authentication, boilerplate
Jwt Spring Security Demo
This is a demo for using JWT (JSON Web Token) with Spring Security and Spring Boot. I completely rewrote my first version. Now this solution is based on the code base from the JHipster Project. I tried to extract the minimal configuration and classes that are needed for JWT-Authentication and did some changes.
Stars: ✭ 2,843 (+5822.92%)
Mutual labels:  authentication, jwt, jwt-authentication
Emqx Auth Jwt
EMQ X JWT Authentication Plugin
Stars: ✭ 26 (-45.83%)
Mutual labels:  authentication, jwt, jwt-authentication
Naperg
Fullstack Boilerplate GraphQL. Made with React & Prisma + authentication & roles
Stars: ✭ 661 (+1277.08%)
Mutual labels:  authentication, jwt, jwt-authentication
Bottle Jwt
JWT Authentication Plugin for bottle.py applications.
Stars: ✭ 30 (-37.5%)
Mutual labels:  authentication, jwt, jwt-authentication
Devise Jwt
JWT token authentication with devise and rails
Stars: ✭ 881 (+1735.42%)
Mutual labels:  authentication, jwt
Access
Ponzu Addon to manage API access grants and tokens for authentication
Stars: ✭ 13 (-72.92%)
Mutual labels:  authentication, jwt
Express Starter
Express Starter
Stars: ✭ 14 (-70.83%)
Mutual labels:  postgresql, jwt-authentication
Loggedin Mixin
A simple logged-in and roles check minxin to use with mdg:validated-method package
Stars: ✭ 20 (-58.33%)
Mutual labels:  authentication, roles
Express Boilerplate
🚀 Starter project for a RESTful API in Node with Express & mongoose component-based
Stars: ✭ 9 (-81.25%)
Mutual labels:  jwt-authentication, boilerplate
Guardian auth
The Guardian Authentication Implementation Using Ecto/Postgresql Elixir Phoenix [ User Authentication ]
Stars: ✭ 15 (-68.75%)
Mutual labels:  postgresql, authentication
Hapi Auth Keycloak
JSON Web Token based Authentication powered by Keycloak
Stars: ✭ 29 (-39.58%)
Mutual labels:  authentication, jwt

Note: this repository is no longer maintained as all its features have been added to Hasura Backend Plus

Authentication with JWT, Hasura claims and multiple roles

This is a sample auth JWT service for authenticating requests to the Hasura GraphQL Engine. This also exposes login and signup endpoints. Note that this repository can also be used in webhook mode in using the /webhook endpoint. The specifics of this repository is that it maps a user_role table to generate x-hasura-allowed-roles in the JWT claim so multiple roles can work with the Hasura Grapqh Engine as a backend of the application.

The endpoints to manage users are very limited (it is only possible to create a new user through the /signup endpoint). This is kind of a choice as this service is meant to be used for authentication only. The user and roles management can be done through the Hasura Graphql Engine or any other service accessing to the same database.

Rationale

See this issue.

Database schema

Three tables are used:

  • user:
    • id: UUID. Primary key. Automatically generated.
    • username: String. Unique user identifier.
    • password: String. Hashed with bcrypt.
    • active: Boolean. If not active, not possible to connect with this user.
  • role:
    • id: UUID. Primary key. Automatically generated.
    • name: String. Unique role identifier.
  • user_role:
    • id: UUID. Primary key. Automatically generated.
    • role_id: UUID. Foreign key that references the id of the role table.
    • user_id: UUID. Foreign key that references the id of the user table.

Prerequisites

  • PostgreSQL
  • Node.js 8.9+

Getting Started

Environment variables

Note: you can find examples of RSA keys in the repository. DO NOT USE THEM FOR PRODUCTION!

  • AUTH_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nypPTIfSzZ399o........"

    RSA private key used to sign the JWT. You need to escape the lines with "\n" in the variable. If the variable is not set, it will try to use the private.pem file.

  • AUTH_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\nV02/4RJi........"

    RSA private key used to deliver the JWK set. You need to escape the lines with "\n" in the variable. Please not that this feature is not working yet. If the variable is not set, it will try to use the public.pem file.

  • AUTH_KEY_ID="<unique-id-for-this-key>"

    Used to identify the key currently used to sign the tokens. If the variable is not set, a hash string will be generated from the public key and used instead.

  • DATABASE_URL=postgres://<username>:<password>@<host>:<port>/<database_name>

    URL to connect to the Postgres database. The format is . For instance: DATABASE_URL=postgres://postgres:@localhost:5432/postgres

  • PORT=8080

The port the server will listen to.

Build and deploy on Docker (production)

First you need to build the image and to tag it:

docker build . -t platyplus/authentication:latest

TODO: document on how to deploy on docker.

You can also have a look at this docker-compose gist to see how I use this service in a docker stack with Hasura and Traefik.

Deploy locally (developpment)

# Clone the repo
git clone https://github.com/platyplus/authentication-server

# Change directory
cd authentication-server

# Install NPM dependencies
npm install

# Generate the RSA keys
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout > public.pem

# print the keys in an escaped format
awk -v ORS='\\n' '1' private.pem
awk -v ORS='\\n' '1' public.pem

export DATABASE_URL=postgres://postgres:@localhost:5432/postgres

# Apply migrations
# (Note) this step creates tables "users", "roles" and "user_roles" in the database
knex migrate:latest

# Then simply start your app
npm start

Configure the Hasura GraphQL Engine

Run the Hasura GraphQL engine with HASURA_GRAPHQL_JWT_SECRET set like this:

{ "type": "RS256", "key": "<AUTH_PUBLIC_KEY>" }

Where <AUTH_PUBLIC_KEY> is your RSA public key in PEM format, with the line breaks escaped with "\n".

You can also configure the server in JWKS mode and set HASURA_GRAPHQL_JWT_SECRET like this:

{ "type": "RS256", "jwk_url": "hostname:port/jwks" }

More information in the Hasura documentation.

Usage

Signup

Once deployed or started locally, we can create an user using /signup API like below:

curl -H "Content-Type: application/json" \
     -d'{"username": "test123", "password": "test123", "confirmPassword": "test123"}' \
     http://localhost:8080/signup

On success, we get the response:

{
  "id": "907f0dc7-6887-4232-8b6e-da3d5908f137",
  "username": "test123",
  "roles": ["user"],
  "token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoicGlsb3UiLCJodHRwczovL2hhc3VyYS5pby9qd3QvY2xhaW1zIjp7IngtaGFzdXJhLWFsbG93ZWQtcm9sZXMiOlsibWFuYWdlciIsInVzZXIiXSwieC1oYXN1cmEtZGVmYXVsdC1yb2xlIjoidXNlciIsIngtaGFzdXJhLXVzZXItaWQiOiI5MDdmMGRjNy02ODg3LTQyMzItOGI2ZS1kYTNkNTkwOGYxMzcifSwiaWF0IjoxNTQ4OTI5MTY2LCJleHAiOjE1NTE1MjExNjYsInN1YiI6IjkwN2YwZGM3LTY4ODctNDIzMi04YjZlLWRhM2Q1OTA4ZjEzNyJ9.hoY-lZ-6rbN_WVFy0Taxbf6QCtDPaTm407l6opv2bz-Hui9T7l7aafStsx9w-UscWUFWHpeStIo1ObV-lT8-j9t-nw9q5fr8wuO2zyKBMXjhD57ykR6BcKvJQMxE1JjyetVLHpj5r4mIb7_kaA8Dj8Vy2yrWFReHXDczYpQGc43mxxC05B5_xdScQrSbs9MkgQRh-Z5EknlLKWkpbuxPvoyWcH1wgLum7UABGNO7drvmcDDaRk6Lt99A3t40sod9mJ3H9UqdooLOfBAg9kcaCSgqWDkmCLBwtM8ONbKZ4cEZ8NEseCQYKqIoyHQH9vbf9Y6GBaJVbBoEay1cI48Hig"
}

Login

Let's use the /login endpoint to fetch the user information and JWT:

curl -H "Content-Type: application/json" \
     -d'{"username": "test123", "password": "test123"}' \
     http://localhost:8080/login

It will then send back user information including the JWT in the same format as the above /signup endoint.

You can use this boilerplate as a webhook server in using the /webhook endpoint to fetch a webhook token:

curl -H "Content-Type: application/json" \
     -d'{"username": "test123", "password": "test123"}' \
     http://localhost:8080/login

Limitations

  • Not tested with Heroku
  • There is no user and role management except to create a single user with no specific role. I myself do this part with a frontend app that access the database through a Hasura GraphQL endpoint.
  • The JWKS endpoint /jwks is not working, I could not find a way to format the modulus (n) part of the JWK that is read by the Hasura graphql-engine without error. A contribution would be much appreciated!
  • This server is designed to work with one RSA key only, and does not handle its regular rotation.
  • No handling of JWT expiration and key turnover.
  • This server is not (yet?) designed to handle authentication through other services such as Google, Github... It would be nice to do so, but to keep this server as a proxy that would add the Hasura claims in querying the database about the roles of the user. Comments or any contribution are welcome as well on this one.
  • No automated tests.
  • another cool feature to be would be to expose the endpoints through hasura remote schema, and not directly to the client

Contributions are welcome!

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].