using System;
using System.IO;
using System.Linq;
using Alkami.Ops.SecretServer.Enum;
using Alkami.Ops.SecretServer.SSWebService;
namespace Alkami.Ops.SecretServer.Model
{
///
/// Represents a Certificate or SSL Certificate secret
///
public class Certificate : SecretBase
{
///
/// The Certificate name from the "Certificate Name" field on the secret
///
public string Name => SecretItems.FirstOrDefault(p => string.Equals(p.FieldName, "Certificate Name", StringComparison.OrdinalIgnoreCase))?.Value;
///
/// The Certificate password from the "Import Password" field on the secret
///
public string Password => SecretItems.FirstOrDefault(p => string.Equals(p.FieldName, "Import Password", StringComparison.OrdinalIgnoreCase) && p.IsPassword)?.Value;
///
/// The FileID of the attachment on the secret from the "Pfx File" field
///
public int? FileId => SecretItems.FirstOrDefault(p => string.Equals(p.FieldName, "Pfx File", StringComparison.OrdinalIgnoreCase) && p.IsFile)?.Id;
///
/// The secret type
///
///
/// Returns .Certificate
///
public new SecretType SecretType => SecretType.Certificate;
///
/// The FileName of the attachment. Added externally from a subsequent call to SecretServer
///
public string FileName { get; set; }
///
/// The FileAttachment as a byte array. Added externally from a subsequent call to SecretServer
///
public byte[] FileAttachment { get; set; }
///
/// Saves the to disk
///
/// The path to save the file to. The FileName is appended from
///
public string SaveFileToDisk(string outputPath)
{
var outFile = Path.Combine(outputPath, FileName);
File.WriteAllBytes(outFile, FileAttachment);
return outFile;
}
///
/// Instantiates a new instance of the Certificate class
///
///
public Certificate(Secret secret) : base(secret)
{
}
}
}