public async Task <string> BuildEntity(dynamic model)
        {
            var type = typeof(T);
            //model = type;

            string result = "";

            if (IsApiClaim(type))
            {
                type = _claimFactory.CreateApiClaims(model);
                await Task.Run(() =>
                {
                    //JB. This is now invoking the correct Overloaded Method by passing ApiResourceClaim Entity
                    result = _claimRepo.AddClaim(model);
                });
            }
            else
            {
                type = _claimFactory.CreateClientClaims(model);
                await Task.Run(() =>
                {
                    //JB. This is now invoking the correct Overloaded Method by passing ClientClaim Entity
                    result = _claimRepo.AddClaim(model);
                });
            }
            return(result);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Post([FromRoute] Guid clientId, [FromRoute] Guid policyId,
                                               [FromBody] Claim claim)
        {
            if (await _clientRepository.GetClient(clientId) == null ||
                await _policyRepository.GetPolicy(policyId) == null)
            {
                return(NotFound());
            }

            var addedClaim = await _claimRepository.AddClaim(policyId, claim);

            return(CreatedAtAction(nameof(Get), new { clientId, policyId, id = addedClaim.Id }, addedClaim));
        }
Ejemplo n.º 4
0
        public IActionResult CreateClaim([FromBody] string email)
        {
            _logger.LogInformation("Processing GetExpenseForCostCenter Request");
            string    requestXML   = _removeInvalidCharFromXML.GetCleanXML(email);
            ClaimsDto processClaim = new ClaimsDto();
            XDocument docx         = XDocument.Parse(requestXML.ToString());

            processClaim.costCenter = docx.Descendants("cost_centre")?.FirstOrDefault().Value.Length > 0 ?
                                      docx.Descendants("cost_centre")?.FirstOrDefault().Value :
                                      "UNKNOWN";

            processClaim.total         = docx.Descendants("total").FirstOrDefault().Value;
            processClaim.PaymentMethod = docx.Descendants("payment_method").FirstOrDefault().Value;
            processClaim.Description   = docx.Descendants("description").FirstOrDefault().Value;
            processClaim.Vendor        = docx.Descendants("vendor").FirstOrDefault().Value;
            processClaim.date          = DateTime.Parse(docx.Descendants("date").FirstOrDefault().Value);

            //If user input is not valid - Send Status code 400 Bad request
            if (processClaim == null)
            {
                _logger.LogError("Invalid user request.  Sending 400 Bad Request");
                return(BadRequest());
            }


            _logger.LogError("User request - {@processClaim}");
            TryValidateModel(processClaim);
            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid user request.  Sending 422 Sstatus Request");
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var claimexpense = Mapper.Map <Expenses>(processClaim);

            _claimRepository.AddClaim(claimexpense);

            //Incase save fails, send 500 Status to the Client.
            if (!_claimRepository.Save())
            {
                _logger.LogError("Request failed to save {@claimexpense}");
                throw new Exception("Fail to add new Claim");
            }

            var claimToReturn = Mapper.Map <ClaimsDto>(claimexpense);

            _logger.LogError("Completed Request, Return status code 201");
            //Return url with status code 201 Created.  In postman under Header section there should be Location Url created
            return(CreatedAtRoute("GetExpense", new { id = new Guid(claimToReturn.Id) }, claimToReturn));
        }
Ejemplo n.º 5
0
 public void PostClaim([FromBody] Claim claim)
 {
     _claimRepository.AddClaim(claim);
 }