Files
sqltoolsservice/src/Microsoft.SqlTools.Credentials/Credentials/Linux/FileTokenStorage.cs
Karl Burtram 0af7bef66d Create MS.SqlTools.Credentials project (#249)
* Initial credential service files

* Clean-up hostloader

* Update build scripts to build credentials archive

* Move hosting files to new assembly

* Add credentials files to MS.SqlTools.Credentials

* Remove duplicate files

* Update namespace in program.cs

* Fix test build breaks

* Update extensions visibility.

* Remove unused resource strings

* Add xproj files to SLN for appveyor builds

* Fix appveyor build break in test project

* Fix extensibility tests

* Fix various typos in latest iteration

* Add settings for Integration build

* Fix codecoverage.bat to use full pdb for new projects

* Fix bug when packing in folder with native images

* Fix typos in xproj

* Reset XLF to fix build.cmd
2017-02-23 16:09:58 -08:00

94 lines
2.8 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.SqlTools.ServiceLayer.Credentials.Contracts;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Newtonsoft.Json;
namespace Microsoft.SqlTools.ServiceLayer.Credentials.Linux
{
#if !WINDOWS_ONLY_BUILD
public class FileTokenStorage
{
private const int OwnerAccessMode = 384; // Permission 0600 - owner read/write, nobody else has access
private object lockObject = new object();
private string fileName;
public FileTokenStorage(string fileName)
{
Validate.IsNotNullOrEmptyString("fileName", fileName);
this.fileName = fileName;
}
public void AddEntries(IEnumerable<Credential> newEntries, IEnumerable<Credential> existingEntries)
{
var allEntries = existingEntries.Concat(newEntries);
this.SaveEntries(allEntries);
}
public void Clear()
{
this.SaveEntries(new List<Credential>());
}
public IEnumerable<Credential> LoadEntries()
{
if(!File.Exists(this.fileName))
{
return Enumerable.Empty<Credential>();
}
string serializedCreds;
lock (lockObject)
{
serializedCreds = File.ReadAllText(this.fileName);
}
CredentialsWrapper creds = JsonConvert.DeserializeObject<CredentialsWrapper>(serializedCreds, Constants.JsonSerializerSettings);
if(creds != null)
{
return creds.Credentials;
}
return Enumerable.Empty<Credential>();
}
public void SaveEntries(IEnumerable<Credential> entries)
{
CredentialsWrapper credentials = new CredentialsWrapper() { Credentials = entries.ToList() };
string serializedCreds = JsonConvert.SerializeObject(credentials, Constants.JsonSerializerSettings);
lock(lockObject)
{
WriteToFile(this.fileName, serializedCreds);
}
}
private static void WriteToFile(string filePath, string fileContents)
{
string dir = Path.GetDirectoryName(filePath);
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
// Overwrite file, then use ChMod to ensure we have
File.WriteAllText(filePath, fileContents);
// set appropriate permissions so only current user can read/write
Interop.Sys.ChMod(filePath, OwnerAccessMode);
}
}
#endif
}