class Doorkeeper::OAuth::PasswordAccessTokenRequest

def access_token

def access_token
  return unless client.present? && resource_owner.present?
  @access_token ||= Doorkeeper::AccessToken.matching_token_for client, resource_owner.id, 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?
    find_or_create_access_token
  end
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  => resource_owner.id,
    :scopes             => scopes.to_s,
    :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, owner, attributes = {})

def initialize(client, owner, attributes = {})
  ATTRIBUTES.each { |attr| instance_variable_set("@#{attr}", attributes[attr]) }
  @resource_owner = owner
  @client = client
  validate
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 scopes

def scopes
  @scopes ||= if scope.present?
    Doorkeeper::OAuth::Scopes.from_string(scope)
  else
    Doorkeeper.configuration.default_scopes
  end
end

def token_type

def token_type
  "bearer"
end

def valid?

def valid?
  self.error.nil?
end

def validate_attributes

def validate_attributes
  grant_type.present?
end

def validate_client

def validate_client
  !!client
end

def validate_grant_type

def validate_grant_type
  grant_type == 'password'
end

def validate_resource_owner

def validate_resource_owner
  !!resource_owner
end

def validate_scope

def validate_scope
  return true unless scope.present?
  ScopeChecker.valid?(scope, configuration.scopes)
end