private dynamic GetClientCredentialsTokenRequestData(string mockClientId,
                                                             MockClientProperties mockClientProperties)
        {
            string authority = null;
            string clientId;
            string clientSecret;

            string[] scopes;

            clientId = mockClientId;

            if (mockClientProperties.Secret != null)
            {
                clientSecret = mockClientProperties.Secret;
            }
            else
            {
                throw new ArgumentException($"MockClientAuthorizationMiddleware requires 'MockClient:{mockClientId}:Secret' configuration key, which is missing.");
            }

            if (mockClientProperties.Scopes != null && mockClientProperties.Scopes.Count() > 0)
            {
                scopes = mockClientProperties.Scopes;
            }
            else if (mockClientProperties.Scope != null)
            {
                scopes = mockClientProperties.Scope.Split(' ');
            }
            else
            {
                throw new ArgumentException($"MockClientAuthorizationMiddleware requires 'MockClient:{mockClientId}:Scopes' configuration key, which is missing.");
            }


            return(new {
                Authority = authority,
                ClientId = clientId,
                ClientSecret = clientSecret,
                Scopes = scopes
            });
        }
        private dynamic GetClientCredentialsTokenRequestData(IConfiguration config)
        {
            //get command-line arguments
            var args = config.GetCommandLineArguments();

            //get AutoLogin entry, if it exists
            var mockClientArg = config.GetCommandLineArguments()
                                .FirstOrDefault(a => a.Key.ToLower() == "mockclient")
                                .Value;

            dynamic tokenRequestData;
            string  authority;

            if (mockClientArg != null)
            {
                var mockClientProperties = new MockClientProperties();
                config.GetSection($"MockClient:{mockClientArg}").Bind(mockClientProperties);
                if (mockClientProperties == null)
                {
                    throw new ArgumentException($"MockClientAuthorizationMiddleware requires 'MockClient:{mockClientArg}' configuration key, which is missing.");
                }
                else
                {
                    tokenRequestData = GetClientCredentialsTokenRequestData(mockClientArg, mockClientProperties);
                }
            }
            else
            {
                var mockClientDictionary = new MockClientDictionary();
                config.GetSection("MockClient").Bind(mockClientDictionary);
                var defaultMockClient = mockClientDictionary
                                        .OrderByDescending(x => x.Value.Default)
                                        .ThenBy(x => x.Key)
                                        .FirstOrDefault();
                if (defaultMockClient.Key == null)
                {
                    throw new ArgumentException("MockClientAuthorizationMiddleware requires 'MockClient...' configuration key, which is missing.");
                }
                else
                {
                    tokenRequestData = GetClientCredentialsTokenRequestData(defaultMockClient.Key, defaultMockClient.Value);
                }
            }
            if (tokenRequestData.Authority == null)
            {
                var apiDict = new Dictionary <string, ApiConfig>();
                config.GetSection("Apis").Bind(apiDict);

                var env = config["ASPNETCORE_ENVIRONMENT"];

                var identityServerApiName = GetIdentityServerApiType().Name;

                if (apiDict.ContainsKey("identityServerApiName"))
                {
                    throw new ApplicationException($"MockClientAuthorizationMiddleware requires the presence of a Apis config entry that is an identity server. No Api having with Secret = null appears in appsettings.{env}.json.");
                }
                var identityServerApi = apiDict[identityServerApiName];
                if (identityServerApi == null)
                {
                    throw new ApplicationException($"MockClientAuthorizationMiddleware requires the presence of a Apis config entry that is an identity server. No Api having with Secret = null appears in appsettings.{env}.json.");
                }

                authority = identityServerApi.BaseAddress;
            }
            else
            {
                authority = tokenRequestData.Authority;
            }

            return(new {
                Authority = authority,
                tokenRequestData.ClientId,
                tokenRequestData.ClientSecret,
                tokenRequestData.Scopes
            });
        }