@adobe/aio-commerce-lib-auth 0.1.0 → 0.2.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.
- package/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +16 -0
- package/README.md +74 -13
- package/dist/cjs/index.cjs +179 -86
- package/dist/cjs/index.d.cts +79 -30
- package/dist/es/index.d.ts +79 -30
- package/dist/es/index.js +176 -86
- package/package.json +5 -2
- package/source/index.ts +23 -2
- package/source/lib/ims-auth/provider.ts +160 -0
- package/source/lib/ims-auth/schema.ts +87 -0
- package/source/lib/integration-auth/provider.ts +145 -0
- package/source/lib/integration-auth/schema.ts +87 -0
- package/test/ims-auth.test.ts +119 -41
- package/test/integration-auth.test.ts +97 -34
- package/source/lib/ims-auth.ts +0 -95
- package/source/lib/integration-auth.ts +0 -95
- package/source/lib/params.ts +0 -34
package/source/lib/ims-auth.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2025 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { context, getToken } from "@adobe/aio-lib-ims";
|
|
14
|
-
import { allNonEmpty } from "./params";
|
|
15
|
-
|
|
16
|
-
const IMS_AUTH_HEADERS = ["Authorization", "x-api-key"] as const;
|
|
17
|
-
const IMS_AUTH_PARAMS = [
|
|
18
|
-
"AIO_COMMERCE_IMS_CLIENT_ID",
|
|
19
|
-
"AIO_COMMERCE_IMS_CLIENT_SECRETS",
|
|
20
|
-
"AIO_COMMERCE_IMS_TECHNICAL_ACCOUNT_ID",
|
|
21
|
-
"AIO_COMMERCE_IMS_TECHNICAL_ACCOUNT_EMAIL",
|
|
22
|
-
"AIO_COMMERCE_IMS_IMS_ORG_ID",
|
|
23
|
-
"AIO_COMMERCE_IMS_SCOPES",
|
|
24
|
-
"AIO_COMMERCE_IMS_ENV",
|
|
25
|
-
"AIO_COMMERCE_IMS_CTX",
|
|
26
|
-
] as const;
|
|
27
|
-
|
|
28
|
-
/** Defines a union of allowed IMS authentication parameters. */
|
|
29
|
-
export type ImsAuthParam = (typeof IMS_AUTH_PARAMS)[number];
|
|
30
|
-
|
|
31
|
-
/** Defines a key-value map of IMS authentication parameters. */
|
|
32
|
-
export type ImsAuthParams = Partial<Record<ImsAuthParam, string>>;
|
|
33
|
-
|
|
34
|
-
/** Defines a union of allowed IMS authentication headers. */
|
|
35
|
-
export type ImsAuthHeader = (typeof IMS_AUTH_HEADERS)[number];
|
|
36
|
-
|
|
37
|
-
/** Defines a key-value map of IMS authentication headers. */
|
|
38
|
-
export type ImsAuthHeaders = Record<ImsAuthHeader, string>;
|
|
39
|
-
|
|
40
|
-
type ImsAccessToken = string;
|
|
41
|
-
|
|
42
|
-
/** Defines an authentication provider for Adobe IMS. */
|
|
43
|
-
export interface ImsAuthProvider {
|
|
44
|
-
getHeaders: () => Promise<ImsAuthHeaders>;
|
|
45
|
-
getAccessToken: () => Promise<ImsAccessToken>;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* If the required IMS parameters are present, this function returns an {@link ImsAuthProvider}.
|
|
50
|
-
* @param params includes IMS parameters
|
|
51
|
-
*/
|
|
52
|
-
export async function getImsAuthProvider(params: ImsAuthParams) {
|
|
53
|
-
const config = resolveImsConfig(params);
|
|
54
|
-
|
|
55
|
-
if (config) {
|
|
56
|
-
const contextName = params.AIO_COMMERCE_IMS_CTX ?? "aio-commerce-sdk-creds";
|
|
57
|
-
await context.set(contextName, config);
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
getAccessToken: async () => getToken(contextName, {}),
|
|
61
|
-
getHeaders: async () => {
|
|
62
|
-
const accessToken = await getToken(contextName, {});
|
|
63
|
-
return {
|
|
64
|
-
Authorization: `Bearer ${accessToken}`,
|
|
65
|
-
"x-api-key": config.client_id,
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
} satisfies ImsAuthProvider;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function resolveImsConfig(params: ImsAuthParams) {
|
|
73
|
-
if (
|
|
74
|
-
allNonEmpty(params, [
|
|
75
|
-
"AIO_COMMERCE_IMS_CLIENT_ID",
|
|
76
|
-
"AIO_COMMERCE_IMS_CLIENT_SECRETS",
|
|
77
|
-
"AIO_COMMERCE_IMS_TECHNICAL_ACCOUNT_ID",
|
|
78
|
-
"AIO_COMMERCE_IMS_TECHNICAL_ACCOUNT_EMAIL",
|
|
79
|
-
"AIO_COMMERCE_IMS_IMS_ORG_ID",
|
|
80
|
-
"AIO_COMMERCE_IMS_SCOPES",
|
|
81
|
-
])
|
|
82
|
-
) {
|
|
83
|
-
return {
|
|
84
|
-
client_id: params.AIO_COMMERCE_IMS_CLIENT_ID,
|
|
85
|
-
client_secrets: JSON.parse(
|
|
86
|
-
params.AIO_COMMERCE_IMS_CLIENT_SECRETS ?? "[]",
|
|
87
|
-
) as string[],
|
|
88
|
-
technical_account_id: params.AIO_COMMERCE_IMS_TECHNICAL_ACCOUNT_ID,
|
|
89
|
-
technical_account_email: params.AIO_COMMERCE_IMS_TECHNICAL_ACCOUNT_EMAIL,
|
|
90
|
-
ims_org_id: params.AIO_COMMERCE_IMS_IMS_ORG_ID,
|
|
91
|
-
scopes: JSON.parse(params.AIO_COMMERCE_IMS_SCOPES ?? "[]") as string[],
|
|
92
|
-
environment: params.AIO_COMMERCE_IMS_ENV ?? "prod",
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
}
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2025 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import crypto from "node:crypto";
|
|
14
|
-
import OAuth1a from "oauth-1.0a";
|
|
15
|
-
|
|
16
|
-
import { allNonEmpty } from "./params";
|
|
17
|
-
|
|
18
|
-
const HTTP_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"] as const;
|
|
19
|
-
const INTEGRATION_AUTH_HEADERS = ["Authorization"] as const;
|
|
20
|
-
const INTEGRATION_AUTH_PARAMS = [
|
|
21
|
-
"AIO_COMMERCE_INTEGRATIONS_CONSUMER_KEY",
|
|
22
|
-
"AIO_COMMERCE_INTEGRATIONS_CONSUMER_SECRET",
|
|
23
|
-
"AIO_COMMERCE_INTEGRATIONS_ACCESS_TOKEN",
|
|
24
|
-
"AIO_COMMERCE_INTEGRATIONS_ACCESS_TOKEN_SECRET",
|
|
25
|
-
] as const;
|
|
26
|
-
|
|
27
|
-
/** Defines a union of allowed integration authentication parameters. */
|
|
28
|
-
export type IntegrationAuthParam = (typeof INTEGRATION_AUTH_PARAMS)[number];
|
|
29
|
-
|
|
30
|
-
/** Defines a key-value map of integration authentication parameters. */
|
|
31
|
-
export type IntegrationAuthParams = Partial<
|
|
32
|
-
Record<IntegrationAuthParam, string>
|
|
33
|
-
>;
|
|
34
|
-
|
|
35
|
-
/** Defines a union of allowed integration authentication headers. */
|
|
36
|
-
export type IntegrationAuthHeader = (typeof INTEGRATION_AUTH_HEADERS)[number];
|
|
37
|
-
|
|
38
|
-
/** Defines a key-value map of integration authentication headers. */
|
|
39
|
-
export type IntegrationAuthHeaders = Record<IntegrationAuthHeader, string>;
|
|
40
|
-
|
|
41
|
-
type HttpMethod = (typeof HTTP_METHODS)[number];
|
|
42
|
-
|
|
43
|
-
/** Defines an authentication provider for integration authentication. */
|
|
44
|
-
export interface IntegrationAuthProvider {
|
|
45
|
-
getHeaders: (method: HttpMethod, url: string) => IntegrationAuthHeaders;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* If the required integration parameters are present, this function returns an {@link IntegrationAuthProvider}.
|
|
50
|
-
* @param params includes integration parameters
|
|
51
|
-
*/
|
|
52
|
-
export function getIntegrationAuthProvider(params: IntegrationAuthParams) {
|
|
53
|
-
const config = resolveIntegrationConfig(params);
|
|
54
|
-
|
|
55
|
-
if (config) {
|
|
56
|
-
const oauth = new OAuth1a({
|
|
57
|
-
consumer: {
|
|
58
|
-
key: config.consumerKey,
|
|
59
|
-
secret: config.consumerSecret,
|
|
60
|
-
},
|
|
61
|
-
signature_method: "HMAC-SHA256",
|
|
62
|
-
hash_function: (baseString, key) =>
|
|
63
|
-
crypto.createHmac("sha256", key).update(baseString).digest("base64"),
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
const oauthToken = {
|
|
67
|
-
key: config.accessToken,
|
|
68
|
-
secret: config.accessTokenSecret,
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
getHeaders(method: HttpMethod, url: string) {
|
|
73
|
-
return oauth.toHeader(oauth.authorize({ url, method }, oauthToken));
|
|
74
|
-
},
|
|
75
|
-
} satisfies IntegrationAuthProvider;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function resolveIntegrationConfig(params: IntegrationAuthParams) {
|
|
80
|
-
if (
|
|
81
|
-
allNonEmpty(params, [
|
|
82
|
-
"AIO_COMMERCE_INTEGRATIONS_CONSUMER_KEY",
|
|
83
|
-
"AIO_COMMERCE_INTEGRATIONS_CONSUMER_SECRET",
|
|
84
|
-
"AIO_COMMERCE_INTEGRATIONS_ACCESS_TOKEN",
|
|
85
|
-
"AIO_COMMERCE_INTEGRATIONS_ACCESS_TOKEN_SECRET",
|
|
86
|
-
])
|
|
87
|
-
) {
|
|
88
|
-
return {
|
|
89
|
-
consumerKey: params.AIO_COMMERCE_INTEGRATIONS_CONSUMER_KEY,
|
|
90
|
-
consumerSecret: params.AIO_COMMERCE_INTEGRATIONS_CONSUMER_SECRET,
|
|
91
|
-
accessToken: params.AIO_COMMERCE_INTEGRATIONS_ACCESS_TOKEN,
|
|
92
|
-
accessTokenSecret: params.AIO_COMMERCE_INTEGRATIONS_ACCESS_TOKEN_SECRET,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
}
|
package/source/lib/params.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2025 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Checks if the given value is non-empty.
|
|
15
|
-
*
|
|
16
|
-
* @param name of the parameter. Required because of `aio app dev` compatibility: inputs mapped to undefined env vars come as $<input_name> in dev mode, but as '' in prod mode.
|
|
17
|
-
* @param value of the parameter.
|
|
18
|
-
*/
|
|
19
|
-
export function nonEmpty(name: string, value: string | undefined) {
|
|
20
|
-
const v = value?.trim();
|
|
21
|
-
return v !== undefined && v !== `$${name}`;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Checks if all required parameters are non-empty.
|
|
26
|
-
* @param params action input parameters.
|
|
27
|
-
* @param required list of required parameter names.
|
|
28
|
-
*/
|
|
29
|
-
export function allNonEmpty<const T extends string>(
|
|
30
|
-
params: Record<string, string | undefined>,
|
|
31
|
-
required: T[],
|
|
32
|
-
): params is Required<Record<T, string> & Record<string, string>> {
|
|
33
|
-
return required.every((name) => nonEmpty(name, params[name]));
|
|
34
|
-
}
|