Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 门卫::授权控制员#创建Can';t验证CSRF令牌的真实性_Ruby On Rails 4_Doorkeeper - Fatal编程技术网

Ruby on rails 4 门卫::授权控制员#创建Can';t验证CSRF令牌的真实性

Ruby on rails 4 门卫::授权控制员#创建Can';t验证CSRF令牌的真实性,ruby-on-rails-4,doorkeeper,Ruby On Rails 4,Doorkeeper,我随后测试了API curl -F grant_type=password \ -F username=foo@bar.com \ -F password=mypass \ -X POST http://localhost:3000/oauth/token 我得到了回应: {“访问令牌”:“6D4398B75D94835631A453AF77016A6F5861B101B58CCF62A5A8492BCE3440”,“令牌类型”:“承载”、“到期时间”:600,“刷新令牌”:“c1445d0a

我随后测试了API

curl -F grant_type=password \
-F username=foo@bar.com \
-F password=mypass \
-X POST http://localhost:3000/oauth/token
我得到了回应:

{“访问令牌”:“6D4398B75D94835631A453AF77016A6F5861B101B58CCF62A5A8492BCE3440”,“令牌类型”:“承载”、“到期时间”:600,“刷新令牌”:“c1445d0a27a8278268c1187c2e3da7163525f1fac8093890430edd328f51c3de”,“创建于”:1429931390}

但当我调用/oauth/authorize时:

curl-F响应类型=6D4398B75D94835631A453AF77016A6F58618B101B58CCF62A5A8492BCE3440\
-F客户id=9c291dc4aa87bfafd6c6a4cf6930d225c106f8fe88e1d0769832047f1ee011c4\
-F客户机密=decba5aca425095978d33653ef03d654f0b74427bcec0596bdde518016708c35\
-F redirect_uri=urn:ietf:wg:oauth:2.0:oob\
-F用户名=foo@bar.com \
-X柱http://localhost:3000/oauth/authorize

但我得到了:

于2015-04-25 00:30:05-0300为127.0.0.1发布“/oauth/authorize” 门卫处理::授权管理员#创建为/ 参数:{“响应类型”=>“6D4398B75D94835631A453AF77016A6F58618B101B58CCF62A5A8492BCE3440”,“客户端id”=>“9c291dc4aa87bfafd6c6a4cf6930d225c106f8fe88e1d0769832047f1ee011c4”,“客户端机密”=>“[过滤]”,“重定向uri”=>“urn:ietf:wg:oauth:2.0:oob”,“用户名”=>”foo@bar.com"} 无法验证CSRF令牌的真实性 在1ms内完成422个不可处理实体 ActionController::InvalidAuthenticationToken(ActionController::InvalidAuthenticationToken):


什么,我做错了吗?

如果您只使用API,我猜您可以通过添加以下行在环境文件(test/development/production.rb)中关闭它:

config.action\u controller.allow\u forgery\u protection=false'


干杯

看起来在第二个请求中,您正在为
响应类型使用令牌。我认为应该是授权码

然而,从第一个响应来看,它似乎是在给你一个无记名令牌。如果是这样,那么要查看受保护的页面(具有before\u操作:doorkeeper\u authorize),命令如下

curl http://localhost:3000/protected_page -H "Authorization: Bearer 6d4398b75d94835631a453af770161a6f58618b101b58ccf62a5a8492bce3440"
OAuth2库 你需要用卷发吗?由于CSRF Authentity令牌认为它是一个表单请求,所以我在使用CSRF Authentity令牌时遇到了同样的失败,但我使用OAuth2 gem获得了它

/oauth/applications
注册一个应用程序(假定该应用程序受保护),转到它,单击
授权
,单击
批准
,您将看到您已发布到类似
http://localhost:3000/oauth/authorize?client_id=abc123&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code
带有 参数
“utf8=>”✓", "真实性\u令牌“=>”[过滤],“状态”=>”,“范围”=>“公共”
abc123
是您的一次性身份验证代码

但你仍然没有授权你的申请。那么,让我们获取访问令牌和刷新令牌

client_id = "9c291dc4aa87bfafd6c6a4cf6930d225c106f8fe88e1d0769832047f1ee011c4"
client_secret = "decba5aca425095978d33653ef03d654f0b74427bcec0596bdde518016708c35"
site = "http://localhost:3000"
redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
code = "abc123" # see above
ENV['OAUTH_DEBUG'] = 'true'
client = OAuth2::Client.new(client_id, client_secret, :site => site)
token = client.auth_code.get_token(code, redirect_uri: redirect_uri)
access_token = token.token
refresh_token = token.refresh_token
# And if you want:
# if token.expired?
#   new_token = token.refresh!
#   new_token.token
#   new_token.refresh_token
# end
如果您转到
http://localhost:3000/oauth/authorized_applications
您现在应该可以看到您的应用程序已在列表中

现在,您可以使用
curl-xget之类的工具查看受保护的页面http://localhost:3000/protected_page -H“授权:承载#{access_token}”

也看到

可能有用的信息:门卫在寻找什么来验证身份验证码

redirect_uri.present?
grant = Doorkeeper::AccessGrant.by_token(authorization_code)
grant.redirect_uri == redirect_uri
application = Doorkeeper::Application.by_uid_and_secret(client_id, client_secret)
dk_client = Doorkeeper::OAuth::Client.new(application)
!!dk_client
grant.application_id == dk_client.id
grant.accessible? #  !grant.expired? && !grant.revoked?


这是预期的行为。授权表单在提供者应用程序中呈现为HTML,幸运的是Rails在表单提交时默认检查它在表单中呈现的CSRF令牌是否匹配。没有该标记的
curl
命令将因此引发“CSRF验证失败”。我知道这一点,但我正在尝试使用doorskeep oauth2编写API,而API没有呈现HTML。如果您了解风险,您可能希望为该控制器操作禁用Rails伪造保护。Doorkeeper没有为此提供选项,您需要重新打开该控制器。如果您希望允许API客户端请求用户名和密码,最好将Doorkeeper gem配置为支持资源所有者密码凭据流()。您使用的流旨在将用户重定向到您的web服务,以安全地登录,并且从不向第三方应用公开您的用户凭据。@MarcosSousa您解决了这个问题吗?这是一个非常糟糕的主意,CSRF保护是有原因的。在本例中,它保护了用户使用浏览器所经历的oauth流的一部分。这就是为什么我首先对API进行了阐述。如果没有任何浏览器访问,CSRF保护就没有真正的用处。