Skip to content

Commit ba5b8bf

Browse files
author
Matt Muller
committed
Refactor of auth_resolver
1 parent 4dc8d57 commit ba5b8bf

1 file changed

Lines changed: 47 additions & 27 deletions

File tree

gems/smithy/lib/smithy/views/client/auth_resolver.rb

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,45 @@ def module_name
1818
@plan.module_name
1919
end
2020

21-
# rubocop:disable Metrics/AbcSize
22-
# rubocop:disable Metrics/MethodLength
2321
def auth_rules_code
2422
lines = []
2523
lines << 'options = []'
2624
auth_operations = operations_with_auth_traits
2725
if auth_operations.empty?
28-
service_auth_schemes.each do |auth_scheme|
29-
lines << "options << '#{auth_scheme}'"
30-
end
26+
add_service_auth_schemes_to_code(lines)
3127
else
32-
lines << 'case parameters.operation_name'
33-
auth_operations.each do |id, operation|
34-
operation_name = Model::Shape.name(id).underscore
35-
lines << "when :#{operation_name}"
36-
operation_auth_schemes(operation).each do |auth_scheme|
37-
lines << " options << '#{auth_scheme}'"
38-
end
39-
end
40-
lines << 'else'
41-
service_auth_schemes.each do |auth_scheme|
42-
lines << " options << '#{auth_scheme}'"
43-
end
44-
lines << 'end'
28+
add_operation_case_to_code(lines, auth_operations)
4529
end
4630
lines << 'options'
4731
lines
4832
end
49-
# rubocop:enable Metrics/MethodLength
50-
# rubocop:enable Metrics/AbcSize
5133

5234
private
5335

36+
def add_service_auth_schemes_to_code(lines)
37+
service_auth_schemes.each do |auth_scheme|
38+
lines << "options << '#{auth_scheme}'"
39+
end
40+
end
41+
42+
def add_operation_case_to_code(lines, auth_operations)
43+
lines << 'case parameters.operation_name'
44+
auth_operations.each do |id, operation|
45+
operation_name = Model::Shape.name(id).underscore
46+
lines << "when :#{operation_name}"
47+
add_operation_auth_options_to_code(lines, operation)
48+
end
49+
lines << 'else'
50+
add_service_auth_schemes_to_code(lines)
51+
lines << 'end'
52+
end
53+
54+
def add_operation_auth_options_to_code(lines, operation)
55+
operation_auth_schemes(operation).each do |auth_scheme|
56+
lines << " options << '#{auth_scheme}'"
57+
end
58+
end
59+
5460
def auth_schemes(welds)
5561
weld_auth_schemes = welds.map(&:add_auth_schemes).reduce([], :+)
5662
weld_auth_schemes -= welds.map(&:remove_auth_schemes).reduce([], :+)
@@ -89,20 +95,34 @@ def optional_operation_auth?(operation)
8995

9096
def operation_auth_schemes(operation)
9197
operation_traits = operation.fetch('traits', {})
98+
auth_schemes = build_operation_auth_schemes(operation, operation_traits)
99+
auth_schemes << 'smithy.api#noAuth' if auth_schemes.empty? || optional_operation_auth?(operation)
100+
auth_schemes
101+
end
102+
103+
def build_operation_auth_schemes(operation, operation_traits)
92104
auth_schemes = []
93105
if operation_auth?(operation)
94-
operation_auth = operation_traits.fetch('smithy.api#auth', [])
95-
add_auth_schemes_from_auth_trait(auth_schemes, operation_auth)
96-
elsif optional_operation_auth?(operation)
97-
auth_schemes = service_auth_schemes << 'smithy.api#noAuth'
98-
auth_schemes.uniq!
99-
else
106+
add_explicit_operation_auth_schemes(auth_schemes, operation_traits)
107+
elsif !service_has_auth_trait? && !optional_operation_auth?(operation)
100108
add_registered_auth_schemes(auth_schemes, operation_traits)
109+
else
110+
add_service_auth_schemes_for_operation(auth_schemes)
101111
end
102-
auth_schemes << 'smithy.api#noAuth' if auth_schemes.empty?
103112
auth_schemes
104113
end
105114

115+
def add_explicit_operation_auth_schemes(auth_schemes, operation_traits)
116+
operation_auth = operation_traits.fetch('smithy.api#auth', [])
117+
add_auth_schemes_from_auth_trait(auth_schemes, operation_auth)
118+
end
119+
120+
def add_service_auth_schemes_for_operation(auth_schemes)
121+
service_auth_schemes.each do |auth_scheme|
122+
auth_schemes << auth_scheme unless auth_scheme == 'smithy.api#noAuth'
123+
end
124+
end
125+
106126
def add_auth_schemes_from_auth_trait(auth_schemes, auth_trait)
107127
auth_trait.each do |auth_scheme|
108128
auth_schemes << auth_scheme if @auth_schemes.include?(auth_scheme)

0 commit comments

Comments
 (0)