/// <summary> /// Constructor that takes ICP4D token options. /// </summary> /// <param name="icp4dTokenOptions"></param> public Credentials(Icp4dTokenOptions icp4dTokenOptions, string serviceUrl = null) { if (!string.IsNullOrEmpty(serviceUrl)) { Url = serviceUrl; } SetCredentials(icp4dTokenOptions, serviceUrl); }
public BaseService(string serviceId) { var credentialsPaths = Utility.GetCredentialsPaths(); if (credentialsPaths.Count > 0) { foreach (string path in credentialsPaths) { if (Utility.LoadEnvFile(path)) { break; } } string ApiKey = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_IAM_APIKEY"); // check for old IAM API key name as well if (string.IsNullOrEmpty(ApiKey)) { ApiKey = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_APIKEY"); } string Username = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_USERNAME"); string Password = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_PASSWORD"); string ServiceUrl = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_URL"); string AuthenticationType = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_AUTHENTICATION_TYPE"); string Icp4dAccessToken = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_ICP4D_ACCESS_TOKEN"); string Icp4dUrl = Environment.GetEnvironmentVariable(serviceId.ToUpper() + "_ICP4D_URL"); if (string.IsNullOrEmpty(ApiKey) && (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))) { 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())); } if (!string.IsNullOrEmpty(ApiKey) || AuthenticationType == "iam") { IamTokenOptions tokenOptions = new IamTokenOptions() { IamApiKey = ApiKey }; credentials = new Credentials(tokenOptions, ServiceUrl); } if (!string.IsNullOrEmpty(Icp4dAccessToken) || AuthenticationType == "icp4d") { Icp4dTokenOptions tokenOptions = new Icp4dTokenOptions() { Username = Username, Password = Password, AccessToken = Icp4dAccessToken, Url = Icp4dUrl }; credentials = new Credentials(tokenOptions, ServiceUrl); if (string.IsNullOrEmpty(credentials.Url)) { credentials.Url = url; } } else if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password)) { credentials = new Credentials(Username, Password, url); } } }
private void SetCredentials(Icp4dTokenOptions icp4dTokenOptions, string serviceUrl = null) { icp4dTokenManager = new Icp4dTokenManager(icp4dTokenOptions); icp4dTokenManager.GetToken(); }