模块 ngx_http_oidc_module

配置示例
指令
     oidc_provider
     auth_oidc
     issuer
     client_id
     client_secret
     config_url
     cookie_name
     extra_auth_args
     redirect_uri
     scope
     session_store
     session_timeout
     ssl_crl
     ssl_trusted_certificate
内嵌变量

ngx_http_oidc_module 模块 (1.27.4) 作为 OpenID Connect 中的依赖方,使用 授权码流程 实现认证。

模块预期 OpenID 提供商的配置可通过 元数据 获取,并且需要动态解析器

该模块可以通过 satisfy 指令与其他访问模块结合使用。请注意,即使使用 satisfy any;,该模块仍可能阻止请求,因为 OpenID 提供商可能不会将用户重定向回 nginx。

此模块包含在我们的商业订阅中。

配置示例

http {
    resolver 10.0.0.1;

    oidc_provider my_idp {
        issuer        "https://provider.domain";
        client_id     "unique_id";
        client_secret "unique_secret";
    }

    server {
        location / {
            auth_oidc my_idp;

            proxy_set_header username $oidc_claim_sub;
            proxy_pass       http://backend;
        }
    }
}

此示例假定 OpenID 提供商端已配置重定向 URI “https://<nginx-host>/oidc_callback”。可以使用 redirect_uri 指令自定义此路径。

指令

语法 oidc_provider name { ... }
默认值
上下文 http

定义一个 OpenID 提供商,与 auth_oidc 指令一起使用。

语法 auth_oidc name | off;
默认值
auth_oidc off;
上下文 http, server, location

启用最终用户使用指定的 OpenID 提供商进行认证。

特殊值 off 取消从上一配置级别继承的 auth_oidc 指令的效果。

语法 issuer URL;
默认值
上下文 oidc_provider

设置 OpenID 提供商的颁发者标识符 URL;必需指令。此 URL 必须精确匹配 OpenID 提供商元数据中 “issuer” 的值,并且需要 “https” 方案。

语法 client_id string;
默认值
上下文 oidc_provider

指定依赖方的客户端 ID;必需指令。

语法 client_secret string;
默认值
上下文 oidc_provider

指定一个密钥值,用于依赖方与 OpenID 提供商进行认证。

语法 config_url URL;
默认值
config_url <issuer>/.well-known/openid-configuration;
上下文 oidc_provider

设置一个自定义 URL,用于获取 OpenID 提供商元数据。

语法 cookie_name name;
默认值
cookie_name NGX_OIDC_SESSION;
上下文 oidc_provider

设置会话 cookie 的名称。

语法 extra_auth_args string;
默认值
上下文 oidc_provider

设置认证请求 URL 的附加查询参数。

extra_auth_args "display=page&prompt=login";

语法 redirect_uri uri;
默认值
redirect_uri /oidc_callback;
上下文 oidc_provider

定义模块预期从 OpenID 提供商接收的认证后重定向的重定向 URI 路径。此 uri 必须与提供商端的配置匹配。

语法 scope scope ...;
默认值
scope openid;
上下文 oidc_provider

设置请求的范围。OIDC 始终需要 openid 范围。

语法 session_store name;
默认值
上下文 oidc_provider

指定一个自定义键值数据库,用于存储会话数据。默认情况下,会自动创建一个名为 oidc_default_store_<provider name> 的 8 兆字节键值数据库。

应为每个提供商配置一个单独的键值数据库,以防止会话在不同提供商之间重复使用。

语法 session_timeout time;
默认值
session_timeout 8h;
上下文 oidc_provider

设置会话被删除的超时时间,除非会话已被刷新

语法 ssl_crl file;
默认值
上下文 oidc_provider

指定一个包含 PEM 格式的已撤销证书列表 (CRL) 的 文件,用于验证 OpenID 提供商端点的证书。

语法 ssl_trusted_certificate file;
默认值
ssl_trusted_certificate system CA bundle;
上下文 oidc_provider

指定一个包含 PEM 格式的受信任 CA 证书的 文件,用于验证 OpenID 提供商端点的证书。

内嵌变量

ngx_http_oidc_module 模块支持内嵌变量

$oidc_id_token
ID 令牌
$oidc_access_token
访问令牌
$oidc_claim_name
顶级 ID 令牌声明

可以使用 auth_jwt 模块获取嵌套声明

http {
    auth_jwt_claim_set $postal_code address postal_code;

    server {
        location / {
            auth_oidc my_idp;
            auth_jwt  off token=$oidc_id_token;

            proxy_set_header x-postal_code $postal_code;
            proxy_pass       http://backend;
        }
    }
}