Class: Ros::TenantMiddleware

Inherits:
Apartment::Elevators::Generic
  • Object
show all
Defined in:
lib/ros/tenant_middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id.



7
8
9
# File 'lib/ros/tenant_middleware.rb', line 7

def access_key_id
  @access_key_id
end

#auth_stringObject

Returns the value of attribute auth_string.



7
8
9
# File 'lib/ros/tenant_middleware.rb', line 7

def auth_string
  @auth_string
end

#auth_typeObject

Returns the value of attribute auth_type.



7
8
9
# File 'lib/ros/tenant_middleware.rb', line 7

def auth_type
  @auth_type
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/ros/tenant_middleware.rb', line 7

def token
  @token
end

Instance Method Details

#credentialObject



27
28
29
30
31
32
33
34
# File 'lib/ros/tenant_middleware.rb', line 27

def credential
  # TODO: Credential.authorization must be an instance variable
  Ros::Sdk::Credential.authorization = auth_string
  Ros::IAM::Credential.where(access_key_id: access_key_id).first
# rescue JsonApiClient::Errors::ServerError => e
# NOTE: Swallow the auth error and return nil which causes tenant to be 'public'
rescue JsonApiClient::Errors::NotAuthorized => e
end

#parse_tenant_name(request) ⇒ Object

Returns the schema_name for Apartment to switch to for this request



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ros/tenant_middleware.rb', line 10

def parse_tenant_name(request)
  @auth_string = request.env['HTTP_AUTHORIZATION']
  return 'public' unless auth_string.present?
  @auth_type, @token = auth_string.split(' ')
  @auth_type.downcase!
  Rails.logger.info("Invalid auth type #{auth_type}") and return 'public' unless auth_type.in? %w(basic bearer)
  Rails.logger.info('Invalid token') and return 'public' if token.nil?
  schema_name = send("tenant_name_from_#{auth_type}")
  Rails.logger.info('Invalid credentials') if schema_name.eql?('public')
  schema_name
end

#tenant_name_from_basicObject



22
23
24
25
# File 'lib/ros/tenant_middleware.rb', line 22

def tenant_name_from_basic
  return 'public' unless @access_key_id = token.split(':').first
  credential.try(:schema_name) || 'public'
end

#tenant_name_from_bearerObject



36
37
38
39
# File 'lib/ros/tenant_middleware.rb', line 36

def tenant_name_from_bearer
  return 'public' unless  = urn.try(:account_id)
  Tenant.()
end

#urnObject



41
# File 'lib/ros/tenant_middleware.rb', line 41

def urn; Urn.from_jwt(token) end