Coder Social home page Coder Social logo

ms-users-haproxy's Introduction

HAPROXY JWT verification helper

Provides JWT token verification script for HAProxy and Service that performs token validation.

Contents

Supported algorithms

  • HMAC{any} - secret based signature verification
  • RS/HS/ES{any} - public key verification

Verification process

  1. Verify the signature of the provided JWT token using the provided list of keys. Supports only Authorization: JWT {token} headers

  2. Verify the token payload and expiration using the token-server sidecar.

Token Server

Provides caching and validation logic for the token verification process. Built on top of the Microfleet,makeomatic/ms-users and Fastify. Used by the HaProxy LUA script as backend.

Installation

Build and deploy container image from Dockerfile.token-server file. Configure HaProxy backend to monitor and use this server.

$@>: docker build -f ./Dockerfile.token-server -t token-server .

See src/config or test/config and consult https://github.com/microfleet/core for configuration examples.

HaProxy verify-jwt script

Installation

Manual

Copy ./src/lua contents into /usr/local/lib/lua/5.3/ and install dependencies.

Dependencies

> luarocks install lua-cjson 2.1.0-1;
> luarocks install LuaSocket;
> luarocks install luaossl;

DOCKER image

Or just use Dockerfile to docker build self-contained haproxy image.

$@>: docker build -f ./Dockerfile -t haproxy-jwt .

Configuration

  • JWT_JWKS_FILE - File that contains JWT verification keys.
  • JWT_JWKS_URL - Url of the file that contains JWT verification keys.
  • JWT_SYNC_INTERVAL - Seconds interval for the verification keys update.
  • JWT_CACHE_TTL - Seconds to cache token-server response.
  • JWT_TOKEN_SERVER_BACKEND - Backend that monitors token-servers and to resolve DNS to IP address.
Sample haproxy.cfg:
global
  setenv JWT_JWKS_FILE /usr/local/etc/haproxy/keys.json
  setenv JWT_JWKS_URL http://host/keys.json
  setenv JWT_SYNC_INTERVAL 400
  setenv JWT_CACHE_TTL 3
  setenv JWT_TOKEN_SERVER_BACKEND jwt-token-server

  lua-load /usr/local/lib/lua/5.3/verify-jwt.lua

listen fe_main
    bind :8080
    http-request lua.verify-jwt
    # ...

backend jwt-token-server
  # server s1 host.docker.internal:4000 check
  server s1 tester:4000 check inter 1s fall 5 rise 1

JWT Signing Keys

Set JWT_JWKS_FILE or JWT_JWKS_URL source. JWT signature verification keys should be provided using specific structure:

// keys.json
[
  {
    "kid": "keyID",
    "secret": "your-super-secure-secret" 
  },
  {
    "kid": "keyID",
    "cert": "contents of the public key" 
  }
]

Request Headers and TXN vars

Script appends additional request headers after token validation process:

And additional variables added to the HaProxy TXN scope:

  • txn.tkn.valid
  • txn.tkn.reason
  • txn.tkn.payload.*
  • txn.tkn.stateless
  • txn.tkn.legacy

ms-users-haproxy's People

Contributors

pajgo avatar semantic-release-bot avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar Alexey Khoroshev avatar bludzhd avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.