-
Notifications
You must be signed in to change notification settings - Fork 18
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 #15 from IBM/feat/auth-methods
Feat/auth methods
- Loading branch information
Showing
20 changed files
with
1,679 additions
and
1,020 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright 2019 IBM Corp. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
using IBM.Cloud.SDK.Connection; | ||
|
||
namespace IBM.Cloud.SDK.Authentication | ||
{ | ||
public class Authenticator | ||
{ | ||
/// <summary> | ||
/// These are the valid authentication types. | ||
/// </summary> | ||
public const string AuthTypeBasic = "basic"; | ||
public const string AuthTypeNoAuth = "noAuth"; | ||
public const string AuthTypeIam = "iam"; | ||
public const string AuthTypeCp4d = "cp4d"; | ||
public const string AuthTypeBearer = "bearerToken"; | ||
|
||
/// <summary> | ||
/// Constants which define the names of external config propreties (credential file, environment variable, etc.). | ||
/// </summary> | ||
public static string PropNameAuthType = "AUTH_TYPE"; | ||
public static string PropNameUsername = "USERNAME"; | ||
public static string PropNamePassword = "PASSWORD"; | ||
public static string PropNameBearerToken = "BEARER_TOKEN"; | ||
public static string PropNameUrl = "AUTH_URL"; | ||
public static string PropNameDisableSslVerification = "AUTH_DISABLE_SSL"; | ||
public static string PropNameApikey = "APIKEY"; | ||
public static string PropNameClientId = "CLIENT_ID"; | ||
public static string PropNameClientSecret = "CLIENT_SECRET"; | ||
|
||
public static string ErrorMessagePropMissing = "The {0} property is required but was not specified."; | ||
public static string ErrorMessagePropInvalid = "The {0} property is invalid. Please remove any surrounding {{, }}, or \" characters."; | ||
public static string ErrorMessageReqFailed = "Error while fetching access token from token service: "; | ||
|
||
public string Url { get; set; } | ||
|
||
/// <summary> | ||
/// Returns the authentication type associated with the Authenticator instance. | ||
/// </summary> | ||
virtual public string AuthenticationType { get; } | ||
|
||
/// <summary> | ||
/// Check if authenticator has everything it needs to authenticate. Every child class overrides this method. | ||
/// </summary> | ||
virtual public bool CanAuthenticate() { | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Perform the necessary authentication steps for the specified request. | ||
/// </summary> | ||
virtual public void Authenticate(RESTConnector connector) { } | ||
|
||
/// <summary> | ||
/// Perform the necessary authentication steps for the specified request. | ||
/// </summary> | ||
virtual public void Authenticate(WSConnector connector) { } | ||
|
||
/// <summary> | ||
/// Validates the current set of configuration information in the Authenticator. | ||
/// </summary> | ||
virtual public void Validate() { } | ||
} | ||
} |
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,129 @@ | ||
/** | ||
* Copyright 2019 IBM Corp. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
using IBM.Cloud.SDK.Connection; | ||
using IBM.Cloud.SDK.Utilities; | ||
using System; | ||
using System.Collections.Generic; | ||
using Utility = IBM.Cloud.SDK.Utilities.Utility; | ||
|
||
namespace IBM.Cloud.SDK.Authentication.BasicAuth | ||
{ | ||
/// <summary> | ||
/// This class implements support for Basic Authentication. The main purpose of this authenticator is to construct the | ||
/// Authorization header and then add it to each outgoing REST API request. | ||
/// </summary> | ||
public class BasicAuthenticator : Authenticator | ||
{ | ||
/// <summary> | ||
/// The username configured on this authenticator | ||
/// </summary> | ||
public string Username { get; private set; } | ||
/// <summary> | ||
/// The password configured on this authenticator | ||
/// </summary> | ||
public string Password { get; private set; } | ||
|
||
/// <summary> | ||
/// Construct a BasicAuthenticator instance with the specified username and password. | ||
/// These values are used to construct an Authorization header value that will be included | ||
/// in outgoing REST API requests. | ||
/// </summary> | ||
/// <param name="username">The basic auth username</param> | ||
/// <param name="password">The basic auth password</param> | ||
public BasicAuthenticator(string username, string password) | ||
{ | ||
Init(username, password); | ||
} | ||
|
||
/// <summary> | ||
/// Construct a BasicAuthenticator using properties retrieved from the specified Map. | ||
/// </summary> | ||
/// <param name="config">A map containing the username and password values</param> | ||
public BasicAuthenticator(Dictionary<string, string> config) | ||
{ | ||
config.TryGetValue(PropNameUsername, out string username); | ||
config.TryGetValue(PropNamePassword, out string password); | ||
Init(username, password); | ||
} | ||
|
||
private void Init(string username, string password) | ||
{ | ||
Username = username; | ||
Password = password; | ||
|
||
Validate(); | ||
} | ||
|
||
public override string AuthenticationType | ||
{ | ||
get { return AuthTypeBasic; } | ||
} | ||
|
||
/// <summary> | ||
/// BasicAuthenticator is not waiting for token data so always return true. | ||
/// </summary> | ||
/// <returns></returns> | ||
public override bool CanAuthenticate() | ||
{ | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// This method is called to authenticate an outgoing REST API request. | ||
/// Here, we'll just set the Authorization header to provide the necessary authentication info. | ||
/// </summary> | ||
/// <param name="connector"></param> | ||
public override void Authenticate(RESTConnector connector) | ||
{ | ||
connector.WithAuthentication(Username, Password); | ||
} | ||
|
||
/// <summary> | ||
/// This method is called to authenticate an outgoing REST API request. | ||
/// Here, we'll just set the Authorization header to provide the necessary authentication info. | ||
/// </summary> | ||
/// <param name="connector"></param> | ||
public override void Authenticate(WSConnector connector) | ||
{ | ||
connector.WithAuthentication(Username, Password); | ||
} | ||
|
||
public override void Validate() | ||
{ | ||
if (string.IsNullOrEmpty(Username)) | ||
{ | ||
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Username")); | ||
} | ||
|
||
if (string.IsNullOrEmpty(Password)) | ||
{ | ||
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Password")); | ||
} | ||
|
||
if (Utility.HasBadFirstOrLastCharacter(Username)) | ||
{ | ||
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Username")); | ||
} | ||
|
||
if (Utility.HasBadFirstOrLastCharacter(Password)) | ||
{ | ||
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Password")); | ||
} | ||
} | ||
} | ||
} |
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,112 @@ | ||
/** | ||
* Copyright 2019 IBM Corp. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
using IBM.Cloud.SDK.Connection; | ||
using IBM.Cloud.SDK.Utilities; | ||
using System; | ||
using System.Collections.Generic; | ||
using Utility = IBM.Cloud.SDK.Utilities.Utility; | ||
|
||
namespace IBM.Cloud.SDK.Authentication.Bearer | ||
{ | ||
/// <summary> | ||
/// This class implements support for Bearer Token Authentication. The main purpose of this authenticator is to construct the | ||
/// Authorization header and then add it to each outgoing REST API request. | ||
/// </summary> | ||
public class BearerTokenAuthenticator : Authenticator | ||
{ | ||
/// <summary> | ||
/// The access token configured for this authenticator | ||
/// </summary> | ||
public string BearerToken { get; set; } | ||
|
||
/// <summary> | ||
/// Construct a BearerTokenAuthenticator instance with the specified access token. | ||
/// The token value will be used to construct an Authorization header that will be included | ||
/// in outgoing REST API requests. | ||
/// </summary> | ||
/// <param name="bearerToken">The access token value</param> | ||
public BearerTokenAuthenticator(string bearerToken) | ||
{ | ||
Init(bearerToken); | ||
} | ||
|
||
/// <summary> | ||
/// Construct a BearerTokenAuthenticator using properties retrieved from the specified Map. | ||
/// </summary> | ||
/// <param name="config">Config a map containing the access token value</param> | ||
public BearerTokenAuthenticator(Dictionary<string, string> config) | ||
{ | ||
config.TryGetValue(PropNameBearerToken, out string bearerToken); | ||
Init(bearerToken); | ||
} | ||
|
||
private void Init(string bearerToken) | ||
{ | ||
BearerToken = bearerToken; | ||
|
||
Validate(); | ||
} | ||
|
||
/// <summary> | ||
/// Do we have BearerToken? | ||
/// </summary> | ||
/// <returns></returns> | ||
public override bool CanAuthenticate() | ||
{ | ||
return BearerToken != null; | ||
} | ||
|
||
public override string AuthenticationType | ||
{ | ||
get { return AuthTypeBearer; } | ||
} | ||
|
||
/// <summary> | ||
/// This method is called to authenticate an outgoing REST API request. | ||
/// Here, we'll just set the Authorization header to provide the necessary authentication info. | ||
/// </summary> | ||
/// <param name="connector"></param> | ||
public override void Authenticate(RESTConnector connector) | ||
{ | ||
connector.WithAuthentication(BearerToken); | ||
} | ||
|
||
/// <summary> | ||
/// This method is called to authenticate an outgoing REST API request. | ||
/// Here, we'll just set the Authorization header to provide the necessary authentication info. | ||
/// </summary> | ||
/// <param name="connector"></param> | ||
public override void Authenticate(WSConnector connector) | ||
{ | ||
connector.WithAuthentication(BearerToken); | ||
} | ||
|
||
public override void Validate() | ||
{ | ||
if (string.IsNullOrEmpty(BearerToken)) | ||
{ | ||
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "BearerToken")); | ||
} | ||
|
||
if (Utility.HasBadFirstOrLastCharacter(BearerToken)) | ||
{ | ||
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "BearerToken")); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.