|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Smithy |
| 4 | + module Client |
| 5 | + # The result of resolving the authentication scheme for a request. |
| 6 | + ResolvedAuth = Struct.new(:scheme_id, :signer, :signer_properties, :identity_provider, keyword_init: true) |
| 7 | + |
| 8 | + # @api private |
| 9 | + module Auth |
| 10 | + class << self |
| 11 | + def resolve(context, endpoint_properties = {}) |
| 12 | + if endpoint_properties.key?('authSchemes') |
| 13 | + resolve_with_endpoint_auth(context.config, endpoint_properties['authSchemes']) |
| 14 | + else |
| 15 | + auth_parameters = context.client.class.auth_parameters.create(context) |
| 16 | + resolve_without_endpoint_auth(context.config, auth_parameters) |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + private |
| 21 | + |
| 22 | + def resolve_with_endpoint_auth(config, endpoint_auth_schemes) |
| 23 | + endpoint_auth_schemes_map = config.endpoint_auth_schemes |
| 24 | + normalized_endpoint_schemes = [] |
| 25 | + endpoint_auth_schemes.each do |scheme| |
| 26 | + normalized_scheme_id = endpoint_auth_schemes_map[scheme['name']] |
| 27 | + next unless normalized_scheme_id |
| 28 | + |
| 29 | + properties = {} |
| 30 | + scheme.each do |key, value| |
| 31 | + next if key == 'name' |
| 32 | + |
| 33 | + properties[key] = value |
| 34 | + end |
| 35 | + normalized_endpoint_schemes << { scheme_id: normalized_scheme_id, signer_properties: properties } |
| 36 | + end |
| 37 | + resolved_auth_options = prioritize_auth_options(normalized_endpoint_schemes, config.auth_scheme_preference) |
| 38 | + resolve_auth_scheme(config.auth_schemes, resolved_auth_options) |
| 39 | + end |
| 40 | + |
| 41 | + def resolve_without_endpoint_auth(config, auth_parameters) |
| 42 | + auth_options = config.auth_resolver.resolve(auth_parameters) |
| 43 | + resolved_auth_options = prioritize_auth_options(auth_options, config.auth_scheme_preference) |
| 44 | + resolve_auth_scheme(config.auth_schemes, resolved_auth_options) |
| 45 | + end |
| 46 | + |
| 47 | + def prioritize_auth_options(auth_options, auth_scheme_preference) |
| 48 | + return auth_options if auth_scheme_preference.empty? |
| 49 | + |
| 50 | + auth_options_by_id = {} |
| 51 | + auth_options.each do |option| |
| 52 | + auth_options_by_id[option[:scheme_id]] = option |
| 53 | + end |
| 54 | + |
| 55 | + preferred_options = [] |
| 56 | + auth_scheme_preference.each do |scheme_id| |
| 57 | + option = auth_options_by_id[scheme_id] |
| 58 | + next unless option |
| 59 | + |
| 60 | + preferred_options << option |
| 61 | + end |
| 62 | + |
| 63 | + preferred_options.empty? ? auth_options : preferred_options |
| 64 | + end |
| 65 | + |
| 66 | + def resolve_auth_scheme(auth_schemes, auth_options) # rubocop:disable Metrics/MethodLength |
| 67 | + raise 'No auth options were resolved' if auth_options.empty? |
| 68 | + |
| 69 | + failures = [] |
| 70 | + auth_options.each do |auth_option| |
| 71 | + scheme_id = auth_option[:scheme_id] |
| 72 | + if scheme_id == 'smithy.api#noAuth' |
| 73 | + return ResolvedAuth.new( |
| 74 | + scheme_id: 'smithy.api#noAuth', signer: NullSigner.new, signer_properties: {}, |
| 75 | + identity_provider: nil |
| 76 | + ) |
| 77 | + end |
| 78 | + auth_scheme = auth_schemes[scheme_id] |
| 79 | + error = validate_auth_scheme(auth_scheme, scheme_id) |
| 80 | + unless error |
| 81 | + return ResolvedAuth.new( |
| 82 | + scheme_id: scheme_id, |
| 83 | + signer: auth_scheme.signer, |
| 84 | + signer_properties: auth_option[:signer_properties] || {}, |
| 85 | + identity_provider: auth_scheme.identity_provider |
| 86 | + ) |
| 87 | + end |
| 88 | + |
| 89 | + failures << error |
| 90 | + end |
| 91 | + |
| 92 | + raise failures.join("\n") |
| 93 | + end |
| 94 | + |
| 95 | + def validate_auth_scheme(auth_scheme, scheme_id) |
| 96 | + return "Auth scheme #{scheme_id} was not enabled for this request" unless auth_scheme |
| 97 | + |
| 98 | + identity_provider = auth_scheme.identity_provider |
| 99 | + return "Auth scheme #{scheme_id} did not have an identity provider configured" unless identity_provider |
| 100 | + return "Auth scheme #{scheme_id} failed to resolve identity" unless identity_provider.set? |
| 101 | + |
| 102 | + nil |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments