lua-resty-tinyauth

一个用于 ngx_lua 和 OpenResty 的 tinyauth 客户端

$ opm get tinyauth/lua-resty-tinyauth

lua-resty-tinyauth

用于在 OpenResty/ngx_lua 上使用 tinyauth 授权操作的 Lua 客户端。

状态

Alpha。

功能

  • 基于用户和组的 ACL,适用于任何可以通过 nginx 代理的服务

  • 使用 URL 映射将您的 API 分解成多个不同的操作,以实现细粒度的权限控制。

概要

`` lua lua_package_path "/path/to/lua-resty-microauth/lib/?.lua;;";

server { location / { access_by_lua_block { local tinyauth = require('resty/tinyauth'); local client = tinyauth.new("http://tinyauth:5000/api/v1/", "gatekeeper", "keymaster")

      client:authorize_token_for_url({
        {"/ip",         {"GET"},  "GetOriginIp"},
        {"/stream/.*",  {"GET"},  "StreamLines"},
      }, "ProxyRequest")
    }

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass   http://app;
}
}
C<>``

连接

new

语法:local client = tinyauth.new()

创建 tinyauth 对象。

authorize_token_for_action

语法:client:authorize_token_for_action(action)

所有受到此授权影响的流量将被视为单一类型的操作。如果您有权访问 API 或没有访问权,这将很有用。

authorize_token_for_url

语法:client:authorize_token_for_url(uri_map)

语法:client:authorize_token_for_url(uri_map, default)

流量 URI 在表中查找,以确定在使用 tinyauth 授权时要使用哪个操作。

如果没有找到匹配项并且未设置默认值,则访问尝试将被禁止。

如果没有找到匹配项并且设置了默认值,则访问尝试将使用默认操作进行授权。

uri_map 是一个 'route' 数组。如果一个路由匹配,我们就知道在查询 tinyauth 时要将它与哪个操作关联。路由可能看起来像

    local routes = {
      {"/ip",       {"GET"},    "GetIp"},
      {"/gzip",     {"GET"},    "GetGzippedData"},
      {"/stream/.*", {"GET"},   "StreamLines"},
    }

authorize_login_for_action

语法:client:authorize_login_for_action(action)

所有受到此授权影响的流量将被视为单一类型的操作。如果您有权访问 API 或没有访问权,这将很有用。

authorize_login_for_url

语法:client:authorize_login_for_url(uri_map)

语法:client:authorize_login_for_url(uri_map, default)

流量 URI 在表中查找,以确定在使用 tinyauth 授权时要使用哪个操作。

如果没有找到匹配项并且未设置默认值,则访问尝试将被禁止。

如果没有找到匹配项并且设置了默认值,则访问尝试将使用默认操作进行授权。

uri_map 是一个 'route' 数组。如果一个路由匹配,我们就知道在查询 tinyauth 时要将它与哪个操作关联。路由可能看起来像

    local routes = {
      {"/ip",       {"GET"},    "GetIp"},
      {"/gzip",     {"GET"},    "GetGzippedData"},
      {"/stream/.*", {"GET"},   "StreamLines"},
    }

使用这些路由,当用户访问 /stream/99 时,我们可以将其映射到 StreamLines,并询问 tinyauth 我们是否可以验证该用户执行 StreamLines 操作。

查看示例

此仓库包含一个使用 tinyauth 来保护一项关键任务服务的示例:httpbin

docker compose 配置将部署在 gunicorn 上运行的 httpbin,前面有一个 openresty 反向代理。lua-resty-tinyauth 将在代理中运行,并通过与简单的 tinyauth 设置(postgres + tinyauth)通信来提供授权。

在终端中

    $ docker-compose build
    $ docker-compose up

这将在前台运行演示。在另一个终端窗口中

    $ docker-compose run --rm tinyauth tinyauth db upgrade
    Starting luarestytinyauth_postgres_1 ... done
    Postgres is up - continuing...
    INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
    INFO  [alembic.runtime.migration] Will assume transactional DDL.
    INFO  [alembic.runtime.migration] Running upgrade  -> 276ef161b610, empty message
    INFO  [alembic.runtime.migration] Running upgrade 276ef161b610 -> 0d42398b4cdc, empty message
    INFO  [alembic.runtime.migration] Running upgrade 0d42398b4cdc -> 0d0c426e7b01, empty message
    INFO  [alembic.runtime.migration] Running upgrade 0d0c426e7b01 -> 7db3f3cca1a9, empty message
    INFO  [alembic.runtime.migration] Running upgrade 7db3f3cca1a9 -> 9efdb5e1f6fb, empty message
    
    $ docker-compose run --rm tinyauth tinyauth createdevuser
    Starting luarestytinyauth_postgres_1 ... done
    Postgres is up - continuing...
    'root' account created
    
    $ curl -u admin:admin http://localhost:80/ip
    <html>
    <head><title>403 Forbidden</title></head>
    <body bgcolor="white">
    <center><h1>403 Forbidden</h1></center>
    <hr><center>openresty/1.11.2.5</center>
    </body>
    </html>
    
    $ curl -u gatekeeper:keymaster http://localhost:80/ip
    {
      "origin": "172.29.0.1"
    }

作者

John Carr (Jc2k)

许可证

apache2

依赖项

版本