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