简体   繁体   English

使用 mod_auth_openidc 和 keycloak 进行 cURL 身份验证

[英]cURL authentication with mod_auth_openidc and keycloak

I have an authentication server running Keycloak, and a Apache2 webserver with mod_auth_openidc to do OAuth2 authorization.我有一个运行 Keycloak 的身份验证服务器和一个带有 mod_auth_openidc 的 Apache2 网络服务器来执行 OAuth2 授权。

With browsers, I can successfully intercept access to protected resource to redirect user to Keycloak login page.使用浏览器,我可以成功拦截对受保护资源的访问,以将用户重定向到 Keycloak 登录页面。 After successful login, user will be redirected to the resource link.登录成功后,用户将被重定向到资源链接。

With Postman, I can also retrieve access token JWT with password grant flow, then use the access token to access protected resource.使用 Postman,我还可以使用密码授予流程检索访问令牌 JWT,然后使用访问令牌访问受保护的资源。 The cURL code provided by postman indicates that the mod_auth_openidc_session cookie is also required. postman 提供的 cURL 代码表明还需要 mod_auth_openidc_session cookie。

Next, I try to do 2-stage cURL command in Linux CLI.接下来,我尝试在 Linux CLI 中执行 2-stage cURL 命令。

First I retrieve the access token using password grant flow as below.首先,我使用密码授予流程检索访问令牌,如下所示。 I initiated the cookie engine to capture session cookies given by mod_auth_openidc.我启动了 cookie 引擎来捕获 mod_auth_openidc 给出的会话 cookie。

# RETRIEVE ACCESS TOKEN JSON
curl -L -b ./cookie.jar -c ./cookie.jar -d 'client_id=CLIENT_ID' -d 'client_secret=368127b1-1ee0-4f3f-8429-29e9a93daf9a' -d 'username=USERNAME' -d 'password=PASSWORD' -d 'grant_type=password' 'https://AUTH_SERVER:PORT/auth/realms/REALM/protocol/openid-connect/token

# PARSE ACCESS TOKEN
access_token=`echo $response|jq '.access_token'|cut -d\" -f2`

Next, with the access token bearer in header and cookie jar file, I try to access to the protected resource.接下来,使用标头和 cookie jar 文件中的访问令牌承载,我尝试访问受保护的资源。

curl -b ./cookie.jar -c ./cookie.jar --insecure -L -X GET 'https://RESOURCE_SERVER:PORT/protected_content' --header "'Authorization: Bearer "$access_token"'"

However, I still got redirected to the Keycloak login page, and the session cookie is not recorded in the cookie jar file.但是,我仍然被重定向到 Keycloak 登录页面,并且会话 cookie 没有记录在 cookie jar 文件中。

Here is the recorded cookie jar file with sensitive info redacted/replaced.这是记录的 cookie jar 文件,其中敏感信息已编辑/替换。

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_[AUTH_SERVER]     FALSE   /auth/realms/master/    TRUE    0       KC_RESTART      eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI2Y2ZiNjYwOC1kMTlhLTQwZGUtOWJiYS04NzcxOTkzZTIwMWUifQ.eyJjaWQi[REDACTED]4aHhkVDBGZmhBZEVTSm8iLCJub25jZSI6IndTYXNYOWhGeGIxd1hKakNrS2FLMXVadVRGX3ZOZzRGVUZnMTJyYXFWbVkifX0.53645krpwlFnJ09cHAcZhNCci-DhGigu4soN5CVsZQ0
#HttpOnly_[AUTH_SERVER]     FALSE   /auth/realms/master/    TRUE    0       AUTH_SESSION_ID_LEGACY  6a23b139-05ba-4d22-b9e3-9ae857074814.[AUTH_SERVER]
#HttpOnly_[AUTH_SERVER]     FALSE   /auth/realms/master/    TRUE    0       AUTH_SESSION_ID 6a23b139-05ba-4d22-b9e3-9ae857074814.[AUTH_SERVER]
#HttpOnly_[RESOURCE_SERVER]     FALSE   /       TRUE    0       mod_auth_openidc_state_XGEq0YKJAwSt8hxdT0FfhAdESJo      NVc9Mk1FmN[REDACTED]lydKVtOw0iL-Y9iZMjzcUinutFPn74rmVvI_ERV3C8Wn1Euio8pID0jEAmu9NEfY_MEeuzOzqe6w7I20HZUNQHX0uh_vXR8

Can anyone tell me what I did wrong in the 2-stage cURL authentication/authorization process?谁能告诉我在 2 阶段 cURL 身份验证/授权过程中我做错了什么?

You have single quotes inside double quotes when setting Authorization header.设置Authorization标头时,双引号内有单引号。 This means instead of expect Authorization: Bearer token server is getting 'Authorization: Bearer token' .这意味着而不是期望Authorization: Bearer token服务器正在获取'Authorization: Bearer token' You can check the contents of your headers by using verbose -v option.您可以使用详细-v选项检查标题的内容。

Following command should work as expected:以下命令应按预期工作:

curl --header "Authorization: Bearer $access_token" -b ./cookie.jar -c ./cookie.jar --insecure -L -X GET https://RESOURCE_SERVER:PORT/protected_content' 

Optionally, if you need double qoutes around token use following:或者,如果您需要围绕令牌使用双引号,请使用以下内容:

--header "Authorization: Bearer \"$access_token\""

Side note: In order not to overwrite existing cookies in cookie.jar , use different file name to store cookies from the request to authorized file.旁注:为了不覆盖cookie.jar现有的 cookie,请使用不同的文件名来存储从请求到授权文件的 cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM