在 Rails 应用中使用非浏览器请求访问 Devise 身份验证背后的 Web 端点
Hit web endpoints behind Devise authentication with non-browser request in a Rails app
如何在 Rails 应用中请求 Devise 身份验证后的页面?
这个 5 年前的 建议点击 /sign_in
然后在以后的请求中使用返回的 access_token。
但是当我请求时,没有返回access_token
。
curl -XPOST -v -H 'Content-Type: application/json' http://localhost:3000/users/sign_in -d '{"email": "me@app.com", "password": "12345678" }'
响应:
响应是这样的,然后是 html 通常返回的页面。
* 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
但我没有可用于提出进一步请求的令牌。
我在 2020 年 Rails 上使用 Ruby 进行的尝试是否可行?
或者是否有破解设计使其工作的方法?
更新:
这是我雇主的网站。很遗憾无法分享。
他们想要一种方法来测试新添加路由背后的逻辑是否有效,而无需登录并单击 UI。
更新
我可以使用 curl、Postman、(任何)等方式访问端点
一种方法是用水豚抓取。你还没有说你为什么尝试 curl,只是 'your employer wants to test logic behind newly added routes'。在那种情况下,您想研究响应。水豚就是为此目的而设计的。
当然你应该用单元测试来测试逻辑,但我不是来判断推理的。
my_scraper.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
如何在 Rails 应用中请求 Devise 身份验证后的页面?
这个 5 年前的 /sign_in
然后在以后的请求中使用返回的 access_token。
但是当我请求时,没有返回access_token
。
curl -XPOST -v -H 'Content-Type: application/json' http://localhost:3000/users/sign_in -d '{"email": "me@app.com", "password": "12345678" }'
响应:
响应是这样的,然后是 html 通常返回的页面。
* 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
但我没有可用于提出进一步请求的令牌。
我在 2020 年 Rails 上使用 Ruby 进行的尝试是否可行?
或者是否有破解设计使其工作的方法?
更新:
这是我雇主的网站。很遗憾无法分享。
他们想要一种方法来测试新添加路由背后的逻辑是否有效,而无需登录并单击 UI。
更新
我可以使用 curl、Postman、(任何)等方式访问端点
一种方法是用水豚抓取。你还没有说你为什么尝试 curl,只是 'your employer wants to test logic behind newly added routes'。在那种情况下,您想研究响应。水豚就是为此目的而设计的。
当然你应该用单元测试来测试逻辑,但我不是来判断推理的。
my_scraper.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