2个不稳定版本
0.2.0 | 2022年7月14日 |
---|---|
0.1.0 | 2021年8月23日 |
#528 in HTTP服务器
55KB
1K SLoC
用于包装Shibboleth IdP的OAuth2服务器
这是一个创建OAuth2服务器(身份提供者)的服务器。它假定它运行在受保护的资源后面(例如,通过在Apache 2服务器中使用AuthType shibboleth来保护它)。作为HTTP头部变量传递的Shibboleth/SAML元数据字段(如X-Remote-User
)可以映射到JWT令牌属性。
背景
当需要使用机构Shibboleth身份提供者(如DFN AAI)时,该项目将被用作ANNIS前端的身份提供者。
安装和配置
配置Apache2与Shibboleth
按照Shibboleth Wiki中的指南之一(如Shibboleth Wiki)配置Apache,以使用Shibboleth保护的地址。这个受保护的地址必须转发到我们将要安装的实际Web服务。
<Location /login>
# Proxy all requests to /login to our service at port 8020
ProxyPass http://localhost:8020
ProxyPassReverse http://localhost:8020
</Location>
<Location /login/authorize>
AuthType shibboleth
ShibRequestSetting requireSession true
<RequireAll>
Require shib-session
# Add more conditions on the user here
</RequireAll>
# This is important since we want to use the forwarded headers
ShibUseHeaders On
</Location>
安装服务二进制文件
- 要安装此二进制文件作为服务,您需要一个工作的Rust编译器环境,可以使用https://rustup.rs/安装
- 使用以下命令编译二进制文件:
cargo install shibboleth-oauth2-forwarding
, - 将生成的二进制文件复制到系统级二进制文件夹
cp ~/.cargo/bin/shibboleth-oauth2-forwarding /usr/local/bin/
- 对于基于systemd的Linux服务器(如Ubuntu 18.04),在
/etc/systemd/system
目录中创建一个以.service
后缀的服务单元定义文件。此文件可能如下所示。同时,请确保选择一个用户(这里为youruser
),此服务应以该用户身份运行。
[Unit]
Description=Authorization token wrapper for ANNIS
[Service]
Type=simple
ExecStart=/usr/local/bin/shibboleth-oauth2-forwarding -c /usr/local/etc/shibboleth-oauth2-forwarding.toml
User=youruser
Group=youruser
WorkingDirectory=/usr/local/
[Install]
WantedBy=multi-user.target
执行
systemctl daemon-reload
使新文件对系统可见。
对于非systemd基于的服务器,请使用操作系统手册来定义相应的服务。
配置文件
在前一个服务定义中,使用了 /usr/local/etc/shibboleth-oauth2-forwarding.toml
文件作为配置文件。您可以从 examples/
文件夹中复制一个示例文件,并根据您的需求进行调整。我们使用TOML文件,其语法在https://toml.cn/有文档说明。
[bind]
# Define the port to use for the service
port = 8020
[mapping]
# List all headers that should be forwared from Apache2 to the
include_headers = ["x-admin"]
# Path to the template file that is used to generate the JWT tokens
token_template = "<path-to-template-file>"
# The default value for the "sub" field
default_sub = "academic"
[client]
# Define the OAuth2 client ID
id = "Shibboleth"
# A valid redirect URI
redirect_uri = "https://youapplicationserver/appcontext/"
[client.token_verification]
# Define a secret to be shared between identity provider and service consuming the JWT token
type = "HS256"
secret = "random-words-are-not-secure-please-change-me"
# Alternativly, you can use a private/public key approach
# type = "RS256"
# private_key = "yourprivatekey"
# public_key = "yourpublikey"
令牌模板
JWT令牌是通过模板文件创建的,该模板文件在配置文件的mapping
部分的token_template
字段中给出。我们使用模板语言Handlebars来包含动态内容,如用户名(以sub
变量给出)。此外,可以在JWT令牌定义中使用在配置变量的include_header
字段中定义的所有转发头。
{
"sub": "{{sub}}",
"exp": {{exp}},
{{#if x-admin}}
"https://corpus-tools.org/annis/roles": ["admin"],
{{/if}}
"https://corpus-tools.org/annis/groups": ["academic"]
}
启动和测试服务
在安装了服务、创建了配置文件并保护了/login
路径后,您应该能够启动新定义的服务。如果服务单元文件名为shib-wrapper.service
,您可以在每次启动时使用以下命令启动和启用服务:
systemctl enable shib-wrapper.service
systemctl start shib-wrapper.service
配置应用程序以使用此OAuth2身份提供者
如果您的应用程序使用Spring Security(例如ANNIS),您可以在应用程序属性中这样配置此OAuth2服务的端点:
spring.security.oauth2.client.registration.shib.client-id=Shibboleth
spring.security.oauth2.client.registration.shib.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.shib.redirect-uri=https://youapplicationserver/appcontext/login/oauth2/code/shib
spring.security.oauth2.client.provider.shib.authorization-uri=https://yourserver/login/authorize
spring.security.oauth2.client.provider.shib.token-uri=https://yourserver/login/authorize/token
spring.security.oauth2.client.provider.shib.user-info-urihttps://yourserver/login/userinfo
spring.security.oauth2.client.provider.shib.user-name-attribute=sub
第三方依赖项
此软件依赖于几个第三方库。这些在文件夹中的"third-party-licenses.html"文件中有文档说明。
依赖关系
~29–43MB
~883K SLoC