module Roda::RodaPlugins::TokenAuth::RequestMethods

Public Instance Methods

header_variable(auth_opts, variable_name) click to toggle source
# File lib/roda/plugins/token_auth.rb, line 40
def header_variable(auth_opts, variable_name)
  env["HTTP_#{auth_opts[variable_name]}".tr("-", "_").upcase]
end
token_auth(opts = {}, &authenticator) click to toggle source
# File lib/roda/plugins/token_auth.rb, line 26
def token_auth(opts = {}, &authenticator)
  auth_opts = roda_class.opts[:token_auth].merge(opts)
  authenticator ||= auth_opts[:authenticator]

  raise "Must provide an authenticator block" if authenticator.nil?
  auth_token = header_variable(auth_opts, :token_variable)
  auth_secret = header_variable(auth_opts, :secret_variable)
  return if authenticator.call(auth_token, auth_secret)
  auth_opts[:unauthorized]&.call(self)
  halt [401,
        auth_opts[:unauthorized_headers].call(auth_opts),
        [auth_opts[:unauthorized_body].call(auth_opts).to_json]]
end