public async Task UpdateByUserCodeAsync(string userCode, DeviceCode data) { try { if (string.IsNullOrWhiteSpace(userCode)) { throw new ArgumentNullException( "user code can't be null or whitespace"); } if (data == null) { throw new ArgumentNullException("data can't be null"); } var deviceFlowCodes = await _repository.GetMultipleByTagsAsync( new string[] { $"{userCode}" }); var existing = deviceFlowCodes.FirstOrDefault(); if (existing == null) { if (_isErrorLoggingEnabled) { _logger.LogError( "{userCode} not found in NCache", userCode); } throw new InvalidOperationException( "Could not update device code"); } var entity = data?.ToEntity(existing.DeviceCode, userCode); if (_isDebugLoggingEnabled) { _logger.LogDebug("{userCode} found in NCache", userCode); } existing.SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value; existing.Data = entity.Data; await _repository.AddAsync(existing); } catch (Exception ex) { if (_isErrorLoggingEnabled) { _logger.LogError( ex, $"Something went wrong with UpdateByUserCodeAsync " + $"for {userCode}"); } throw; } }
public async Task StoreDeviceAuthorizationAsync( string deviceCode, string userCode, DeviceCode data) { try { if ((await FindByUserCodeAsync(userCode)) != null || (await FindByDeviceCodeAsync(deviceCode)) != null) { throw new Exception( $"Device flow codes with given user code and/or device " + $"code already exists"); } var deviceFlowCode = data?.ToEntity(deviceCode, userCode); await _repository.AddAsync(deviceFlowCode); if (_isDebugLoggingEnabled) { _logger.LogDebug( $"Device code with id {deviceCode} and user " + $"code {userCode} stored"); } } catch (Exception ex) { if (_isErrorLoggingEnabled) { _logger.LogError( ex, $"Something went wrong with " + $"StoreDeviceAuthorizationAsync " + $"for user code {userCode} and id {deviceCode}"); } throw; } }