Code Monkey home page Code Monkey logo

jwt_auth's Introduction

JWT Authentication for K8s ingress

Simple external authentication microservice for a Nginx. Implements JWT with RS256 algorithm

This application uses PyJwt to validate signature https://pyjwt.readthedocs.io/en/latest/

This code was created for use with the NGINX Ingress Controller and Kubernates Ingress Controller not tested with other controllers.

TODO: Helm chart

Http server expects auth token in the "Authorization: Bearer {JWT}" header service decodes claims and sends extended headers in format

X-JWT-{claim-key}: {claim-value}

so these claims are accessable via auth_request_set $jwt_claim_name1 $upstream_http_x_jwt_name1; in Nginx configuration file

Example of JWT-claim based routing in the Nginx-Ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    certmanager.k8s.io/cluster-issuer: letsencrypt-production
    kubernetes.io/ingress.class: nginx
    nginx.org/server-snippets: |
        resolver 10.0.0.10 ipv6=off;

        location = /_auth {
            internal;
            proxy_pass http://jwt.default.svc.cluster.local:8000;
            proxy_pass_request_body off;
            proxy_set_header  Content-Length "";
            proxy_set_header  X-Original-URI $request_uri;
        } 

        location ~ /(.*) {
            proxy_buffering off;
            proxy_connect_timeout 360s;
            proxy_read_timeout 360s;
            proxy_send_timeout 360s;
            client_max_body_size 10m;
            #proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            
            auth_request /_auth;
            auth_request_set $jwt_claim_tenant $upstream_http_x_jwt_tenant;
            proxy_pass "http://$jwt_claim_tenant-$1.default.svc.cluster.local:8080/";
            }
  name: example
  
spec:
  rules:
  - host: domain.example.com
    http:
      paths:
      - backend:
          serviceName: unsecure
          servicePort: 8000
        path: /unsecure
  tls:
  - hosts:
    - domain.example.com
    secretName: tls-secret

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.