-
Notifications
You must be signed in to change notification settings - Fork 18
Feat/auth methods #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6716d05
feat(auth): add new authenticator methods
mamoonraja 9fd52c8
feat(connectors): update connectors to work with new design
mamoonraja 349bc1b
feat(base): add support for authenticator in base service
mamoonraja 2b913e4
chore: remove unneeded methods and classes
mamoonraja dd30d6d
refactor(Auth): modify headers in connector
mamoonraja eb3f66c
refactor(WSConnector): do not authenticate in WS connector
mamoonraja 9d8e7ef
style: fix doc issues for Connectors
mamoonraja 0f71b32
fix: fix name for CP4DTokenResponse
mamoonraja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,81 @@ | ||
/** | ||
* 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; } | ||
public string Username { get; private set; } | ||
public string Password { get; private set; } | ||
|
||
/// <summary> | ||
/// Returns the authentication type associated with the Authenticator instance. | ||
/// </summary> | ||
virtual public string AuthenticationType { get; } | ||
|
||
/// <summary> | ||
/// Check if token data is available. | ||
/// </summary> | ||
virtual public bool HasTokenData() { | ||
return false; | ||
} | ||
mamoonraja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <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 hidden or 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,128 @@ | ||
/** | ||
* 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> | ||
/// 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) | ||
{ | ||
if (connector.Headers == null) | ||
{ | ||
connector.Headers = new Dictionary<string,string>();; | ||
} | ||
connector.Headers.Add("Authorization", Utility.CreateAuthorization(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) | ||
{ | ||
if (connector.Headers == null) | ||
{ | ||
connector.Headers = new Dictionary<string,string>();; | ||
} | ||
connector.Headers.Add("Authorization", Utility.CreateAuthorization(Username, Password)); | ||
mamoonraja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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 hidden or 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,111 @@ | ||
/** | ||
* 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(); | ||
} | ||
|
||
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) | ||
{ | ||
if (connector.Headers == null) | ||
{ | ||
connector.Headers = new Dictionary<string,string>();; | ||
} | ||
connector.Headers.Add("Authorization", string.Format("Bearer {0}", 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) | ||
{ | ||
if (connector.Headers == null) | ||
{ | ||
connector.Headers = new Dictionary<string,string>();; | ||
} | ||
connector.Headers.Add("Authorization", string.Format("Bearer {0}", 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.