Class: Shaf::Authenticator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/shaf/authenticator/base.rb

Direct Known Subclasses

BasicAuth, Spec::Authenticator

Defined Under Namespace

Classes: InvalidParameterError, MissingParametersError, WrongCredentialsReturnTypeError

Class Method Summary collapse

Class Method Details

.challenges_for(realm) ⇒ Object



67
68
69
70
71
# File 'lib/shaf/authenticator/base.rb', line 67

def challenges_for(realm)
  challenges.select do |challenge|
    challenge.realm? realm
  end
end

.inherited(child) ⇒ Object



32
33
34
# File 'lib/shaf/authenticator/base.rb', line 32

def inherited(child)
  Authenticator.register(child)
end

.param(name, required: true, default: nil, values: nil) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/shaf/authenticator/base.rb', line 52

def param(name, required: true, default: nil, values: nil)
  params[name.to_sym] = Parameter.new(
    name,
    required: required,
    default: default,
    values: values
  )
end

.paramsObject



88
89
90
# File 'lib/shaf/authenticator/base.rb', line 88

def params
  @params ||= superclass.respond_to?(:params) ? superclass.params.dup : {}
end

.restricted(**parameters, &block) ⇒ Object



61
62
63
64
65
# File 'lib/shaf/authenticator/base.rb', line 61

def restricted(**parameters, &block)
  validate! parameters
  add_defaults! parameters
  challenges << Challenge.new(scheme, **parameters, &block)
end

.scheme(scheme = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/shaf/authenticator/base.rb', line 36

def scheme(scheme = nil)
  if scheme
    @scheme = scheme.to_s
  elsif @scheme
    @scheme
  else
    raise Error, "#{self} must specify a scheme!"
  end
end

.scheme?(str) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/shaf/authenticator/base.rb', line 46

def scheme?(str)
  return false unless scheme

  str.to_s.downcase == scheme.downcase
end

.user(request, realm: nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/shaf/authenticator/base.rb', line 73

def user(request, realm: nil)
  auth = authorization(request)
  cred = credentials(auth, request) || {}
  raise WrongCredentialsReturnTypeError.new(self, cred.class) unless cred.kind_of? Hash

  return if cred.compact.empty?

  challenges_for(realm).each do |challenge|
    user = challenge.test(**cred)
    return user if user
  end

  nil
end