-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add apikey authenticator #46
Merged
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d92e59b
feat(cpd-authentication): add apikey for authentication step and upda…
nan2iz 8a851ae
test(cpd-tests): add testsing for CloudPakAuthentication
nan2iz d49dcfd
fix(detect-secrets): update detect-secrets for false positive
nan2iz 60fb13f
fix(node-version): update appveyor.yml to use node version 15.6.0
nan2iz 4c1c026
fix(renaming): rename function and vararaiables
nan2iz f4399a4
chore(detect-secrets): ran audit for detect-secrets
nan2iz d3291fe
fix(cp4d): fixed setter method with the correct naming
nan2iz 5e61544
test(unit-test): remove unused tests
nan2iz 055eef4
test(unit-testing): fix unit testing and remove unused testing function
nan2iz 55e88e9
Merge branch 'main' of github.com:IBM/dotnet-sdk-core into add-apikey…
nan2iz b955890
fix(cpd-constuctor): revert the code for ConstructionDictionary
nan2iz 65502b0
fix(cpd-authenticator): update apikey for CloudPakForDataAuthenticato…
nan2iz 11f3c02
fix(semantic-release): update semantic-release version
nan2iz 0ca8e45
fix(cpd-authenticator): renaming builder pattern functions and update…
nan2iz 14691dd
fix(cpd-authenticate): remove unused condition
nan2iz 18b32e4
fix(cpd-authentication): fix init function for initalize with password
nan2iz cb67218
fix(cpd-authenticator): add default value for password and apikey as …
nan2iz 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 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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -19,7 +19,8 @@ | |||||||
using IBM.Cloud.SDK.Core.Util; | ||||||||
using System; | ||||||||
using System.Collections.Generic; | ||||||||
|
||||||||
using System.Net.Http; | ||||||||
|
||||||||
namespace IBM.Cloud.SDK.Core.Authentication.Cp4d | ||||||||
{ | ||||||||
/// <summary> | ||||||||
|
@@ -30,29 +31,40 @@ public class CloudPakForDataAuthenticator : Authenticator | |||||||
public IClient Client { get; set; } | ||||||||
|
||||||||
// This is the suffix we'll need to add to the user-supplied URL to retrieve an access token. | ||||||||
private static string UrlSuffix = "/v1/preauth/validateAuth"; | ||||||||
private static string UrlSuffix = "/v1/authorize"; | ||||||||
|
||||||||
// These are keys for body request for cpd authorization token | ||||||||
private const string KeyUsername = "username"; | ||||||||
private const string KeyPassword = "password"; | ||||||||
private const string KeyApikey = "api_key"; | ||||||||
|
||||||||
// Configuration properties for this authenticator. | ||||||||
public string Url { get; private set; } | ||||||||
public string Username { get; private set; } | ||||||||
public string Password { get; private set; } | ||||||||
public string Apikey { get; private set; } | ||||||||
public bool? DisableSslVerification { get; set; } | ||||||||
public Dictionary<string, string> Headers { get; set; } | ||||||||
|
||||||||
// This field holds an access token and its expiration time. | ||||||||
private CloudPakForDataToken tokenData; | ||||||||
|
||||||||
// this empty constructor will be used by builder | ||||||||
private CloudPakForDataAuthenticator() | ||||||||
{ | ||||||||
} | ||||||||
|
||||||||
/// <summary> | ||||||||
/// Constructs a CloudPakForDataAuthenticator with all properties. | ||||||||
/// </summary> | ||||||||
/// <param name="url">The base URL associated with the token server. The path "/v1/preauth/validateAuth" will be appended to this value automatically.</param> | ||||||||
/// <param name="url">The base URL associated with the token server. The path "/v1/authorize" will be appended to this value automatically.</param> | ||||||||
/// <param name="username">The username to be used when retrieving the access token</param> | ||||||||
/// <param name="password">The password to be used when retrieving the access token</param> | ||||||||
/// <param name="disableSslVerification">A flag indicating whether SSL hostname verification should be disabled</param> | ||||||||
/// <param name="headers">A set of user-supplied headers to be included in token server interactions</param> | ||||||||
public CloudPakForDataAuthenticator(string url, string username, string password, bool? disableSslVerification = null, Dictionary<string, string> headers = null) | ||||||||
{ | ||||||||
Init(url, username, password, disableSslVerification, headers); | ||||||||
Init(url, username, password, null, disableSslVerification, headers); | ||||||||
} | ||||||||
|
||||||||
/// <summary> | ||||||||
|
@@ -64,17 +76,75 @@ public CloudPakForDataAuthenticator(Dictionary<string, string> config) | |||||||
config.TryGetValue(PropNameUrl, out string url); | ||||||||
config.TryGetValue(PropNameUsername, out string username); | ||||||||
config.TryGetValue(PropNamePassword, out string password); | ||||||||
config.TryGetValue(PropNameApikey, out string apikey); | ||||||||
config.TryGetValue(PropNameDisableSslVerification, out string disableSslVerficiationString); | ||||||||
bool.TryParse(disableSslVerficiationString, out bool disableSslVerification); | ||||||||
Init(url, username, password, disableSslVerification); | ||||||||
Init(url, username, password, apikey, disableSslVerification); | ||||||||
} | ||||||||
|
||||||||
private void Init(string url, string username, string password, bool? disableSslVerification = null, Dictionary<string, string> headers = null) | ||||||||
public CloudPakForDataAuthenticator SetUrl(string url) | ||||||||
{ | ||||||||
Url = url; | ||||||||
return this; | ||||||||
} | ||||||||
|
||||||||
public CloudPakForDataAuthenticator SetUsername(string username) | ||||||||
{ | ||||||||
Username = username; | ||||||||
return this; | ||||||||
} | ||||||||
|
||||||||
public CloudPakForDataAuthenticator SetPassword(string password) | ||||||||
{ | ||||||||
Password = password; | ||||||||
return this; | ||||||||
} | ||||||||
|
||||||||
public CloudPakForDataAuthenticator SetApikey(string apikey) | ||||||||
{ | ||||||||
Apikey = apikey; | ||||||||
return this; | ||||||||
} | ||||||||
|
||||||||
public CloudPakForDataAuthenticator SetDisableSslVerification(bool disableSslVerification) | ||||||||
{ | ||||||||
DisableSslVerification = disableSslVerification; | ||||||||
return this; | ||||||||
} | ||||||||
|
||||||||
public CloudPakForDataAuthenticator SetHeaders(Dictionary<string, string> headers) | ||||||||
{ | ||||||||
Headers = headers; | ||||||||
return this; | ||||||||
} | ||||||||
|
||||||||
public static CloudPakForDataAuthenticator InitBuilder() | ||||||||
{ | ||||||||
return new CloudPakForDataAuthenticator(); | ||||||||
} | ||||||||
|
||||||||
public CloudPakForDataAuthenticator Build() | ||||||||
{ | ||||||||
Init(); | ||||||||
|
||||||||
return this; | ||||||||
} | ||||||||
|
||||||||
private void Init(string url, string username, string password = null, string apikey = null, bool? disableSslVerification = null, Dictionary<string, string> headers = null) | ||||||||
{ | ||||||||
Url = url; | ||||||||
Username = username; | ||||||||
Password = password; | ||||||||
|
||||||||
if (password != null) | ||||||||
{ | ||||||||
Password = password; | ||||||||
} | ||||||||
|
||||||||
if (apikey != null) | ||||||||
{ | ||||||||
Apikey = apikey; | ||||||||
} | ||||||||
|
||||||||
if (disableSslVerification != null) | ||||||||
{ | ||||||||
DisableSslVerification = disableSslVerification; | ||||||||
|
@@ -85,12 +155,17 @@ private void Init(string url, string username, string password, bool? disableSsl | |||||||
Headers = headers; | ||||||||
} | ||||||||
|
||||||||
Validate(); | ||||||||
Init(); | ||||||||
} | ||||||||
|
||||||||
Client = new IBMHttpClient() | ||||||||
{ | ||||||||
ServiceUrl = Url + UrlSuffix | ||||||||
}; | ||||||||
public void Init() | ||||||||
{ | ||||||||
Validate(); | ||||||||
|
||||||||
Client = new IBMHttpClient() | ||||||||
{ | ||||||||
ServiceUrl = Url + UrlSuffix | ||||||||
}; | ||||||||
} | ||||||||
|
||||||||
public override string AuthenticationType | ||||||||
|
@@ -133,8 +208,9 @@ protected DetailedResponse<CloudPakForDataTokenResponse> RequestToken() | |||||||
|
||||||||
try | ||||||||
{ | ||||||||
IClient client = Client.WithAuthentication(Username, Password); | ||||||||
var request = Client.GetAsync(Url + UrlSuffix); | ||||||||
var request = Client.PostAsync(Url + UrlSuffix); | ||||||||
request.WithHeader("Content-type", "application/x-www-form-urlencoded"); | ||||||||
|
||||||||
if (DisableSslVerification != null) | ||||||||
{ | ||||||||
Client.DisableSslVerification((bool)DisableSslVerification); | ||||||||
|
@@ -145,6 +221,23 @@ protected DetailedResponse<CloudPakForDataTokenResponse> RequestToken() | |||||||
request.WithHeaders(Headers); | ||||||||
} | ||||||||
|
||||||||
List<KeyValuePair<string, string>> content = new List<KeyValuePair<string, string>>(); | ||||||||
mediumTaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
KeyValuePair<string, string> username = new KeyValuePair<string, string>(KeyUsername, Username); | ||||||||
content.Add(username); | ||||||||
|
||||||||
if (string.IsNullOrEmpty(Password)) | ||||||||
{ | ||||||||
KeyValuePair<string, string> apikey = new KeyValuePair<string, string>(KeyApikey, Apikey); | ||||||||
content.Add(apikey); | ||||||||
} else | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah this else should be on a separate line
Suggested change
|
||||||||
{ | ||||||||
KeyValuePair<string, string> password = new KeyValuePair<string, string>(KeyPassword, Password); | ||||||||
content.Add(password); | ||||||||
} | ||||||||
|
||||||||
var formData = new FormUrlEncodedContent(content); | ||||||||
request.WithBodyContent(formData); | ||||||||
|
||||||||
result = request.As<CloudPakForDataTokenResponse>().Result; | ||||||||
if (result == null) | ||||||||
{ | ||||||||
|
@@ -171,9 +264,9 @@ public override void Validate() | |||||||
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Username")); | ||||||||
} | ||||||||
|
||||||||
if (string.IsNullOrEmpty(Password)) | ||||||||
if (string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(Apikey)) | ||||||||
{ | ||||||||
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Password")); | ||||||||
throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Password or Apikey")); | ||||||||
} | ||||||||
|
||||||||
if (CredentialUtils.HasBadStartOrEndChar(Url)) | ||||||||
|
@@ -186,10 +279,15 @@ public override void Validate() | |||||||
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Username")); | ||||||||
} | ||||||||
|
||||||||
if (CredentialUtils.HasBadStartOrEndChar(Password)) | ||||||||
if (!string.IsNullOrEmpty(Password) && CredentialUtils.HasBadStartOrEndChar(Password)) | ||||||||
{ | ||||||||
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Password")); | ||||||||
} | ||||||||
|
||||||||
if (!string.IsNullOrEmpty(Apikey) && CredentialUtils.HasBadStartOrEndChar(Apikey)) | ||||||||
{ | ||||||||
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Apikey")); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need apikey in init?