Skip to content

Commit c885c23

Browse files
author
Matt Muller
committed
WIP: rework of credentials
1 parent 8d0ea6b commit c885c23

67 files changed

Lines changed: 807 additions & 616 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

gems/smithy-client/lib/smithy-client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
# identity and auth
5252

53-
require_relative 'smithy-client/identity'
53+
require_relative 'smithy-client/auth'
5454
require_relative 'smithy-client/identity_provider'
5555
require_relative 'smithy-client/refreshing_identity_provider'
5656

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Smithy
4+
module Client
5+
# Identity class for API Key authentication.
6+
class ApiKey
7+
def initialize(options = {})
8+
@key = options[:key]
9+
end
10+
11+
# @return [String, nil]
12+
attr_reader :key
13+
14+
# @return [Boolean]
15+
def set?
16+
!!@key && !@key.empty?
17+
end
18+
19+
# @api private
20+
def inspect
21+
super.gsub(/@key="(\\"|[^"])*"/, '@key=[FILTERED]')
22+
end
23+
end
24+
end
25+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module Smithy
4+
module Client
5+
# Provides an API key for authentication.
6+
class ApiKeyProvider
7+
include IdentityProvider
8+
9+
# @param [Hash] options
10+
# @option options [String, nil] :key
11+
def initialize(options = {})
12+
@identity = ApiKey.new(key: options[:key])
13+
end
14+
end
15+
end
16+
end
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
module Smithy
2+
module Client
3+
# @api private
4+
module Auth
5+
class << self
6+
def resolve_auth(context, endpoint_properties = {})
7+
if endpoint_properties.key?('authSchemes')
8+
resolve_auth_scheme_with_endpoint(context, endpoint_properties['authSchemes'])
9+
else
10+
resolve_auth_scheme_without_endpoint(context)
11+
end
12+
end
13+
14+
private
15+
16+
def resolve_auth_scheme_with_endpoint(context, endpoint_auth_schemes)
17+
normalized_endpoint_schemes = []
18+
endpoint_auth_schemes.each do |scheme|
19+
scheme_id = context.config.endpoint_auth_schemes[scheme['name']]
20+
next unless scheme_id
21+
22+
normalized_scheme = { scheme_id: scheme_id }
23+
scheme.each do |key, value|
24+
next if key == 'name'
25+
26+
normalized_scheme[key] = value
27+
end
28+
normalized_endpoint_schemes << normalized_scheme
29+
end
30+
resolved_auth_options = prioritize_auth_options(
31+
normalized_endpoint_schemes,
32+
context.config.auth_scheme_preference
33+
)
34+
resolve_auth_scheme(context.config.auth_schemes, resolved_auth_options)
35+
end
36+
37+
def resolve_auth_scheme_without_endpoint(context)
38+
auth_parameters = context.client.class.auth_parameters.create(context)
39+
auth_options = context.config.auth_resolver.resolve(auth_parameters)
40+
resolved_auth_options = prioritize_auth_options(auth_options, context.config.auth_scheme_preference)
41+
resolve_auth_scheme(context.config.auth_schemes, resolved_auth_options)
42+
end
43+
44+
def prioritize_auth_options(auth_options, auth_scheme_preference)
45+
return auth_options if auth_scheme_preference.empty?
46+
47+
auth_options_by_id = {}
48+
auth_options.each do |option|
49+
auth_options_by_id[option[:scheme_id]] = option
50+
end
51+
52+
preferred_options = []
53+
auth_scheme_preference.each do |scheme_id|
54+
option = auth_options_by_id[scheme_id]
55+
next unless option
56+
57+
preferred_options << option
58+
end
59+
60+
preferred_options.empty? ? auth_options : preferred_options
61+
end
62+
63+
def resolve_auth_scheme(auth_schemes, auth_options)
64+
raise 'No auth options were resolved' if auth_options.empty?
65+
66+
failures = []
67+
auth_options.each do |auth_option|
68+
scheme_id = auth_option[:scheme_id]
69+
70+
# Anonymous auth does not have a plugin and does not sign
71+
return auth_option if scheme_id == 'smithy.api#noAuth'
72+
73+
error = validate_auth_scheme(auth_schemes, scheme_id)
74+
return auth_option unless error
75+
76+
failures << error
77+
end
78+
79+
raise failures.join("\n")
80+
end
81+
82+
def validate_auth_scheme(auth_schemes, scheme_id)
83+
return "Auth scheme #{scheme_id} was not enabled for this request" unless auth_schemes.key?(scheme_id)
84+
85+
identity_provider = auth_schemes[scheme_id]
86+
return "Auth scheme #{scheme_id} did not have an identity provider configured" unless identity_provider
87+
return "Auth scheme #{scheme_id} failed to resolve identity" unless identity_provider.set?
88+
89+
nil
90+
end
91+
end
92+
end
93+
end
94+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Smithy
4+
module Client
5+
# Identity class for Bearer token authentication.
6+
class BearerToken
7+
def initialize(options = {})
8+
@token = options[:token]
9+
end
10+
11+
# @return [String, nil]
12+
attr_reader :token
13+
14+
# @return [Boolean]
15+
def set?
16+
!!@token && !@token.empty?
17+
end
18+
19+
# @api private
20+
def inspect
21+
super.gsub(/@token="(\\"|[^"])*"/, '@token=[FILTERED]')
22+
end
23+
end
24+
end
25+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module Smithy
4+
module Client
5+
# Provides a Bearer token for authentication.
6+
class BearerTokenProvider
7+
include IdentityProvider
8+
9+
# @param [Hash] options
10+
# @option options [String] :token
11+
def initialize(options = {})
12+
@identity = BearerToken.new(token: options[:token])
13+
end
14+
end
15+
end
16+
end

gems/smithy-client/lib/smithy-client/http_api_key_provider.rb

Lines changed: 0 additions & 15 deletions
This file was deleted.

gems/smithy-client/lib/smithy-client/http_bearer_provider.rb

Lines changed: 0 additions & 15 deletions
This file was deleted.

gems/smithy-client/lib/smithy-client/http_login_provider.rb

Lines changed: 0 additions & 16 deletions
This file was deleted.

gems/smithy-client/lib/smithy-client/identities/api_key.rb

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)