@aws/amazon-location-utilities-auth-helper 1.0.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.
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.withAPIKey = void 0;
6
+ /**
7
+ * Creates an auth helper instance using APIKey. Its `getLocationClientConfig` function creates a signer to set the
8
+ * APIKey in all the commands of a client.
9
+ *
10
+ * @param apiKey APIKey
11
+ */
12
+ async function withAPIKey(apiKey) {
13
+ return {
14
+ getLocationClientConfig: () => ({
15
+ signer: {
16
+ sign: async (requestToSign) => {
17
+ var _a;
18
+ // APIKey in the command can override the APIKey set by auth helper.
19
+ requestToSign.query = Object.assign({ key: apiKey }, ((_a = requestToSign.query) !== null && _a !== void 0 ? _a : {}));
20
+ return requestToSign;
21
+ },
22
+ },
23
+ }),
24
+ };
25
+ }
26
+ exports.withAPIKey = withAPIKey;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.withIdentityPoolId = void 0;
6
+ const credential_providers_1 = require("@aws-sdk/credential-providers");
7
+ const core_1 = require("@aws-amplify/core");
8
+ /**
9
+ * Creates an auth helper instance using credentials from Cognito.
10
+ *
11
+ * @param identityPoolId Cognito Identity Pool Id
12
+ */
13
+ async function withIdentityPoolId(identityPoolId) {
14
+ const region = identityPoolId.split(":")[0];
15
+ const credentialsProvider = (0, credential_providers_1.fromCognitoIdentityPool)({
16
+ identityPoolId,
17
+ clientConfig: {
18
+ region,
19
+ },
20
+ });
21
+ let credentials;
22
+ async function refreshCredentials() {
23
+ credentials = await credentialsProvider();
24
+ let timeToRefresh = 3600000; // default to 1 hour if credentials does not have expiration field
25
+ if (credentials.expiration) {
26
+ timeToRefresh = credentials.expiration.getTime() - new Date().getTime();
27
+ }
28
+ // timeToRefresh minus 1 minute to give some time for the actual refresh to happen.
29
+ setTimeout(refreshCredentials, timeToRefresh - 60000);
30
+ }
31
+ await refreshCredentials();
32
+ return {
33
+ getMapAuthenticationOptions: () => ({
34
+ transformRequest: (url) => {
35
+ // Only sign aws URLs
36
+ if (url.includes("amazonaws.com")) {
37
+ return {
38
+ url: core_1.Signer.signUrl(url, {
39
+ access_key: credentials.accessKeyId,
40
+ secret_key: credentials.secretAccessKey,
41
+ session_token: credentials.sessionToken,
42
+ }),
43
+ };
44
+ }
45
+ return { url };
46
+ },
47
+ }),
48
+ getLocationClientConfig: () => ({
49
+ credentials: credentialsProvider,
50
+ }),
51
+ getCredentials: () => credentials,
52
+ };
53
+ }
54
+ exports.withIdentityPoolId = withIdentityPoolId;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
+ if (k2 === undefined) k2 = k;
6
+ var desc = Object.getOwnPropertyDescriptor(m, k);
7
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
+ desc = { enumerable: true, get: function() { return m[k]; } };
9
+ }
10
+ Object.defineProperty(o, k2, desc);
11
+ }) : (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ o[k2] = m[k];
14
+ }));
15
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ __exportStar(require("./common/types"), exports);
20
+ __exportStar(require("./cognito"), exports);
21
+ __exportStar(require("./apikey"), exports);
@@ -0,0 +1,22 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * Creates an auth helper instance using APIKey. Its `getLocationClientConfig` function creates a signer to set the
5
+ * APIKey in all the commands of a client.
6
+ *
7
+ * @param apiKey APIKey
8
+ */
9
+ export async function withAPIKey(apiKey) {
10
+ return {
11
+ getLocationClientConfig: () => ({
12
+ signer: {
13
+ sign: async (requestToSign) => {
14
+ var _a;
15
+ // APIKey in the command can override the APIKey set by auth helper.
16
+ requestToSign.query = Object.assign({ key: apiKey }, ((_a = requestToSign.query) !== null && _a !== void 0 ? _a : {}));
17
+ return requestToSign;
18
+ },
19
+ },
20
+ }),
21
+ };
22
+ }
@@ -0,0 +1,50 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers";
4
+ import { Signer } from "@aws-amplify/core";
5
+ /**
6
+ * Creates an auth helper instance using credentials from Cognito.
7
+ *
8
+ * @param identityPoolId Cognito Identity Pool Id
9
+ */
10
+ export async function withIdentityPoolId(identityPoolId) {
11
+ const region = identityPoolId.split(":")[0];
12
+ const credentialsProvider = fromCognitoIdentityPool({
13
+ identityPoolId,
14
+ clientConfig: {
15
+ region,
16
+ },
17
+ });
18
+ let credentials;
19
+ async function refreshCredentials() {
20
+ credentials = await credentialsProvider();
21
+ let timeToRefresh = 3600000; // default to 1 hour if credentials does not have expiration field
22
+ if (credentials.expiration) {
23
+ timeToRefresh = credentials.expiration.getTime() - new Date().getTime();
24
+ }
25
+ // timeToRefresh minus 1 minute to give some time for the actual refresh to happen.
26
+ setTimeout(refreshCredentials, timeToRefresh - 60000);
27
+ }
28
+ await refreshCredentials();
29
+ return {
30
+ getMapAuthenticationOptions: () => ({
31
+ transformRequest: (url) => {
32
+ // Only sign aws URLs
33
+ if (url.includes("amazonaws.com")) {
34
+ return {
35
+ url: Signer.signUrl(url, {
36
+ access_key: credentials.accessKeyId,
37
+ secret_key: credentials.secretAccessKey,
38
+ session_token: credentials.sessionToken,
39
+ }),
40
+ };
41
+ }
42
+ return { url };
43
+ },
44
+ }),
45
+ getLocationClientConfig: () => ({
46
+ credentials: credentialsProvider,
47
+ }),
48
+ getCredentials: () => credentials,
49
+ };
50
+ }
@@ -0,0 +1,3 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export {};
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export * from "./common/types";
4
+ export * from "./cognito";
5
+ export * from "./apikey";
@@ -0,0 +1,8 @@
1
+ import { SDKAuthHelper } from "../common/types";
2
+ /**
3
+ * Creates an auth helper instance using APIKey. Its `getLocationClientConfig` function creates a signer to set the
4
+ * APIKey in all the commands of a client.
5
+ *
6
+ * @param apiKey APIKey
7
+ */
8
+ export declare function withAPIKey(apiKey: string): Promise<SDKAuthHelper>;
@@ -0,0 +1,7 @@
1
+ import { MapAuthHelper, SDKAuthHelper } from "../common/types";
2
+ /**
3
+ * Creates an auth helper instance using credentials from Cognito.
4
+ *
5
+ * @param identityPoolId Cognito Identity Pool Id
6
+ */
7
+ export declare function withIdentityPoolId(identityPoolId: string): Promise<MapAuthHelper & SDKAuthHelper>;
@@ -0,0 +1,20 @@
1
+ import { AwsCredentialIdentity, Provider, RequestSigner } from "@aws-sdk/types";
2
+ export interface MapAuthenticationOptions {
3
+ transformRequest: (url: string, resourceType?: string) => {
4
+ url: string;
5
+ };
6
+ }
7
+ export interface LocationClientConfig {
8
+ signer?: RequestSigner;
9
+ credentials?: Provider<AwsCredentialIdentity>;
10
+ }
11
+ export type getMapAuthenticationOptionsFunc = () => MapAuthenticationOptions;
12
+ export type getLocationClientConfigFunc = () => LocationClientConfig;
13
+ export type getCredentialsFunc = () => AwsCredentialIdentity;
14
+ export interface MapAuthHelper {
15
+ getMapAuthenticationOptions: getMapAuthenticationOptionsFunc;
16
+ getCredentials: getCredentialsFunc;
17
+ }
18
+ export interface SDKAuthHelper {
19
+ getLocationClientConfig: getLocationClientConfigFunc;
20
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./common/types";
2
+ export * from "./cognito";
3
+ export * from "./apikey";
package/package.json ADDED
@@ -0,0 +1,94 @@
1
+ {
2
+ "name": "@aws/amazon-location-utilities-auth-helper",
3
+ "description": "Amazon Location Utilities - Authentication Helper for JavaScript",
4
+ "license": "Apache-2.0",
5
+ "version": "1.0.0",
6
+ "keywords": [],
7
+ "author": {
8
+ "name": "Amazon Web Services",
9
+ "email": "",
10
+ "url": "https://aws.amazon.com/"
11
+ },
12
+ "homepage": "https://github.com/aws-geospatial/amazon-location-utilities-auth-helper-js",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/aws-geospatial/amazon-location-utilities-auth-helper-js"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/aws-geospatial/amazon-location-utilities-auth-helper-js/issues",
19
+ "mail": ""
20
+ },
21
+ "contributors": [
22
+ "Eason Huang <yuxuanh@amazon.com>"
23
+ ],
24
+ "engines": {
25
+ "node": ">= 16.0.0"
26
+ },
27
+ "main": "./dist/cjs/index.js",
28
+ "module": "./dist/esm/index.js",
29
+ "types": "./dist/types/index.d.ts",
30
+ "files": [
31
+ "./LICENSE.txt",
32
+ "./LICENSE-THIRD-PARTY.txt",
33
+ "./CODE_OF_CONDUCT.md",
34
+ "./CONTRIBUTING.md",
35
+ "./Notice.txt",
36
+ "./README.md",
37
+ "./package.json",
38
+ "./dist"
39
+ ],
40
+ "scripts": {
41
+ "clean": "rm -r dist",
42
+ "prettier": "prettier -w .",
43
+ "prettier:check": "prettier -c .",
44
+ "lint": "eslint .",
45
+ "lint:fix": "eslint --fix .",
46
+ "typedoc": "typedoc",
47
+ "test": "jest --coverage",
48
+ "build": "npm-run-all build:*",
49
+ "build:ts": "npm-run-all build-ts:*",
50
+ "build:bundle": "rollup -c",
51
+ "build-ts:types": "tsc --declaration --emitDeclarationOnly --outDir dist/types",
52
+ "build-ts:cjs": "tsc --module commonjs --outDir dist/cjs",
53
+ "build-ts:esm": "tsc --esModuleInterop --module esnext --outDir dist/esm",
54
+ "prepare": "husky install",
55
+ "prepublishOnly": "npm-run-all clean lint prettier:check test build"
56
+ },
57
+ "lint-staged": {
58
+ "*.{js,ts}": [
59
+ "eslint --fix",
60
+ "prettier --write"
61
+ ],
62
+ "!(*.{js,ts})": "prettier --write --ignore-unknown"
63
+ },
64
+ "dependencies": {
65
+ "@aws-amplify/core": "^5.5.0",
66
+ "@aws-sdk/client-location": "^3.359.0",
67
+ "@aws-sdk/credential-providers": "^3.359.0",
68
+ "@aws-sdk/types": "^3.347.0"
69
+ },
70
+ "devDependencies": {
71
+ "@babel/core": "^7.21.8",
72
+ "@babel/preset-env": "^7.21.5",
73
+ "@babel/preset-typescript": "^7.21.5",
74
+ "@rollup/plugin-babel": "^6.0.3",
75
+ "@rollup/plugin-commonjs": "^25.0.2",
76
+ "@rollup/plugin-json": "^6.0.0",
77
+ "@rollup/plugin-node-resolve": "^15.1.0",
78
+ "@types/jest": "^29.5.0",
79
+ "@typescript-eslint/eslint-plugin": "^5.57.0",
80
+ "@typescript-eslint/parser": "^5.57.0",
81
+ "eslint": "^8.37.0",
82
+ "husky": "^8.0.3",
83
+ "jest": "^29.5.0",
84
+ "lint-staged": "^13.2.2",
85
+ "npm-run-all": "^4.1.5",
86
+ "prettier": "^2.8.7",
87
+ "prettier-plugin-jsdoc": "^0.4.2",
88
+ "rollup": "^3.22.0",
89
+ "ts-jest": "^29.1.0",
90
+ "ts-node": "^10.9.1",
91
+ "typedoc": "^0.24.1",
92
+ "typescript": "^5.0.2"
93
+ }
94
+ }