Giter VIP home page Giter VIP logo

curityio / nginx-lua-oauth-proxy-plugin Goto Github PK

View Code? Open in Web Editor NEW
11.0 5.0 2.0 206 KB

A LUA plugin for getting access tokens from encrypted cookies. The plugin implements the OAuth Proxy component from the Token Handler pattern.

Home Page: https://curity.io/resources/learn/oauth-proxy-plugin-lua/

License: Apache License 2.0

Lua 51.53% Dockerfile 1.93% JavaScript 5.86% Shell 40.68%
api-gateway token-handler oauth2 nginx oauth-proxy

nginx-lua-oauth-proxy-plugin's Introduction

OAuth Proxy Plugin for NGINX LUA Systems

Quality Availability

A LUA plugin that is used during API calls from SPA clients, to forward JWTs to APIs.
This is part of a Backend for Frontend solution for SPAs, in line with best practices for browser based apps.

The Token Handler Pattern

The Token Handler Pattern is a modern evolution of a Backend for Frontend approach.
The SPA uses only SameSite encrypted HTTP Only cookies in the browser, and sends them during API requests.
This plugin performs the role of an OAuth Proxy in this solution, to make API calls work seamlessly:

Logical Components

The plugin translates from encrypted cookies to tokens, so that APIs receive JWTs in the standard way.
See the Curity OAuth for Web Home Page for further details on this pattern.

Components

The plugin can be used standalone, or in conjunction with the Phantom Token Plugin:

API Flow

See also the following resources:

Installation

Kong API Gateway

If you are using luarocks, execute the following command to install the plugin:

luarocks install kong-oauth-proxy 1.3.0

Or deploy the .lua files into Kong's plugin directory, eg /usr/local/share/lua/5.1/kong/plugins/oauth-proxy.

OpenResty

If you are using luarocks, execute the following command to install the plugin:

luarocks install lua-resty-oauth-proxy 1.3.0

Or deploy the access.lua file to resty/oauth-proxy.lua, where the resty folder is in the lua_package_path.
A typical install location for LUA files is at /usr/local/openresty/luajit/share/lua/5.1/resty.

Required Configuration Directives

All of the settings in this section are required:

cookie_name_prefix

Syntax: cookie_name_prefix string

Context: location

The prefix used in the SPA's cookie name, typically representing a company or product name.
The value supplied must not be empty, and example would lead to full cookie names such as example-at.

encryption_key

Syntax: encryption_key string

Context: location

This must be a 32 byte encryption key expressed as 64 hex characters.
It is used to decrypt AES256 encrypted secure cookies.
The key is initially generated with a tool such as openssl, as explained in Curity tutorials.

trusted_web_origins

Syntax: trusted_web_origins string[]

Context: location

A whitelist of at least one web origin from which the plugin will accept requests.
Multiple origins could be used in special cases where cookies are shared across subdomains.

cors_enabled

Syntax: cors_enabled boolean

Default: true

Context: location

When enabled, the OAuth proxy returns CORS response headers on behalf of the API.
When an origin header is received that is in the trusted_web_origins whitelist, response headers are written.
The access-control-allow-origin header is returned, so that the SPA can call the API.
The access-control-allow-credentials header is returned, so that the SPA can send secured cookies to the API.

Optional Configuration Directives

allow_tokens

Syntax: allow_tokens boolean

Default: false

Context: location

If set to true, then requests that already have a bearer token are passed straight through to APIs.
This can be useful when web and mobile clients share the same API routes.

remove_cookie_headers

Syntax: remove_cookie_headers boolean

Default: true

Context: location

If set to true, then cookie and CSRF headers are not forwarded to APIs.
This provides cleaner requests to APIs, which only receive a JWT in the HTTP Authorization header.

cors_allow_methods

Syntax: cors_allow_methods string[]

Default: ['OPTIONS', 'GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE']

Context: location

When CORS is enabled, these values are returned in the access-control-allow-methods response header.
The SPA is then allowed to call a particular API endpoint with those HTTP methods (eg GET, POST).
A '*' wildcard value should not be configured here, since it will not work with credentialed requests.

cors_allow_headers

Syntax: cors_allow_headers string[]

Default: ['x-example-csrf']

Context: location

When CORS is enabled, the plugin returns these values in the access-contol-allow-headers response header.
Include here any additional non-safelisted request headers that the SPA needs to send in API requests.
To implement data changing requests, include the CSRF request header name, eg x-example-csrf.
A '*' wildcard value should not be configured here, since it will not work with credentialed requests.

cors_expose_headers

Syntax: cors_expose_headers string[]

Default: []

Context: location

When CORS is enabled, the plugin returns these values in the access-contol-expose-headers response header.
Include here any additional non-safelisted response headers that the SPA needs to read from API responses.
A '*' wildcard value should not be configured here, since it will not work with credentialed requests.

cors_max_age

Syntax: cors_max_age number

Default: 86400

Context: location

When CORS is enabled, the plugin returns this value in the access-contol-max-age response header.
When a value is configured, this prevents excessive pre-flight OPTIONS requests to improve efficiency.

Example Configurations

Standard settings would be expressed similar to the following if expressed in an nginx configuration file:

local config = {
    cookie_name_prefix = 'example',
    encryption_key = '4e4636356d65563e4c73233847503e3b21436e6f7629724950526f4b5e2e4e50',
    trusted_web_origins = {
        'http://www.example.com'
    },
    cors_enabled = true
}

The equivalent Kong configuration is expressed via YAML when using declarative configuration:

plugins:
  - name: oauth-proxy
    config:
      cookie_name_prefix: example
      encryption_key: 4e4636356d65563e4c73233847503e3b21436e6f7629724950526f4b5e2e4e50
      trusted_web_origins:
      - http://www.example.com
      cors_enabled: true

All API endpoints will then return these CORS headers to browsers in response headers:

access-control-allow-origin: http://www.example.com
access-control-allow-credentials: true
access-control-allow-methods: OPTIONS,HEAD,GET,POST,PUT,PATCH,DELETE
access-control-allow-headers: x-example-csrf
access-control-max-age: 86400

If you prefer you can override default settings:

local config = {
    cookie_name_prefix = 'example',
    encryption_key = '4e4636356d65563e4c73233847503e3b21436e6f7629724950526f4b5e2e4e50',
    trusted_web_origins = {
        'http://www.example.com'
    },
    cors_enabled = true,
    allow_tokens = false,
    remove_cookie_headers = true,
    cors_allow_methods = {
        'OPTIONS', 'GET', 'POST'
    },
    cors_allow_headers = {
        'my-header',
        'x-example-csrf'
    },
    cors_expose_headers = {
    },
    cors_max_age = 600
}

Or in Kong this would be configured like this:

plugins:
  - name: oauth-proxy
    config:
      cookie_name_prefix: example
      encryption_key: 4e4636356d65563e4c73233847503e3b21436e6f7629724950526f4b5e2e4e50
      trusted_web_origins:
      - http://www.example.com
      cors_enabled: true
      allow_tokens: false
      remove_cookie_headers: true
      cors_allow_methods:
      - OPTIONS
      - GET
      - POST
      cors_allow_headers:
      - my-header
      - x-example-csrf
      cors_expose_headers: []
      cors_max_age: 600

If you prefer you can configure cors_enabled=false, in which case you'll need to handle CORS in your API.

Deployment

The example Docker Compose File provides OpenResty and Kong deployment examples.

Development and Testing

The following resources provide further details on how to make code changes to this repo:

More Information

Please visit curity.io for more information about the Curity Identity Server.

nginx-lua-oauth-proxy-plugin's People

Contributors

gary-archer avatar mtrojanowski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

nginx-lua-oauth-proxy-plugin's Issues

Add support for Kong 3.0

Kong has always had particular requirements around plugins, and they have changed for the 3.0 release.

Firstly use of a base plugin is no longer supported:
https://docs.konghq.com/gateway/2.8.x/plugin-development/custom-logic/#migrating-from-baseplugin-module

Secondly, in the handler class you must indicate the VERSION property:

local TokenHandler = {
    PRIORITY = 2000,
    VERSION = "1.3.0",
}

Do some minor updates to ensure that this plugin works in both Kong 2.x and Kong 3.x.
#13

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.