Module: Rails::Auth::Credentials

Included in:
Rails::Auth
Defined in:
lib/rails/auth/credentials.rb,
lib/rails/auth/credentials/injector_middleware.rb

Overview

Functionality for storing credentials in the Rack environment

Defined Under Namespace

Classes: InjectorMiddleware

Instance Method Summary collapse

Instance Method Details

#add_credential(env, type, credential) ⇒ Object

Add a credential to the Rack environment

Parameters:

  • :env (Hash)

    Rack environment

  • :type (String)

    credential type to add to the environment

  • :credential (Object)

    object to add to the environment

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rails/auth/credentials.rb', line 25

def add_credential(env, type, credential)
  credentials = env[CREDENTIALS_ENV_KEY] ||= {}

  # Adding a credential is idempotent, so attempting to reregister
  # the same credential should be harmless
  return env if credentials.key?(type) && credentials[type] == credential

  # raise if we already have a cred, but it didn't short-circuit as
  # being == to the one supplied
  raise ArgumentError, "credential #{type} already added to request" if credentials.key?(type)

  credentials[type] = credential

  env
end

#credentials(env) ⇒ Object

Obtain credentials from a Rack environment

Parameters:

  • :env (Hash)

    Rack environment



15
16
17
# File 'lib/rails/auth/credentials.rb', line 15

def credentials(env)
  env.fetch(CREDENTIALS_ENV_KEY, {})
end