Ruby on rails 在Rails应用程序中使用非浏览器请求设计身份验证后命中web端点
如何在Rails应用程序中请求支持Desive身份验证的页面 5年前的建议是在中点击Ruby on rails 在Rails应用程序中使用非浏览器请求设计身份验证后命中web端点,ruby-on-rails,authentication,curl,devise,Ruby On Rails,Authentication,Curl,Devise,如何在Rails应用程序中请求支持Desive身份验证的页面 5年前的建议是在中点击/sign\u,然后在将来的请求中使用返回的访问令牌 但是,当我发出请求时,不会返回访问\u令牌 curl -XPOST -v -H 'Content-Type: application/json' http://localhost:3000/users/sign_in -d '{"email": "me@app.com", "password": &q
/sign\u,然后在将来的请求中使用返回的访问令牌
但是,当我发出请求时,不会返回访问\u令牌
curl -XPOST -v -H 'Content-Type: application/json' http://localhost:3000/users/sign_in -d '{"email": "me@app.com", "password": "12345678" }'
* upload completely sent off: 56 out of 56 bytes
< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Type: text/html; charset=utf-8
< ETag: W/"2477...80"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: 2351...ad
< X-Runtime: 0.785395
< Transfer-Encoding: chunked
响应:
响应是this,然后是通常返回的页面的html
curl -XPOST -v -H 'Content-Type: application/json' http://localhost:3000/users/sign_in -d '{"email": "me@app.com", "password": "12345678" }'
* upload completely sent off: 56 out of 56 bytes
< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Type: text/html; charset=utf-8
< ETag: W/"2477...80"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: 2351...ad
< X-Runtime: 0.785395
< Transfer-Encoding: chunked
*上传已完全发送:56个字节中的56个
但是我没有可以提出进一步要求的代币
我在2020年尝试的RubyonRails是否可行
或者有没有一种方法可以让它工作
更新:
这是我雇主的地盘。不幸的是不能分享
他们需要一种方法来测试新添加的路由背后的逻辑是否有效,而无需登录并通过UI单击
更新
我可以用curl、Postman(任何东西)等达到终点。一种方法是用水豚刮。你并没有说为什么要尝试curl,只是说“你的雇主想测试新添加路线背后的逻辑”。在这种情况下,您需要研究响应。水豚就是为了这个目的而设计的
Ofc你应该用单元测试来测试逻辑,但我不是来判断推理的
my_.rb
require 'capybara/dsl'
require 'webdrivers'
# Google chrome stable must be installed locally:
# https://linuxconfig.org/google-chrome-web-browser-installation-on-debian-9-stretch-linux
class MyScraper
include Capybara::DSL
attr_reader :domain, :email, :password
def initialize(email:, password:, domain: "http://localhost:3000")
@domain = domain
@email = email
@password = password
register_chrome_driver
Capybara.default_driver = Capybara.javascript_driver = :headless_chrome
Capybara.default_max_wait_time = 60
log_in
end
def log_in
visit "#{domain}/users/sign_in"
fill_in 'Email', with: email
fill_in 'Password', with: password
click_button 'Logg inn'
end
def test_urls
visit "#{domain}/some/url"
#check results. See the documentation
visit "#{domain}/some/url2"
visit "#{domain}/another/path"
# ...
end
# Source: https://gist.github.com/bbonamin/4b01be9ed5dd1bdaf909462ff4fdca95
def register_chrome_driver
options = ::Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--window-size=1920,1080')
Capybara.register_driver(:headless_chrome) do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
end
end
运行它
$ irb
load 'my_scraper.rb'
MyScraper.new(email: "me@app.com", password: "12345678").test_urls
Desive不支持基于令牌的即时身份验证(它从未支持)。它是基于会话(cookie)的。所以你期望你能卷曲并得到一个令牌,这可能是完全不现实的。给我们展示一个使用Desive的实时网站@max its cookies可能与CSRF令牌组合使用什么用例?这就是解决方案。如果是您自己的应用程序,那么ofc将添加一个令牌身份验证端点来设计(参见他们的文档)或简单的基本身份验证。如果它是另一个网站,你要寻找的是刮削技术。为此,您可以使用capybara和capybara.javascript\u driver=:headless\u chrome
@oma我在“更新”中添加了更多的上下文。谢谢