-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingle_auth.rb
47 lines (40 loc) · 1.55 KB
/
single_auth.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module CoreLibrary
# The class to handle the single authentication for a particular request.
class Single < Authentication
# Getter for the error message for auth.
# @return [String] The error message while applying the auth.
def error_message
"[#{@error_message}]"
end
# Initializes a new instance of Single.
# @param [String] auth_participant Auth participant name.
def initialize(auth_participant)
@auth_participant = auth_participant
@mapped_auth = nil
@error_message = nil
@is_valid = false
end
# Extracts out the auth from the given auth managers.
# @param [Hash] auth_managers The hash of auth managers.
# @return [Single] An updated instance of itself.
def with_auth_managers(auth_managers)
raise ArgumentError, 'Auth key is invalid.' unless auth_managers.key?(@auth_participant)
@mapped_auth = auth_managers[@auth_participant]
self
end
# Checks if the associated auth is valid.
# @return [Boolean] True if the associated auth is valid, false otherwise.
def valid
raise ArgumentError, 'The auth manager entry must not have a nil value.' if @mapped_auth.nil?
@is_valid = @mapped_auth.valid
@error_message = @mapped_auth.error_message unless @is_valid
@is_valid
end
# Applies the associated auth to the HTTP request.
# @param [HttpRequest] http_request The HTTP request on which the auth is to be applied.
def apply(http_request)
return unless @is_valid
@mapped_auth.apply(http_request)
end
end
end