Module: Roda::RodaPlugins::TokenAuth::RequestMethods

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

Instance Method Summary collapse

Instance Method Details

#header_variable(auth_opts, variable_name) ⇒ Object



40
41
42
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# 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