require "warden"

class Warden::Strategies::Doorkeeper < ::Warden::Strategies::Base
  VERSION = "0.1.0"

  attr_reader :token, :scope

  def initialize(env, scope=nil)
    super

    @token = OAuth::Token.authenticate request, *Doorkeeper.configuration.access_token_methods
    @scope = scope
  end

  def valid?
    @token && @token.accessible? && @token.acceptable?(@scope)
  end

  def authenticate!
    user = User.where(id: @token.resource_owner_id).first
    if user
      success!(user)
    else
      fail!("No such user")
    end
  end

  # Returns the configuration data for the default user scope.
  def config
    scopes = env["warden"].config[:scope_defaults]
    if scopes && scopes[:user]
      scopes[:user][:config]
    else
      {}
    end
  end
end

require "warden"

class Warden::Strategies::Doorkeeper < ::Warden::Strategies::Base
  VERSION = "0.1.0"

  attr_reader :token, :scope

  def initialize(env, scope=nil)
    super

    @scope = scope
  end

  def valid?
    @token = OAuth::Token.authenticate(request, *Doorkeeper.configuration.access_token_methods)
    @token && @token.accessible? && @token.acceptable?(@scope)
  end

  def authenticate!
    user = User.where(id: @token.resource_owner_id).first
    if user
      success!(user)
    else
      fail!("No such user")
    end
  end

  # Returns the configuration data for the default user scope.
  def config
    scopes = env["warden"].config[:scope_defaults]
    if scopes && scopes[:user]
      scopes[:user][:config]
    else
      {}
    end
  end
end

Move token retrieval outside of initializer method
Move token retrieval outside of initializer method

I don't believe Warden guarantees the `request` method during strategy
initialization, so I've moved it to the `valid?` method for now. It may be
necessary to move it to the actual `authenticate!` call where it is definitely
guaranteed to exist and be accurate.
