Skip to content

Commit a05939b

Browse files
committed
add secrets SDK tests, imperative typedoc
Signed-off-by: Trae Yelovich <[email protected]>
1 parent 8e983f8 commit a05939b

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

packages/imperative/src/security/src/DefaultCredentialManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ export class DefaultCredentialManager extends AbstractCredentialManager {
110110
break;
111111
}
112112

113+
Logger.getImperativeLogger().trace(`[DefaultCredentialManager] Persist value (win32): ${this.persistValueWin32}`);
114+
113115
/* Gather all services. We will load secure properties for the first
114116
* successful service found in the order that they are placed in this array.
115117
*/

packages/imperative/src/security/src/doc/IDefaultCredentialManagerOptions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,24 @@
1111

1212
import type { ICredentialManagerOptions } from "./ICredentialManagerOptions";
1313

14+
/**
15+
* Used to map value in `imperative.json` to respective value for Win32 persistence flag in CredentialA. See {@link PersistenceValue} for flag values.
16+
*/
1417
export enum PersistenceLevel {
1518
SessionOnly = "session",
1619
LocalMachine = "local_machine",
1720
Enterprise = "enterprise"
1821
}
1922

23+
/**
24+
* Note: Values map to `Persist` variable in [CredentialA](https://learn.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentiala) structure.
25+
*/
2026
export enum PersistenceValue {
27+
// CRED_PERSIST_SESSION
2128
SessionOnly = 1,
29+
// CRED_PERSIST_LOCAL_MACHINE
2230
LocalMachine = 2,
31+
// CRED_PERSIST_ENTERPRISE
2332
Enterprise = 3
2433
}
2534

packages/secrets/src/keyring/__test__/index.spec.mjs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const randomAsciiString = (len) => {
2222
return str;
2323
};
2424

25+
const isWin32 = process.platform === "win32";
26+
2527
const TEST_CREDENTIALS = [
2628
{ service: "TestKeyring", account: "TestASCII" },
2729
{ service: "TestKeyring", account: "TestUTF8" },
@@ -32,6 +34,9 @@ const TEST_CREDENTIALS = [
3234
{ service: "TestEmptyAccount", account: "" },
3335
{ service: "", account: "TestEmptyService" },
3436
{ service: "TestKeyring", account: "PwNullTerm" },
37+
{ service: "TestKeyring", account: "PersistSession", password: "SessionPw" },
38+
{ service: "TestKeyring", account: "PersistLocalMachine", password: "LocalMachinePw" },
39+
{ service: "TestKeyring", account: "PersistEnterprise", password: "EnterprisePw" },
3540
];
3641

3742
test.serial("get/setPassword with binary data", async (t) => {
@@ -112,6 +117,29 @@ test.serial("get/setPassword fails with null/undefined data", async (t) => {
112117
}
113118
});
114119

120+
if (isWin32) {
121+
// Unit test specific to Windows API call (needs to be called before rest of Win32 tests)
122+
test.serial(
123+
"get/setPassword works for all 3 Win32 persistence levels",
124+
async (t) => {
125+
const credsToSave = TEST_CREDENTIALS.slice(-3).map((c, i) => ({ ...c, persist: i + 1 }));
126+
127+
try {
128+
for (const cred of credsToSave) {
129+
await setPassword(cred.service, cred.account, cred.password, cred.persist);
130+
t.is(await getPassword(cred.service, cred.account), cred.password);
131+
}
132+
} catch (err) {
133+
t.fail(
134+
"setPassword should not throw an exception when setting credentials with a persistence flag"
135+
);
136+
}
137+
138+
t.pass();
139+
}
140+
);
141+
}
142+
115143
test.serial(
116144
"get/setPassword with password containing extra null terminators",
117145
async (t) => {
@@ -146,6 +174,9 @@ test.serial(
146174
},
147175
{ account: "TestUTF16", password: "🌞🌙🌟🌴" },
148176
{ account: "PwNullTerm", password: "PW\x00" },
177+
{ account: "PersistSession", password: "SessionPw" },
178+
{ account: "PersistLocalMachine", password: "LocalMachinePw" },
179+
{ account: "PersistEnterprise", password: "EnterprisePw" }
149180
];
150181
const actual = await findCredentials("TestKeyring");
151182
t.is(
@@ -219,7 +250,7 @@ test("deletePassword deletes all test credentials", async (t) => {
219250
});
220251

221252
// Unit tests specific to Windows API calls
222-
if (process.platform === "win32") {
253+
if (isWin32) {
223254
test.serial(
224255
"setPassword fails when blob exceeds CRED_MAX_CREDENTIAL_BLOB_SIZE",
225256
async (t) => {

0 commit comments

Comments
 (0)