class Doorkeeper::OAuth::AccessTokenRequest

def access_token

def access_token
  @access_token ||= Doorkeeper::AccessToken.matching_token_for client, base_token.resource_owner_id, base_token.scopes
end

def authorization

def authorization
  auth = {
    'access_token' => access_token.token,
    'token_type'   => access_token.token_type,
    'expires_in'   => access_token.expires_in,
  }
  auth.merge!({'refresh_token' => access_token.refresh_token}) if refresh_token_enabled?
  auth
end

def authorize

def authorize
  if valid?
    revoke_base_token
    find_or_create_access_token
  end
end

def base_token

def base_token
  @base_token ||= refresh_token? ? token_via_refresh_token : token_via_authorization_code
end

def configuration

def configuration
  Doorkeeper.configuration
end

def create_access_token

def create_access_token
  @access_token = Doorkeeper::AccessToken.create!({
    :application_id    => client.id,
    :resource_owner_id => base_token.resource_owner_id,
    :scopes            => base_token.scopes_string,
    :expires_in        => configuration.access_token_expires_in,
    :use_refresh_token => refresh_token_enabled?
  })
end

def error_response

def error_response
  Doorkeeper::OAuth::ErrorResponse.from_request(self)
end

def find_or_create_access_token

def find_or_create_access_token
  if access_token
    access_token.expired? ? revoke_and_create_access_token : access_token
  else
    create_access_token
  end
end

def initialize(client, attributes = {})

def initialize(client, attributes = {})
  ATTRIBUTES.each { |attr| instance_variable_set("@#{attr}", attributes[attr]) }
  @client = client
  validate
end

def refresh_token?

def refresh_token?
  grant_type == "refresh_token"
end

def refresh_token_enabled?

def refresh_token_enabled?
  configuration.refresh_token_enabled?
end

def revoke_and_create_access_token

def revoke_and_create_access_token
  access_token.revoke
  create_access_token
end

def revoke_base_token

def revoke_base_token
  base_token.revoke
end

def token_type

def token_type
  "bearer"
end

def token_via_authorization_code

def token_via_authorization_code
  Doorkeeper::AccessGrant.authenticate(code)
end

def token_via_refresh_token

def token_via_refresh_token
  Doorkeeper::AccessToken.by_refresh_token(refresh_token)
end

def valid?

def valid?
  self.error.nil?
end

def validate_attributes

def validate_attributes
  return false unless grant_type.present?
  if refresh_token_enabled? && refresh_token?
    refresh_token.present?
  else
    code.present? && redirect_uri.present?
  end
end

def validate_client

def validate_client
  !!client
end

def validate_grant

def validate_grant
  return false unless base_token && base_token.application_id == client.id
  refresh_token? ? !base_token.revoked? : base_token.accessible?
end

def validate_grant_type

def validate_grant_type
  %w(authorization_code refresh_token).include? grant_type
end

def validate_redirect_uri

def validate_redirect_uri
  refresh_token? ? true : base_token.redirect_uri == redirect_uri
end