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
340 def hmac_path(opts=OPTS, &block)
341   orig_path = remaining_path
342   mpath = matched_path
343 
344   on String do |submitted_hmac|
345     rpath = remaining_path
346 
347     if submitted_hmac.bytesize == 64
348       on String do |flags|
349         if flags.include?('n') ^ !scope.hmac_path_namespace(opts).nil?
350           # Namespace required and not provided, or provided and not required.
351           # Bail early to avoid unnecessary HMAC calculation.
352           @remaining_path = orig_path
353           return
354         end
355 
356         if flags.include?('m')
357           rpath = "#{env['REQUEST_METHOD'].to_s.upcase}:#{rpath}"
358         end
359 
360         if flags.include?('p')
361           rpath = "#{rpath}?#{env["QUERY_STRING"]}"
362         end
363 
364         if hmac_path_valid?(mpath, rpath, submitted_hmac, opts)
365           if flags.include?('t')
366             on Integer do |int|
367               if int >= Time.now.to_i
368                 always(&block)
369               else
370                 # Return from method without matching
371                 @remaining_path = orig_path
372                 return
373               end
374             end
375           else
376             always(&block)
377           end
378         end
379       end
380 
381       # Return from method without matching
382       @remaining_path = orig_path
383       return
384     end
385 
386     # Return from method without matching
387     @remaining_path = orig_path
388     return
389   end
390 end