Skip to content

Commit

Permalink
little refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Jan 3, 2025
1 parent 867e953 commit f1ab9a1
Showing 1 changed file with 59 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,6 @@ internal SFCredentialManagerFileImpl(FileOperations fileOperations, DirectoryOpe
_environmentOperations = environmentOperations;
}

internal void WriteToJsonFile(string content)
{
s_logger.Debug($"Writing credentials to json file in {_fileStorage.JsonCacheFilePath}");
if (!_directoryOperations.Exists(_fileStorage.JsonCacheDirectory))
{
_directoryOperations.CreateDirectory(_fileStorage.JsonCacheDirectory);
}
if (_fileOperations.Exists(_fileStorage.JsonCacheFilePath))
{
s_logger.Info($"The existing json file for credential cache in {_fileStorage.JsonCacheFilePath} will be overwritten");
}
else
{
s_logger.Info($"Creating the json file for credential cache in {_fileStorage.JsonCacheFilePath}");
var createFileResult = _unixOperations.CreateFileWithPermissions(_fileStorage.JsonCacheFilePath,
FilePermissions.S_IRUSR | FilePermissions.S_IWUSR);
if (createFileResult == -1)
{
var errorMessage = "Failed to create the JSON token cache file";
s_logger.Error(errorMessage);
throw new Exception(errorMessage);
}
}
_fileOperations.Write(_fileStorage.JsonCacheFilePath, content, ValidateFilePermissions);
}

internal KeyTokenDict ReadJsonFile()
{
var contentFile = _fileOperations.ReadAllText(_fileStorage.JsonCacheFilePath, ValidateFilePermissions);
try
{
var fileContent = JsonConvert.DeserializeObject<CredentialsFileContent>(contentFile);
return (fileContent == null || fileContent.Tokens == null) ? new KeyTokenDict() : fileContent.Tokens;
}
catch (Exception)
{
s_logger.Error("Failed to parse the file with cached credentials");
return new KeyTokenDict();
}
}

public string GetCredentials(string key)
{
s_logger.Debug($"Getting credentials for key: {key}");
Expand Down Expand Up @@ -145,8 +104,7 @@ public void RemoveCredentials(string key)
{
var keyTokenPairs = ReadJsonFile();
keyTokenPairs.Remove(key);
var credentials = new CredentialsFileContent { Tokens = keyTokenPairs };
WriteToJsonFile(JsonConvert.SerializeObject(credentials));
WriteToJsonFile(keyTokenPairs);
}
}
catch (Exception exception)
Expand Down Expand Up @@ -178,9 +136,7 @@ public void SaveCredentials(string key, string token)
{
KeyTokenDict keyTokenPairs = _fileOperations.Exists(_fileStorage.JsonCacheFilePath) ? ReadJsonFile() : new KeyTokenDict();
keyTokenPairs[key] = token;
var credentials = new CredentialsFileContent { Tokens = keyTokenPairs };
string jsonString = JsonConvert.SerializeObject(credentials);
WriteToJsonFile(jsonString);
WriteToJsonFile(keyTokenPairs);
}
catch (Exception exception)
{
Expand All @@ -194,6 +150,63 @@ public void SaveCredentials(string key, string token)
}
}

private void WriteToJsonFile(KeyTokenDict keyTokenPairs)
{
var credentials = new CredentialsFileContent { Tokens = keyTokenPairs };
var jsonString = JsonConvert.SerializeObject(credentials);
WriteContentToJsonFile(jsonString);
}

private void WriteContentToJsonFile(string content)
{
s_logger.Debug($"Writing credentials to json file in {_fileStorage.JsonCacheFilePath}");
if (!_directoryOperations.Exists(_fileStorage.JsonCacheDirectory))
{
_directoryOperations.CreateDirectory(_fileStorage.JsonCacheDirectory);
}
if (_fileOperations.Exists(_fileStorage.JsonCacheFilePath))
{
s_logger.Info($"The existing json file for credential cache in {_fileStorage.JsonCacheFilePath} will be overwritten");
}
else
{
s_logger.Info($"Creating the json file for credential cache in {_fileStorage.JsonCacheFilePath}");
var createFileResult = _unixOperations.CreateFileWithPermissions(_fileStorage.JsonCacheFilePath,
FilePermissions.S_IRUSR | FilePermissions.S_IWUSR);
if (createFileResult == -1)
{
var errorMessage = "Failed to create the JSON token cache file";
s_logger.Error(errorMessage);
throw new Exception(errorMessage);
}
}
_fileOperations.Write(_fileStorage.JsonCacheFilePath, content, ValidateFilePermissions);
}

private KeyTokenDict ReadJsonFile()
{
string contentFile;
try
{
contentFile = _fileOperations.ReadAllText(_fileStorage.JsonCacheFilePath, ValidateFilePermissions);
}
catch (FileNotFoundException)
{
s_logger.Error("Failed to read the file with cached credentials because it does not exist");
return new KeyTokenDict();
}
try
{
var fileContent = JsonConvert.DeserializeObject<CredentialsFileContent>(contentFile);
return (fileContent == null || fileContent.Tokens == null) ? new KeyTokenDict() : fileContent.Tokens;
}
catch (Exception)
{
s_logger.Error("Failed to parse the file with cached credentials");
return new KeyTokenDict();
}
}

private void InitializeFileStorageIfNeeded()
{
if (_fileStorage != null)
Expand Down

0 comments on commit f1ab9a1

Please sign in to comment.