Module: Roda::RodaPlugins::HmacPaths::RequestMethods

Defined in:
lib/roda/plugins/hmac_paths.rb

Instance Method Summary collapse

Instance Method Details

#hmac_path(opts = OPTS, &block) ⇒ Object

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.



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/roda/plugins/hmac_paths.rb', line 312

def hmac_path(opts=OPTS, &block)
  orig_path = remaining_path
  mpath = matched_path

  on String do ||
    rpath = remaining_path

    if .bytesize == 64
      on String do |flags|
        if flags.bytesize >= 1
          if flags.include?('n') ^ !scope.hmac_path_namespace(opts).nil?
            # Namespace required and not provided, or provided and not required.
            # Bail early to avoid unnecessary HMAC calculation.
            @remaining_path = orig_path
            return
          end

          if flags.include?('m')
            rpath = "#{env['REQUEST_METHOD'].to_s.upcase}:#{rpath}"
          end

          if flags.include?('p')
            rpath = "#{rpath}?#{env["QUERY_STRING"]}"
          end

          if hmac_path_valid?(mpath, rpath, , opts)
            always(&block)
          end
        end

        # Return from method without matching
        @remaining_path = orig_path
        return
      end
    end

    # Return from method without matching
    @remaining_path = orig_path
    return
  end
end