Class: AuthenticationStrategies::BasicStrategy

Inherits:
Warden::Strategies::Base
  • Object
show all
Defined in:
lib/authentication_strategies/basic_strategy.rb

Instance Method Summary collapse

Instance Method Details

#auth_requestObject


3
4
5
# File 'lib/authentication_strategies/basic_strategy.rb', line 3

def auth_request
  @auth_request ||= Rack::Auth::Basic::Request.new(env)
end

#authenticate!Object

See Also:


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/authentication_strategies/basic_strategy.rb', line 22

def authenticate!
  Rails.logger.debug "[AuthN] [#{self.class}] Authenticating ..."

  unless valid_username_provided?(auth_request.username)
    fail! 'Provided username contains invalid characters!'
    return
  end

  user = Hashie::Mash.new
  user.auth!.type = 'basic'
  user.auth!.credentials!.username = auth_request.username
  user.auth!.credentials!.password = auth_request.credentials.last
  user.identity = auth_request.username

  Rails.logger.debug "[AuthN] [#{self.class}] Authenticated #{user.to_hash.inspect}"
  success! user.deep_freeze
end

#store?Boolean

Returns:

  • (Boolean)

See Also:


8
9
10
# File 'lib/authentication_strategies/basic_strategy.rb', line 8

def store?
  false
end

#valid?Boolean

Returns:

  • (Boolean)

See Also:


13
14
15
16
17
18
19
# File 'lib/authentication_strategies/basic_strategy.rb', line 13

def valid?
  Rails.logger.debug "[AuthN] [#{self.class}] Checking for applicability"
  result = auth_request.provided? && auth_request.basic?

  Rails.logger.debug "[AuthN] [#{self.class}] Strategy is #{result ? '' : 'not '}applicable!"
  result
end

#valid_username_provided?(username) ⇒ Boolean

Returns:

  • (Boolean)

40
41
42
# File 'lib/authentication_strategies/basic_strategy.rb', line 40

def valid_username_provided?(username)
  /^[[:print:]]+$/.match(username) && /^\S+$/.match(username)
end