Skip to content

Commit 8d80c1e

Browse files
kpande-kssk-keeper
authored andcommitted
Add Keeper NSF examples (#476)
1 parent d90569c commit 8d80c1e

20 files changed

Lines changed: 766 additions & 13 deletions
Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Threading.Tasks;
35
using KeeperSecurity.Vault;
46

5-
67
namespace Sample.FoldersExample
78
{
89
public static class ListFolderExample
@@ -12,19 +13,54 @@ public static async Task ListFolder(VaultOnline vault = null)
1213
vault = await AuthenticateAndGetVault.ResolveVaultAsync(vault);
1314
if (vault == null) return;
1415
ListFolderSimple(vault);
15-
1616
}
1717

1818
public static void ListFolderSimple(VaultOnline vault)
1919
{
20-
Console.WriteLine("{0,-30} {1,-46}", "Folder Name", "Folder UID");
21-
Console.WriteLine(new string('-', 30) + " " + new string('-', 46));
20+
var folders = new List<(string Uid, string Name, string Category, int Subfolders, int Records)>();
21+
22+
foreach (var nsfFolder in vault.KeeperNSFFolderNodes)
23+
{
24+
folders.Add((
25+
nsfFolder.FolderUid,
26+
nsfFolder.Name,
27+
"Nested Shared Folder",
28+
nsfFolder.Subfolders.Count,
29+
nsfFolder.Records.Count));
30+
}
2231

2332
foreach (var folder in vault.Folders)
2433
{
25-
Console.WriteLine("{0,-30} {1,-46}", folder.Name, folder.FolderUid);
34+
if (string.IsNullOrEmpty(folder.FolderUid))
35+
continue;
36+
37+
folders.Add((
38+
folder.FolderUid,
39+
folder.Name,
40+
"Classic",
41+
folder.Subfolders.Count,
42+
folder.Records.Count));
2643
}
27-
}
2844

45+
folders = folders.OrderBy(f => f.Name).ToList();
46+
47+
if (folders.Count == 0)
48+
{
49+
Console.WriteLine("No folders found.");
50+
return;
51+
}
52+
53+
Console.WriteLine("Found {0} folder(s)", folders.Count);
54+
Console.WriteLine("{0,-30} {1,-46} {2,-22} {3,-10} {4,-8}",
55+
"Folder Name", "Folder UID", "Category", "Subfolders", "Records");
56+
Console.WriteLine(new string('-', 30) + " " + new string('-', 46) + " " +
57+
new string('-', 22) + " " + new string('-', 10) + " " + new string('-', 8));
58+
59+
foreach (var folder in folders)
60+
{
61+
Console.WriteLine("{0,-30} {1,-46} {2,-22} {3,-10} {4,-8}",
62+
folder.Name, folder.Uid, folder.Category, folder.Subfolders, folder.Records);
63+
}
64+
}
2965
}
3066
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using KeeperSecurity.Vault;
4+
5+
namespace Sample.KeeperNSFExamples
6+
{
7+
public static class CreateKeeperNSFFolderExample
8+
{
9+
public static async Task Create(
10+
VaultOnline vault,
11+
string folderName,
12+
string parentFolderUid = null,
13+
string color = null,
14+
bool inheritPermissions = true)
15+
{
16+
vault = await AuthenticateAndGetVault.ResolveVaultAsync(vault);
17+
if (vault == null) return;
18+
19+
try
20+
{
21+
var folderUid = await vault.CreateKeeperNSFFolder(
22+
folderName, parentFolderUid, color, inheritPermissions);
23+
Console.WriteLine($"Keeper NSF folder '{folderName}' created (UID: {folderUid}).");
24+
}
25+
catch (Exception ex)
26+
{
27+
Console.WriteLine($"Error creating Keeper NSF folder: {ex.Message}");
28+
}
29+
}
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using KeeperSecurity.Vault;
5+
6+
namespace Sample.KeeperNSFExamples
7+
{
8+
public static class CreateKeeperNSFRecordExample
9+
{
10+
public static async Task Create(
11+
VaultOnline vault,
12+
string title,
13+
string recordType = "login",
14+
string folderUid = null,
15+
string notes = null,
16+
IDictionary<string, object> fields = null)
17+
{
18+
vault = await AuthenticateAndGetVault.ResolveVaultAsync(vault);
19+
if (vault == null) return;
20+
21+
try
22+
{
23+
var recordUid = await vault.CreateKeeperNSFRecord(
24+
title, recordType, folderUid, notes, ToStringFields(fields));
25+
Console.WriteLine($"Keeper NSF record '{title}' created (UID: {recordUid}).");
26+
}
27+
catch (Exception ex)
28+
{
29+
Console.WriteLine($"Error creating Keeper NSF record: {ex.Message}");
30+
}
31+
}
32+
33+
private static IDictionary<string, string> ToStringFields(IDictionary<string, object> fields)
34+
{
35+
if (fields == null) return null;
36+
37+
var result = new Dictionary<string, string>();
38+
foreach (var kvp in fields)
39+
{
40+
result[kvp.Key] = kvp.Value?.ToString();
41+
}
42+
43+
return result;
44+
}
45+
}
46+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using KeeperSecurity.Vault;
5+
6+
namespace Sample.KeeperNSFExamples
7+
{
8+
public static class GetKeeperNSFRecordExample
9+
{
10+
public static async Task ShowDetails(VaultOnline vault, string recordUidOrTitle)
11+
{
12+
vault = await AuthenticateAndGetVault.ResolveVaultAsync(vault);
13+
if (vault == null) return;
14+
15+
if (!vault.TryResolveKeeperNSFRecord(recordUidOrTitle, out var record))
16+
{
17+
Console.WriteLine($"Keeper NSF record '{recordUidOrTitle}' not found.");
18+
return;
19+
}
20+
21+
Console.WriteLine("======== Keeper NSF Record ========");
22+
Console.WriteLine($"UID: {record.RecordUid}");
23+
Console.WriteLine($"Title: {record.Title}");
24+
Console.WriteLine($"Type: {record.Type}");
25+
Console.WriteLine($"Version: {record.Version}");
26+
Console.WriteLine($"Revision: {record.Revision}");
27+
Console.WriteLine($"Shared: {record.Shared}");
28+
if (!string.IsNullOrEmpty(record.Notes))
29+
{
30+
Console.WriteLine($"Notes: {record.Notes}");
31+
}
32+
33+
var folders = vault.GetKeeperNSFFoldersForRecord(record.RecordUid).ToList();
34+
if (folders.Count > 0)
35+
{
36+
Console.WriteLine("Folders: " + string.Join(", ", folders));
37+
}
38+
39+
if (record.Fields != null && record.Fields.Count > 0)
40+
{
41+
Console.WriteLine("Fields:");
42+
foreach (var field in record.Fields)
43+
{
44+
var value = field.Value != null ? string.Join(", ", field.Value) : "";
45+
Console.WriteLine($" {field.Type}: {value}");
46+
}
47+
}
48+
49+
Console.WriteLine("===================================");
50+
}
51+
}
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using KeeperSecurity.Vault;
4+
5+
namespace Sample.KeeperNSFExamples
6+
{
7+
public static class GetKeeperNSFShortcutsExample
8+
{
9+
public static async Task ListShortcuts(
10+
VaultOnline vault = null,
11+
string recordUid = null,
12+
string folderUid = null)
13+
{
14+
vault = await AuthenticateAndGetVault.ResolveVaultAsync(vault);
15+
if (vault == null) return;
16+
17+
var shortcuts = vault.GetKeeperNSFShortcuts(recordUid, folderUid);
18+
if (shortcuts == null || shortcuts.Count == 0)
19+
{
20+
Console.WriteLine("No Keeper NSF shortcuts found.");
21+
return;
22+
}
23+
24+
foreach (var entry in shortcuts)
25+
{
26+
Console.WriteLine($"Record: {entry.RecordUid} ({entry.Title})");
27+
foreach (var folder in entry.Folders)
28+
{
29+
Console.WriteLine($" Folder: {folder.FolderUid} ({folder.Name})");
30+
}
31+
}
32+
33+
Console.WriteLine($"\nTotal: {shortcuts.Count} shortcut(s)");
34+
}
35+
}
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using KeeperSecurity.Vault;
4+
5+
namespace Sample.KeeperNSFExamples
6+
{
7+
public static class GrantKeeperNSFFolderAccessExample
8+
{
9+
/// <param name="role">viewer, shared-manager, content-manager, content-share-manager, or full-manager</param>
10+
public static async Task Grant(
11+
VaultOnline vault,
12+
string folderUid,
13+
string userEmail,
14+
string role = "viewer")
15+
{
16+
vault = await AuthenticateAndGetVault.ResolveVaultAsync(vault);
17+
if (vault == null) return;
18+
19+
try
20+
{
21+
await vault.GrantKeeperNSFFolderAccess(folderUid, userEmail, role);
22+
Console.WriteLine($"Granted '{role}' on folder {folderUid} to {userEmail}.");
23+
}
24+
catch (Exception ex)
25+
{
26+
Console.WriteLine($"Error granting folder access: {ex.Message}");
27+
}
28+
}
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using KeeperSecurity.Vault;
4+
5+
namespace Sample.KeeperNSFExamples
6+
{
7+
public static class KeepKeeperNSFRecordInFolderExample
8+
{
9+
public static async Task KeepInFolder(
10+
VaultOnline vault,
11+
string recordUid,
12+
string keepFolderUid)
13+
{
14+
vault = await AuthenticateAndGetVault.ResolveVaultAsync(vault);
15+
if (vault == null) return;
16+
17+
try
18+
{
19+
var result = await vault.KeepKeeperNSFRecordInFolder(recordUid, keepFolderUid);
20+
Console.WriteLine($"Kept record {recordUid} in folder {keepFolderUid}.");
21+
Console.WriteLine($" Removed from {result.Removals?.Count ?? 0} other folder(s).");
22+
}
23+
catch (Exception ex)
24+
{
25+
Console.WriteLine($"Error keeping record in folder: {ex.Message}");
26+
}
27+
}
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using KeeperSecurity.Vault;
4+
5+
namespace Sample.KeeperNSFExamples
6+
{
7+
public static class LinkKeeperNSFRecordToFolderExample
8+
{
9+
public static async Task Link(
10+
VaultOnline vault,
11+
string recordUidOrTitle,
12+
string folderUidOrName)
13+
{
14+
vault = await AuthenticateAndGetVault.ResolveVaultAsync(vault);
15+
if (vault == null) return;
16+
17+
try
18+
{
19+
await vault.LinkKeeperNSFRecordToFolder(recordUidOrTitle, folderUidOrName);
20+
Console.WriteLine($"Linked record '{recordUidOrTitle}' into folder '{folderUidOrName}'.");
21+
}
22+
catch (Exception ex)
23+
{
24+
Console.WriteLine($"Error linking Keeper NSF record: {ex.Message}");
25+
}
26+
}
27+
}
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using KeeperSecurity.Vault;
5+
6+
namespace Sample.KeeperNSFExamples
7+
{
8+
public static class ListKeeperNSFFoldersExample
9+
{
10+
public static async Task ListAll(VaultOnline vault = null)
11+
{
12+
vault = await AuthenticateAndGetVault.ResolveVaultAsync(vault);
13+
if (vault == null) return;
14+
15+
var folders = vault.KeeperNSFFolderNodes.ToList();
16+
if (folders.Count == 0)
17+
{
18+
Console.WriteLine("No Keeper NSF folders found.");
19+
return;
20+
}
21+
22+
Console.WriteLine("{0,-4} {1,-40} {2,-30} {3,-10} {4,-10}",
23+
"#", "Folder UID", "Name", "Subfolders", "Records");
24+
Console.WriteLine(new string('-', 100));
25+
26+
var index = 1;
27+
foreach (var folder in folders)
28+
{
29+
Console.WriteLine("{0,-4} {1,-40} {2,-30} {3,-10} {4,-10}",
30+
index, folder.FolderUid, folder.Name, folder.Subfolders.Count, folder.Records.Count);
31+
index++;
32+
}
33+
34+
Console.WriteLine($"\nTotal: {folders.Count} Keeper NSF folder(s)");
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)