@adobe/aio-commerce-lib-auth 0.6.2 → 0.8.0

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.
@@ -1,33 +1,141 @@
1
- /**
2
- * @license
3
- *
4
- * Copyright 2025 Adobe. All rights reserved.
5
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License. You may obtain a copy
7
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software distributed under
10
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
11
- * OF ANY KIND, either express or implied. See the License for the specific language
12
- * governing permissions and limitations under the License.
13
- */
14
-
15
- import * as valibot21 from "valibot";
1
+ import * as v from "valibot";
16
2
  import { InferOutput } from "valibot";
17
3
 
4
+ //#region source/lib/ims-auth/types.d.ts
5
+ /** Defines the headers required for IMS authentication. */
6
+ type ImsAuthHeaders = {
7
+ Authorization: string;
8
+ "x-api-key"?: string;
9
+ };
10
+ /** Defines an authentication provider for Adobe IMS. */
11
+ type ImsAuthProvider = {
12
+ getAccessToken: () => Promise<string> | string;
13
+ getHeaders: () => Promise<ImsAuthHeaders> | ImsAuthHeaders;
14
+ };
15
+ //#endregion
16
+ //#region source/lib/ims-auth/forwarding.d.ts
17
+ declare const ForwardedImsAuthSourceSchema: v.VariantSchema<"from", [v.ObjectSchema<{
18
+ readonly from: v.LiteralSchema<"headers", undefined>;
19
+ readonly headers: v.RecordSchema<v.StringSchema<undefined>, v.OptionalSchema<v.StringSchema<undefined>, undefined>, undefined>;
20
+ }, undefined>, v.ObjectSchema<{
21
+ readonly from: v.LiteralSchema<"getter", undefined>;
22
+ readonly getHeaders: v.CustomSchema<() => ImsAuthHeaders | Promise<ImsAuthHeaders>, v.ErrorMessage<v.CustomIssue> | undefined>;
23
+ }, undefined>, v.ObjectSchema<{
24
+ readonly from: v.LiteralSchema<"params", undefined>;
25
+ readonly params: v.LooseObjectSchema<{
26
+ readonly AIO_COMMERCE_AUTH_IMS_TOKEN: v.StringSchema<`Expected a string value for '${string}'`>;
27
+ readonly AIO_COMMERCE_AUTH_IMS_API_KEY: v.OptionalSchema<v.StringSchema<`Expected a string value for '${string}'`>, undefined>;
28
+ }, undefined>;
29
+ }, undefined>], undefined>;
30
+ /**
31
+ * Discriminated union for different sources of forwarded IMS auth credentials.
32
+ *
33
+ * - `headers`: Extract credentials from a raw headers object (e.g. an HTTP request).
34
+ * - `getter`: Use a function that returns IMS auth headers (sync or async).
35
+ * - `params`: Read credentials from a params object using `AIO_COMMERCE_AUTH_IMS_TOKEN` and `AIO_COMMERCE_AUTH_IMS_API_KEY` keys.
36
+ */
37
+ type ForwardedImsAuthSource = v.InferOutput<typeof ForwardedImsAuthSourceSchema>;
38
+ /**
39
+ * Creates an {@link ImsAuthProvider} by forwarding authentication credentials from various sources.
40
+ *
41
+ * @param source The source of the credentials to forward, as a {@link ForwardedImsAuthSource}.
42
+ * @returns An {@link ImsAuthProvider} instance that returns the forwarded access token and headers.
43
+ *
44
+ * @throws {CommerceSdkValidationError} If the source object is invalid.
45
+ * @throws {CommerceSdkValidationError} If `from: "headers"` is used and the `Authorization` header is missing.
46
+ * @throws {CommerceSdkValidationError} If `from: "headers"` is used and the `Authorization` header is not in Bearer token format.
47
+ * @throws {CommerceSdkValidationError} If `from: "params"` is used and `AIO_COMMERCE_AUTH_IMS_TOKEN` is missing or empty.
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * import { getForwardedImsAuthProvider } from "@adobe/aio-commerce-lib-auth";
52
+ *
53
+ * // From raw headers (e.g. from an HTTP request).
54
+ * const provider1 = getForwardedImsAuthProvider({
55
+ * from: "headers",
56
+ * headers: params.__ow_headers,
57
+ * });
58
+ *
59
+ * // From async getter (e.g. fetch from secret manager)
60
+ * const provider2 = getForwardedImsAuthProvider({
61
+ * from: "getter",
62
+ * getHeaders: async () => {
63
+ * const token = await secretManager.getSecret("ims-token");
64
+ * return { Authorization: `Bearer ${token}` };
65
+ * },
66
+ * });
67
+ *
68
+ * // From a params object (using AIO_COMMERCE_AUTH_IMS_TOKEN and AIO_COMMERCE_AUTH_IMS_API_KEY keys)
69
+ * const provider3 = getForwardedImsAuthProvider({
70
+ * from: "params",
71
+ * params: actionParams,
72
+ * });
73
+ *
74
+ * // Use the provider
75
+ * const token = await provider1.getAccessToken();
76
+ * const headers = await provider1.getHeaders();
77
+ * ```
78
+ */
79
+ declare function getForwardedImsAuthProvider(source: v.InferInput<typeof ForwardedImsAuthSourceSchema>): ImsAuthProvider;
80
+ /**
81
+ * Creates an {@link ImsAuthProvider} by forwarding authentication credentials from runtime action parameters.
82
+ *
83
+ * This function automatically detects the source of credentials by trying multiple strategies in order:
84
+ * 1. **Params token** - Looks for `AIO_COMMERCE_AUTH_IMS_TOKEN` (and optionally `AIO_COMMERCE_AUTH_IMS_API_KEY`) in the params object
85
+ * 2. **HTTP headers** - Falls back to extracting the `Authorization` header from `__ow_headers`
86
+ *
87
+ * Use this function when building actions that receive authenticated requests and need to forward
88
+ * those credentials to downstream services (proxy pattern).
89
+ *
90
+ * @param params The runtime action parameters object. Can contain either:
91
+ * - `AIO_COMMERCE_AUTH_IMS_TOKEN` and optionally `AIO_COMMERCE_AUTH_IMS_API_KEY` for direct token forwarding
92
+ * - `__ow_headers` with an `Authorization` header for HTTP request forwarding
93
+ * @returns An {@link ImsAuthProvider} instance that returns the forwarded access token and headers.
94
+ *
95
+ * @throws {Error} If neither a valid token param nor Authorization header is found.
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * import { forwardImsAuthProvider } from "@adobe/aio-commerce-lib-auth";
100
+ *
101
+ * export async function main(params: Record<string, unknown>) {
102
+ * // Automatically detects credentials from params or headers
103
+ * const authProvider = forwardImsAuthProvider(params);
104
+ *
105
+ * // Get the access token
106
+ * const token = await authProvider.getAccessToken();
107
+ *
108
+ * // Get headers for downstream API requests
109
+ * const headers = await authProvider.getHeaders();
110
+ * // {
111
+ * // Authorization: "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...",
112
+ * // "x-api-key": "my-api-key" // Only if available
113
+ * // }
114
+ *
115
+ * // Use the forwarded credentials in downstream API calls
116
+ * const response = await fetch("https://api.adobe.io/some-endpoint", {
117
+ * headers,
118
+ * });
119
+ *
120
+ * return { statusCode: 200, body: await response.json() };
121
+ * }
122
+ * ```
123
+ */
124
+ declare function forwardImsAuthProvider(params: Record<string, unknown>): ImsAuthProvider;
125
+ //#endregion
18
126
  //#region source/lib/ims-auth/schema.d.ts
19
127
  /** Validation schema for IMS auth environment values. */
20
- declare const ImsAuthEnvSchema: valibot21.PicklistSchema<["prod", "stage"], undefined>;
128
+ declare const ImsAuthEnvSchema: v.PicklistSchema<["prod", "stage"], undefined>;
21
129
  /** Defines the schema to validate the necessary parameters for the IMS auth service. */
22
- declare const ImsAuthParamsSchema: valibot21.ObjectSchema<{
23
- readonly clientId: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
24
- readonly clientSecrets: valibot21.SchemaWithPipe<readonly [valibot21.ArraySchema<valibot21.StringSchema<undefined>, `Expected a string array value for the IMS auth parameter ${string}`>, valibot21.MinLengthAction<string[], number, `Expected at least ${number} items for the IMS auth parameter ${string}`>]>;
25
- readonly technicalAccountId: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
26
- readonly technicalAccountEmail: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<"Expected a string value for the IMS auth parameter technicalAccountEmail">, valibot21.EmailAction<string, "Expected a valid email format for technicalAccountEmail">]>;
27
- readonly imsOrgId: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
28
- readonly environment: valibot21.SchemaWithPipe<readonly [valibot21.OptionalSchema<valibot21.PicklistSchema<["prod", "stage"], undefined>, undefined>]>;
29
- readonly context: valibot21.SchemaWithPipe<readonly [valibot21.OptionalSchema<valibot21.StringSchema<undefined>, undefined>]>;
30
- readonly scopes: valibot21.SchemaWithPipe<readonly [valibot21.ArraySchema<valibot21.StringSchema<undefined>, `Expected a string array value for the IMS auth parameter ${string}`>, valibot21.MinLengthAction<string[], number, `Expected at least ${number} items for the IMS auth parameter ${string}`>]>;
130
+ declare const ImsAuthParamsSchema: v.ObjectSchema<{
131
+ readonly clientId: v.SchemaWithPipe<readonly [v.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, v.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
132
+ readonly clientSecrets: v.SchemaWithPipe<readonly [v.ArraySchema<v.StringSchema<undefined>, `Expected a string array value for the IMS auth parameter ${string}`>, v.MinLengthAction<string[], number, `Expected at least ${number} items for the IMS auth parameter ${string}`>]>;
133
+ readonly technicalAccountId: v.SchemaWithPipe<readonly [v.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, v.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
134
+ readonly technicalAccountEmail: v.SchemaWithPipe<readonly [v.StringSchema<"Expected a string value for the IMS auth parameter technicalAccountEmail">, v.EmailAction<string, "Expected a valid email format for technicalAccountEmail">]>;
135
+ readonly imsOrgId: v.SchemaWithPipe<readonly [v.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, v.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
136
+ readonly environment: v.SchemaWithPipe<readonly [v.OptionalSchema<v.PicklistSchema<["prod", "stage"], undefined>, undefined>]>;
137
+ readonly context: v.SchemaWithPipe<readonly [v.OptionalSchema<v.StringSchema<undefined>, undefined>]>;
138
+ readonly scopes: v.SchemaWithPipe<readonly [v.ArraySchema<v.StringSchema<undefined>, `Expected a string array value for the IMS auth parameter ${string}`>, v.MinLengthAction<string[], number, `Expected at least ${number} items for the IMS auth parameter ${string}`>]>;
31
139
  }, undefined>;
32
140
  /** Defines the parameters for the IMS auth service. */
33
141
  type ImsAuthParams = InferOutput<typeof ImsAuthParamsSchema>;
@@ -35,15 +143,6 @@ type ImsAuthParams = InferOutput<typeof ImsAuthParamsSchema>;
35
143
  type ImsAuthEnv = InferOutput<typeof ImsAuthEnvSchema>;
36
144
  //#endregion
37
145
  //#region source/lib/ims-auth/provider.d.ts
38
- /** Defines the header keys used for IMS authentication. */
39
- type ImsAuthHeader = "Authorization" | "x-api-key";
40
- /** Defines the headers required for IMS authentication. */
41
- type ImsAuthHeaders = Record<ImsAuthHeader, string>;
42
- /** Defines an authentication provider for Adobe IMS. */
43
- type ImsAuthProvider = {
44
- getAccessToken: () => Promise<string>;
45
- getHeaders: () => Promise<ImsAuthHeaders>;
46
- };
47
146
  /**
48
147
  * Type guard to check if a value is an ImsAuthProvider instance.
49
148
  *
@@ -103,10 +202,7 @@ declare function isImsAuthProvider(provider: unknown): provider is ImsAuthProvid
103
202
  */
104
203
  declare function getImsAuthProvider(authParams: ImsAuthParams): {
105
204
  getAccessToken: () => Promise<string>;
106
- getHeaders: () => Promise<{
107
- Authorization: string;
108
- "x-api-key": string;
109
- }>;
205
+ getHeaders: () => Promise<ImsAuthHeaders>;
110
206
  };
111
207
  //#endregion
112
208
  //#region source/lib/ims-auth/utils.d.ts
@@ -156,11 +252,11 @@ type HttpMethodInput = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
156
252
  * The schema for the Commerce Integration parameters.
157
253
  * This is used to validate the parameters passed to the Commerce Integration provider.
158
254
  */
159
- declare const IntegrationAuthParamsSchema: valibot21.NonOptionalSchema<valibot21.ObjectSchema<{
160
- readonly consumerKey: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
161
- readonly consumerSecret: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
162
- readonly accessToken: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
163
- readonly accessTokenSecret: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
255
+ declare const IntegrationAuthParamsSchema: v.NonOptionalSchema<v.ObjectSchema<{
256
+ readonly consumerKey: v.SchemaWithPipe<readonly [v.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, v.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
257
+ readonly consumerSecret: v.SchemaWithPipe<readonly [v.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, v.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
258
+ readonly accessToken: v.SchemaWithPipe<readonly [v.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, v.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
259
+ readonly accessTokenSecret: v.SchemaWithPipe<readonly [v.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, v.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
164
260
  }, undefined>, undefined>;
165
261
  /** Defines the parameters required for Commerce Integration authentication. */
166
262
  type IntegrationAuthParams = InferOutput<typeof IntegrationAuthParamsSchema>;
@@ -272,7 +368,8 @@ declare function assertIntegrationAuthParams(config: Record<PropertyKey, unknown
272
368
  * }
273
369
  * ```
274
370
  */
275
- declare function resolveAuthParams(params: Record<string, unknown>): ({
371
+ declare function resolveAuthParams(params: Record<string, unknown>): {
372
+ strategy: "ims";
276
373
  clientId: string;
277
374
  clientSecrets: string[];
278
375
  technicalAccountId: string;
@@ -281,15 +378,12 @@ declare function resolveAuthParams(params: Record<string, unknown>): ({
281
378
  environment?: "prod" | "stage" | undefined;
282
379
  context?: string | undefined;
283
380
  scopes: string[];
284
- } & {
285
- readonly strategy: "ims";
286
- }) | ({
381
+ } | {
382
+ strategy: "integration";
287
383
  consumerKey: string;
288
384
  consumerSecret: string;
289
385
  accessToken: string;
290
386
  accessTokenSecret: string;
291
- } & {
292
- readonly strategy: "integration";
293
- });
387
+ };
294
388
  //#endregion
295
- export { type ImsAuthEnv, type ImsAuthParams, type ImsAuthProvider, type IntegrationAuthParams, type IntegrationAuthProvider, assertImsAuthParams, assertIntegrationAuthParams, getImsAuthProvider, getIntegrationAuthProvider, isImsAuthProvider, isIntegrationAuthProvider, resolveAuthParams };
389
+ export { type ForwardedImsAuthSource, type ImsAuthEnv, type ImsAuthHeaders, type ImsAuthParams, type ImsAuthProvider, type IntegrationAuthParams, type IntegrationAuthProvider, assertImsAuthParams, assertIntegrationAuthParams, forwardImsAuthProvider, getForwardedImsAuthProvider, getImsAuthProvider, getIntegrationAuthProvider, isImsAuthProvider, isIntegrationAuthProvider, resolveAuthParams };
@@ -0,0 +1 @@
1
+ export { };