module Doorkeeper::AccessTokenMixin::ClassMethods

def by_refresh_token(refresh_token)

Returns:
  • (Doorkeeper::AccessToken, nil) - AccessToken object or nil

Parameters:
  • refresh_token (#to_s) --
def by_refresh_token(refresh_token)
  find_by(refresh_token: refresh_token.to_s)
end

def by_token(token)

Returns:
  • (Doorkeeper::AccessToken, nil) - AccessToken object or nil

Parameters:
  • token (#to_s) --
def by_token(token)
  find_by(token: token.to_s)
end

def find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token)

Returns:
  • (Doorkeeper::AccessToken) - existing record or a new one

Parameters:
  • use_refresh_token (Boolean) --
  • expires_in (Integer) --
  • scopes (#to_s) --
  • resource_owner_id (ActiveRecord::Base, Integer) --
  • application (Doorkeeper::Application) --
def find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token)
  if Doorkeeper.configuration.reuse_access_token
    access_token = matching_token_for(application, resource_owner_id, scopes)
    if access_token && !access_token.expired?
      return access_token
    end
  end
  create!(
    application_id:    application.try(:id),
    resource_owner_id: resource_owner_id,
    scopes:            scopes.to_s,
    expires_in:        expires_in,
    use_refresh_token: use_refresh_token
  )
end

def last_authorized_token_for(application_id, resource_owner_id)

Returns:
  • (Doorkeeper::AccessToken, nil) - matching AccessToken object or

Parameters:
  • resource_owner_id (Integer) --
  • application_id (Integer) --
def last_authorized_token_for(application_id, resource_owner_id)
  ordered_by(:created_at, :desc).
    find_by(application_id: application_id,
            resource_owner_id: resource_owner_id,
            revoked_at: nil)
end

def matching_token_for(application, resource_owner_or_id, scopes)

Returns:
  • (Doorkeeper::AccessToken, nil) - Access Token instance or

Parameters:
  • scopes (String, Doorkeeper::OAuth::Scopes) --
  • resource_owner_or_id (ActiveRecord::Base, Integer) --
  • application (Doorkeeper::Application) --
def matching_token_for(application, resource_owner_or_id, scopes)
  resource_owner_id = if resource_owner_or_id.respond_to?(:to_key)
                        resource_owner_or_id.id
                      else
                        resource_owner_or_id
                      end
  token = last_authorized_token_for(application.try(:id), resource_owner_id)
  if token && scopes_match?(token.scopes, scopes, application.try(:scopes))
    token
  end
end

def revoke_all_for(application_id, resource_owner, clock = Time)

Parameters:
  • resource_owner (ActiveRecord::Base) --
  • application_id (Integer) --
def revoke_all_for(application_id, resource_owner, clock = Time)
  where(application_id: application_id,
        resource_owner_id: resource_owner.id,
        revoked_at: nil).
    update_all(revoked_at: clock.now.utc)
end

def scopes_match?(token_scopes, param_scopes, app_scopes)

Returns:
  • (Boolean) - true if all scopes are blank or matches

Parameters:
  • app_scopes (String) --
  • param_scopes (String) --
  • token_scopes (#to_s) --
def scopes_match?(token_scopes, param_scopes, app_scopes)
  (!token_scopes.present? && !param_scopes.present?) ||
    Doorkeeper::OAuth::Helpers::ScopeChecker.match?(
      token_scopes.to_s,
      param_scopes,
      app_scopes
    )
end