使用cURL使用用户名和密码登录到Gitlab

迈克尔·里斯

为了针对Docker中的Gitlab实例测试命令行工具,我想使用用户名和密码登录到Gitlab,并抓住创建的会话来认证我的API请求。

因此,我执行以下操作:

  1. 卷曲用户登录页面 curl -i http://localhost:8080/users/sign_in -s
  2. _gitlab_session从标题获取我
  3. authenticity_token从登录表单获取我的信息
  4. 发送第二个卷曲请求

curl 'http://localhost:8080/users/sign_in' \
  -H "_gitlab_session=${cookie}" \
  -H 'Origin: http://localhost:8080' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' \
  -H 'Referer: http://localhost:8080/users/sign_in' \
  -H 'Connection: keep-alive' \
  --data-urlencode "authenticity_token=${token}" \
  --data-urlencode "user[login]=root" \
  --data-urlencode "user[password]=12341234" \
  --data-urlencode "user[remember_me]=0"

但是,我没有一个有效的用户登录名,而是

422 - The change you requested was rejected.

在日志文件中,我看到了

==> gitlab-rails/production.log <==
Started POST "/users/sign_in" for 172.17.0.1 at 2017-12-23 00:22:16 +0000
Processing by SessionsController#create as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"login"=>"root", "password"=>"[FILTERED]", "remember_me"=>"0"}}
Can't verify CSRF token authenticity
Completed 422 Unprocessable Entity in 125ms (ActiveRecord: 7.8ms)

==> gitlab-rails/production_json.log <==
{"method":"POST","path":"/users/sign_in","format":"html","controller":"SessionsController",  
"action":"create","status":422,"error":" 
ActionController::InvalidAuthenticityToken:ActionController::InvalidAuthenticityToken",
"duration":126.29,"view":0.0,"db":7.78,"time":"2017-12-23T00:22:16.039Z",
"params":{"authenticity_token":"[FILTERED]","user":{"login":"root","password":"
[FILTERED]","remember_me":"0"}},"remote_ip":"172.17.0.1",
"user_id":1,"username":"root"}

==> gitlab-rails/production.log <==

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
  lib/gitlab/middleware/multipart.rb:93:in `call'
  lib/gitlab/request_profiler/middleware.rb:14:in `call'
  lib/gitlab/middleware/go.rb:18:in `call'
  lib/gitlab/etag_caching/middleware.rb:11:in `call'
  lib/gitlab/middleware/read_only.rb:31:in `call'
  lib/gitlab/request_context.rb:18:in `call'
  lib/gitlab/metrics/requests_rack_middleware.rb:27:in `call'

我假设我忘了在第二个请求中传递必要的成分-但我不知道是哪个。

迈克尔·里斯

通过其他答案和评论的一些有用技巧,我终于想到了以下解决方案:

gitlab_host="http://localhost:8080"
gitlab_user="root"
gitlab_password="12341234"

# curl for the login page to get a session cookie and the sources with the auth tokens
body_header=$(curl -c cookies.txt -i "${gitlab_host}/users/sign_in" -s)

# grep the auth token for the user login for
#   not sure whether another token on the page will work, too - there are 3 of them
csrf_token=$(echo $body_header | perl -ne 'print "$1\n" if /new_user.*?authenticity_token"[[:blank:]]value="(.+?)"/' | sed -n 1p)

# send login credentials with curl, using cookies and token from previous request
curl -b cookies.txt -c cookies.txt -i "${gitlab_host}/users/sign_in" \
    --data "user[login]=${gitlab_user}&user[password]=${gitlab_password}" \
    --data-urlencode "authenticity_token=${csrf_token}"

# send curl GET request to personal access token page to get auth token
body_header=$(curl -H 'user-agent: curl' -b cookies.txt -i "${gitlab_host}/profile/personal_access_tokens" -s)
csrf_token=$(echo $body_header | perl -ne 'print "$1\n" if /authenticity_token"[[:blank:]]value="(.+?)"/' | sed -n 1p)

# curl POST request to send the "generate personal access token form"
# the response will be a redirect, so we have to follow using `-L`
body_header=$(curl -L -b cookies.txt "${gitlab_host}/profile/personal_access_tokens" \
    --data-urlencode "authenticity_token=${csrf_token}" \
    --data 'personal_access_token[name]=golab-generated&personal_access_token[expires_at]=&personal_access_token[scopes][]=api')

# Scrape the personal access token from the response HTML
personal_access_token=$(echo $body_header | perl -ne 'print "$1\n" if /created-personal-access-token"[[:blank:]]value="(.+?)"/' | sed -n 1p)

根据GitLab API文档,您现在可以使用会话cookie来验证API请求:

curl --header "Private-Token: ${personal_access_token}" https://gitlab.example.com/api/v4/projects

一些提示:

  • 我首先对curl -c file(从标头读取cookie并将其写入文件)和curl -b file(使用文件中的cookie并随请求发送它们)感到困惑。
  • 不幸的是,我没有找到可以使用的正则表达式,sed因此必须在perl这里使用
  • 在Chrome中使用开发者控制台并将POST请求复制为curl命令非常有帮助:https : //www.alexkras.com/copy-any-api-call-as-curl-request-with-chrome-developer-tools/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

curl 使用用户名密码和登录按钮登录网站

使用帐户,用户名,密码登录到AWS Console

Amazon Cognito使用用户名和密码登录用户

Flutter和Firebase-如何仅使用用户名和密码登录

如何创建登录但使用用户名或电子邮件和密码

如何允许客户端使用用户名和密码登录Samba共享?

如何使用用户名和密码连接LDAP?

使用用户名和密码进行密钥验证

使用用户名和密码的Python github Pull

使用用户名和密码访问Kubernetes API

使用用户名和密码的OpenVPN

使用用户名和密码打开HTTP会话

使用用户名和密码连接到openvpn

sqlite连接使用用户名和密码

使用用户名和密码输入的JOptionPane

使用用户名和密码打开的网址

使用用户名和登录名的SFTP?

如何使用OAuth2和Microsoft登录名和HTTP请求使用用户名/密码登录

Excel VBA 用户表单在服务器上使用用户名和密码登录

如何识别用户名和密码字段以使用Selenium和Python登录到USPS.com

PHP登录“无效的用户名和密码”使用MySQL

不使用密码和用户名登录

如何在没有公钥的情况下登录ssh?仅使用用户名和密码

如何通过Windows登录名使用用户名和密码访问Outlook Exchange Server?

将JavaScript客户端添加到IdeniityServer4并使用用户名和密码登录

如何使我的Spring Boot应用程序使用给定的用户名和密码登录到keycloak?

我可以使用任何用户名和密码登录到Firebird 3数据库

使用用户名登录或使用Cakephp 3登录

Laravel Passport:仅使用用户名登录,无需密码