Class: Faraday::Middlewares::BuildService::Authentication
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Faraday::Middlewares::BuildService::Authentication
show all
- Includes:
- HttpHelpers
- Defined in:
- lib/faraday/middlewares/build_service/authentication.rb
Instance Method Summary
collapse
#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.
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
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
env[:body] = request_body
super(env)
end
|
#do_basic_auth!(_env, _details) ⇒ Object
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
value = Base64.encode64([@username, @password].join(":"))
value.delete!("\n")
@authorization = "Basic #{value}"
end
|
#do_signature_auth!(env, details) ⇒ Object
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
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
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 (cookie = env[:response_headers]["set-cookie"])
@cookie_jar.parse(cookie, env[:url]) and return
end
return unless env[:response_headers]["www-authenticate"]
challenges = (env[:response_headers]["www-authenticate"])
return if @authorization
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
raise BuildService::RetryWithAuthError.new("Retry!")
end
|
#on_request(env) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/faraday/middlewares/build_service/authentication.rb', line 42
def on_request(env)
if (cookie = @cookie_jar.cookies(env.url) && cookie&.any?)
env.["cookie"] = ::HTTP::Cookie.cookie_value(cookie)
return
end
env.["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
|