Module: Sinatra::DigestAuthorization
Overview
liberally lifted and modified from Rack’s source Code slightly refactored for Amp
Constant Summary
collapse
- QOP =
'auth'.freeze
Instance Method Summary
collapse
#authorized?, #bad_request!, #current_user, #unauthorized!
Instance Method Details
#A1(auth, password) ⇒ Object
183
184
185
|
# File 'lib/amp/server/extension/authorization.rb', line 183
def A1(auth, password)
[ auth.username, auth.realm, password ] * ':'
end
|
187
188
189
|
# File 'lib/amp/server/extension/authorization.rb', line 187
def A2(auth)
[ auth.method, auth.uri ] * ':'
end
|
#auth_params(hash = {}) ⇒ Object
143
144
145
146
147
148
149
150
151
|
# File 'lib/amp/server/extension/authorization.rb', line 143
def auth_params(hash = {})
param = Rack::Auth::Digest::Params.new do |param|
param['realm'] = options.authorization_realm
param['nonce'] = Rack::Auth::Digest::Nonce.new.to_s
param['opaque'] = H(opaque)
param['qop'] = QOP
hash.each { |k, v| param[k] = v }
end
end
|
#challenge(hash = {}) ⇒ Object
153
154
155
|
# File 'lib/amp/server/extension/authorization.rb', line 153
def challenge(hash = {})
"Digest #{auth_params(hash)}"
end
|
#digest(auth, password) ⇒ Object
191
192
193
194
195
196
|
# File 'lib/amp/server/extension/authorization.rb', line 191
def digest(auth, password)
password_hash = false ? password : H(A1(auth, password))
KD(password_hash, [ auth.nonce, auth.nc, auth.cnonce, QOP, H(A2(auth)) ] * ':')
end
|
#KD(secret, data) ⇒ Object
179
180
181
|
# File 'lib/amp/server/extension/authorization.rb', line 179
def KD(secret, data)
H([secret, data] * ':')
end
|
#login_required ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/amp/server/extension/authorization.rb', line 99
def login_required
auth = Rack::Auth::Digest::Request.new(request.env)
unauthorized! unless auth.provided?
bad_request! if !auth.digest?
if valid?(auth)
if auth.nonce.stale?
return unauthorized!(challenge(:stale => true))
else
request.env["REMOTE_USER"] = auth.username
return true
end
end
unauthorized!
end
|
#md5(data) ⇒ Object
Also known as:
H
173
174
175
|
# File 'lib/amp/server/extension/authorization.rb', line 173
def md5(data)
::Digest::MD5.hexdigest(data)
end
|
95
|
# File 'lib/amp/server/extension/authorization.rb', line 95
def opaque; "DEADBEEF"; end
|
#valid?(auth) ⇒ Boolean
157
158
159
|
# File 'lib/amp/server/extension/authorization.rb', line 157
def valid?(auth)
valid_opaque?(auth) && valid_nonce?(auth) && valid_digest?(auth)
end
|
#valid_digest?(auth) ⇒ Boolean
This method verifies that the digest provided is accurate. This is the only method involved in the authentication process that requires knowledge of the login system, so it is exposed here, rather than Sinatra::DigestAuthorization.
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/amp/server/extension/authorization.rb', line 121
def valid_digest?(auth)
repo = self.class.amp_repositories[request.path_info]
return true unless repo && repos[repo]
user = get_user_and_permissions repo, auth.username
return false unless user
if repo_is_private?(repo)
return false if command_reads?(params["cmd"]) && !user[:read]
end
return false if command_writes?(params["cmd"]) && !user[:write]
digest(auth, user[:user].password) == auth.response
end
|
#valid_nonce?(auth) ⇒ Boolean
169
170
171
|
# File 'lib/amp/server/extension/authorization.rb', line 169
def valid_nonce?(auth)
auth.nonce.valid?
end
|
#valid_opaque?(auth) ⇒ Boolean
165
166
167
|
# File 'lib/amp/server/extension/authorization.rb', line 165
def valid_opaque?(auth)
H(opaque) == auth.opaque
end
|
#valid_qop?(auth) ⇒ Boolean
161
162
163
|
# File 'lib/amp/server/extension/authorization.rb', line 161
def valid_qop?(auth)
QOP == auth.qop
end
|