Sha256: 7ad3abc5292305aaa2bad8d8d727dfabe2f7b7fd88d96ae8b35e7b4360d4d87e

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

module OauthProviderEngine
  class AccessToken < OauthProviderEngine::Base

    belongs_to :application, :class_name => "::OauthProviderEngine::Application"

    before_validation :generate_keys
    before_create :generate_expiry
    validates_presence_of :application_id, :user_id, :token, :secret
    validates_numericality_of :application_id, :user_id, :allow_nil => true

    scope :expired, where("expires_at <= NOW()")
    scope :not_expired, where("expires_at IS NULL OR expires_at > NOW()")
    scope :for_user, lambda{|user_id| where(:user_id => user_id)}

    def to_query
      params = {
        :oauth_token => token,
        :oauth_token_secret => secret
      }
      params[:oauth_authorization_expires_at] = expires_at.to_i if expires_at.present?
      params.to_query
    end

    def extend_access
      generate_expiry
    end

    def extend_access!
      extend_access
      self.save
    end

    protected

    def generate_keys
      self.token ||= OauthProviderEngine.generate_key
      self.secret ||= OauthProviderEngine.generate_key
    end

    def generate_expiry
      return true unless OauthProviderEngine.access_token_expiry

      if OauthProviderEngine.access_token_expiry.respond_to?(:call)
        self.expires_at = OauthProviderEngine.access_token_expiry.call(self)
      else
        self.expires_at = Time.now + OauthProviderEngine.access_token_expiry
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oauth_provider_engine-0.0.5 app/models/oauth_provider_engine/access_token.rb