/*
         * Gets the first instance of an app secret corresponding to the given platform name, or returns the string
         * as-is if no identifier can be found. Logs a message if no identifiers can be found.
         */
        private static string GetSecretForPlatform(string secrets, string platformIdentifier)
        {
            /*
             * If there are no colons, then there are no named identifiers, but log a message in case the developer made
             * a typing error.
             */
            if (!secrets.Contains("="))
            {
                MobileCenterLog.Debug(MobileCenterLog.LogTag, "No named identifier found in appSecret; using as-is");
                return(secrets);
            }

            /*
             * This assumes that the key contains only lowercase letters, digits, and hyphens
             * (and that it has at least one character)
             */
            var pattern = platformIdentifier + @"=([^;]+)";
            var match   = Regex.Match(secrets, pattern);

            if (match.Value == string.Empty)
            {
                var message = "Error parsing key for '" + platformIdentifier + "'";
                throw new ArgumentException(message, nameof(platformIdentifier));
            }
            return(match.Groups[1].Value);
        }
Ejemplo n.º 2
0
        // Gets the first instance of an app secret corresponding to the given platform name, or returns the string
        // as-is if no identifier can be found. Logs a message if no identifiers can be found.
        internal static string GetSecretForPlatform(string secrets, string platformIdentifier)
        {
            if (string.IsNullOrEmpty(secrets))
            {
                throw new MobileCenterException("App secrets string is null or empty");
            }

            // If there are no equals signs, then there are no named identifiers, but log a message in case the developer made
            // a typing error.
            if (!secrets.Contains("="))
            {
                MobileCenterLog.Debug(MobileCenterLog.LogTag, "No named identifier found in appSecret; using as-is");
                return(secrets);
            }

            var parseErrorMessage = $"Error parsing key for '{platformIdentifier}'";

            var platformIndicator = platformIdentifier + "=";
            var secretIdx         = secrets.IndexOf(platformIndicator, StringComparison.Ordinal);

            if (secretIdx == -1)
            {
                throw new MobileCenterException(parseErrorMessage);
            }
            secretIdx += platformIndicator.Length;
            var platformSecret = string.Empty;

            while (secretIdx < secrets.Length)
            {
                var nextChar = secrets[secretIdx++];
                if (nextChar == ';')
                {
                    break;
                }

                platformSecret += nextChar;
            }

            if (platformSecret == string.Empty)
            {
                throw new MobileCenterException(parseErrorMessage);
            }

            return(platformSecret);
        }
        /*
         * Gets the first instance of an app secret corresponding to the given platform name, or returns the string
         * as-is if no identifier can be found. Logs a message if no identifiers can be found.
         */
        internal static string GetSecretForPlatform(string secrets, string platformIdentifier)
        {
            if (string.IsNullOrEmpty(secrets))
            {
                throw new MobileCenterException("App secrets string is null or empty");
            }

            /*
             * If there are no colons, then there are no named identifiers, but log a message in case the developer made
             * a typing error.
             */
            if (!secrets.Contains("="))
            {
                MobileCenterLog.Debug(MobileCenterLog.LogTag, "No named identifier found in appSecret; using as-is");
                return(secrets);
            }

            var parseErrorMessage = $"Error parsing key for '{platformIdentifier}'";

            /*
             * This assumes that the key contains only lowercase letters, digits, and hyphens
             * (and that it has at least one character)
             */
            var pattern = platformIdentifier + @"=([^;]+)";

            try
            {
                var match = Regex.Match(secrets, pattern);
                if (match.Value == string.Empty)
                {
                    throw new MobileCenterException(parseErrorMessage);
                }
                return(match.Groups[1].Value);
            }
            catch (ArgumentException e)
            {
                throw new MobileCenterException(parseErrorMessage, e);
            }
            catch (RegexMatchTimeoutException e)
            {
                throw new MobileCenterException(parseErrorMessage, e);
            }
        }