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