module Roda::RodaPlugins::HmacPaths::RequestMethods

  1. lib/roda/plugins/hmac_paths.rb

Methods

Public Instance

  1. hmac_path

Public Instance methods

hmac_path(opts=OPTS, &block)

Looks at the first segment of the remaining path, and if it contains a valid HMAC for the rest of the path considering the flags in the second segment and the given options, the block matches and is yielded to, and the result of the block is returned. Otherwise, the block does not matches and routing continues after the call.

[show source]
    # File lib/roda/plugins/hmac_paths.rb
312 def hmac_path(opts=OPTS, &block)
313   orig_path = remaining_path
314   mpath = matched_path
315 
316   on String do |submitted_hmac|
317     rpath = remaining_path
318 
319     if submitted_hmac.bytesize == 64
320       on String do |flags|
321         if flags.bytesize >= 1
322           if flags.include?('n') ^ !scope.hmac_path_namespace(opts).nil?
323             # Namespace required and not provided, or provided and not required.
324             # Bail early to avoid unnecessary HMAC calculation.
325             @remaining_path = orig_path
326             return
327           end
328 
329           if flags.include?('m')
330             rpath = "#{env['REQUEST_METHOD'].to_s.upcase}:#{rpath}"
331           end
332 
333           if flags.include?('p')
334             rpath = "#{rpath}?#{env["QUERY_STRING"]}"
335           end
336 
337           if hmac_path_valid?(mpath, rpath, submitted_hmac, opts)
338             always(&block)
339           end
340         end
341 
342         # Return from method without matching
343         @remaining_path = orig_path
344         return
345       end
346     end
347 
348     # Return from method without matching
349     @remaining_path = orig_path
350     return
351   end
352 end