【问题标题】:Why am I unable to sign out using devise_token_auth and curl?为什么我无法使用 devise_token_auth 和 curl 退出?
【发布时间】:2015-12-07 01:55:57
【问题描述】:

我正在使用带有 devise_token_auth 的 rails-api 进行身份验证。我可以通过以下方式毫无问题地登录:

curl -i http://localhost:3000/api/v1/auth/sign_in -F email="user@nowhere.org" -F password="password"

但是,退出失败并显示 404 和错误“用户未找到或未登录”。通过:

curl -X DELETE -i http://localhost:3000/api/v1/auth/sign_out

我已经尝试过使用多种参数和标头值组合的命令变体,例如:

curl -X DELETE -i http://localhost:3000/api/v1/auth/sign_out -F email="user@nowhere.org" -H Access-Token="Ae1yaTYLkSAgdhz3LtPAZg" -H Client="9AmYF6NS8tP6EOD5nPSuxw" -H Expiry="1443073493" -H Uid="user@nowhere.org" -H Token-Type="Bearer"

无济于事。类似构造的 RSpec 测试也失败并出现相同的响应和错误,并且日志表明请求是通过 DeviseTokenAuth::SessionsController#destroy 作为 JSON 处理的。

当然,我实际上并没有使用 curl 进行身份验证;只是在编写相关代码之前验证请求结构。

【问题讨论】:

标签: ruby-on-rails curl rspec devise devise-token-auth


【解决方案1】:

回答我自己的问题,我没有通过 sign_out 请求正确返回某些 sign_in 响应标头值。

I found a related post 指出了一些关键的标头。诀窍是从 sign_in 响应中捕获访问令牌、客户端和 uid 标头值,然后将它们作为参数包含在 sign_out 请求中:

curl -i -X DELETE http://localhost:3000/api/v1/auth/sign_out -F access-token="Ae1yaTYLkSAgdhz3LtPAZg" -F client="9AmYF6NS8tP6EOD5nPSuxw" -F uid="user@nowhere.org"

这里有一个 RSpec 测试来说明和验证:

require 'rails_helper'

RSpec.describe "Authentication", type: :request do

  it "logs a user out" do
    user = User.create!(
      name: "Some Dude", 
      email: "user@nowhere.org",
      password: "password", 
      confirmed_at: Date.today
    )

    # initial sign in to generate a token and response
    post api_v1_user_session_path, {
      email: user.email, 
      password: user.password
    }

    expect(user.reload.tokens.count).to eq 1

    # sign out request using header values from sign in response
    delete destroy_api_v1_user_session_path, {
      "access-token": response.header["access-token"], 
      client: response.header["client"], 
      uid: response.header["uid"]
    }

    response_body = JSON.load(response.body)
    expect(response_body["errors"]).to be_blank
    expect(response.status).to eq 200
    # user token should be deleted following sign out
    expect(user.reload.tokens.count).to eq 0
  end

end

【讨论】:

  • 我用的是同样的东西,检查了标题但仍然无法正常工作
  • 帖子链接也过期了
  • 我更喜欢这样curl -H "access-token: HbJiAUvTFoPWbCnHvdplDQ" -H "client: 0TtJ9D7TyeSTkfz1KatYug" -H "uid: busation4@gmail.com" -X DELETE "http://localhost:3000/api/v1/auth/sign_out"
【解决方案2】:

具体来说,请执行以下步骤:

第 1 步:

curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/api/v1/auth/sign_in -d "{\"email\":\"user@example.com\",\"password\":\"password\"}"

你会得到这样的回应:

*   Trying ::1...
* Connected to localhost (::1) port 3000 (#0)
> POST /api/v1/auth/sign_in HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.43.0
> Content-Type: application/json
> Accept: application/json
> Content-Length: 50
>
* upload completely sent off: 50 out of 50 bytes
< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Access-Token: BqXcWQi0-9faLyxP1LnUKw
< Token-Type: Bearer
< Client: dYSqVgM9VT6fV9Y5MFWpJQ
< Expiry: 1465679853
< Uid: user@example.com
< Content-Type: application/json; charset=utf-8
< Etag: W/"9ad6a23f014a744a7ec83b4e0e9d27aa"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: 6566bd38-1ad7-491a-a1ab-e41458b9b704
< X-Runtime: 0.184807
< Server: WEBrick/1.3.1 (Ruby/2.3.0/2015-12-25)
< Date: Sat, 28 May 2016 21:17:33 GMT
< Content-Length: 135
< Connection: Keep-Alive
<
* Connection #0 to host localhost left intact
{"data":{"id":6,"provider":"email","uid":"user@example.com","name":"testuser","nickname":null,"image":null,"email":"user@example.com"}}%

第 2 步:

现在,您要注销用户。

curl -i -X DELETE http://localhost:3000/api/v1/auth/sign_out -F access-token="BqXcWQi0-9faLyxP1LnUKw" -F client="dYSqVgM9VT6fV9Y5MFWpJQ" -F uid="user@example.com"

你会得到这样的回应:

HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
Etag: W/"7363e85fe9edee6f053a4b319588c086"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 8f7a297a-6a72-4c9d-a210-48c29fb4bfe0
X-Runtime: 0.095060
Server: WEBrick/1.3.1 (Ruby/2.3.0/2015-12-25)
Date: Sat, 28 May 2016 21:19:18 GMT
Content-Length: 16
Connection: Keep-Alive

{"success":true}%

在第 2 步中,我根据我们在第 1 步中收到的响应添加了 access-tokenclientAccess-Token: BqXcWQi0 -9faLyxP1LnUKw客户:dYSqVgM9VT6fV9Y5MFWpJQ)。

就是这样! :)

我得到了@user3006381 的回答的帮助!所有功劳归于他。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多