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

Instance Method Summary collapse

Methods included from DependencyInjection

container_instance

Constructor Details

#initialize(value) ⇒ AuthorizationHeader

Returns a new instance of AuthorizationHeader.



294
295
296
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 294

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

Instance Method Details

#basic_auth?Boolean

Returns:

  • (Boolean)


322
323
324
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 322

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

#blank?Boolean

Returns:

  • (Boolean)


326
327
328
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 326

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

#present?Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 310

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

#raw_headerObject



306
307
308
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 306

def raw_header
  @value
end

#token_auth?Boolean

Returns:

  • (Boolean)


318
319
320
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 318

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

#unauthorized_token?Boolean

Returns:

  • (Boolean)


314
315
316
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 314

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

#userObject



298
299
300
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 298

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.



331
332
333
334
335
336
337
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 331

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

Returns:

  • (Boolean)


302
303
304
# File 'lib/smart_proxy_container_gateway/container_gateway_api.rb', line 302

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