Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion gems/smithy-client/lib/smithy-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@

# identity and auth

require_relative 'smithy-client/identity'
require_relative 'smithy-client/identity_provider'
require_relative 'smithy-client/refreshing_identity_provider'

Expand Down
25 changes: 25 additions & 0 deletions gems/smithy-client/lib/smithy-client/api_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Smithy
module Client
# Identity class for API Key authentication.
class ApiKey
def initialize(options = {})
@key = options[:key]
end

# @return [String, nil]
attr_reader :key

# @return [Boolean]
def set?
!!@key && [email protected]?
end

# @api private
def inspect
super.gsub(/@key="(\\"|[^"])*"/, '@key=[FILTERED]')
end
end
end
end
16 changes: 16 additions & 0 deletions gems/smithy-client/lib/smithy-client/api_key_provider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Smithy
module Client
# Provides an API key for authentication.
class ApiKeyProvider
include IdentityProvider

# @param [Hash] options
# @option options [String, nil] :key
def initialize(options = {})
@identity = ApiKey.new(key: options[:key])
end
end
end
end
25 changes: 25 additions & 0 deletions gems/smithy-client/lib/smithy-client/bearer_token.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Smithy
module Client
# Identity class for Bearer token authentication.
class BearerToken
def initialize(options = {})
@token = options[:token]
end

# @return [String, nil]
attr_reader :token

# @return [Boolean]
def set?
!!@token && [email protected]?
end

# @api private
def inspect
super.gsub(/@token="(\\"|[^"])*"/, '@token=[FILTERED]')
end
end
end
end
16 changes: 16 additions & 0 deletions gems/smithy-client/lib/smithy-client/bearer_token_provider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Smithy
module Client
# Provides a Bearer token for authentication.
class BearerTokenProvider
include IdentityProvider

# @param [Hash] options
# @option options [String] :token
def initialize(options = {})
@identity = BearerToken.new(token: options[:token])
end
end
end
end
15 changes: 0 additions & 15 deletions gems/smithy-client/lib/smithy-client/http_api_key_provider.rb

This file was deleted.

15 changes: 0 additions & 15 deletions gems/smithy-client/lib/smithy-client/http_bearer_provider.rb

This file was deleted.

16 changes: 0 additions & 16 deletions gems/smithy-client/lib/smithy-client/http_login_provider.rb

This file was deleted.

24 changes: 0 additions & 24 deletions gems/smithy-client/lib/smithy-client/identities/api_key.rb

This file was deleted.

22 changes: 0 additions & 22 deletions gems/smithy-client/lib/smithy-client/identities/http_login.rb

This file was deleted.

24 changes: 0 additions & 24 deletions gems/smithy-client/lib/smithy-client/identities/token.rb

This file was deleted.

15 changes: 0 additions & 15 deletions gems/smithy-client/lib/smithy-client/identity.rb

This file was deleted.

11 changes: 9 additions & 2 deletions gems/smithy-client/lib/smithy-client/identity_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

module Smithy
module Client
# This module provides basic accessors and methods for an
# Identity Provider class which provides an Identity.
# This module provides basic accessors and methods for an Identity provider.
module IdentityProvider
# @return [Identity]
attr_reader :identity

# @return [Time, nil]
attr_reader :expiration

# @return [Boolean]
def set?
!!identity && identity.set?
end
end
end
end
30 changes: 30 additions & 0 deletions gems/smithy-client/lib/smithy-client/login.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Smithy
module Client
# Identity class for login authentication.
class Login
def initialize(options = {})
@username = options[:username]
@password = options[:password]
end

# @return [String]
attr_reader :username

# @return [String]
attr_reader :password

# @return [Boolean]
def set?
# username and password can be empty strings
[email protected]? && [email protected]?
end

# @api private
def inspect
super.gsub(/@password="(\\"|[^"])*"/, '@password=[FILTERED]')
end
end
end
end
17 changes: 17 additions & 0 deletions gems/smithy-client/lib/smithy-client/login_provider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Smithy
module Client
# Provides a login credentials for authentication.
class LoginProvider
include IdentityProvider

# @param [Hash] options
# @option options [String, nil] :username
# @option options [String. nil] :password
def initialize(options = {})
@identity = Login.new(username: options[:username], password: options[:password])
end
end
end
end
25 changes: 14 additions & 11 deletions gems/smithy-client/lib/smithy-client/plugins/http_api_key_auth.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
# frozen_string_literal: true

require_relative '../http_api_key_provider'
require_relative '../identities/api_key'
require_relative '../api_key'
require_relative '../api_key_provider'

module Smithy
module Client
module Plugins
# @api private
class HttpApiKeyAuth < Plugin
option(
:http_api_key,
:api_key,
doc_type: String,
docstring: 'The API key to use for authentication.'
) do |config|
'stubbed-api-key' if config.stub_responses
end

option(
:http_api_key_provider,
doc_type: HttpApiKeyProvider,
:api_key_provider,
doc_type: ApiKeyProvider,
docstring: <<~DOCS) do |config|
An API key identity provider. This can be an instance of a {Smithy::Client::HttpApiKeyProvider} or any
class that responds to #identity and returns a {Smithy::Client::Identities::HttpApiKey}.
An API key identity provider. This can be an instance of a {Smithy::Client::ApiKeyProvider} or any
class that responds to #identity and returns a {Smithy::Client::ApiKey}.
DOCS
HttpApiKeyProvider.new(config.http_api_key) if config.http_api_key
provider = ApiKeyProvider.new(key: config.api_key)
provider if provider.set?
end

def after_initialize(client)
client.config.auth_schemes['smithy.api#httpApiKeyAuth'] = client.config.http_api_key_provider
client.config.auth_schemes['smithy.api#httpApiKeyAuth'] = client.config.api_key_provider
end

# @api private
Expand All @@ -41,9 +42,11 @@ def call(context)

def sign(context)
properties = context.config.service.traits['smithy.api#httpApiKeyAuth']
http_request = context.http_request
identity = context.config.api_key_provider.identity
case properties['in']
when 'header' then sign_in_header(properties, context.http_request, context.auth[:identity])
when 'query' then sign_in_query_param(properties, context.http_request, context.auth[:identity])
when 'header' then sign_in_header(properties, http_request, identity)
when 'query' then sign_in_query_param(properties, http_request, identity)
end
end

Expand Down
Loading