private static SetClientResponse CreateClient(SetClientParam param)
        {
            if (!string.IsNullOrWhiteSpace(param.GrantTypesToRemove))
            {
                throw new InvalidParamException("Grant types to remove given when client do not exists and is to be created");
            }
            if (!string.IsNullOrWhiteSpace(param.ScopesToRemove))
            {
                throw new InvalidParamException("Scopes to remove given when client do not exists and is to be created");
            }
            if (string.IsNullOrWhiteSpace(param.GrantTypesToAdd))
            {
                throw new InvalidParamException("No grant types to add given when client do not exists and is to be created");
            }

            try
            {
                var client = new Client()
                {
                    ClientId     = param.ClientId,
                    ProtocolType = Protocol.Name
                };

                return(new SetClientResponse
                {
                    Created = true,
                    Client = client
                });
            }
            catch (Exception ex)
            {
                throw new FailedToCreateClientException(ex);
            }
        }
        public async Task <SetClientResponse> SetClientAsync(SetClientParam param, CancellationToken cancellationToken)
        {
            var retVal = await GetClientAsync(param, cancellationToken);

            SetProperties(retVal.Client, param);
            SetClientSecret(retVal, param);

            retVal.Client.AllowedScopes = retVal.Client.AllowedScopes.UnionWithTokens(param.ScopesToAdd);
            retVal.Client.AllowedScopes = retVal.Client.AllowedScopes.RemoveTokens(param.ScopesToRemove);

            retVal.Client.AllowedGrantTypes = retVal.Client.AllowedGrantTypes.UnionWithTokens(param.GrantTypesToAdd);
            retVal.Client.AllowedGrantTypes = retVal.Client.AllowedGrantTypes.RemoveTokens(param.GrantTypesToRemove);
            if (retVal.Client.AllowedGrantTypes.Count == 0)
            {
                throw new NoAllowedGrantsGivenException();
            }

            if (!param.DryRun)
            {
                await _repository.SetClientAsync(retVal.Client, param, cancellationToken);
            }

            retVal.DryRun = param.DryRun;
            return(retVal);
        }
 private static void SetProperties(Client client, SetClientParam param)
 {
     if (!string.IsNullOrEmpty(param.ClientName))
     {
         client.ClientName = param.ClientName;
     }
     if (!string.IsNullOrEmpty(param.Description))
     {
         client.Description = param.Description;
     }
     if (!string.IsNullOrEmpty(param.ClientUri))
     {
         client.ClientUri = param.ClientUri;
     }
     if (!string.IsNullOrEmpty(param.LogoUri))
     {
         client.LogoUri = param.LogoUri;
     }
     if (param.Enabled != null)
     {
         client.Enabled = param.Enabled.Value;
     }
     if (param.RequireClientSecret != null)
     {
         client.RequireClientSecret = param.RequireClientSecret.Value;
     }
     if (param.RequireConsent != null)
     {
         client.RequireConsent = param.RequireConsent.Value;                               // research
     }
     if (param.AllowRememberConsent != null)
     {
         client.AllowRememberConsent = param.AllowRememberConsent.Value;                                     // research
     }
 }
        public async Task SetClientAsync(Client client, SetClientParam param, CancellationToken cancellationToken)
        {
            var config = await GetConfigDaoAsync(param.Authority, cancellationToken);

            var newClients = config.Clients == null ? new List <Client>() : new List <Client>(config.Clients);
            var oldClient  = newClients.Where(e => e.ClientId.Equals(client.ClientId, StringComparison.Ordinal)).FirstOrDefault();

            if (oldClient != null)
            {
                newClients.Remove(oldClient);
            }
            newClients.Add(client);
            config.Clients = newClients;
            await SetConfigDaoAsync(param.Authority, cancellationToken, config);
        }
        private static void SetClientSecret(SetClientResponse clientResponse, SetClientParam param)
        {
            var client = clientResponse.Client;

            Secret clientSecret = null;

            if (!string.IsNullOrEmpty(param.ClientSecret))
            {
                clientSecret = new Secret
                {
                    Value = param.ClientSecret.Sha256()
                };

                clientResponse.Secret = param.ClientSecret;
            }
            else if (clientResponse.Client.RequireClientSecret)
            {
                var(plain, hash) = SecretGenerator.GenerateSharedSecret();
                clientSecret     = new Secret
                {
                    Type        = "SharedSecret",
                    Value       = hash,
                    Description = "SharedSecret for client " + (client.ClientName ?? client.ClientId)
                };

                clientResponse.Secret = plain;
            }

            if (clientSecret != null)
            {
                clientSecret.Type        = "SharedSecret";
                clientSecret.Description = "SharedSecret for client " + (client.ClientName ?? client.ClientId);

                if (client.ClientSecrets == null)
                {
                    client.ClientSecrets = new List <Secret>();
                }
                client.ClientSecrets.Add(clientSecret);
            }
        }
        private async Task <SetClientResponse> GetClientAsync(SetClientParam param, CancellationToken cancellationToken)
        {
            var client = await _repository.GetClientAsync(new GetClientParam { Authority = param.Authority, ClientId = param.ClientId }, cancellationToken);

            if (client == null)
            {
                if (param.CreateIfDoesNotExists)
                {
                    var retVal = CreateClient(param);
                    return(retVal);
                }
                else
                {
                    throw new ClientDoesNotExistsException(param.ClientId);
                }
            }

            return(new SetClientResponse
            {
                Client = client
            });
        }