Class: Proxy::ContainerGateway::Api::AuthorizationHeader

Inherits:
Object
  • Object
show all
Extended by:
DependencyInjection
Defined in:
lib/smart_proxy_container_gateway/container_gateway_api.rb

Constant Summary collapse

UNAUTHORIZED_TOKEN =
'unauthorized'.freeze
UNAUTHENTICATED_TOKEN =
'unauthenticated'.freeze

Instance Method Summary collapse

Methods included from DependencyInjection

container_instance

Constructor Details

#initialize(value) ⇒ AuthorizationHeader



422
423
424
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 422

def initialize(value)
  @value = value || ''
end

Instance Method Details

#basic_auth?Boolean



454
455
456
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 454

def basic_auth?
  @value.split(' ')[0] == 'Basic'
end

#blank?Boolean



458
459
460
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 458

def blank?
  Base64.decode64(@value.split(' ')[1]) == ':'
end

#present?Boolean



438
439
440
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 438

def present?
  !@value.nil? && @value != ""
end

#raw_headerObject



434
435
436
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 434

def raw_header
  @value
end

#token_auth?Boolean



450
451
452
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 450

def token_auth?
  @value.split(' ')[0] == 'Bearer'
end

#unauthenticated_token?Boolean



446
447
448
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 446

def unauthenticated_token?
  @value.split(' ')[1] == UNAUTHENTICATED_TOKEN
end

#unauthorized_token?Boolean



442
443
444
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 442

def unauthorized_token?
  @value.split(' ')[1] == UNAUTHORIZED_TOKEN
end

#userObject



426
427
428
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 426

def user
  container_gateway_main.token_user(@value.split(' ')[1])
end

#v1_foreman_authorized_usernameObject

A special case for the V1 API. Defer authentication to Foreman and return the username. ‘nil` if not authorized.



463
464
465
466
467
468
469
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 463

def v1_foreman_authorized_username
  username = Base64.decode64(@value.split(' ')[1]).split(':')[0]
  auth_response = ForemanApi.new.fetch_token(raw_header, { 'account' => username })
  return username if auth_response.code.to_i == 200 && (JSON.parse(auth_response.body)['token'] != 'unauthenticated')

  nil
end

#valid_user_token?Boolean



430
431
432
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 430

def valid_user_token?
  token_auth? && container_gateway_main.valid_token?(@value.split(' ')[1])
end