class Rails::Auth::Env

Wrapper for Rack environments with Rails::Auth helpers

Constants

ALLOWED_BY_ENV_KEY

Rack environment key for storing what allowed the request

AUTHORIZED_ENV_KEY

Rack environment key for marking external authorization

CREDENTIALS_ENV_KEY

Rack environment key for all rails-auth credentials

Attributes

allowed_by[R]
credentials[R]

Public Class Methods

new(env, credentials: {}, authorized: false, allowed_by: nil) click to toggle source

@param [Hash] :env Rack environment

# File lib/rails/auth/env.rb, line 19
def initialize(env, credentials: {}, authorized: false, allowed_by: nil)
  raise TypeError, "expected Hash for credentials, got #{credentials.class}" unless credentials.is_a?(Hash)

  @env         = env
  @credentials = Credentials.new(credentials.merge(@env.fetch(CREDENTIALS_ENV_KEY, {})))
  @authorized  = env.fetch(AUTHORIZED_ENV_KEY, authorized)
  @allowed_by  = env.fetch(ALLOWED_BY_ENV_KEY, allowed_by)
end

Public Instance Methods

allowed_by=(allowed_by) click to toggle source

Set the name of the authority which authorized the request

@param [String] :allowed_by label of what allowed the request

# File lib/rails/auth/env.rb, line 44
def allowed_by=(allowed_by)
  raise AlreadyAuthorizedError, "already allowed by #{@allowed_by.inspect}" if @allowed_by
  raise TypeError, "expected String for allowed_by, got #{allowed_by.class}" unless allowed_by.is_a?(String)

  @allowed_by = allowed_by
end
authorize(allowed_by) click to toggle source

Mark the environment as authorized to access the requested resource

@param [String] :allowed_by label of what allowed the request

# File lib/rails/auth/env.rb, line 36
def authorize(allowed_by)
  self.allowed_by = allowed_by
  @authorized = true
end
authorized?() click to toggle source

Check whether a request has been authorized

# File lib/rails/auth/env.rb, line 29
def authorized?
  @authorized
end
to_rack() click to toggle source

Return a Rack environment

@return [Hash] Rack environment

# File lib/rails/auth/env.rb, line 54
def to_rack
  @env[CREDENTIALS_ENV_KEY] = (@env[CREDENTIALS_ENV_KEY] || {}).merge(@credentials.to_hash)

  @env[AUTHORIZED_ENV_KEY] = @authorized if @authorized
  @env[ALLOWED_BY_ENV_KEY] = @allowed_by if @allowed_by

  @env
end