class Digestive::Auth::Service

Handles authentication of a credentialed object, using a given provider. The credentialed object will normally be a user, and the provider ‘ActionController::HttpAuthentication::Digest::ControllerMethods`. @!attribute [r] user

@return [Object]
  The currently-authenticated user, or nil

Attributes

user[R]

Public Class Methods

new(credentialed, provider=nil) click to toggle source

Create a new Digest::Auth::Service. @param [Object] credentialed

An object responding to constant DIGEST_REALM and
method find_by_username. See {Digestive::User}.

@param [Object] provider

An object responding to methods
`authenticate_with_http_digest` and
`request_http_digest_authentication`.
Defaults to nil; if not given, {Digestive::Auth::Service}
will expect the methods to be mixed-in, which
in the context of Rails, they should be.
# File lib/digestive/auth/service.rb, line 26
def initialize(credentialed, provider=nil)
  @credentialed = credentialed
  @provider = provider || self
end

Public Instance Methods

authenticate(&strategy) click to toggle source

Authenticate, optionally with an authorization strategy. If authentication can’t be verified, prompt for credentials. @param [Block] strategy

Block accepting one argument (an object returned by
@credentialed.find_by_username) and returning a boolean.
# File lib/digestive/auth/service.rb, line 36
def authenticate(&strategy)
  unless authenticated?(strategy)
    realm = @credentialed::DIGEST_REALM
    @provider.request_http_digest_authentication(realm)
  end
end

Private Instance Methods

authenticated?(strategy=nil) click to toggle source
# File lib/digestive/auth/service.rb, line 45
def authenticated?(strategy=nil)
  realm = @credentialed::DIGEST_REALM
  strategy ||= ->(user) { true }
  user = nil
  @provider.authenticate_with_http_digest(realm) do |username|
    user = @credentialed.find_by_username(username)
    user.nil? ? nil : user.password
  end
  @user = user if strategy.call(user)
end