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

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[]>;
@@ -79,5 +79,25 @@ export declare function createAwsesh(options: AwseshOptions): {
79
79
  save: (credential: LastSetCredential) => Promise<void>;
80
80
  clear: () => Promise<void>;
81
81
  };
82
+ /**
83
+ * High-level API: Set credentials with all tracking automatically handled.
84
+ * Writes to ~/.aws/credentials, updates activeCredentials, lastSetCredential, and lastSelected.
85
+ */
86
+ setCredential(options: SetCredentialOptions): Promise<SetCredentialResult>;
87
+ /**
88
+ * High-level API: Clear a specific credential.
89
+ * Removes from activeCredentials and clears lastSetCredential if it matches.
90
+ */
91
+ clearCredential(accountId: string, roleName: string, removeProfile?: string): Promise<void>;
92
+ /**
93
+ * High-level API: Clear all credentials for a session.
94
+ * Removes all matching credentials and clears lastSetCredential if it matches.
95
+ */
96
+ clearSessionCredentials(sessionName: string, removeProfiles?: boolean): Promise<void>;
97
+ /**
98
+ * High-level API: Clear all credentials.
99
+ * Removes all tracked credentials and clears lastSetCredential.
100
+ */
101
+ clearAllCredentials(removeProfiles?: boolean): Promise<void>;
82
102
  };
83
103
  export type Awsesh = ReturnType<typeof createAwsesh>;
package/index.js CHANGED
@@ -20460,6 +20460,104 @@ 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 profileName = customProfileName || "default";
20475
+ const isDefault = !customProfileName;
20476
+ await Credentials.write({
20477
+ awsDir,
20478
+ profileName,
20479
+ credentials,
20480
+ region
20481
+ });
20482
+ await storage.update("credentials/active", (existing) => {
20483
+ const list = Array.isArray(existing) ? existing : [];
20484
+ const now = new Date;
20485
+ 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");
20486
+ return [
20487
+ ...filtered,
20488
+ {
20489
+ profileName,
20490
+ accountId,
20491
+ accountName,
20492
+ roleName,
20493
+ sessionName,
20494
+ expiration: credentials.expiration.toISOString(),
20495
+ isDefault
20496
+ }
20497
+ ];
20498
+ });
20499
+ await storage.write("credentials/last-set", {
20500
+ profileName,
20501
+ accountId,
20502
+ accountName,
20503
+ roleName,
20504
+ sessionName,
20505
+ region,
20506
+ setAt: new Date().toISOString()
20507
+ });
20508
+ await storage.update("preference/last-selected", (existing) => ({
20509
+ ...existing,
20510
+ session: sessionName,
20511
+ account: accountName,
20512
+ role: roleName
20513
+ }));
20514
+ return {
20515
+ profileName,
20516
+ expiration: credentials.expiration,
20517
+ isDefault
20518
+ };
20519
+ },
20520
+ async clearCredential(accountId, roleName, removeProfile) {
20521
+ await storage.update("credentials/active", (existing) => {
20522
+ if (!existing || !Array.isArray(existing))
20523
+ return [];
20524
+ return existing.filter((c) => !(c.accountId === accountId && c.roleName === roleName));
20525
+ });
20526
+ const lastSet = await storage.read("credentials/last-set");
20527
+ if (lastSet && lastSet.accountId === accountId && lastSet.roleName === roleName) {
20528
+ await storage.remove("credentials/last-set");
20529
+ }
20530
+ if (removeProfile) {
20531
+ await Credentials.removeProfile({ awsDir, profileName: removeProfile });
20532
+ }
20533
+ },
20534
+ async clearSessionCredentials(sessionName, removeProfiles) {
20535
+ const active = await storage.read("credentials/active");
20536
+ const sessionCreds = (active || []).filter((c) => c.sessionName === sessionName);
20537
+ if (removeProfiles) {
20538
+ for (const cred of sessionCreds) {
20539
+ await Credentials.removeProfile({ awsDir, profileName: cred.profileName });
20540
+ }
20541
+ }
20542
+ await storage.update("credentials/active", (existing) => {
20543
+ if (!existing || !Array.isArray(existing))
20544
+ return [];
20545
+ return existing.filter((c) => c.sessionName !== sessionName);
20546
+ });
20547
+ const lastSet = await storage.read("credentials/last-set");
20548
+ if (lastSet && lastSet.sessionName === sessionName) {
20549
+ await storage.remove("credentials/last-set");
20550
+ }
20551
+ },
20552
+ async clearAllCredentials(removeProfiles) {
20553
+ if (removeProfiles) {
20554
+ const active = await storage.read("credentials/active");
20555
+ for (const cred of active || []) {
20556
+ await Credentials.removeProfile({ awsDir, profileName: cred.profileName });
20557
+ }
20558
+ }
20559
+ await storage.write("credentials/active", []);
20560
+ await storage.remove("credentials/last-set");
20463
20561
  }
20464
20562
  };
20465
20563
  }
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.202601241326",
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
+ }