Exemple #1
0
        void Load(IConfiguration configuration, string cryptoCode, string serviceName, ExternalServiceTypes type, string errorMessage, string displayName)
        {
            var setting = $"{cryptoCode}.external.{serviceName}";
            var connStr = configuration.GetOrDefault <string>(setting, string.Empty);

            if (connStr.Length != 0)
            {
                if (!ExternalConnectionString.TryParse(connStr, out var connectionString, out var error))
                {
                    throw new ConfigException(string.Format(CultureInfo.InvariantCulture, errorMessage, setting, error));
                }
                this.Add(new ExternalService()
                {
                    Type = type, ConnectionString = connectionString, CryptoCode = cryptoCode, DisplayName = displayName, ServiceName = serviceName
                });
            }
        }
        public static bool TryParse(string str, out ExternalConnectionString result, out string error)
        {
            if (str == null)
            {
                throw new ArgumentNullException(nameof(str));
            }
            error  = null;
            result = null;
            var resultTemp = new ExternalConnectionString();

            foreach (var kv in str.Split(';')
                     .Select(part => part.Split('='))
                     .Where(kv => kv.Length == 2))
            {
                switch (kv[0].ToLowerInvariant())
                {
                case "server":
                    if (resultTemp.Server != null)
                    {
                        error = "Duplicated server attribute";
                        return(false);
                    }
                    if (!Uri.IsWellFormedUriString(kv[1], UriKind.RelativeOrAbsolute))
                    {
                        error = "Invalid URI";
                        return(false);
                    }
                    resultTemp.Server = new Uri(kv[1], UriKind.RelativeOrAbsolute);
                    if (!resultTemp.Server.IsAbsoluteUri && (kv[1].Length == 0 || kv[1][0] != '/'))
                    {
                        resultTemp.Server = new Uri($"/{kv[1]}", UriKind.RelativeOrAbsolute);
                    }
                    break;

                case "cookiefile":
                case "cookiefilepath":
                    if (resultTemp.CookieFilePath != null)
                    {
                        error = "Duplicated cookiefile attribute";
                        return(false);
                    }

                    resultTemp.CookieFilePath = kv[1];
                    break;

                case "macaroondirectorypath":
                    resultTemp.MacaroonDirectoryPath = kv[1];
                    break;

                case "certthumbprint":
                    resultTemp.CertificateThumbprint = kv[1];
                    break;

                case "macaroonfilepath":
                    resultTemp.MacaroonFilePath = kv[1];
                    break;

                case "api-token":
                    resultTemp.APIToken = kv[1];
                    break;

                case "access-key":
                    resultTemp.AccessKey = kv[1];
                    break;
                }
            }
            result = resultTemp;
            return(true);
        }