Esempio n. 1
0
        /// <summary>
        /// JB. Asyncronously create a new Client. Add Secret and Scope and return a response witht he info needed for Client Integration.
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public async Task <ClientResponseDto> AddClient(Client client, ClientBindingDto dto)
        {
            //JB. Build a newly randomdized Secret, this is what is passed to the client and it is not hashed yet. It will be hashed at persisting time.
            string NewSecret = await RandomStringGenerator.GeneratedString();

            ClientResponseDto response = null;
            int clientId = 0;

            try
            {
                await Task.Run(async() =>
                {
                    using (/*var*/ ctx /*= new ResourceConfigDbContext()*/)
                    {
                        ctx.Clients.Add(client);
                        ctx.SaveChanges();
                        clientId = client.Id;
                    };
                    //JB. Add now Secret
                    _secretsRepo.AddClientSecret(_factory.CreateClientSecret(clientId, NewSecret));

                    //JB. Add Client Grant Type
                    await _grantRepo.AddGrantType(new ClientGrantType {
                        ClientId = clientId, GrantType = IdentityServer4.Models.GrantType.ClientCredentials
                    });

                    //JB. Add Scopes this client is allowed in the system.
                    foreach (var scopev in dto.AllowedScopes)
                    {
                        await _scopeRepo.CreateClientScope(new ClientScope {
                            ClientId = clientId, Scope = scopev
                        });
                    }

                    response = new ClientResponseDto
                    {
                        ClientName    = client.ClientName,
                        Client_Id     = client.ClientId,
                        Secret        = NewSecret,
                        AllowedScopes = dto.AllowedScopes,
                        Claims        = dto.Claims
                    };
                    //JB. Add claims. Info about this Client
                    foreach (var c in dto.Claims)
                    {
                        await _claimRepo.AddClaim(new ClientClaim {
                            ClientId = clientId, Type = c["Type"], Value = c["Value"]
                        });
                    }
                });
            }
            catch (Exception ex)
            {
                ClientErrorResponseDto errorResponse = new ClientErrorResponseDto {
                    Error = "Not Found. " + ex.Message
                };
            }

            return(response);
        }
        /// <summary>
        /// Add new Client Claim
        /// </summary>
        /// <param name="claim"></param>
        /// <returns></returns>
        public async Task <string> AddClaim(ClientClaim claim)
        {
            string result = "";
            await Task.Run(() => {
                try {
                    using (/*var*/ ctx /*= new ResourceConfigDbContext()*/) {
                        ctx.ClientClaims.Add(claim);
                        ctx.SaveChanges();
                        result = "ClaimId " + claim.Id.ToString();
                    }
                }
                catch (Exception ex) {
                    result = ex.Message;
                }
            });

            return(result);
        }
        public string AddApiSecret(ApiSecret model)
        {
            string result;

            try
            {
                using (/*var*/ _ctx /*= new ResourceConfigDbContext()*/)
                {
                    _ctx.ApiSecrets.Add(model);
                    _ctx.SaveChanges();
                    //int Id =
                    result = model.Id.ToString();
                }
            }
            catch (Exception ex)
            {
                result = ex.Message.ToString();
            }
            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// Add new ApiResource to Database.
        /// </summary>
        /// <param name="resource"></param>
        /// <returns>Int ID of newly created ApiResource</returns>
        public int CreateApiResource(ApiResource resource)
        {
            int daId;

            using (_ctx)
            {
                _ctx.ApiResources.Add(resource);
                _ctx.SaveChanges();
                daId = resource.Id;
            }
            return(daId);
        }
Esempio n. 5
0
        /// <summary>
        /// JB. Create a New ApiScope when a new ApiResource is added to the Database.
        /// </summary>
        /// <param name="scope">ApiScope Type</param>
        /// <returns>String Result</returns>
        public async Task <string> CreateApiScope(ApiScope scope)
        {
            string result = "";

            try
            {
                await Task.Run(() =>
                {
                    using (ctx)
                    {
                        ctx.ApiScopes.Add(scope);
                        ctx.SaveChanges();
                        result = "Api scope successfully added";
                    }
                });
            }
            catch (Exception ex)
            {
                result = ex.Message.ToString();
            }
            return(result);
        }
Esempio n. 6
0
        public async Task <string> AddGrantType(ClientGrantType grant)
        {
            string result  = "";
            int    grantId = 0;
            await Task.Run(() => {
                using (/*var*/ ctx /*= new ResourceConfigDbContext()*/)
                {
                    ctx.ClientGrantTypes.Add(grant);
                    ctx.SaveChanges();
                    grantId = grant.Id;
                    result  = "Grant " + grantId + "added successfully";
                }
            });

            return(result);
        }