Class: PrestoHTTP::DigestAuth

Inherits:
Object
  • Object
show all
Includes:
PrestoCore::Utils
Defined in:
lib/presto/http/auth/digest.rb

Defined Under Namespace

Classes: Nonce

Constant Summary

QOP =
'auth'.freeze

Constants included from PrestoCore::Utils

PrestoCore::Utils::PATH_MODIFIERS, PrestoCore::Utils::STATUS__NOT_FOUND, PrestoCore::Utils::STATUS__OK, PrestoCore::Utils::STATUS__PERMANENT_REDIRECT, PrestoCore::Utils::STATUS__REDIRECT, PrestoCore::Utils::STATUS__RESTRICTED, PrestoCore::Utils::STATUS__SERVER_ERROR

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from PrestoCore::Utils

build_path, #extract_controllers, is_controller?, normalize_path, rootify_url

Constructor Details

- (DigestAuth) initialize(controller_instance, env, setup = {})

A new instance of DigestAuth



23
24
25
26
27
# File 'lib/presto/http/auth/digest.rb', line 23

def initialize controller_instance, env, setup = {}
  @controller_instance = controller_instance
  @env, @setup = env.dup, setup.dup
  @realm = @setup[:realm] || 'Access Restricted'
end

Instance Attribute Details

- (Object) user (readonly)

Returns the value of attribute user



7
8
9
# File 'lib/presto/http/auth/digest.rb', line 7

def user
  @user
end

Instance Method Details

- (Object) body



72
73
74
# File 'lib/presto/http/auth/digest.rb', line 72

def body
  @setup[:body] || 'Access Restricted'
end

- (Object) headers(opts = {})



61
62
63
64
65
66
67
68
69
70
# File 'lib/presto/http/auth/digest.rb', line 61

def headers opts = {}
  params = [
      'realm="%s"' % @realm,
      'qop="%s"' % QOP,
      'nonce="%s"' % nonce,
      'opaque="%s"' % opaque,
  ]
  params << 'stale="true"' if opts[:stale]
  {'Content-Type' => CONTENT_TYPE__PLAIN, 'WWW-Authenticate' => 'Digest %s' % params.join(',')}
end

- (Boolean) pass_validation?

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/presto/http/auth/digest.rb', line 37

def pass_validation?

  return unless key = provided?
  @params = split_header(@env[key]).inject(Hash.new) do |params, param|
    k, v = param.split('=', 2)
    params.update k => dequote(v)
  end
  @nonce = @params['nonce'] || Nonce.new
  if (ttl = @setup[:nonce_ttl]) && Nonce.age(@nonce) > ttl
    @nonce = Nonce.new
    @stale = true
  end

  password = @controller_instance.instance_exec(@params['username'], &@setup[:proc])
  password = md5(a1(password)) if @setup[:plain]

  digest = [*@params.values_at(*%w[nonce nc cnonce qop]), md5(a2)] * ':'
  valid_response = md5([password, digest] * ':')
  if valid_response == @params['response']
    @user = @params['username']
  end
  user
end

- (Object) post_validation_headers



80
81
82
# File 'lib/presto/http/auth/digest.rb', line 80

def post_validation_headers
  stale? ? headers(stale: true) : nil
end

- (Object) post_validation_status_code



84
85
86
# File 'lib/presto/http/auth/digest.rb', line 84

def post_validation_status_code
  stale? ? status_code : nil
end

- (Boolean) provided?

Returns:

  • (Boolean)


33
34
35
# File 'lib/presto/http/auth/digest.rb', line 33

def provided?
  AUTHORIZATION_KEYS.detect { |key| @env.has_key?(key) }
end

- (Object) reset



88
89
90
# File 'lib/presto/http/auth/digest.rb', line 88

def reset
  # does not work for Digest auth
end

- (Boolean) stale?

Returns:

  • (Boolean)


29
30
31
# File 'lib/presto/http/auth/digest.rb', line 29

def stale?
  @stale
end

- (Object) status_code



76
77
78
# File 'lib/presto/http/auth/digest.rb', line 76

def status_code
  STATUS__RESTRICTED
end