Skip to content

Commit cf248f1

Browse files
committed
killed whitespace cruft
Sorry, it just drives me nuts.
1 parent 1ab3d90 commit cf248f1

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

lib/rack/auth/cookie.rb

+42-42
Original file line numberDiff line numberDiff line change
@@ -58,163 +58,163 @@ def initialize(app, options = {})
5858
def call(env)
5959
request = Rack::Request.new(env)
6060
auth_fail = false
61-
61+
6262
# Only authenticate if there's a cookie in the request named @@cookie_name
6363
unless request.cookies.has_key?(@@cookie_name)
6464
return finish(@app, env)
6565
end
66-
66+
6767
# Get the data from the cookie
6868
begin
6969
cookie_value = request.cookies[@@cookie_name]
7070
hash_data = read_cookie(cookie_value)
7171
rescue Exception => e
7272
auth_fail = e.message
7373
end
74-
74+
7575
# Do not authenticate if either one of these is set
7676
# This check is done late so that we'll have already
7777
# checked the cookie
7878
if env['AUTH_USER'] || env['AUTH_FAIL']
7979
return finish(@app, env, cookie_value)
8080
end
81-
81+
8282
if !auth_fail
8383
auth_datetime = Time.at(hash_data['AUTH_DATETIME']).utc
8484
auth_expire_datetime = Time.at(hash_data['AUTH_EXPIRE_DATETIME']).utc
85-
85+
8686
if auth_datetime + @@max_lifetime < Time.now.utc
8787
auth_fail = "You have been signed out since you signed in more than #{@@max_lifetime/3600} hours ago"
8888
end
89-
89+
9090
if auth_expire_datetime < Time.now.utc
9191
auth_fail = "You have been signed out due to inactivity"
9292
end
9393
end
94-
94+
9595
if auth_fail
9696
env['AUTH_FAIL'] = auth_fail
9797
else
9898
# Put the values from the hash into the environment
9999
env['AUTH_USER'] = hash_data['AUTH_USER']
100-
100+
101101
env['AUTH_TYPE'] = hash_data['AUTH_TYPE']
102102
env['AUTH_TYPE_USER'] = hash_data['AUTH_TYPE_USER']
103-
103+
104104
env['AUTH_TYPE_THIS_REQUEST'] = "Cookie"
105-
105+
106106
env['AUTH_DATETIME'] = auth_datetime
107107
env['AUTH_EXPIRE_DATETIME'] = auth_expire_datetime
108108
end
109-
109+
110110
finish(@app, env, cookie_value)
111111
end
112-
112+
113113
def finish(app, env, cookie_value_from_request = nil)
114114
status, headers, body = @app.call(env)
115-
115+
116116
# Assume our cookie isn't in the response unless/until we find it
117117
response_cookie = false
118-
118+
119119
if headers.has_key?("Set-Cookie")
120120
set_cookie = headers["Set-Cookie"]
121121
set_cookie_pieces = set_cookie.split(";")
122-
122+
123123
# TODO: parse cookies from header and find @@cookie_name
124124
set_cookie_pieces.each_with_index do |piece, index|
125125
if piece[@@cookie_name]
126126
response_cookie = true
127127
end
128128
end
129129
end
130-
130+
131131
# If the application isn't making any changes to the cookie, we can modify it
132132
if cookie_value_from_request && !response_cookie
133-
133+
134134
# If authentication succeeded earlier, send back a new token
135135
if env['AUTH_USER']
136136
cookie = self.class.create_auth_cookie(env)
137-
137+
138138
if headers["Set-Cookie"]
139139
headers["Set-Cookie"] << cookie
140140
else
141141
headers["Set-Cookie"] = cookie
142142
end
143143
end
144-
144+
145145
# If authentication failed earlier, tell the client to clear the cookie
146146
if env['AUTH_FAIL']
147147
cookie = self.class.create_clear_cookie(env)
148-
148+
149149
if headers["Set-Cookie"]
150150
headers["Set-Cookie"] << cookie
151151
else
152152
headers["Set-Cookie"] = cookie
153153
end
154154
end
155155
end
156-
156+
157157
[status, headers, body]
158158
end
159-
159+
160160
def read_cookie(cookie_value)
161161
# Separate the cookie data and the digest
162162
raw_data, digest = cookie_value.split("--")
163-
163+
164164
# Check for evidence of tampering
165165
unless digest == self.class.generate_hmac(raw_data)
166166
raise "Invalid cookie digest!"
167167
end
168-
168+
169169
# Unpack the cookie data back to a hash
170170
begin
171171
unpacked_data = raw_data.unpack("m*").first
172172
hash_data = JSON.parse(unpacked_data)
173173
rescue
174174
raise "Unable to read cookie!"
175175
end
176-
176+
177177
hash_data
178178
end
179-
179+
180180
def self.cookie_name
181181
@@cookie_name
182182
end
183-
183+
184184
def self.create_auth_token(env)
185185
# Copy relevant auth info for storage in a token
186186
auth_info = Hash.new
187-
187+
188188
auth_info['AUTH_USER'] = env['AUTH_USER']
189-
189+
190190
auth_info['AUTH_TYPE'] = env['AUTH_TYPE'] || "Unknown"
191191
auth_info['AUTH_TYPE_USER'] = env['AUTH_TYPE_USER'] || env['AUTH_USER']
192-
192+
193193
# Expecting env['AUTH_DATETIME'] to hold an instance of Time
194194
if env['AUTH_DATETIME']
195195
auth_info['AUTH_DATETIME'] = env['AUTH_DATETIME'].to_i
196196
else
197197
auth_info['AUTH_DATETIME'] = Time.now.utc.to_i
198198
end
199-
199+
200200
auth_info['AUTH_EXPIRE_DATETIME'] = Time.now.utc.to_i + @@idle_timeout
201-
201+
202202
# Pack the auth_info hash for cookie storage
203203
json_data = auth_info.to_json
204204
packed_data = [json_data].pack('m*')
205-
205+
206206
# Add a digest value to cookie_data to prevent tampering
207207
"#{packed_data}--#{generate_hmac(packed_data)}"
208208
end
209-
209+
210210
def self.create_auth_cookie(env)
211211
cookie_value = create_auth_token(env)
212212
cookie = "#{@@cookie_name}=#{URI.escape(cookie_value)}; "
213213
cookie += "domain=#{cookie_domain(env)}; "
214214
cookie += "path=/; "
215215
cookie += "HttpOnly; "
216216
end
217-
217+
218218
def self.create_clear_cookie(env)
219219
cookie_value = ""
220220
cookie = "#{@@cookie_name}=; "
@@ -223,11 +223,11 @@ def self.create_clear_cookie(env)
223223
cookie += "expires=Thu, 01-Jan-1970 00:00:00 GMT; "
224224
cookie += "HttpOnly; "
225225
end
226-
226+
227227
def self.generate_hmac(data)
228228
OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, @@secret, data)
229229
end
230-
230+
231231
def self.raw_host_with_port(env)
232232
if forwarded = env["HTTP_X_FORWARDED_HOST"]
233233
forwarded.split(/,\s?/).last
@@ -236,24 +236,24 @@ def self.raw_host_with_port(env)
236236
"#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
237237
end
238238
end
239-
239+
240240
def self.host(env)
241241
raw_host_with_port(env).sub(/:\d+$/, '')
242242
end
243-
243+
244244
def self.cookie_domain(env)
245245
result = host(env)
246-
246+
247247
if @@domain_tree_depth != nil
248248
components = result.split('.')
249249
components.slice!(0, @@domain_tree_depth)
250250
result = components.join('.')
251251
end
252-
252+
253253
if @@share_with_subdomains
254254
result = "." + result
255255
end
256-
256+
257257
result
258258
end
259259
end

0 commit comments

Comments
 (0)