我在 NginX 代理管理器后面运行 Emby 服务器。尽管 Emby 不支持 SSO,但我仍然能够使用 URL 登录 Web UI:。schenme://emby.domain.com/web/index.html?userId=abc&accessToken=xxx&e=1
可以在 Authentik 中生成用于验证 Emby 的此 URI,并且可以在验证后传递该值。我需要在 NginX 中执行此操作的帮助。
我将写下我使用的步骤和方法,以防万一。
在 Authentik > 管理界面 > 目录 > 用户中:编辑所需用户以添加 emby 身份验证。只需在属性部分添加以下值:
emby_password: ****
emby_username: abc
在 Authentik > 管理界面 > 自定义 > 属性映射中创建一个新的范围映射。名称为“Emby Token”,范围名称为“ak_proxy”。表达式需要一个可以从 Emby UI 获取的 API Token。不要忘记编辑 URL,以便 Authentik 可以访问 Emby:
import json
from urllib.parse import urlencode
from urllib.request import Request, urlopen
if request.user.username == "":
return "null"
else:
embyuser = request.user.attributes.get("emby_username", "")
embypass = request.user.attributes.get("emby_password", "")
base_url = "http://embyserver:80"
end_point = "/Users/AuthenticateByName?api_key=xyz"
json_data = {'Username': embyuser,'Pw': embypass}
postdata = json.dumps(json_data).encode()
headers = {"Content-Type": "application/json; charset=UTF-8"}
try:
httprequest = Request(base_url + end_point, data=postdata, method="POST", headers=headers)
with urlopen(httprequest) as response:
responddata = json.loads(response.read().decode())
AccessToken = responddata['AccessToken']
UserId = responddata['User']['Id']
except:
AccessToken = "null"
UserId = "null"
return {"ak_proxy": {"user_attributes": {"additionalHeaders": {"X-Emby-Uri": "/web/index.html?userId=" + UserId + "&accessToken=" + AccessToken + "&e=1"}}}}
如果 Authentik 可以访问 Emby Server 并且正确提供了登录属性,则此范围应该返回/web/index.html?userId=abc&accessToken=xxx&e=1
可用于登录的内容。
之后,我创建了一个应用程序和提供程序,并将它们添加到 Outpost。身份验证按预期工作。尝试在 NginX 代理管理器中传递此值,如下所示,遗憾的是不起作用:
client_max_body_size 100M;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Sec-WebSocket-Extensions $http_sec_websocket_extensions;
proxy_set_header Sec-WebSocket-Key $http_sec_websocket_key;
proxy_set_header Sec-WebSocket-Version $http_sec_websocket_version;
#proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_redirect off;
proxy_buffering off;
location / {
proxy_pass $forward_scheme://$server:$port;
}
location /ssoauth {
proxy_set_header Upgrade $http_upgrade;
auth_request /outpost.goauthentik.io/auth/nginx;
error_page 401 = gnin;
auth_request_set $auth_cookie $upstream_http_set_cookie;
add_header Set-Cookie $auth_cookie;
auth_request_set $authentik_embyuri $upstream_http_x_emby_uri;
rewrite ^ $authentik_embyuri;
proxy_pass $forward_scheme://$server:$port/;
}
location /outpost.goauthentik.io {
proxy_pass https://authentik-server:9443/outpost.goauthentik.io;
proxy_set_header Host $host;
proxy_set_header X-Original-URL $scheme://$http_host$request_uri;
add_header Set-Cookie $auth_cookie;
auth_request_set $auth_cookie $upstream_http_set_cookie;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
}
location gnin {
internal;
add_header Set-Cookie $auth_cookie;
return 302 /outpost.goauthentik.io/start?rd=$request_uri;
}
通过转到schenme://emby.domain.com/ssoauth
,我被提升为使用 Authentik 登录。身份验证后,使用 加载登录 uri auth_request_set $authentik_embyuri $upstream_http_x_emby_uri;
。检查值并测试它,我能够登录。但是,将 uri 从/SSO auto
重写为$authentik_embyuri
使用rewrite ^ $authentik_embyuri;
不起作用。使用return
绕过 Authentik 登录过程,并且不会加载登录 uri。
我希望最终目标和方法是明确的。
问题仍然存在:我如何管理从NginX重定向/SSO auto
到的返回值?我可以通过覆盖标头来做到这一点吗(尝试过这个没有用)?$authentik_embyuri
Location