Class: Faraday::Middlewares::BuildService::Authentication

Inherits:
Faraday::Middleware
  • Object
show all
Includes:
HttpHelpers
Defined in:
lib/faraday/middlewares/build_service/authentication.rb

Instance Method Summary collapse

Methods included from HttpHelpers

#pairs_to_payload, #pairs_to_quoted_string, #parse_authorization_header, #parse_list_header, #unquote

Constructor Details

#initialize(app, credentials: {}) ⇒ Authentication

Returns a new instance of Authentication.

Parameters:

  • env (Faraday::Env)
  • credentials (Hash) (defaults to: {})


14
15
16
17
18
19
20
21
22
# File 'lib/faraday/middlewares/build_service/authentication.rb', line 14

def initialize(app, credentials: {})
  super(app)

  @username = credentials[:username]
  @password = credentials[:password]
  @ssh_key = credentials[:ssh_key]

  reset!
end

Instance Method Details

#call(env) ⇒ Object

Parameters:

  • env (Faraday::Env)


30
31
32
33
34
35
36
37
38
39
# File 'lib/faraday/middlewares/build_service/authentication.rb', line 30

def call(env)
  request_body = env[:body]
  super(env)
rescue BuildService::RetryWithAuthError
  # after failure env[:body] is set to the response body
  # see https://github.com/lostisland/faraday-retry/blob/main/lib/faraday/retry/middleware.rb
  env[:body] = request_body
  # retry only once
  super(env)
end

#do_basic_auth!(_env, _details) ⇒ Object

Parameters:

  • env (Faraday::Env)
  • _details (Hash)

Raises:



81
82
83
84
85
86
87
88
# File 'lib/faraday/middlewares/build_service/authentication.rb', line 81

def do_basic_auth!(_env, _details)
  raise MissingCredentialsError.new("Missing Username / Password") unless @username || @password

  # Build an HTTP Basic Auth header
  value = Base64.encode64([@username, @password].join(":"))
  value.delete!("\n")
  @authorization = "Basic #{value}"
end

#do_signature_auth!(env, details) ⇒ Object

Parameters:

  • env (Faraday::Env)
  • _details (Hash)

Raises:



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/faraday/middlewares/build_service/authentication.rb', line 92

def do_signature_auth!(env, details)
  raise MissingCredentialsError.new("Missing Username / SSH Key") unless @username || @ssh_key

  # Build an HTTP Signature Auth header
  payload = SshSigner.new(details, env[:response_headers], credentials: {
    username: @username,
    password: @password,
    ssh_key: @ssh_key
  }).generate
  @authorization = "Signature #{payload}"
end

#on_complete(env) ⇒ Object

Parameters:

  • env (Faraday::Env)

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/faraday/middlewares/build_service/authentication.rb', line 53

def on_complete(env)
  # If we have get cookie, we just save it, nothing else to do.
  if (cookie = env[:response_headers]["set-cookie"])
    @cookie_jar.parse(cookie, env[:url]) and return
  end

  # if there's no challenge response, means we're good
  return unless env[:response_headers]["www-authenticate"]

  challenges = parse_authorization_header(env[:response_headers]["www-authenticate"])
  # If there's a challenge response AND we did authorization, then we failed.
  # Stop here and let the failure bubble up.
  return if @authorization

  # Do the authorization challenges
  challenges.each do |type, details|
    next do_basic_auth!(env, details) if type.to_s.casecmp("basic").zero?
    next do_signature_auth!(env, details) if type.to_s.casecmp("signature").zero?

    raise UnknownChallengeError.new("Unknown challenge #{type}: #{details.inspect}")
  end

  # Signal to #call that we need to retry the request.
  raise BuildService::RetryWithAuthError.new("Retry!")
end

#on_request(env) ⇒ Object

Parameters:

  • env (Faraday::Env)


42
43
44
45
46
47
48
49
50
# File 'lib/faraday/middlewares/build_service/authentication.rb', line 42

def on_request(env)
  # If we have a cookie, use it and skip setting any other header.
  if (cookie = @cookie_jar.cookies(env.url) && cookie&.any?)
    env.request_headers["cookie"] = ::HTTP::Cookie.cookie_value(cookie)
    return
  end

  env.request_headers["Authorization"] = @authorization if @authorization
end

#reset!Object



24
25
26
27
# File 'lib/faraday/middlewares/build_service/authentication.rb', line 24

def reset!
  @cookie_jar = ::HTTP::CookieJar.new
  @authorization = nil
end