Skip to content

Commit 349bc1b

Browse files
committed
feat(base): add support for authenticator in base service
1 parent 9fd52c8 commit 349bc1b

File tree

3 files changed

+434
-72
lines changed

3 files changed

+434
-72
lines changed

BaseService.cs

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,86 +16,89 @@
1616
*/
1717

1818
using IBM.Cloud.SDK.Utilities;
19+
using IBM.Cloud.SDK.Connection;
1920
using IBM.Cloud.SDK.Authentication;
21+
using IBM.Cloud.SDK.Authentication.NoAuth;
2022
using System;
2123
using System.Collections.Generic;
2224

2325
namespace IBM.Cloud.SDK
2426
{
2527
public class BaseService
2628
{
27-
protected Credentials credentials;
28-
protected string url;
29+
protected Authenticator authenticator;
30+
protected string Url;
31+
public string ServiceId { get; set; }
2932
protected Dictionary<string, string> customRequestHeaders = new Dictionary<string, string>();
30-
33+
public static string PropNameServiceUrl = "URL";
34+
public static string propnameDisableSsl = "DISABLE_SSL";
35+
private const string ErrorMessageNoAuthenticator = "Authentication information was not properly configured.";
3136
public BaseService(string serviceId)
3237
{
33-
var credentialsPaths = Utility.GetCredentialsPaths();
34-
if (credentialsPaths.Count > 0)
35-
{
36-
foreach (string path in credentialsPaths)
37-
{
38-
if (Utility.LoadEnvFile(path))
39-
{
40-
break;
41-
}
42-
}
38+
authenticator = ConfigBasedAuthenticatorFactory.GetAuthenticator(serviceId);
39+
}
4340

44-
string ApiKey = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_IAM_APIKEY");
45-
// check for old IAM API key name as well
46-
if (string.IsNullOrEmpty(ApiKey)) {
47-
ApiKey = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_APIKEY");
48-
}
49-
string Username = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_USERNAME");
50-
string Password = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_PASSWORD");
51-
string ServiceUrl = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_URL");
52-
string AuthenticationType = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_AUTHENTICATION_TYPE");
53-
string Icp4dAccessToken = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_ICP4D_ACCESS_TOKEN");
54-
string Icp4dUrl = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_ICP4D_URL");
55-
56-
if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password)))
57-
{
58-
throw new NullReferenceException(string.Format("Either {0}_APIKEY or {0}_USERNAME and {0}_PASSWORD did not exist. Please add credentials with this key in ibm-credentials.env.", serviceId.ToUpper()));
59-
}
41+
public BaseService(string versionDate, string serviceId) : this(serviceId) { }
6042

61-
if (!string.IsNullOrEmpty(ApiKey) || AuthenticationType == "iam")
62-
{
63-
IamTokenOptions tokenOptions = new IamTokenOptions()
64-
{
65-
IamApiKey = ApiKey
66-
};
67-
credentials = new Credentials(tokenOptions, ServiceUrl);
68-
}
43+
public BaseService(string versionDate, Authenticator authenticator, string serviceId) : this(authenticator, serviceId) { }
6944

70-
if (!string.IsNullOrEmpty(Icp4dAccessToken) || AuthenticationType == "icp4d")
71-
{
72-
Icp4dTokenOptions tokenOptions = new Icp4dTokenOptions()
73-
{
74-
Username = Username,
75-
Password = Password,
76-
AccessToken = Icp4dAccessToken,
77-
Url = Icp4dUrl
78-
};
79-
80-
credentials = new Credentials(tokenOptions, ServiceUrl);
81-
82-
if (string.IsNullOrEmpty(credentials.Url))
83-
{
84-
credentials.Url = url;
85-
}
86-
}
87-
else if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
88-
{
89-
credentials = new Credentials(Username, Password, url);
90-
}
45+
public BaseService(Authenticator authenticator, string serviceId) {
46+
ServiceId = serviceId;
47+
48+
this.authenticator = authenticator ?? throw new ArgumentNullException(ErrorMessageNoAuthenticator);
49+
50+
// Try to retrieve the service URL from either a credential file, environment, or VCAP_SERVICES.
51+
Dictionary<string, string> props = CredentialUtils.GetServiceProperties(serviceId);
52+
props.TryGetValue(PropNameServiceUrl, out string url);
53+
if (!string.IsNullOrEmpty(url))
54+
{
55+
SetEndpoint(url);
9156
}
57+
58+
// Check to see if "disable ssl" was set in the service properties.
59+
// bool disableSsl = false;
60+
// props.TryGetValue(PropNameServiceDisableSslVerification, out string tempDisableSsl);
61+
// if (!string.IsNullOrEmpty(tempDisableSsl))
62+
// {
63+
// bool.TryParse(tempDisableSsl, out disableSsl);
64+
// }
65+
66+
// DisableSslVerification(disableSsl);
9267
}
9368

94-
public BaseService(string versionDate, string serviceId) : this(serviceId) { }
69+
protected BaseService(string versionDate, string serviceId, string url)
70+
{
71+
ServiceId = serviceId;
72+
authenticator = new NoAuthAuthenticator();
73+
74+
if (!string.IsNullOrEmpty(url))
75+
Url = url;
76+
}
77+
78+
protected void SetAuthentication(RESTConnector connector)
79+
{
80+
if (authenticator != null)
81+
{
82+
authenticator.Authenticate(connector);
83+
}
84+
else
85+
{
86+
throw new ArgumentException("Authentication information was not properly configured.");
87+
}
88+
}
9589

96-
public BaseService(string versionDate, Credentials credentials, string serviceId) { }
90+
public void SetEndpoint(string url)
91+
{
92+
Url = url;
93+
}
9794

98-
public BaseService(Credentials credentials, string serviceId) { }
95+
/// <summary>
96+
/// Returns the authenticator for the service.
97+
/// </summary>
98+
public Authenticator GetAuthenticator()
99+
{
100+
return authenticator;
101+
}
99102

100103
public void WithHeader(string name, string value)
101104
{

0 commit comments

Comments
 (0)