Sha256: 7b7a135b66eed250101f364d56e43cc1fec058e23606dda516abaf13ee215b82

Contents?: true

Size: 1.96 KB

Versions: 9

Compression:

Stored size: 1.96 KB

Contents

module Doorkeeper
  class AccessToken
    include Doorkeeper::OAuth::Helpers
    include Doorkeeper::Models::Expirable
    include Doorkeeper::Models::Revocable
    include Doorkeeper::Models::Accessible
    include Doorkeeper::Models::Scopes

    belongs_to :application, :class_name => "Doorkeeper::Application", :inverse_of => :access_tokens

    validates :application_id, :token, :presence => true
    validates :token, :uniqueness => true
    validates :refresh_token, :uniqueness => true, :if => :use_refresh_token?

    attr_accessor :use_refresh_token
    attr_accessible :application_id, :resource_owner_id, :expires_in, :scopes, :use_refresh_token

    before_validation :generate_token, :on => :create
    before_validation :generate_refresh_token, :on => :create, :if => :use_refresh_token?

    def self.authenticate(token)
      where(:token => token).first
    end

    def self.by_refresh_token(refresh_token)
      where(:refresh_token => refresh_token).first
    end

    def self.revoke_all_for(application_id, resource_owner)
      delete_all_for(application_id, resource_owner)
    end

    def self.matching_token_for(application, resource_owner_or_id, scopes)
      resource_owner_id = resource_owner_or_id.respond_to?(:to_key) ? resource_owner_or_id.id : resource_owner_or_id
      token = last_authorized_token_for(application, resource_owner_id)
      token if token && ScopeChecker.matches?(token.scopes, scopes)
    end

    def token_type
      "bearer"
    end

    def use_refresh_token?
      self.use_refresh_token
    end

    def as_json(options={})
      {
        :resource_owner_id => self.resource_owner_id,
        :scopes => self.scopes,
        :expires_in_seconds => self.expires_in_seconds,
        :application => { :uid => self.application.uid }
      }
    end

    private

    def generate_refresh_token
      write_attribute :refresh_token, UniqueToken.generate
    end

    def generate_token
      self.token = UniqueToken.generate
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
doorkeeper-0.6.7 lib/doorkeeper/models/access_token.rb
doorkeeper-0.6.6 lib/doorkeeper/models/access_token.rb
doorkeeper-0.6.5 lib/doorkeeper/models/access_token.rb
doorkeeper-0.6.4 lib/doorkeeper/models/access_token.rb
doorkeeper-0.6.3 lib/doorkeeper/models/access_token.rb
doorkeeper-0.6.2 lib/doorkeeper/models/access_token.rb
doorkeeper-0.6.1 lib/doorkeeper/models/access_token.rb
doorkeeper-0.6.0 lib/doorkeeper/models/access_token.rb
doorkeeper-0.6.0.rc1 lib/doorkeeper/models/access_token.rb