簡體   English   中英

Rails:如何檢查access_token是否已過期?

[英]Rails: How can I check if the access_token is expired?

我正在為帶有Rails的android應用程序編寫一個api。 由於我是Rails和android的新手,所以我無法弄清access_token的內容。

我有一個令牌模型,每次用戶登錄時,都會將一個新的access_token添加到令牌表中。 我的問題是,如果我將令牌創建后的有效期限設置為== 3天,那么當用戶發送帶有access_token的請求時,驗證此錯誤的正確方法是什么? 如何刪除那些過期的令牌?

這是我的令牌模型:

# Table name: tokens
#
#  id           :integer          not null, primary key
#  access_token :string(255)
#  user_id      :integer
#  created_at   :datetime         not null
#  updated_at   :datetime         not null

class Token < ActiveRecord::Base
  attr_accessible   :access_token, :user_id
  before_validation :generate_access_token
  belongs_to        :user

  validates :access_token, presence: true, uniqueness: true
  validates :user_id,      presence: true, uniqueness: true

private

  def generate_access_token
    begin
      self.access_token = SecureRandom.hex
    end while self.class.exists?(access_token: access_token)
  end
end

您可以將expires_at列添加到令牌模型中以進行處理。 您還將需要一種檢索新令牌的方法,該方法可以更新現有令牌的密鑰和到期時間,也可以為用戶生成新的令牌記錄並返回該記錄。 后者更多是Facebook為其訪問令牌所采用的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM