|
16 | 16 | */
|
17 | 17 |
|
18 | 18 | using IBM.Cloud.SDK.Utilities;
|
| 19 | +using IBM.Cloud.SDK.Connection; |
19 | 20 | using IBM.Cloud.SDK.Authentication;
|
| 21 | +using IBM.Cloud.SDK.Authentication.NoAuth; |
20 | 22 | using System;
|
21 | 23 | using System.Collections.Generic;
|
22 | 24 |
|
23 | 25 | namespace IBM.Cloud.SDK
|
24 | 26 | {
|
25 | 27 | public class BaseService
|
26 | 28 | {
|
27 |
| - protected Credentials credentials; |
28 |
| - protected string url; |
| 29 | + protected Authenticator authenticator; |
| 30 | + protected string Url; |
| 31 | + public string ServiceId { get; set; } |
29 | 32 | 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."; |
31 | 36 | public BaseService(string serviceId)
|
32 | 37 | {
|
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 | + } |
43 | 40 |
|
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) { } |
60 | 42 |
|
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) { } |
69 | 44 |
|
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); |
91 | 56 | }
|
| 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); |
92 | 67 | }
|
93 | 68 |
|
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 | + } |
95 | 89 |
|
96 |
| - public BaseService(string versionDate, Credentials credentials, string serviceId) { } |
| 90 | + public void SetEndpoint(string url) |
| 91 | + { |
| 92 | + Url = url; |
| 93 | + } |
97 | 94 |
|
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 | + } |
99 | 102 |
|
100 | 103 | public void WithHeader(string name, string value)
|
101 | 104 | {
|
|
0 commit comments