@awsesh/core 1.0.0-beta.202601241323 → 1.0.0-beta.202601241355

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export { AWSClient } from "./client";
3
3
  export { Credentials } from "./credentials";
4
4
  export { Sessions } from "./sessions";
5
5
  export { Storage } from "./storage";
6
- import type { AwseshOptions, SSOSession, SSOLoginInfo, TokenCache, AccountCache, RoleCredentials, LastSelected, LastSelectedPerSession, ActiveCredential, LastSetCredential } from "./types";
6
+ import type { AwseshOptions, SSOSession, SSOLoginInfo, TokenCache, AccountCache, RoleCredentials, LastSelected, LastSelectedPerSession, ActiveCredential, LastSetCredential, SetCredentialOptions, SetCredentialResult } from "./types";
7
7
  export declare function createAwsesh(options: AwseshOptions): {
8
8
  sessions: {
9
9
  list: () => Promise<SSOSession[]>;
@@ -62,11 +62,13 @@ export declare function createAwsesh(options: AwseshOptions): {
62
62
  save: (sessionName: string, accountId: string, region: string) => Promise<void>;
63
63
  getAll: (sessionName: string) => Promise<Record<string, string>>;
64
64
  };
65
+ /** @internal Low-level primitive. Prefer `setCredential()` for most use cases. */
65
66
  credentials: {
66
67
  write: (profileName: string, creds: RoleCredentials, region?: string) => Promise<void>;
67
68
  removeProfile: (profileName: string) => Promise<void>;
68
69
  listProfiles: () => Promise<string[]>;
69
70
  };
71
+ /** @internal Low-level primitive. Prefer `setCredential()` / `clearCredential()` for most use cases. */
70
72
  activeCredentials: {
71
73
  list: () => Promise<ActiveCredential[]>;
72
74
  save: (credential: ActiveCredential) => Promise<void>;
@@ -74,10 +76,32 @@ export declare function createAwsesh(options: AwseshOptions): {
74
76
  cleanup: () => Promise<void>;
75
77
  remove: (accountId: string, roleName: string) => Promise<void>;
76
78
  };
79
+ /** @internal Low-level primitive. Prefer `setCredential()` / `clearCredential()` for most use cases. */
77
80
  lastSetCredential: {
78
81
  get: () => Promise<LastSetCredential | undefined>;
79
82
  save: (credential: LastSetCredential) => Promise<void>;
80
83
  clear: () => Promise<void>;
81
84
  };
85
+ /**
86
+ * High-level API: Set credentials with all tracking automatically handled.
87
+ * Writes to ~/.aws/credentials, updates activeCredentials, lastSetCredential, and lastSelected.
88
+ * If no profileName is provided, looks up the configured profile for this session/account/role.
89
+ */
90
+ setCredential(options: SetCredentialOptions): Promise<SetCredentialResult>;
91
+ /**
92
+ * High-level API: Clear a specific credential.
93
+ * Removes from activeCredentials and clears lastSetCredential if it matches.
94
+ */
95
+ clearCredential(accountId: string, roleName: string, removeProfile?: string): Promise<void>;
96
+ /**
97
+ * High-level API: Clear all credentials for a session.
98
+ * Removes all matching credentials and clears lastSetCredential if it matches.
99
+ */
100
+ clearSessionCredentials(sessionName: string, removeProfiles?: boolean): Promise<void>;
101
+ /**
102
+ * High-level API: Clear all credentials.
103
+ * Removes all tracked credentials and clears lastSetCredential.
104
+ */
105
+ clearAllCredentials(removeProfiles?: boolean): Promise<void>;
82
106
  };
83
107
  export type Awsesh = ReturnType<typeof createAwsesh>;
package/index.js CHANGED
@@ -20460,6 +20460,105 @@ function createAwsesh(options) {
20460
20460
  clear: async () => {
20461
20461
  await storage.remove("credentials/last-set");
20462
20462
  }
20463
+ },
20464
+ async setCredential(options2) {
20465
+ const {
20466
+ credentials,
20467
+ sessionName,
20468
+ accountId,
20469
+ accountName,
20470
+ roleName,
20471
+ region,
20472
+ profileName: customProfileName
20473
+ } = options2;
20474
+ const configuredProfile = customProfileName === undefined ? await storage.read("preference/profile-names").then((data) => data?.[sessionName]?.[accountName]?.[roleName]) : undefined;
20475
+ const profileName = customProfileName || configuredProfile || "default";
20476
+ const isDefault = !customProfileName && !configuredProfile;
20477
+ await Credentials.write({
20478
+ awsDir,
20479
+ profileName,
20480
+ credentials,
20481
+ region
20482
+ });
20483
+ await storage.update("credentials/active", (existing) => {
20484
+ const list = Array.isArray(existing) ? existing : [];
20485
+ const now = new Date;
20486
+ const filtered = list.filter((c) => new Date(c.expiration) > now).filter((c) => !(c.accountId === accountId && c.roleName === roleName)).map((c) => isDefault ? { ...c, isDefault: false } : c).filter((c) => c.isDefault || c.profileName !== "default");
20487
+ return [
20488
+ ...filtered,
20489
+ {
20490
+ profileName,
20491
+ accountId,
20492
+ accountName,
20493
+ roleName,
20494
+ sessionName,
20495
+ expiration: credentials.expiration.toISOString(),
20496
+ isDefault
20497
+ }
20498
+ ];
20499
+ });
20500
+ await storage.write("credentials/last-set", {
20501
+ profileName,
20502
+ accountId,
20503
+ accountName,
20504
+ roleName,
20505
+ sessionName,
20506
+ region,
20507
+ setAt: new Date().toISOString()
20508
+ });
20509
+ await storage.update("preference/last-selected", (existing) => ({
20510
+ ...existing,
20511
+ session: sessionName,
20512
+ account: accountName,
20513
+ role: roleName
20514
+ }));
20515
+ return {
20516
+ profileName,
20517
+ expiration: credentials.expiration,
20518
+ isDefault
20519
+ };
20520
+ },
20521
+ async clearCredential(accountId, roleName, removeProfile) {
20522
+ await storage.update("credentials/active", (existing) => {
20523
+ if (!existing || !Array.isArray(existing))
20524
+ return [];
20525
+ return existing.filter((c) => !(c.accountId === accountId && c.roleName === roleName));
20526
+ });
20527
+ const lastSet = await storage.read("credentials/last-set");
20528
+ if (lastSet && lastSet.accountId === accountId && lastSet.roleName === roleName) {
20529
+ await storage.remove("credentials/last-set");
20530
+ }
20531
+ if (removeProfile) {
20532
+ await Credentials.removeProfile({ awsDir, profileName: removeProfile });
20533
+ }
20534
+ },
20535
+ async clearSessionCredentials(sessionName, removeProfiles) {
20536
+ const active = await storage.read("credentials/active");
20537
+ const sessionCreds = (active || []).filter((c) => c.sessionName === sessionName);
20538
+ if (removeProfiles) {
20539
+ for (const cred of sessionCreds) {
20540
+ await Credentials.removeProfile({ awsDir, profileName: cred.profileName });
20541
+ }
20542
+ }
20543
+ await storage.update("credentials/active", (existing) => {
20544
+ if (!existing || !Array.isArray(existing))
20545
+ return [];
20546
+ return existing.filter((c) => c.sessionName !== sessionName);
20547
+ });
20548
+ const lastSet = await storage.read("credentials/last-set");
20549
+ if (lastSet && lastSet.sessionName === sessionName) {
20550
+ await storage.remove("credentials/last-set");
20551
+ }
20552
+ },
20553
+ async clearAllCredentials(removeProfiles) {
20554
+ if (removeProfiles) {
20555
+ const active = await storage.read("credentials/active");
20556
+ for (const cred of active || []) {
20557
+ await Credentials.removeProfile({ awsDir, profileName: cred.profileName });
20558
+ }
20559
+ }
20560
+ await storage.write("credentials/active", []);
20561
+ await storage.remove("credentials/last-set");
20463
20562
  }
20464
20563
  };
20465
20564
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awsesh/core",
3
- "version": "1.0.0-beta.202601241323",
3
+ "version": "1.0.0-beta.202601241355",
4
4
  "description": "AWS SSO session management SDK",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/types.d.ts CHANGED
@@ -73,3 +73,17 @@ export interface LastSetCredential {
73
73
  region?: string;
74
74
  setAt: string;
75
75
  }
76
+ export interface SetCredentialOptions {
77
+ credentials: RoleCredentials;
78
+ sessionName: string;
79
+ accountId: string;
80
+ accountName: string;
81
+ roleName: string;
82
+ region?: string;
83
+ profileName?: string;
84
+ }
85
+ export interface SetCredentialResult {
86
+ profileName: string;
87
+ expiration: Date;
88
+ isDefault: boolean;
89
+ }