Sha256: 3d4233363bf3125f3d712852640a081d1bd155ca9b122bc947e24ed78dcd903e

Contents?: true

Size: 747 Bytes

Versions: 12

Compression:

Stored size: 747 Bytes

Contents

class Oauth2Token < ActiveRecord::Base
  cattr_accessor :default_lifetime
  self.default_lifetime = 1.minute

  belongs_to :user
  belongs_to :client,
             class_name: "Site::Client",
             foreign_key: :site_id

  validates :client, :expires_at, presence: true
  validates :token, presence: true, uniqueness: true

  before_validation :setup, on: :create

  scope :valid, lambda {
    where('expires_at >= ?', Time.now.utc)
  }

  def expires_in
    (expires_at - Time.now.utc).to_i
  end

  def expire!
    update_attribute(:expires_at, Time.now.utc) || raise(ActiveRecord::RecordInvalid)
  end

  protected

  def setup
    self.token = SecureRandom.urlsafe_base64(64)
    self.expires_at ||= default_lifetime.from_now
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
social_stream-2.2.2 oauth2_server/app/models/oauth2_token.rb
social_stream-oauth2_server-2.2.2 app/models/oauth2_token.rb
social_stream-2.2.1 oauth2_server/app/models/oauth2_token.rb
social_stream-oauth2_server-2.2.1 app/models/oauth2_token.rb
social_stream-2.2.0 oauth2_server/app/models/oauth2_token.rb
social_stream-oauth2_server-2.2.0 app/models/oauth2_token.rb
social_stream-2.1.1 oauth2_server/app/models/oauth2_token.rb
social_stream-oauth2_server-2.1.1 app/models/oauth2_token.rb
social_stream-2.1.0 oauth2_server/app/models/oauth2_token.rb
social_stream-oauth2_server-2.1.0 app/models/oauth2_token.rb
social_stream-2.0.4 oauth2_server/app/models/oauth2_token.rb
social_stream-oauth2_server-2.0.3 app/models/oauth2_token.rb