Class: Digestive::Auth::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/digestive/auth/service.rb

Overview

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`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentialed, provider = nil) ⇒ Service

Create a new Digest::Auth::Service.

Parameters:

  • credentialed (Object)

    An object responding to constant DIGEST_REALM and method find_by_username. See User.

  • provider (Object) (defaults to: nil)

    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.



26
27
28
29
# File 'lib/digestive/auth/service.rb', line 26

def initialize(credentialed, provider=nil)
  @credentialed = credentialed
  @provider = provider || self
end

Instance Attribute Details

#userObject (readonly)

Returns The currently-authenticated user, or nil.

Returns:

  • (Object)

    The currently-authenticated user, or nil



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/digestive/auth/service.rb', line 11

class Service

  attr_reader :user

  # 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.
  def initialize(credentialed, provider=nil)
    @credentialed = credentialed
    @provider = provider || self
  end

  # 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.
  def authenticate(&strategy)
    unless authenticated?(strategy)
      realm = @credentialed::DIGEST_REALM
      @provider.request_http_digest_authentication(realm)
    end
  end

  private

  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
end

Instance Method Details

#authenticate(&strategy) ⇒ Object

Authenticate, optionally with an authorization strategy. If authentication can’t be verified, prompt for credentials.

Parameters:

  • strategy (Block)

    Block accepting one argument (an object returned by @credentialed.find_by_username) and returning a boolean.



36
37
38
39
40
41
# 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