【发布时间】:2021-09-26 20:37:05
【问题描述】:
我有以下设置:
- 带有基于通用 oauth (https://grafana.com/docs/grafana/latest/auth/generic-oauth/) 的身份验证的 grafana,
- 身份服务器4,
- .net 核心 API。
我想为 grafana 开发自定义前端面板插件,该插件使用登录的用户凭据来授权对我的 API 的调用。根据文档,Grafana 将令牌存储在名为 grafana_session (https://grafana.com/docs/grafana/latest/administration/configuration/#login_cookie_name) 的 cookie 中。此外,with credentials 标志设置为include cookie 与调用 (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#sending_a_request_with_credentials_included)。
这是我的 identityserver4 客户端配置:
// Grafana client
new Client
{
ClientId = "grafana_client_id",
AllowedGrantTypes = GrantTypes.Code,
ClientSecrets = { new Secret("grafana_client_secret".Sha256()) },
AllowedScopes = { "openid", "profile", "email", "grafana_role" },
RedirectUris = { "https://localhost:3000/login/generic_oauth" },
RequirePkce = false,
},
// API
new Client
{
ClientId = "myAPI_id",
AllowedGrantTypes = GrantTypes.Code,
ClientSecrets = { new Secret("myAPI_secret".Sha256()) },
AllowedScopes = { "openid", "profile", "email", "myAPI" },
RedirectUris = { "https://localhost:5005/signin-oidc" },
RequirePkce = false,
},
API 端的身份验证设置:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie()
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "https://localhost:5001";
options.ClientId = "myAPI_id";
options.ClientSecret = "myAPI_secret";
options.ResponseType = "code";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("myAPI");
});
我可以使用 grafana 授权用户(id_token 保存在 cookie 中),但是当从浏览器调用 API 时,/connect/authorize 会再次使用 API 客户端的设置调用(使用 ClientId = myAPI_id 的那个)。然后,没有任何事情发生,API 端点没有被调用(看起来 API 正在等待用户再次登录)。是否可以设置身份验证以使用 Grafana 令牌来授权对我的 API 的调用?任何帮助将不胜感激。
【问题讨论】:
-
只是为了澄清 - 我无法获得 IS 生成的
access_token并且保存在 cookie 中的令牌不是id_token而是 Grafana 间隔身份验证令牌。
标签: .net-core oauth identityserver4 openid-connect grafana