-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from IBM/1.0.0.rc
1.0.0
- Loading branch information
Showing
25 changed files
with
989 additions
and
695 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require("json") | ||
|
||
module IBMCloudSdkCore | ||
# Authenticator | ||
class Authenticator | ||
AUTH_TYPE_BASIC = "basic" | ||
AUTH_TYPE_BEARER_TOKEN = "bearerToken" | ||
AUTH_TYPE_CP4D = "cp4d" | ||
AUTH_TYPE_IAM = "iam" | ||
AUTH_TYPE_NO_AUTH = "noAuth" | ||
|
||
def authenticate | ||
# Adds the Authorization header, if possible | ||
end | ||
|
||
def validate | ||
# Checks if all the inputs needed are present | ||
end | ||
end | ||
end |
36 changes: 36 additions & 0 deletions
36
lib/ibm_cloud_sdk_core/authenticators/basic_authenticator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require("json") | ||
require_relative("./authenticator.rb") | ||
require_relative("../utils.rb") | ||
|
||
module IBMCloudSdkCore | ||
# Basic Authenticator | ||
class BasicAuthenticator < Authenticator | ||
attr_accessor :username, :password, :authentication_type | ||
def initialize(vars) | ||
defaults = { | ||
username: nil, | ||
password: nil | ||
} | ||
vars = defaults.merge(vars) | ||
@username = vars[:username] | ||
@password = vars[:password] | ||
@authentication_type = AUTH_TYPE_BASIC | ||
validate | ||
end | ||
|
||
# Adds the Authorization header, if possible | ||
def authenticate(headers) | ||
base64_authentication = Base64.strict_encode64("#{@username}:#{@password}") | ||
headers["Authorization"] = "Basic #{base64_authentication}" | ||
end | ||
|
||
# Checks if all the inputs needed are present | ||
def validate | ||
raise ArgumentError.new("The username and password shouldn\'t be None.") if @username.nil? || @password.nil? | ||
raise ArgumentError.new('The username shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your username') if check_bad_first_or_last_char(@username) | ||
raise ArgumentError.new('The password shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your password') if check_bad_first_or_last_char(@password) | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
lib/ibm_cloud_sdk_core/authenticators/bearer_token_authenticator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require("json") | ||
require_relative("./authenticator.rb") | ||
require_relative("../utils.rb") | ||
|
||
module IBMCloudSdkCore | ||
# Basic Authenticator | ||
class BearerTokenAuthenticator < Authenticator | ||
attr_accessor :authentication_type | ||
def initialize(vars) | ||
defaults = { | ||
bearer_token: nil | ||
} | ||
vars = defaults.merge(vars) | ||
@bearer_token = vars[:bearer_token] | ||
@authentication_type = AUTH_TYPE_BEARER_TOKEN | ||
validate | ||
end | ||
|
||
# Adds the Authorization header, if possible | ||
def authenticate(headers) | ||
headers["Authorization"] = "Bearer #{@bearer_token}" | ||
end | ||
|
||
# Checks if all the inputs needed are present | ||
def validate | ||
raise ArgumentError.new("The bearer token shouldn\'t be None.") if @bearer_token.nil? | ||
end | ||
end | ||
end |
38 changes: 38 additions & 0 deletions
38
lib/ibm_cloud_sdk_core/authenticators/config_based_authenticator_factory.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require("json") | ||
require_relative("./authenticator.rb") | ||
require_relative("./basic_authenticator.rb") | ||
require_relative("./bearer_token_authenticator.rb") | ||
require_relative("./cp4d_authenticator.rb") | ||
require_relative("./iam_authenticator.rb") | ||
require_relative("./no_auth_authenticator.rb") | ||
require_relative("../utils.rb") | ||
|
||
module IBMCloudSdkCore | ||
# Authenticator | ||
class ConfigBasedAuthenticatorFactory < Authenticator | ||
# Checks the credentials file and VCAP_SERVICES environment variable | ||
# :param service_name: The service name | ||
# :return: the authenticator | ||
def get_authenticator(service_name:) | ||
config = get_service_properties(service_name) | ||
return construct_authenticator(config) unless config.nil? || config.empty? | ||
end | ||
|
||
def construct_authenticator(config) | ||
if config[:auth_type].nil? | ||
auth_type = "iam" | ||
else | ||
auth_type = config[:auth_type] | ||
end | ||
config.delete(:url) unless config[:url].nil? | ||
config[:url] = config[:auth_url] unless config[:auth_url].nil? | ||
return BasicAuthenticator.new(config) if auth_type.casecmp(AUTH_TYPE_BASIC).zero? | ||
return BearerTokenAuthenticator.new(config) if auth_type.casecmp(AUTH_TYPE_BEARER_TOKEN).zero? | ||
return CloudPakForDataAuthenticator.new(config) if auth_type.casecmp(AUTH_TYPE_CP4D).zero? | ||
return IamAuthenticator.new(config) if auth_type.casecmp(AUTH_TYPE_IAM).zero? | ||
return NoAuthAuthenticator.new if auth_type.casecmp(AUTH_TYPE_NO_AUTH).zero? | ||
end | ||
end | ||
end |
49 changes: 49 additions & 0 deletions
49
lib/ibm_cloud_sdk_core/authenticators/cp4d_authenticator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require("json") | ||
require_relative("./authenticator.rb") | ||
require_relative("../token_managers/cp4d_token_manager.rb") | ||
require_relative("../utils.rb") | ||
|
||
module IBMCloudSdkCore | ||
# Basic Authenticator | ||
class CloudPakForDataAuthenticator < Authenticator | ||
attr_accessor :authentication_type, :disable_ssl_verification | ||
def initialize(vars) | ||
defaults = { | ||
username: nil, | ||
password: nil, | ||
url: nil, | ||
disable_ssl_verification: false | ||
} | ||
vars = defaults.merge(vars) | ||
@username = vars[:username] | ||
@password = vars[:password] | ||
@url = vars[:url] | ||
@disable_ssl_verification = vars[:disable_ssl_verification] | ||
@authentication_type = AUTH_TYPE_CP4D | ||
|
||
validate | ||
@token_manager = CP4DTokenManager.new( | ||
url: @url, | ||
username: @username, | ||
password: @password, | ||
disable_ssl_verification: @disable_ssl_verification | ||
) | ||
end | ||
|
||
# Adds the Authorization header, if possible | ||
def authenticate(headers) | ||
headers["Authorization"] = "Bearer #{@token_manager.access_token}" | ||
end | ||
|
||
# Checks if all the inputs needed are present | ||
def validate | ||
raise ArgumentError.new("The username or password shouldn\'t be None.") if @username.nil? || @password.nil? | ||
raise ArgumentError.new("The url shouldn\'t be None.") if @url.nil? | ||
raise ArgumentError.new('The username shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your username') if check_bad_first_or_last_char(@username) | ||
raise ArgumentError.new('The password shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your password') if check_bad_first_or_last_char(@password) | ||
raise ArgumentError.new('The url shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your url') if check_bad_first_or_last_char(@url) | ||
end | ||
end | ||
end |
50 changes: 50 additions & 0 deletions
50
lib/ibm_cloud_sdk_core/authenticators/iam_authenticator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
require("json") | ||
require_relative("./authenticator.rb") | ||
require_relative("../token_managers/iam_token_manager.rb") | ||
require_relative("../utils.rb") | ||
|
||
module IBMCloudSdkCore | ||
# Basic Authenticator | ||
class IamAuthenticator < Authenticator | ||
attr_accessor :authentication_type, :disable_ssl_verification, :client_id, :client_secret | ||
def initialize(vars) | ||
defaults = { | ||
url: nil, | ||
client_id: nil, | ||
client_secret: nil, | ||
disable_ssl_verification: nil | ||
} | ||
vars = defaults.merge(vars) | ||
@apikey = vars[:apikey] | ||
@url = vars[:url] | ||
@client_id = vars[:client_id] | ||
@client_secret = vars[:client_secret] | ||
@disable_ssl_verification = vars[:disable_ssl_verification] | ||
@authentication_type = AUTH_TYPE_IAM | ||
|
||
validate | ||
@token_manager = IAMTokenManager.new( | ||
apikey: @apikey, | ||
url: @url, | ||
client_id: @client_id, | ||
client_secret: @client_secret, | ||
disable_ssl_verification: @disable_ssl_verification | ||
) | ||
end | ||
|
||
def authenticate(headers) | ||
headers["Authorization"] = "Bearer #{@token_manager.access_token}" | ||
end | ||
|
||
def validate | ||
# Adds the Authorization header, if possible | ||
raise ArgumentError.new("The apikey shouldn\'t be None.") if @apikey.nil? | ||
raise ArgumentError.new('The apikey shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your apikey') if check_bad_first_or_last_char(@apikey) | ||
|
||
# Both the client id and secret should be provided or neither should be provided. | ||
raise ArgumentError.new("Only one of 'client_id' or 'client_secret' were specified, but both parameters should be specified together.") if (@client_id.nil? && !@client_secret.nil?) || (!@client_id.nil? && @client_secret.nil?) | ||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
lib/ibm_cloud_sdk_core/authenticators/no_auth_authenticator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require("json") | ||
require_relative("./authenticator.rb") | ||
|
||
module IBMCloudSdkCore | ||
# Authenticator | ||
class NoAuthAuthenticator < Authenticator | ||
def initialize | ||
@authentication_type = AUTH_TYPE_NO_AUTH | ||
end | ||
|
||
def authenticate | ||
nil | ||
end | ||
|
||
def validate | ||
nil | ||
end | ||
end | ||
end |
Oops, something went wrong.