class Doorkeeper::AccessToken

def self.authenticate(token)

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

def self.by_refresh_token(refresh_token)

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

def self.create_indexes

def self.create_indexes
  ensure_index :token, :unique => true
  ensure_index [[:refresh_token, 1]], :unique => true, :sparse => true
end

def self.delete_all_for(application_id, resource_owner)

def self.delete_all_for(application_id, resource_owner)
  where(:application_id => application_id,
        :resource_owner_id => resource_owner.id).delete_all
end

def self.delete_all_for(application_id, resource_owner)

def self.delete_all_for(application_id, resource_owner)
  delete_all(:application_id => application_id,
             :resource_owner_id => resource_owner.id)
end

def self.delete_all_for(application_id, resource_owner)

def self.delete_all_for(application_id, resource_owner)
  where(:application_id => application_id,
        :resource_owner_id => resource_owner.id).delete_all
end

def self.delete_all_for(application_id, resource_owner)

def self.delete_all_for(application_id, resource_owner)
  where(:application_id => application_id,
        :resource_owner_id => resource_owner.id).delete_all
end

def self.last

def self.last
  self.sort(:created_at).last
end

def self.last_authorized_token_for(application, resource_owner_id)

def self.last_authorized_token_for(application, resource_owner_id)
  where(:application_id => application.id,
        :resource_owner_id => resource_owner_id,
        :revoked_at => nil).
  order('created_at desc').
  limit(1).
  first
end

def self.last_authorized_token_for(application, resource_owner_id)

def self.last_authorized_token_for(application, resource_owner_id)
  where(:application_id => application.id,
        :resource_owner_id => resource_owner_id,
        :revoked_at => nil).
  sort(:created_at.desc).
  limit(1).
  first
end

def self.last_authorized_token_for(application, resource_owner_id)

def self.last_authorized_token_for(application, resource_owner_id)
  where(:application_id => application.id,
        :resource_owner_id => resource_owner_id,
        :revoked_at => nil).
  order_by([:created_at, :desc]).
  limit(1).
  first
end

def self.last_authorized_token_for(application, resource_owner_id)

def self.last_authorized_token_for(application, resource_owner_id)
  where(:application_id => application.id,
        :resource_owner_id => resource_owner_id,
        :revoked_at => nil).
  order_by([:created_at, :desc]).
  limit(1).
  first
end

def self.matching_token_for(application, resource_owner_or_id, scopes)

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 self.revoke_all_for(application_id, resource_owner)

def self.revoke_all_for(application_id, resource_owner)
  where(:application_id => application_id,
        :resource_owner_id => resource_owner.id,
        :revoked_at => nil)
  .map(&:revoke)
end

def as_json(options={})

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

def generate_refresh_token

def generate_refresh_token
  write_attribute :refresh_token, UniqueToken.generate
end

def generate_token

def generate_token
  self.token = UniqueToken.generate
end

def refresh_token

def refresh_token
  self[:refresh_token]
end

def refresh_token

def refresh_token
  self[:refresh_token]
end

def refresh_token

def refresh_token
  self[:refresh_token]
end

def scopes=(value)

def scopes=(value)
  write_attribute :scopes, value if value.present?
end

def token_type

def token_type
  "bearer"
end

def use_refresh_token?

def use_refresh_token?
  self.use_refresh_token
end