public async Task <IActionResult> Add(CreateKeyRequest request) { var key = new Key { Body = request.Body, Sha = Sha256.Compute(request.Body) }; try { var result = await _keyService.CreateKey(key); var keyCreatedResponse = new KeyCreatedResponse { Body = result.Body, Id = result.ShortSha }; return(CreatedAtAction("Get", "Keys", new { id = keyCreatedResponse.Id }, keyCreatedResponse)); } catch (ConflictingKeyConflictException e) { _logger.LogError(e, "caught conflicting exception"); ModelState.AddModelError("Errors", "Key already exists"); return(Conflict(new ValidationProblemDetails(ModelState))); } catch (Exception e) { ModelState.AddModelError("Errors", "Unknown error occurred"); _logger.LogError(e, "caught exception"); return(new ObjectResult(new ValidationProblemDetails(ModelState)) { StatusCode = StatusCodes.Status500InternalServerError }); } }
public async Task <string> ResolveKey(string keyId) { var response = await _keyStoreClient.Keys.GetAsync(keyId); return(response switch { KeyCreatedResponse keyCreatedResponse => keyCreatedResponse.Body, _ => throw new KeyNotFoundException($"key: '{keyId}' not found") });
public async Task <IActionResult> Get(string id) { var key = await _keyRepository.FindByShortSha(id); if (key == null) { ModelState.AddModelError("Errors", "Not found"); return(NotFound(new ValidationProblemDetails(ModelState))); } var res = new KeyCreatedResponse { Body = key.Body, Id = key.ShortSha }; return(Ok(res)); }
public async Task <LockingResponse> Lock(string key) { var response = await _keyStoreClient.Keys.AddAsync(new CreateKeyRequest { Body = key }); return(response switch { KeyCreatedResponse keyCreatedResponse => new LockingResponse { Acquired = true }, _ => new LockingResponse { Acquired = false } });