@esri/arcgis-rest-developer-credentials 1.1.0 → 2.1.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.
Files changed (65) hide show
  1. package/dist/bundled/developer-credentials.esm.js +254 -182
  2. package/dist/bundled/developer-credentials.esm.js.map +1 -1
  3. package/dist/bundled/developer-credentials.esm.min.js +3 -3
  4. package/dist/bundled/developer-credentials.esm.min.js.map +1 -1
  5. package/dist/bundled/developer-credentials.umd.js +257 -182
  6. package/dist/bundled/developer-credentials.umd.js.map +1 -1
  7. package/dist/bundled/developer-credentials.umd.min.js +3 -3
  8. package/dist/bundled/developer-credentials.umd.min.js.map +1 -1
  9. package/dist/cjs/createApiKey.js +37 -12
  10. package/dist/cjs/createApiKey.js.map +1 -1
  11. package/dist/cjs/index.js +1 -2
  12. package/dist/cjs/index.js.map +1 -1
  13. package/dist/cjs/invalidateApiKey.js +46 -0
  14. package/dist/cjs/invalidateApiKey.js.map +1 -0
  15. package/dist/cjs/shared/enum/privileges.js +0 -27
  16. package/dist/cjs/shared/enum/privileges.js.map +1 -1
  17. package/dist/cjs/shared/generateApiKeyToken.js +27 -0
  18. package/dist/cjs/shared/generateApiKeyToken.js.map +1 -0
  19. package/dist/cjs/shared/helpers.js +94 -16
  20. package/dist/cjs/shared/helpers.js.map +1 -1
  21. package/dist/cjs/shared/registerApp.js +1 -5
  22. package/dist/cjs/shared/registerApp.js.map +1 -1
  23. package/dist/cjs/shared/types/apiKeyType.js.map +1 -1
  24. package/dist/cjs/shared/types/appType.js.map +1 -1
  25. package/dist/cjs/updateApiKey.js +45 -26
  26. package/dist/cjs/updateApiKey.js.map +1 -1
  27. package/dist/esm/createApiKey.d.ts +9 -3
  28. package/dist/esm/createApiKey.js +39 -14
  29. package/dist/esm/createApiKey.js.map +1 -1
  30. package/dist/esm/index.d.ts +1 -2
  31. package/dist/esm/index.js +1 -2
  32. package/dist/esm/index.js.map +1 -1
  33. package/dist/esm/invalidateApiKey.d.ts +18 -0
  34. package/dist/esm/invalidateApiKey.js +42 -0
  35. package/dist/esm/invalidateApiKey.js.map +1 -0
  36. package/dist/esm/shared/enum/privileges.d.ts +2 -23
  37. package/dist/esm/shared/enum/privileges.js +1 -26
  38. package/dist/esm/shared/enum/privileges.js.map +1 -1
  39. package/dist/esm/shared/generateApiKeyToken.d.ts +11 -0
  40. package/dist/esm/shared/generateApiKeyToken.js +23 -0
  41. package/dist/esm/shared/generateApiKeyToken.js.map +1 -0
  42. package/dist/esm/shared/helpers.d.ts +42 -7
  43. package/dist/esm/shared/helpers.js +88 -14
  44. package/dist/esm/shared/helpers.js.map +1 -1
  45. package/dist/esm/shared/registerApp.d.ts +1 -1
  46. package/dist/esm/shared/registerApp.js +2 -6
  47. package/dist/esm/shared/registerApp.js.map +1 -1
  48. package/dist/esm/shared/types/apiKeyType.d.ts +54 -5
  49. package/dist/esm/shared/types/apiKeyType.js.map +1 -1
  50. package/dist/esm/shared/types/appType.d.ts +7 -3
  51. package/dist/esm/shared/types/appType.js.map +1 -1
  52. package/dist/esm/updateApiKey.d.ts +8 -2
  53. package/dist/esm/updateApiKey.js +47 -28
  54. package/dist/esm/updateApiKey.js.map +1 -1
  55. package/package.json +1 -1
  56. package/dist/cjs/deleteApiKey.js +0 -43
  57. package/dist/cjs/deleteApiKey.js.map +0 -1
  58. package/dist/cjs/deleteOAuthApp.js +0 -43
  59. package/dist/cjs/deleteOAuthApp.js.map +0 -1
  60. package/dist/esm/deleteApiKey.d.ts +0 -27
  61. package/dist/esm/deleteApiKey.js +0 -39
  62. package/dist/esm/deleteApiKey.js.map +0 -1
  63. package/dist/esm/deleteOAuthApp.d.ts +0 -27
  64. package/dist/esm/deleteOAuthApp.js +0 -39
  65. package/dist/esm/deleteOAuthApp.js.map +0 -1
@@ -23,13 +23,19 @@ const arcgis_rest_request_1 = require("@esri/arcgis-rest-request");
23
23
  * password: "xyz_pw"
24
24
  * });
25
25
  *
26
+ * const threeDaysFromToday = new Date();
27
+ * threeDaysFromToday.setDate(threeDaysFromToday.getDate() + 3);
28
+ * threeDaysFromToday.setHours(23, 59, 59, 999);
29
+ *
26
30
  * updateApiKey({
27
31
  * itemId: "xyz_itemId",
28
- * privileges: [Privileges.Geocode],
32
+ * privileges: ["premium:user:geocode:temporary"],
29
33
  * httpReferrers: [], // httpReferrers will be set to be empty
30
34
  * authentication: authSession
35
+ * generateToken1: true, // optional,generate a new token
36
+ * apiToken1ExpirationDate: threeDaysFromToday // optional, update expiration date
31
37
  * }).then((updatedAPIKey: IApiKeyResponse) => {
32
- * // => {apiKey: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
38
+ * // => {accessToken1: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
33
39
  * }).catch(e => {
34
40
  * // => an exception object
35
41
  * });
@@ -39,33 +45,46 @@ const arcgis_rest_request_1 = require("@esri/arcgis-rest-request");
39
45
  * @returns A Promise that will resolve to an {@linkcode IApiKeyResponse} object representing updated API key.
40
46
  */
41
47
  async function updateApiKey(requestOptions) {
42
- // privileges validation
43
- if (requestOptions.privileges &&
44
- !(0, helpers_js_1.arePrivilegesValid)(requestOptions.privileges)) {
45
- throw new Error("The `privileges` option contains invalid privileges.");
46
- }
47
48
  requestOptions.httpMethod = "POST";
48
- // get app
49
49
  const baseRequestOptions = (0, helpers_js_1.extractBaseRequestOptions)(requestOptions); // get base requestOptions snapshot
50
- const getAppOption = Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication, itemId: requestOptions.itemId });
51
- const appResponse = await (0, getRegisteredAppInfo_js_1.getRegisteredAppInfo)(getAppOption);
52
- // appType must be APIKey to continue
53
- if (appResponse.appType !== "apikey" || !("apiKey" in appResponse)) {
54
- throw new Error("Item is not an API key.");
50
+ /**
51
+ * step 1: update expiration dates if provided. Build the object up to avoid overwriting any existing properties.
52
+ */
53
+ if (requestOptions.apiToken1ExpirationDate ||
54
+ requestOptions.apiToken2ExpirationDate) {
55
+ const updateParams = (0, helpers_js_1.buildExpirationDateParams)(requestOptions);
56
+ await (0, arcgis_rest_portal_1.updateItem)(Object.assign(Object.assign({}, baseRequestOptions), { item: Object.assign({ id: requestOptions.itemId }, updateParams), authentication: requestOptions.authentication }));
57
+ }
58
+ /**
59
+ * step 2: update privileges and httpReferrers if provided. Build the object up to avoid overwriting any existing properties.
60
+ */
61
+ if (requestOptions.privileges || requestOptions.httpReferrers) {
62
+ const getAppOption = Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication, itemId: requestOptions.itemId });
63
+ const appResponse = await (0, getRegisteredAppInfo_js_1.getRegisteredAppInfo)(getAppOption);
64
+ const clientId = appResponse.client_id;
65
+ const options = (0, arcgis_rest_request_1.appendCustomParams)(Object.assign(Object.assign({}, appResponse), requestOptions), // object with the custom params to look in
66
+ ["privileges", "httpReferrers"] // keys you want copied to the params object
67
+ );
68
+ options.params.f = "json";
69
+ // encode special params value (e.g. array type...) in advance in order to make encodeQueryString() works correctly
70
+ (0, helpers_js_1.stringifyArrays)(options);
71
+ const url = (0, arcgis_rest_portal_1.getPortalUrl)(options) + `/oauth2/apps/${clientId}/update`;
72
+ // Raw response from `/oauth2/apps/${clientId}/update`, apiKey not included because key is same.
73
+ const updateResponse = await (0, arcgis_rest_request_1.request)(url, Object.assign(Object.assign({}, options), { authentication: requestOptions.authentication }));
55
74
  }
56
- const clientId = appResponse.client_id;
57
- const options = (0, arcgis_rest_request_1.appendCustomParams)(Object.assign(Object.assign({}, appResponse), requestOptions), // object with the custom params to look in
58
- ["privileges", "httpReferrers"] // keys you want copied to the params object
59
- );
60
- options.params.f = "json";
61
- // encode special params value (e.g. array type...) in advance in order to make encodeQueryString() works correctly
62
- (0, helpers_js_1.stringifyArrays)(options);
63
- const url = (0, arcgis_rest_portal_1.getPortalUrl)(options) + `/oauth2/apps/${clientId}/update`;
64
- // Raw response from `/oauth2/apps/${clientId}/update`, apiKey not included because key is same.
65
- const updateResponse = await (0, arcgis_rest_request_1.request)(url, Object.assign(Object.assign({}, options), { authentication: requestOptions.authentication }));
66
- const app = (0, helpers_js_1.registeredAppResponseToApp)(Object.assign(Object.assign({}, updateResponse), { apiKey: appResponse.apiKey }));
67
- const itemInfo = await (0, arcgis_rest_portal_1.getItem)(requestOptions.itemId, Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication, params: { f: "json" } }));
68
- return Object.assign(Object.assign({}, (0, helpers_js_1.appToApiKeyProperties)(app)), { item: itemInfo });
75
+ /**
76
+ * step 3: get the updated item info to return to the user.
77
+ */
78
+ const updatedItemInfo = await (0, arcgis_rest_portal_1.getItem)(requestOptions.itemId, Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication, params: { f: "json" } }));
79
+ /**
80
+ * step 4: generate tokens if requested
81
+ */
82
+ const generatedTokens = await (0, helpers_js_1.generateApiKeyTokens)(requestOptions.itemId, (0, helpers_js_1.generateOptionsToSlots)(requestOptions.generateToken1, requestOptions.generateToken2), Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication }));
83
+ /**
84
+ * step 5: get updated registered app info
85
+ */
86
+ const updatedRegisteredAppResponse = await (0, getRegisteredAppInfo_js_1.getRegisteredAppInfo)(Object.assign(Object.assign({}, baseRequestOptions), { itemId: requestOptions.itemId, authentication: requestOptions.authentication }));
87
+ return Object.assign(Object.assign(Object.assign({}, generatedTokens), (0, helpers_js_1.appToApiKeyProperties)(updatedRegisteredAppResponse)), { item: updatedItemInfo });
69
88
  }
70
89
  exports.updateApiKey = updateApiKey;
71
90
  //# sourceMappingURL=updateApiKey.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"updateApiKey.js","sourceRoot":"","sources":["../../src/updateApiKey.ts"],"names":[],"mappings":";AAAA;gBACgB;;;AAMhB,8EAAwE;AACxE,oDAM6B;AAC7B,iEAAiE;AACjE,mEAAwE;AAOxE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACI,KAAK,UAAU,YAAY,CAChC,cAAoC;IAEpC,wBAAwB;IACxB,IACE,cAAc,CAAC,UAAU;QACzB,CAAC,IAAA,+BAAkB,EAAC,cAAc,CAAC,UAAU,CAAC,EAC9C;QACA,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;KACzE;IAED,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC;IAEnC,UAAU;IACV,MAAM,kBAAkB,GAAG,IAAA,sCAAyB,EAAC,cAAc,CAAC,CAAC,CAAC,mCAAmC;IACzG,MAAM,YAAY,mCACb,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,EAC7C,MAAM,EAAE,cAAc,CAAC,MAAM,GAC9B,CAAC;IAEF,MAAM,WAAW,GAAG,MAAM,IAAA,8CAAoB,EAAC,YAAY,CAAC,CAAC;IAE7D,qCAAqC;IACrC,IAAI,WAAW,CAAC,OAAO,KAAK,QAAQ,IAAI,CAAC,CAAC,QAAQ,IAAI,WAAW,CAAC,EAAE;QAClE,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;KAC5C;IAED,MAAM,QAAQ,GAAG,WAAW,CAAC,SAAS,CAAC;IACvC,MAAM,OAAO,GAAG,IAAA,wCAAkB,kCAC3B,WAAW,GAAK,cAAc,GAAI,2CAA2C;IAClF,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC,4CAA4C;KAC7E,CAAC;IACF,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC;IAE1B,mHAAmH;IACnH,IAAA,4BAAe,EAAC,OAAO,CAAC,CAAC;IAEzB,MAAM,GAAG,GAAG,IAAA,iCAAY,EAAC,OAAO,CAAC,GAAG,gBAAgB,QAAQ,SAAS,CAAC;IAEtE,gGAAgG;IAChG,MAAM,cAAc,GAA2B,MAAM,IAAA,6BAAO,EAAC,GAAG,kCAC3D,OAAO,KACV,cAAc,EAAE,cAAc,CAAC,cAAc,IAC7C,CAAC;IAEH,MAAM,GAAG,GAAS,IAAA,uCAA0B,kCACvC,cAAc,KACjB,MAAM,EAAE,WAAW,CAAC,MAAM,IAC1B,CAAC;IAEH,MAAM,QAAQ,GAAG,MAAM,IAAA,4BAAO,EAAC,cAAc,CAAC,MAAM,kCAC/C,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,EAC7C,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,IACrB,CAAC;IAEH,uCACK,IAAA,kCAAqB,EAAC,GAAG,CAAC,KAC7B,IAAI,EAAE,QAAQ,IACd;AACJ,CAAC;AA7DD,oCA6DC","sourcesContent":["/* Copyright (c) 2023 Environmental Systems Research Institute, Inc.\n * Apache-2.0 */\n\nimport {\n IApiKeyResponse,\n IUpdateApiKeyOptions\n} from \"./shared/types/apiKeyType.js\";\nimport { getRegisteredAppInfo } from \"./shared/getRegisteredAppInfo.js\";\nimport {\n appToApiKeyProperties,\n extractBaseRequestOptions,\n arePrivilegesValid,\n stringifyArrays,\n registeredAppResponseToApp\n} from \"./shared/helpers.js\";\nimport { getItem, getPortalUrl } from \"@esri/arcgis-rest-portal\";\nimport { appendCustomParams, request } from \"@esri/arcgis-rest-request\";\nimport {\n IApp,\n IGetAppInfoOptions,\n IRegisteredAppResponse\n} from \"./shared/types/appType.js\";\n\n/**\n * Used to update an API key.\n *\n * Notes about `privileges` and `httpReferrers` options:\n * 1. Provided option will override corresponding old option.\n * 2. Unprovided option will not trigger corresponding option updates.\n *\n * ```js\n * import { updateApiKey, IApiKeyResponse } from '@esri/arcgis-rest-developer-credentials';\n * import { ArcGISIdentityManager } from \"@esri/arcgis-rest-request\";\n *\n * const authSession: ArcGISIdentityManager = await ArcGISIdentityManager.signIn({\n * username: \"xyz_usrName\",\n * password: \"xyz_pw\"\n * });\n *\n * updateApiKey({\n * itemId: \"xyz_itemId\",\n * privileges: [Privileges.Geocode],\n * httpReferrers: [], // httpReferrers will be set to be empty\n * authentication: authSession\n * }).then((updatedAPIKey: IApiKeyResponse) => {\n * // => {apiKey: \"xyz_key\", item: {tags: [\"xyz_tag1\", \"xyz_tag2\"], ...}, ...}\n * }).catch(e => {\n * // => an exception object\n * });\n * ```\n *\n * @param requestOptions - Options for {@linkcode updateApiKey | updateApiKey()}, including `itemId` of which API key to be operated on, optional new `privileges`, optional new `httpReferrers` and an {@linkcode ArcGISIdentityManager} authentication session.\n * @returns A Promise that will resolve to an {@linkcode IApiKeyResponse} object representing updated API key.\n */\nexport async function updateApiKey(\n requestOptions: IUpdateApiKeyOptions\n): Promise<IApiKeyResponse> {\n // privileges validation\n if (\n requestOptions.privileges &&\n !arePrivilegesValid(requestOptions.privileges)\n ) {\n throw new Error(\"The `privileges` option contains invalid privileges.\");\n }\n\n requestOptions.httpMethod = \"POST\";\n\n // get app\n const baseRequestOptions = extractBaseRequestOptions(requestOptions); // get base requestOptions snapshot\n const getAppOption: IGetAppInfoOptions = {\n ...baseRequestOptions,\n authentication: requestOptions.authentication,\n itemId: requestOptions.itemId\n };\n\n const appResponse = await getRegisteredAppInfo(getAppOption);\n\n // appType must be APIKey to continue\n if (appResponse.appType !== \"apikey\" || !(\"apiKey\" in appResponse)) {\n throw new Error(\"Item is not an API key.\");\n }\n\n const clientId = appResponse.client_id;\n const options = appendCustomParams(\n { ...appResponse, ...requestOptions }, // object with the custom params to look in\n [\"privileges\", \"httpReferrers\"] // keys you want copied to the params object\n );\n options.params.f = \"json\";\n\n // encode special params value (e.g. array type...) in advance in order to make encodeQueryString() works correctly\n stringifyArrays(options);\n\n const url = getPortalUrl(options) + `/oauth2/apps/${clientId}/update`;\n\n // Raw response from `/oauth2/apps/${clientId}/update`, apiKey not included because key is same.\n const updateResponse: IRegisteredAppResponse = await request(url, {\n ...options,\n authentication: requestOptions.authentication\n });\n\n const app: IApp = registeredAppResponseToApp({\n ...updateResponse,\n apiKey: appResponse.apiKey\n });\n\n const itemInfo = await getItem(requestOptions.itemId, {\n ...baseRequestOptions,\n authentication: requestOptions.authentication,\n params: { f: \"json\" }\n });\n\n return {\n ...appToApiKeyProperties(app),\n item: itemInfo\n };\n}\n"]}
1
+ {"version":3,"file":"updateApiKey.js","sourceRoot":"","sources":["../../src/updateApiKey.ts"],"names":[],"mappings":";AAAA;gBACgB;;;AAMhB,8EAAwE;AACxE,oDAQ6B;AAC7B,iEAA6E;AAC7E,mEAAwE;AAOxE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACI,KAAK,UAAU,YAAY,CAChC,cAAoC;IAEpC,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC;IACnC,MAAM,kBAAkB,GAAG,IAAA,sCAAyB,EAAC,cAAc,CAAC,CAAC,CAAC,mCAAmC;IAEzG;;OAEG;IACH,IACE,cAAc,CAAC,uBAAuB;QACtC,cAAc,CAAC,uBAAuB,EACtC;QACA,MAAM,YAAY,GAAG,IAAA,sCAAyB,EAAC,cAAc,CAAC,CAAC;QAC/D,MAAM,IAAA,+BAAU,kCACX,kBAAkB,KACrB,IAAI,kBACF,EAAE,EAAE,cAAc,CAAC,MAAM,IACtB,YAAY,GAEjB,cAAc,EAAE,cAAc,CAAC,cAAc,IAC7C,CAAC;KACJ;IAED;;OAEG;IACH,IAAI,cAAc,CAAC,UAAU,IAAI,cAAc,CAAC,aAAa,EAAE;QAC7D,MAAM,YAAY,mCACb,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,EAC7C,MAAM,EAAE,cAAc,CAAC,MAAM,GAC9B,CAAC;QACF,MAAM,WAAW,GAAG,MAAM,IAAA,8CAAoB,EAAC,YAAY,CAAC,CAAC;QAC7D,MAAM,QAAQ,GAAG,WAAW,CAAC,SAAS,CAAC;QACvC,MAAM,OAAO,GAAG,IAAA,wCAAkB,kCAC3B,WAAW,GAAK,cAAc,GAAI,2CAA2C;QAClF,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC,4CAA4C;SAC7E,CAAC;QACF,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC;QAE1B,mHAAmH;QACnH,IAAA,4BAAe,EAAC,OAAO,CAAC,CAAC;QAEzB,MAAM,GAAG,GAAG,IAAA,iCAAY,EAAC,OAAO,CAAC,GAAG,gBAAgB,QAAQ,SAAS,CAAC;QAEtE,gGAAgG;QAChG,MAAM,cAAc,GAA2B,MAAM,IAAA,6BAAO,EAAC,GAAG,kCAC3D,OAAO,KACV,cAAc,EAAE,cAAc,CAAC,cAAc,IAC7C,CAAC;KACJ;IAED;;OAEG;IACH,MAAM,eAAe,GAAG,MAAM,IAAA,4BAAO,EAAC,cAAc,CAAC,MAAM,kCACtD,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,EAC7C,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,IACrB,CAAC;IAEH;;OAEG;IACH,MAAM,eAAe,GAAG,MAAM,IAAA,iCAAoB,EAChD,cAAc,CAAC,MAAM,EACrB,IAAA,mCAAsB,EACpB,cAAc,CAAC,cAAc,EAC7B,cAAc,CAAC,cAAc,CAC9B,kCAEI,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,IAEhD,CAAC;IAEF;;OAEG;IACH,MAAM,4BAA4B,GAAG,MAAM,IAAA,8CAAoB,kCAC1D,kBAAkB,KACrB,MAAM,EAAE,cAAc,CAAC,MAAM,EAC7B,cAAc,EAAE,cAAc,CAAC,cAAc,IAC7C,CAAC;IAEH,qDACK,eAAe,GACf,IAAA,kCAAqB,EAAC,4BAA4B,CAAC,KACtD,IAAI,EAAE,eAAe,IACrB;AACJ,CAAC;AA3FD,oCA2FC","sourcesContent":["/* Copyright (c) 2023 Environmental Systems Research Institute, Inc.\n * Apache-2.0 */\n\nimport {\n IApiKeyResponse,\n IUpdateApiKeyOptions\n} from \"./shared/types/apiKeyType.js\";\nimport { getRegisteredAppInfo } from \"./shared/getRegisteredAppInfo.js\";\nimport {\n appToApiKeyProperties,\n extractBaseRequestOptions,\n stringifyArrays,\n registeredAppResponseToApp,\n generateApiKeyTokens,\n generateOptionsToSlots,\n buildExpirationDateParams\n} from \"./shared/helpers.js\";\nimport { getItem, getPortalUrl, updateItem } from \"@esri/arcgis-rest-portal\";\nimport { appendCustomParams, request } from \"@esri/arcgis-rest-request\";\nimport {\n IApp,\n IGetAppInfoOptions,\n IRegisteredAppResponse\n} from \"./shared/types/appType.js\";\n\n/**\n * Used to update an API key.\n *\n * Notes about `privileges` and `httpReferrers` options:\n * 1. Provided option will override corresponding old option.\n * 2. Unprovided option will not trigger corresponding option updates.\n *\n * ```js\n * import { updateApiKey, IApiKeyResponse } from '@esri/arcgis-rest-developer-credentials';\n * import { ArcGISIdentityManager } from \"@esri/arcgis-rest-request\";\n *\n * const authSession: ArcGISIdentityManager = await ArcGISIdentityManager.signIn({\n * username: \"xyz_usrName\",\n * password: \"xyz_pw\"\n * });\n *\n * const threeDaysFromToday = new Date();\n * threeDaysFromToday.setDate(threeDaysFromToday.getDate() + 3);\n * threeDaysFromToday.setHours(23, 59, 59, 999);\n *\n * updateApiKey({\n * itemId: \"xyz_itemId\",\n * privileges: [\"premium:user:geocode:temporary\"],\n * httpReferrers: [], // httpReferrers will be set to be empty\n * authentication: authSession\n * generateToken1: true, // optional,generate a new token\n * apiToken1ExpirationDate: threeDaysFromToday // optional, update expiration date\n * }).then((updatedAPIKey: IApiKeyResponse) => {\n * // => {accessToken1: \"xyz_key\", item: {tags: [\"xyz_tag1\", \"xyz_tag2\"], ...}, ...}\n * }).catch(e => {\n * // => an exception object\n * });\n * ```\n *\n * @param requestOptions - Options for {@linkcode updateApiKey | updateApiKey()}, including `itemId` of which API key to be operated on, optional new `privileges`, optional new `httpReferrers` and an {@linkcode ArcGISIdentityManager} authentication session.\n * @returns A Promise that will resolve to an {@linkcode IApiKeyResponse} object representing updated API key.\n */\nexport async function updateApiKey(\n requestOptions: IUpdateApiKeyOptions\n): Promise<IApiKeyResponse> {\n requestOptions.httpMethod = \"POST\";\n const baseRequestOptions = extractBaseRequestOptions(requestOptions); // get base requestOptions snapshot\n\n /**\n * step 1: update expiration dates if provided. Build the object up to avoid overwriting any existing properties.\n */\n if (\n requestOptions.apiToken1ExpirationDate ||\n requestOptions.apiToken2ExpirationDate\n ) {\n const updateParams = buildExpirationDateParams(requestOptions);\n await updateItem({\n ...baseRequestOptions,\n item: {\n id: requestOptions.itemId,\n ...updateParams\n },\n authentication: requestOptions.authentication\n });\n }\n\n /**\n * step 2: update privileges and httpReferrers if provided. Build the object up to avoid overwriting any existing properties.\n */\n if (requestOptions.privileges || requestOptions.httpReferrers) {\n const getAppOption: IGetAppInfoOptions = {\n ...baseRequestOptions,\n authentication: requestOptions.authentication,\n itemId: requestOptions.itemId\n };\n const appResponse = await getRegisteredAppInfo(getAppOption);\n const clientId = appResponse.client_id;\n const options = appendCustomParams(\n { ...appResponse, ...requestOptions }, // object with the custom params to look in\n [\"privileges\", \"httpReferrers\"] // keys you want copied to the params object\n );\n options.params.f = \"json\";\n\n // encode special params value (e.g. array type...) in advance in order to make encodeQueryString() works correctly\n stringifyArrays(options);\n\n const url = getPortalUrl(options) + `/oauth2/apps/${clientId}/update`;\n\n // Raw response from `/oauth2/apps/${clientId}/update`, apiKey not included because key is same.\n const updateResponse: IRegisteredAppResponse = await request(url, {\n ...options,\n authentication: requestOptions.authentication\n });\n }\n\n /**\n * step 3: get the updated item info to return to the user.\n */\n const updatedItemInfo = await getItem(requestOptions.itemId, {\n ...baseRequestOptions,\n authentication: requestOptions.authentication,\n params: { f: \"json\" }\n });\n\n /**\n * step 4: generate tokens if requested\n */\n const generatedTokens = await generateApiKeyTokens(\n requestOptions.itemId,\n generateOptionsToSlots(\n requestOptions.generateToken1,\n requestOptions.generateToken2\n ),\n {\n ...baseRequestOptions,\n authentication: requestOptions.authentication\n }\n );\n\n /**\n * step 5: get updated registered app info\n */\n const updatedRegisteredAppResponse = await getRegisteredAppInfo({\n ...baseRequestOptions,\n itemId: requestOptions.itemId,\n authentication: requestOptions.authentication\n });\n\n return {\n ...generatedTokens,\n ...appToApiKeyProperties(updatedRegisteredAppResponse),\n item: updatedItemInfo\n };\n}\n"]}
@@ -11,14 +11,20 @@ import { IApiKeyResponse, ICreateApiKeyOptions } from "./shared/types/apiKeyType
11
11
  * password: "xyz_pw"
12
12
  * });
13
13
  *
14
+ * const threeDaysFromToday = new Date();
15
+ * threeDaysFromToday.setDate(threeDaysFromToday.getDate() + 3);
16
+ * threeDaysFromToday.setHours(23, 59, 59, 999);
17
+ *
14
18
  * createApiKey({
15
19
  * title: "xyz_title",
16
20
  * description: "xyz_desc",
17
21
  * tags: ["xyz_tag1", "xyz_tag2"],
18
- * privileges: [Privileges.Geocode, Privileges.FeatureReport],
19
- * authentication: authSession
22
+ * privileges: ["premium:user:networkanalysis:routing"],
23
+ * authentication: authSession,
24
+ * generateToken1: true, // optional,generate a new token
25
+ * apiToken1ExpirationDate: threeDaysFromToday // optional, update expiration date
20
26
  * }).then((registeredAPIKey: IApiKeyResponse) => {
21
- * // => {apiKey: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
27
+ * // => {accessToken1: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
22
28
  * }).catch(e => {
23
29
  * // => an exception object
24
30
  * });
@@ -1,8 +1,9 @@
1
1
  /* Copyright (c) 2023 Environmental Systems Research Institute, Inc.
2
2
  * Apache-2.0 */
3
- import { createItem, getItem } from "@esri/arcgis-rest-portal";
3
+ import { createItem, getItem, updateItem } from "@esri/arcgis-rest-portal";
4
4
  import { registerApp } from "./shared/registerApp.js";
5
- import { appToApiKeyProperties, filterKeys, extractBaseRequestOptions, arePrivilegesValid } from "./shared/helpers.js";
5
+ import { appToApiKeyProperties, filterKeys, extractBaseRequestOptions, generateApiKeyTokens, generateOptionsToSlots, buildExpirationDateParams } from "./shared/helpers.js";
6
+ import { getRegisteredAppInfo } from "./shared/getRegisteredAppInfo.js";
6
7
  /**
7
8
  * Used to register an API key. See the [security and authentication](https://developers.arcgis.com/documentation/mapping-apis-and-services/security/api-keys/) for more information about API key.
8
9
  *
@@ -15,14 +16,20 @@ import { appToApiKeyProperties, filterKeys, extractBaseRequestOptions, arePrivil
15
16
  * password: "xyz_pw"
16
17
  * });
17
18
  *
19
+ * const threeDaysFromToday = new Date();
20
+ * threeDaysFromToday.setDate(threeDaysFromToday.getDate() + 3);
21
+ * threeDaysFromToday.setHours(23, 59, 59, 999);
22
+ *
18
23
  * createApiKey({
19
24
  * title: "xyz_title",
20
25
  * description: "xyz_desc",
21
26
  * tags: ["xyz_tag1", "xyz_tag2"],
22
- * privileges: [Privileges.Geocode, Privileges.FeatureReport],
23
- * authentication: authSession
27
+ * privileges: ["premium:user:networkanalysis:routing"],
28
+ * authentication: authSession,
29
+ * generateToken1: true, // optional,generate a new token
30
+ * apiToken1ExpirationDate: threeDaysFromToday // optional, update expiration date
24
31
  * }).then((registeredAPIKey: IApiKeyResponse) => {
25
- * // => {apiKey: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
32
+ * // => {accessToken1: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
26
33
  * }).catch(e => {
27
34
  * // => an exception object
28
35
  * });
@@ -32,9 +39,6 @@ import { appToApiKeyProperties, filterKeys, extractBaseRequestOptions, arePrivil
32
39
  * @returns A Promise that will resolve to an {@linkcode IApiKeyResponse} object representing the newly registered API key.
33
40
  */
34
41
  export async function createApiKey(requestOptions) {
35
- if (!arePrivilegesValid(requestOptions.privileges)) {
36
- throw new Error("The `privileges` option contains invalid privileges.");
37
- }
38
42
  requestOptions.httpMethod = "POST";
39
43
  // filter param buckets:
40
44
  const baseRequestOptions = extractBaseRequestOptions(requestOptions); // snapshot of basic IRequestOptions before customized params being built into it
@@ -54,15 +58,36 @@ export async function createApiKey(requestOptions) {
54
58
  "typeKeywords",
55
59
  "url"
56
60
  ];
57
- // step 1: add item
58
- const createItemOption = Object.assign(Object.assign({ item: Object.assign(Object.assign({}, filterKeys(requestOptions, itemAddProperties)), { type: "API Key" }) }, baseRequestOptions), { authentication: requestOptions.authentication, params: {
61
+ /**
62
+ * step 1: create item
63
+ */
64
+ const createItemOption = Object.assign(Object.assign({ item: Object.assign(Object.assign({}, filterKeys(requestOptions, itemAddProperties)), { type: "Application" }) }, baseRequestOptions), { authentication: requestOptions.authentication, params: {
59
65
  f: "json"
60
66
  } });
61
67
  const createItemResponse = await createItem(createItemOption);
62
- // step 2: register app
63
- const registerAppOption = Object.assign(Object.assign({ itemId: createItemResponse.id, appType: "apikey", redirect_uris: [], httpReferrers: requestOptions.httpReferrers || [], privileges: requestOptions.privileges }, baseRequestOptions), { authentication: requestOptions.authentication });
64
- const registeredAppResponse = await registerApp(registerAppOption);
68
+ /**
69
+ * getRegisteredAppInfoRoute
70
+ */
71
+ const registerAppOptions = Object.assign(Object.assign({ itemId: createItemResponse.id, appType: "multiple", redirect_uris: ["urn:ietf:wg:oauth:2.0:oob"], httpReferrers: requestOptions.httpReferrers || [], privileges: requestOptions.privileges }, baseRequestOptions), { authentication: requestOptions.authentication });
72
+ const registeredAppResponse = await registerApp(registerAppOptions);
73
+ /**
74
+ * step 3: update item with desired expiration dates
75
+ * you cannot set the expiration date propierties until you
76
+ * regiester the app so this has to be a seperate step
77
+ */
78
+ await updateItem(Object.assign(Object.assign({}, baseRequestOptions), { item: Object.assign({ id: createItemResponse.id }, buildExpirationDateParams(requestOptions, true)), authentication: requestOptions.authentication }));
79
+ /*
80
+ * step 4: get item info
81
+ */
65
82
  const itemInfo = await getItem(registeredAppResponse.itemId, Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication, params: { f: "json" } }));
66
- return Object.assign(Object.assign({}, appToApiKeyProperties(registeredAppResponse)), { item: itemInfo });
83
+ /**
84
+ * step 5: generate tokens if requested
85
+ */
86
+ const generatedTokens = await generateApiKeyTokens(itemInfo.id, generateOptionsToSlots(requestOptions.generateToken1, requestOptions.generateToken2), Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication }));
87
+ /**
88
+ * step 6: get registered app info to get updated active key status
89
+ */
90
+ const updatedRegisteredAppResponse = await getRegisteredAppInfo(Object.assign(Object.assign({}, baseRequestOptions), { itemId: itemInfo.id, authentication: requestOptions.authentication }));
91
+ return Object.assign(Object.assign(Object.assign({}, generatedTokens), appToApiKeyProperties(updatedRegisteredAppResponse)), { item: itemInfo });
67
92
  }
68
93
  //# sourceMappingURL=createApiKey.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"createApiKey.js","sourceRoot":"","sources":["../../src/createApiKey.ts"],"names":[],"mappings":"AAAA;gBACgB;AAEhB,OAAO,EAEL,UAAU,EACV,OAAO,EAER,MAAM,0BAA0B,CAAC;AAMlC,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,EACL,qBAAqB,EACrB,UAAU,EACV,yBAAyB,EACzB,kBAAkB,EACnB,MAAM,qBAAqB,CAAC;AAE7B;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,cAAoC;IAEpC,IAAI,CAAC,kBAAkB,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE;QAClD,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;KACzE;IAED,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC;IAEnC,wBAAwB;IAExB,MAAM,kBAAkB,GAAG,yBAAyB,CAAC,cAAc,CAAC,CAAC,CAAC,iFAAiF;IAEvJ,MAAM,iBAAiB,GAA0B;QAC/C,YAAY;QACZ,SAAS;QACT,aAAa;QACb,eAAe;QACf,QAAQ;QACR,OAAO;QACP,YAAY;QACZ,SAAS;QACT,kBAAkB;QAClB,MAAM;QACN,OAAO;QACP,MAAM;QACN,cAAc;QACd,KAAK;KACN,CAAC;IAEF,mBAAmB;IACnB,MAAM,gBAAgB,iCACpB,IAAI,kCACC,UAAU,CAAC,cAAqB,EAAE,iBAAiB,CAAC,KACvD,IAAI,EAAE,SAAS,OAEd,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,EAC7C,MAAM,EAAE;YACN,CAAC,EAAE,MAAM;SACV,GACF,CAAC;IAEF,MAAM,kBAAkB,GAAG,MAAM,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAE9D,uBAAuB;IACvB,MAAM,iBAAiB,iCACrB,MAAM,EAAE,kBAAkB,CAAC,EAAE,EAC7B,OAAO,EAAE,QAAQ,EACjB,aAAa,EAAE,EAAE,EACjB,aAAa,EAAE,cAAc,CAAC,aAAa,IAAI,EAAE,EACjD,UAAU,EAAE,cAAc,CAAC,UAAU,IAClC,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,GAC9C,CAAC;IAEF,MAAM,qBAAqB,GAAG,MAAM,WAAW,CAAC,iBAAiB,CAAC,CAAC;IACnE,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,qBAAqB,CAAC,MAAM,kCACtD,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,EAC7C,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,IACrB,CAAC;IAEH,uCACK,qBAAqB,CAAC,qBAAqB,CAAC,KAC/C,IAAI,EAAE,QAAQ,IACd;AACJ,CAAC","sourcesContent":["/* Copyright (c) 2023 Environmental Systems Research Institute, Inc.\n * Apache-2.0 */\n\nimport {\n ICreateItemOptions,\n createItem,\n getItem,\n IItemAdd\n} from \"@esri/arcgis-rest-portal\";\nimport {\n IApiKeyResponse,\n ICreateApiKeyOptions\n} from \"./shared/types/apiKeyType.js\";\n\nimport { registerApp } from \"./shared/registerApp.js\";\nimport { IRegisterAppOptions } from \"./shared/types/appType.js\";\nimport {\n appToApiKeyProperties,\n filterKeys,\n extractBaseRequestOptions,\n arePrivilegesValid\n} from \"./shared/helpers.js\";\n\n/**\n * Used to register an API key. See the [security and authentication](https://developers.arcgis.com/documentation/mapping-apis-and-services/security/api-keys/) for more information about API key.\n *\n * ```js\n * import { createApiKey, IApiKeyResponse } from '@esri/arcgis-rest-developer-credentials';\n * import { ArcGISIdentityManager } from \"@esri/arcgis-rest-request\";\n *\n * const authSession: ArcGISIdentityManager = await ArcGISIdentityManager.signIn({\n * username: \"xyz_usrName\",\n * password: \"xyz_pw\"\n * });\n *\n * createApiKey({\n * title: \"xyz_title\",\n * description: \"xyz_desc\",\n * tags: [\"xyz_tag1\", \"xyz_tag2\"],\n * privileges: [Privileges.Geocode, Privileges.FeatureReport],\n * authentication: authSession\n * }).then((registeredAPIKey: IApiKeyResponse) => {\n * // => {apiKey: \"xyz_key\", item: {tags: [\"xyz_tag1\", \"xyz_tag2\"], ...}, ...}\n * }).catch(e => {\n * // => an exception object\n * });\n * ```\n *\n * @param requestOptions - Options for {@linkcode createApiKey | createApiKey()}, including necessary params to register an API key and an {@linkcode ArcGISIdentityManager} authentication session.\n * @returns A Promise that will resolve to an {@linkcode IApiKeyResponse} object representing the newly registered API key.\n */\nexport async function createApiKey(\n requestOptions: ICreateApiKeyOptions\n): Promise<IApiKeyResponse> {\n if (!arePrivilegesValid(requestOptions.privileges)) {\n throw new Error(\"The `privileges` option contains invalid privileges.\");\n }\n\n requestOptions.httpMethod = \"POST\";\n\n // filter param buckets:\n\n const baseRequestOptions = extractBaseRequestOptions(requestOptions); // snapshot of basic IRequestOptions before customized params being built into it\n\n const itemAddProperties: Array<keyof IItemAdd> = [\n \"categories\",\n \"culture\",\n \"description\",\n \"documentation\",\n \"extent\",\n \"owner\",\n \"properties\",\n \"snippet\",\n \"spatialReference\",\n \"tags\",\n \"title\",\n \"type\",\n \"typeKeywords\",\n \"url\"\n ];\n\n // step 1: add item\n const createItemOption: ICreateItemOptions = {\n item: {\n ...filterKeys(requestOptions as any, itemAddProperties),\n type: \"API Key\"\n },\n ...baseRequestOptions,\n authentication: requestOptions.authentication,\n params: {\n f: \"json\"\n }\n };\n\n const createItemResponse = await createItem(createItemOption);\n\n // step 2: register app\n const registerAppOption: IRegisterAppOptions = {\n itemId: createItemResponse.id,\n appType: \"apikey\",\n redirect_uris: [],\n httpReferrers: requestOptions.httpReferrers || [],\n privileges: requestOptions.privileges,\n ...baseRequestOptions,\n authentication: requestOptions.authentication\n };\n\n const registeredAppResponse = await registerApp(registerAppOption);\n const itemInfo = await getItem(registeredAppResponse.itemId, {\n ...baseRequestOptions,\n authentication: requestOptions.authentication,\n params: { f: \"json\" }\n });\n\n return {\n ...appToApiKeyProperties(registeredAppResponse),\n item: itemInfo\n };\n}\n"]}
1
+ {"version":3,"file":"createApiKey.js","sourceRoot":"","sources":["../../src/createApiKey.ts"],"names":[],"mappings":"AAAA;gBACgB;AAEhB,OAAO,EAEL,UAAU,EACV,OAAO,EAEP,UAAU,EACX,MAAM,0BAA0B,CAAC;AAMlC,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,EACL,qBAAqB,EACrB,UAAU,EACV,yBAAyB,EACzB,oBAAoB,EACpB,sBAAsB,EACtB,yBAAyB,EAC1B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,cAAoC;IAEpC,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC;IAEnC,wBAAwB;IACxB,MAAM,kBAAkB,GAAG,yBAAyB,CAAC,cAAc,CAAC,CAAC,CAAC,iFAAiF;IAEvJ,MAAM,iBAAiB,GAA0B;QAC/C,YAAY;QACZ,SAAS;QACT,aAAa;QACb,eAAe;QACf,QAAQ;QACR,OAAO;QACP,YAAY;QACZ,SAAS;QACT,kBAAkB;QAClB,MAAM;QACN,OAAO;QACP,MAAM;QACN,cAAc;QACd,KAAK;KACN,CAAC;IAEF;;OAEG;IACH,MAAM,gBAAgB,iCACpB,IAAI,kCACC,UAAU,CAAC,cAAqB,EAAE,iBAAiB,CAAC,KACvD,IAAI,EAAE,aAAa,OAElB,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,EAC7C,MAAM,EAAE;YACN,CAAC,EAAE,MAAM;SACV,GACF,CAAC;IAEF,MAAM,kBAAkB,GAAG,MAAM,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAE9D;;OAEG;IACH,MAAM,kBAAkB,iCACtB,MAAM,EAAE,kBAAkB,CAAC,EAAE,EAC7B,OAAO,EAAE,UAAU,EACnB,aAAa,EAAE,CAAC,2BAA2B,CAAC,EAC5C,aAAa,EAAE,cAAc,CAAC,aAAa,IAAI,EAAE,EACjD,UAAU,EAAE,cAAc,CAAC,UAAU,IAClC,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,GAC9C,CAAC;IAEF,MAAM,qBAAqB,GAAG,MAAM,WAAW,CAAC,kBAAkB,CAAC,CAAC;IAEpE;;;;OAIG;IACH,MAAM,UAAU,iCACX,kBAAkB,KACrB,IAAI,kBACF,EAAE,EAAE,kBAAkB,CAAC,EAAE,IACtB,yBAAyB,CAAC,cAAc,EAAE,IAAI,CAAC,GAEpD,cAAc,EAAE,cAAc,CAAC,cAAc,IAC7C,CAAC;IAEH;;OAEG;IACH,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,qBAAqB,CAAC,MAAM,kCACtD,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,EAC7C,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,IACrB,CAAC;IAEH;;OAEG;IACH,MAAM,eAAe,GAAG,MAAM,oBAAoB,CAChD,QAAQ,CAAC,EAAE,EACX,sBAAsB,CACpB,cAAc,CAAC,cAAc,EAC7B,cAAc,CAAC,cAAc,CAC9B,kCAEI,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,IAEhD,CAAC;IAEF;;OAEG;IACH,MAAM,4BAA4B,GAAG,MAAM,oBAAoB,iCAC1D,kBAAkB,KACrB,MAAM,EAAE,QAAQ,CAAC,EAAE,EACnB,cAAc,EAAE,cAAc,CAAC,cAAc,IAC7C,CAAC;IAEH,qDACK,eAAe,GACf,qBAAqB,CAAC,4BAA4B,CAAC,KACtD,IAAI,EAAE,QAAQ,IACd;AACJ,CAAC","sourcesContent":["/* Copyright (c) 2023 Environmental Systems Research Institute, Inc.\n * Apache-2.0 */\n\nimport {\n ICreateItemOptions,\n createItem,\n getItem,\n IItemAdd,\n updateItem\n} from \"@esri/arcgis-rest-portal\";\nimport {\n IApiKeyResponse,\n ICreateApiKeyOptions\n} from \"./shared/types/apiKeyType.js\";\n\nimport { registerApp } from \"./shared/registerApp.js\";\nimport { IRegisterAppOptions } from \"./shared/types/appType.js\";\nimport {\n appToApiKeyProperties,\n filterKeys,\n extractBaseRequestOptions,\n generateApiKeyTokens,\n generateOptionsToSlots,\n buildExpirationDateParams\n} from \"./shared/helpers.js\";\nimport { getRegisteredAppInfo } from \"./shared/getRegisteredAppInfo.js\";\n\n/**\n * Used to register an API key. See the [security and authentication](https://developers.arcgis.com/documentation/mapping-apis-and-services/security/api-keys/) for more information about API key.\n *\n * ```js\n * import { createApiKey, IApiKeyResponse } from '@esri/arcgis-rest-developer-credentials';\n * import { ArcGISIdentityManager } from \"@esri/arcgis-rest-request\";\n *\n * const authSession: ArcGISIdentityManager = await ArcGISIdentityManager.signIn({\n * username: \"xyz_usrName\",\n * password: \"xyz_pw\"\n * });\n *\n * const threeDaysFromToday = new Date();\n * threeDaysFromToday.setDate(threeDaysFromToday.getDate() + 3);\n * threeDaysFromToday.setHours(23, 59, 59, 999);\n *\n * createApiKey({\n * title: \"xyz_title\",\n * description: \"xyz_desc\",\n * tags: [\"xyz_tag1\", \"xyz_tag2\"],\n * privileges: [\"premium:user:networkanalysis:routing\"],\n * authentication: authSession,\n * generateToken1: true, // optional,generate a new token\n * apiToken1ExpirationDate: threeDaysFromToday // optional, update expiration date\n * }).then((registeredAPIKey: IApiKeyResponse) => {\n * // => {accessToken1: \"xyz_key\", item: {tags: [\"xyz_tag1\", \"xyz_tag2\"], ...}, ...}\n * }).catch(e => {\n * // => an exception object\n * });\n * ```\n *\n * @param requestOptions - Options for {@linkcode createApiKey | createApiKey()}, including necessary params to register an API key and an {@linkcode ArcGISIdentityManager} authentication session.\n * @returns A Promise that will resolve to an {@linkcode IApiKeyResponse} object representing the newly registered API key.\n */\nexport async function createApiKey(\n requestOptions: ICreateApiKeyOptions\n): Promise<IApiKeyResponse> {\n requestOptions.httpMethod = \"POST\";\n\n // filter param buckets:\n const baseRequestOptions = extractBaseRequestOptions(requestOptions); // snapshot of basic IRequestOptions before customized params being built into it\n\n const itemAddProperties: Array<keyof IItemAdd> = [\n \"categories\",\n \"culture\",\n \"description\",\n \"documentation\",\n \"extent\",\n \"owner\",\n \"properties\",\n \"snippet\",\n \"spatialReference\",\n \"tags\",\n \"title\",\n \"type\",\n \"typeKeywords\",\n \"url\"\n ];\n\n /**\n * step 1: create item\n */\n const createItemOption: ICreateItemOptions = {\n item: {\n ...filterKeys(requestOptions as any, itemAddProperties),\n type: \"Application\"\n },\n ...baseRequestOptions,\n authentication: requestOptions.authentication,\n params: {\n f: \"json\"\n }\n };\n\n const createItemResponse = await createItem(createItemOption);\n\n /**\n * getRegisteredAppInfoRoute\n */\n const registerAppOptions: IRegisterAppOptions = {\n itemId: createItemResponse.id,\n appType: \"multiple\",\n redirect_uris: [\"urn:ietf:wg:oauth:2.0:oob\"],\n httpReferrers: requestOptions.httpReferrers || [],\n privileges: requestOptions.privileges,\n ...baseRequestOptions,\n authentication: requestOptions.authentication\n };\n\n const registeredAppResponse = await registerApp(registerAppOptions);\n\n /**\n * step 3: update item with desired expiration dates\n * you cannot set the expiration date propierties until you\n * regiester the app so this has to be a seperate step\n */\n await updateItem({\n ...baseRequestOptions,\n item: {\n id: createItemResponse.id,\n ...buildExpirationDateParams(requestOptions, true)\n },\n authentication: requestOptions.authentication\n });\n\n /*\n * step 4: get item info\n */\n const itemInfo = await getItem(registeredAppResponse.itemId, {\n ...baseRequestOptions,\n authentication: requestOptions.authentication,\n params: { f: \"json\" }\n });\n\n /**\n * step 5: generate tokens if requested\n */\n const generatedTokens = await generateApiKeyTokens(\n itemInfo.id,\n generateOptionsToSlots(\n requestOptions.generateToken1,\n requestOptions.generateToken2\n ),\n {\n ...baseRequestOptions,\n authentication: requestOptions.authentication\n }\n );\n\n /**\n * step 6: get registered app info to get updated active key status\n */\n const updatedRegisteredAppResponse = await getRegisteredAppInfo({\n ...baseRequestOptions,\n itemId: itemInfo.id,\n authentication: requestOptions.authentication\n });\n\n return {\n ...generatedTokens,\n ...appToApiKeyProperties(updatedRegisteredAppResponse),\n item: itemInfo\n };\n}\n"]}
@@ -1,8 +1,7 @@
1
1
  export * from "./createApiKey.js";
2
2
  export * from "./updateApiKey.js";
3
3
  export * from "./getApiKey.js";
4
- export * from "./deleteApiKey.js";
5
- export * from "./deleteOAuthApp.js";
4
+ export * from "./invalidateApiKey.js";
6
5
  export * from "./getOAuthApp.js";
7
6
  export * from "./updateOAuthApp.js";
8
7
  export * from "./createOAuthApp.js";
package/dist/esm/index.js CHANGED
@@ -3,8 +3,7 @@
3
3
  export * from "./createApiKey.js";
4
4
  export * from "./updateApiKey.js";
5
5
  export * from "./getApiKey.js";
6
- export * from "./deleteApiKey.js";
7
- export * from "./deleteOAuthApp.js";
6
+ export * from "./invalidateApiKey.js";
8
7
  export * from "./getOAuthApp.js";
9
8
  export * from "./updateOAuthApp.js";
10
9
  export * from "./createOAuthApp.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;gBACgB;AAEhB,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC","sourcesContent":["/* Copyright (c) 2023 Environmental Systems Research Institute, Inc.\n * Apache-2.0 */\n\nexport * from \"./createApiKey.js\";\nexport * from \"./updateApiKey.js\";\nexport * from \"./getApiKey.js\";\nexport * from \"./deleteApiKey.js\";\nexport * from \"./deleteOAuthApp.js\";\nexport * from \"./getOAuthApp.js\";\nexport * from \"./updateOAuthApp.js\";\nexport * from \"./createOAuthApp.js\";\nexport * from \"./shared/enum/privileges.js\";\nexport * from \"./shared/types/appType.js\";\nexport * from \"./shared/types/apiKeyType.js\";\nexport * from \"./shared/types/oAuthType.js\";\nexport * from \"./shared/getRegisteredAppInfo.js\";\nexport * from \"./shared/registerApp.js\";\nexport * from \"./shared/unregisterApp.js\";\nexport * from \"./shared/helpers.js\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;gBACgB;AAEhB,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC","sourcesContent":["/* Copyright (c) 2023 Environmental Systems Research Institute, Inc.\n * Apache-2.0 */\n\nexport * from \"./createApiKey.js\";\nexport * from \"./updateApiKey.js\";\nexport * from \"./getApiKey.js\";\nexport * from \"./invalidateApiKey.js\";\nexport * from \"./getOAuthApp.js\";\nexport * from \"./updateOAuthApp.js\";\nexport * from \"./createOAuthApp.js\";\nexport * from \"./shared/enum/privileges.js\";\nexport * from \"./shared/types/appType.js\";\nexport * from \"./shared/types/apiKeyType.js\";\nexport * from \"./shared/types/oAuthType.js\";\nexport * from \"./shared/getRegisteredAppInfo.js\";\nexport * from \"./shared/registerApp.js\";\nexport * from \"./shared/unregisterApp.js\";\nexport * from \"./shared/helpers.js\";\n"]}
@@ -0,0 +1,18 @@
1
+ import { IInvalidateApiKeyOptions, IInvalidateApiKeyResponse } from "./shared/types/apiKeyType.js";
2
+ /**
3
+ * Used to invalidate an API key.
4
+ *
5
+ * ```js
6
+ * import { invalidateApiKey } from "@esri/arcgis-rest-developer-credentials";
7
+ *
8
+ * invalidateApiKey({
9
+ * itemId: ITEM_ID,
10
+ * authentication,
11
+ * apiKey: 1, // invalidate the key in slot 1
12
+ * }).then((response) => {
13
+ * // => {success: true}
14
+ * }).catch(e => {
15
+ * // => an exception object
16
+ * });
17
+ */
18
+ export declare function invalidateApiKey(requestOptions: IInvalidateApiKeyOptions): Promise<IInvalidateApiKeyResponse>;
@@ -0,0 +1,42 @@
1
+ /* Copyright (c) 2023 Environmental Systems Research Institute, Inc.
2
+ * Apache-2.0 */
3
+ import { getRegisteredAppInfo } from "./shared/getRegisteredAppInfo.js";
4
+ import { getPortalUrl } from "@esri/arcgis-rest-portal";
5
+ import { request } from "@esri/arcgis-rest-request";
6
+ import { slotForInvalidationKey } from "./shared/helpers.js";
7
+ /**
8
+ * Used to invalidate an API key.
9
+ *
10
+ * ```js
11
+ * import { invalidateApiKey } from "@esri/arcgis-rest-developer-credentials";
12
+ *
13
+ * invalidateApiKey({
14
+ * itemId: ITEM_ID,
15
+ * authentication,
16
+ * apiKey: 1, // invalidate the key in slot 1
17
+ * }).then((response) => {
18
+ * // => {success: true}
19
+ * }).catch(e => {
20
+ * // => an exception object
21
+ * });
22
+ */
23
+ export async function invalidateApiKey(requestOptions) {
24
+ const portal = getPortalUrl(requestOptions);
25
+ const url = `${portal}/oauth2/revokeToken`;
26
+ const appInfo = await getRegisteredAppInfo({
27
+ itemId: requestOptions.itemId,
28
+ authentication: requestOptions.authentication
29
+ });
30
+ const params = {
31
+ client_id: appInfo.client_id,
32
+ client_secret: appInfo.client_secret,
33
+ apiToken: slotForInvalidationKey(requestOptions.apiKey),
34
+ regenerateApiToken: true,
35
+ grant_type: "client_credentials"
36
+ };
37
+ // authentication is not being passed to the request because client_secret acts as the auth
38
+ return request(url, {
39
+ params
40
+ });
41
+ }
42
+ //# sourceMappingURL=invalidateApiKey.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invalidateApiKey.js","sourceRoot":"","sources":["../../src/invalidateApiKey.ts"],"names":[],"mappings":"AAAA;gBACgB;AAMhB,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACpD,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAE7D;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,cAAwC;IAExC,MAAM,MAAM,GAAG,YAAY,CAAC,cAAc,CAAC,CAAC;IAC5C,MAAM,GAAG,GAAG,GAAG,MAAM,qBAAqB,CAAC;IAE3C,MAAM,OAAO,GAAG,MAAM,oBAAoB,CAAC;QACzC,MAAM,EAAE,cAAc,CAAC,MAAM;QAC7B,cAAc,EAAE,cAAc,CAAC,cAAc;KAC9C,CAAC,CAAC;IAEH,MAAM,MAAM,GAAG;QACb,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,QAAQ,EAAE,sBAAsB,CAAC,cAAc,CAAC,MAAM,CAAC;QACvD,kBAAkB,EAAE,IAAI;QACxB,UAAU,EAAE,oBAAoB;KACjC,CAAC;IAEF,2FAA2F;IAC3F,OAAO,OAAO,CAAC,GAAG,EAAE;QAClB,MAAM;KACP,CAAC,CAAC;AACL,CAAC","sourcesContent":["/* Copyright (c) 2023 Environmental Systems Research Institute, Inc.\n * Apache-2.0 */\n\nimport {\n IInvalidateApiKeyOptions,\n IInvalidateApiKeyResponse\n} from \"./shared/types/apiKeyType.js\";\nimport { getRegisteredAppInfo } from \"./shared/getRegisteredAppInfo.js\";\nimport { getPortalUrl } from \"@esri/arcgis-rest-portal\";\nimport { request } from \"@esri/arcgis-rest-request\";\nimport { slotForInvalidationKey } from \"./shared/helpers.js\";\n\n/**\n * Used to invalidate an API key.\n *\n * ```js\n * import { invalidateApiKey } from \"@esri/arcgis-rest-developer-credentials\";\n *\n * invalidateApiKey({\n * itemId: ITEM_ID,\n * authentication,\n * apiKey: 1, // invalidate the key in slot 1\n * }).then((response) => {\n * // => {success: true}\n * }).catch(e => {\n * // => an exception object\n * });\n */\nexport async function invalidateApiKey(\n requestOptions: IInvalidateApiKeyOptions\n): Promise<IInvalidateApiKeyResponse> {\n const portal = getPortalUrl(requestOptions);\n const url = `${portal}/oauth2/revokeToken`;\n\n const appInfo = await getRegisteredAppInfo({\n itemId: requestOptions.itemId,\n authentication: requestOptions.authentication\n });\n\n const params = {\n client_id: appInfo.client_id,\n client_secret: appInfo.client_secret,\n apiToken: slotForInvalidationKey(requestOptions.apiKey),\n regenerateApiToken: true,\n grant_type: \"client_credentials\"\n };\n\n // authentication is not being passed to the request because client_secret acts as the auth\n return request(url, {\n params\n });\n}\n"]}
@@ -1,25 +1,4 @@
1
1
  /**
2
- * Used to describe privilege list of an app.
2
+ * Used to describe privilege list of an app. For a comlete list of privileges, see the [list of privileges in the security and authentication guide](https://developers.arcgis.com/documentation/security-and-authentication/reference/privileges/#list-of-privileges).
3
3
  */
4
- export declare enum Privileges {
5
- Basemaps = "portal:apikey:basemaps",
6
- Demographics = "premium:user:demographics",
7
- Elevation = "premium:user:elevation",
8
- FeatureReport = "premium:user:featurereport",
9
- Geocode = "premium:user:geocode",
10
- GeocodeStored = "premium:user:geocode:stored",
11
- GeocodeTemporary = "premium:user:geocode:temporary",
12
- GeoEnrichment = "premium:user:geoenrichment",
13
- NetworkAnalysis = "premium:user:networkanalysis",
14
- NetworkAnalysisRouting = "premium:user:networkanalysis:routing",
15
- NetworkAnalysisOptimizedRouting = "premium:user:networkanalysis:optimizedrouting",
16
- NetworkAnalysisClosestFacility = "premium:user:networkanalysis:closestfacility",
17
- NetworkAnalysisServiceArea = "premium:user:networkanalysis:servicearea",
18
- NetworkAnalysisLocationalLocation = "premium:user:networkanalysis:locationallocation",
19
- NetworkAnalysisVehicleRouting = "premium:user:networkanalysis:vehiclerouting",
20
- NetworkAnalysisOriginDestinationCostMatrix = "premium:user:networkanalysis:origindestinationcostmatrix",
21
- Places = "premium:user:places",
22
- SpatialAnalysis = "premium:user:spatialanalysis",
23
- GeoAnalytics = "premium:publisher:geoanalytics",
24
- RasterAnalysis = "premium:publisher:rasteranalysis"
25
- }
4
+ export declare type Privileges = string[];
@@ -1,27 +1,2 @@
1
- /**
2
- * Used to describe privilege list of an app.
3
- */
4
- export var Privileges;
5
- (function (Privileges) {
6
- Privileges["Basemaps"] = "portal:apikey:basemaps";
7
- Privileges["Demographics"] = "premium:user:demographics";
8
- Privileges["Elevation"] = "premium:user:elevation";
9
- Privileges["FeatureReport"] = "premium:user:featurereport";
10
- Privileges["Geocode"] = "premium:user:geocode";
11
- Privileges["GeocodeStored"] = "premium:user:geocode:stored";
12
- Privileges["GeocodeTemporary"] = "premium:user:geocode:temporary";
13
- Privileges["GeoEnrichment"] = "premium:user:geoenrichment";
14
- Privileges["NetworkAnalysis"] = "premium:user:networkanalysis";
15
- Privileges["NetworkAnalysisRouting"] = "premium:user:networkanalysis:routing";
16
- Privileges["NetworkAnalysisOptimizedRouting"] = "premium:user:networkanalysis:optimizedrouting";
17
- Privileges["NetworkAnalysisClosestFacility"] = "premium:user:networkanalysis:closestfacility";
18
- Privileges["NetworkAnalysisServiceArea"] = "premium:user:networkanalysis:servicearea";
19
- Privileges["NetworkAnalysisLocationalLocation"] = "premium:user:networkanalysis:locationallocation";
20
- Privileges["NetworkAnalysisVehicleRouting"] = "premium:user:networkanalysis:vehiclerouting";
21
- Privileges["NetworkAnalysisOriginDestinationCostMatrix"] = "premium:user:networkanalysis:origindestinationcostmatrix";
22
- Privileges["Places"] = "premium:user:places";
23
- Privileges["SpatialAnalysis"] = "premium:user:spatialanalysis";
24
- Privileges["GeoAnalytics"] = "premium:publisher:geoanalytics";
25
- Privileges["RasterAnalysis"] = "premium:publisher:rasteranalysis";
26
- })(Privileges || (Privileges = {}));
1
+ export {};
27
2
  //# sourceMappingURL=privileges.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"privileges.js","sourceRoot":"","sources":["../../../../src/shared/enum/privileges.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAN,IAAY,UAqBX;AArBD,WAAY,UAAU;IACpB,iDAAmC,CAAA;IACnC,wDAA0C,CAAA;IAC1C,kDAAoC,CAAA;IACpC,0DAA4C,CAAA;IAC5C,8CAAgC,CAAA;IAChC,2DAA6C,CAAA;IAC7C,iEAAmD,CAAA;IACnD,0DAA4C,CAAA;IAC5C,8DAAgD,CAAA;IAChD,6EAA+D,CAAA;IAC/D,+FAAiF,CAAA;IACjF,6FAA+E,CAAA;IAC/E,qFAAuE,CAAA;IACvE,mGAAqF,CAAA;IACrF,2FAA6E,CAAA;IAC7E,qHAAuG,CAAA;IACvG,4CAA8B,CAAA;IAC9B,8DAAgD,CAAA;IAChD,6DAA+C,CAAA;IAC/C,iEAAmD,CAAA;AACrD,CAAC,EArBW,UAAU,KAAV,UAAU,QAqBrB","sourcesContent":["/**\n * Used to describe privilege list of an app.\n */\nexport enum Privileges {\n Basemaps = \"portal:apikey:basemaps\",\n Demographics = \"premium:user:demographics\",\n Elevation = \"premium:user:elevation\",\n FeatureReport = \"premium:user:featurereport\",\n Geocode = \"premium:user:geocode\",\n GeocodeStored = \"premium:user:geocode:stored\",\n GeocodeTemporary = \"premium:user:geocode:temporary\",\n GeoEnrichment = \"premium:user:geoenrichment\",\n NetworkAnalysis = \"premium:user:networkanalysis\",\n NetworkAnalysisRouting = \"premium:user:networkanalysis:routing\",\n NetworkAnalysisOptimizedRouting = \"premium:user:networkanalysis:optimizedrouting\",\n NetworkAnalysisClosestFacility = \"premium:user:networkanalysis:closestfacility\",\n NetworkAnalysisServiceArea = \"premium:user:networkanalysis:servicearea\",\n NetworkAnalysisLocationalLocation = \"premium:user:networkanalysis:locationallocation\",\n NetworkAnalysisVehicleRouting = \"premium:user:networkanalysis:vehiclerouting\",\n NetworkAnalysisOriginDestinationCostMatrix = \"premium:user:networkanalysis:origindestinationcostmatrix\",\n Places = \"premium:user:places\",\n SpatialAnalysis = \"premium:user:spatialanalysis\",\n GeoAnalytics = \"premium:publisher:geoanalytics\",\n RasterAnalysis = \"premium:publisher:rasteranalysis\"\n}\n"]}
1
+ {"version":3,"file":"privileges.js","sourceRoot":"","sources":["../../../../src/shared/enum/privileges.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Used to describe privilege list of an app. For a comlete list of privileges, see the [list of privileges in the security and authentication guide](https://developers.arcgis.com/documentation/security-and-authentication/reference/privileges/#list-of-privileges).\n */\nexport type Privileges = string[];\n"]}
@@ -0,0 +1,11 @@
1
+ import { IRequestOptions, IAuthenticationManager } from "@esri/arcgis-rest-request";
2
+ export interface IGenerateApiKeyTokenOptions extends IRequestOptions {
3
+ itemId: string;
4
+ apiKey: 1 | 2;
5
+ portal?: string;
6
+ authentication: IAuthenticationManager;
7
+ }
8
+ export declare function generateApiKeyToken(options: IGenerateApiKeyTokenOptions): Promise<{
9
+ access_token: string;
10
+ expires_in: number;
11
+ }>;
@@ -0,0 +1,23 @@
1
+ import { request } from "@esri/arcgis-rest-request";
2
+ import { getRegisteredAppInfo } from "./getRegisteredAppInfo.js";
3
+ import { getPortalUrl } from "@esri/arcgis-rest-portal";
4
+ export async function generateApiKeyToken(options) {
5
+ const portal = getPortalUrl(options);
6
+ const url = `${portal}/oauth2/token`;
7
+ const appInfo = await getRegisteredAppInfo({
8
+ itemId: options.itemId,
9
+ authentication: options.authentication
10
+ });
11
+ const params = {
12
+ client_id: appInfo.client_id,
13
+ client_secret: appInfo.client_secret,
14
+ apiToken: options.apiKey,
15
+ regenerateApiToken: true,
16
+ grant_type: "client_credentials"
17
+ };
18
+ // authentication is not being passed to the request because client_secret acts as the auth
19
+ return request(url, {
20
+ params
21
+ });
22
+ }
23
+ //# sourceMappingURL=generateApiKeyToken.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateApiKeyToken.js","sourceRoot":"","sources":["../../../src/shared/generateApiKeyToken.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EAGR,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AASxD,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,OAAoC;IAEpC,MAAM,MAAM,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IACrC,MAAM,GAAG,GAAG,GAAG,MAAM,eAAe,CAAC;IAErC,MAAM,OAAO,GAAG,MAAM,oBAAoB,CAAC;QACzC,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,cAAc,EAAE,OAAO,CAAC,cAAc;KACvC,CAAC,CAAC;IAEH,MAAM,MAAM,GAAG;QACb,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,QAAQ,EAAE,OAAO,CAAC,MAAM;QACxB,kBAAkB,EAAE,IAAI;QACxB,UAAU,EAAE,oBAAoB;KACjC,CAAC;IAEF,2FAA2F;IAC3F,OAAO,OAAO,CAAC,GAAG,EAAE;QAClB,MAAM;KACP,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n request,\n IRequestOptions,\n IAuthenticationManager\n} from \"@esri/arcgis-rest-request\";\nimport { getRegisteredAppInfo } from \"./getRegisteredAppInfo.js\";\nimport { getPortalUrl } from \"@esri/arcgis-rest-portal\";\n\nexport interface IGenerateApiKeyTokenOptions extends IRequestOptions {\n itemId: string;\n apiKey: 1 | 2;\n portal?: string;\n authentication: IAuthenticationManager;\n}\n\nexport async function generateApiKeyToken(\n options: IGenerateApiKeyTokenOptions\n): Promise<{ access_token: string; expires_in: number }> {\n const portal = getPortalUrl(options);\n const url = `${portal}/oauth2/token`;\n\n const appInfo = await getRegisteredAppInfo({\n itemId: options.itemId,\n authentication: options.authentication\n });\n\n const params = {\n client_id: appInfo.client_id,\n client_secret: appInfo.client_secret,\n apiToken: options.apiKey,\n regenerateApiToken: true,\n grant_type: \"client_credentials\"\n };\n\n // authentication is not being passed to the request because client_secret acts as the auth\n return request(url, {\n params\n });\n}\n"]}
@@ -1,13 +1,7 @@
1
- import { IRequestOptions } from "@esri/arcgis-rest-request";
2
- import { Privileges } from "./enum/privileges.js";
1
+ import { IRequestOptions, IAuthenticationManager } from "@esri/arcgis-rest-request";
3
2
  import { IRegisteredAppResponse, IApp } from "./types/appType.js";
4
3
  import { IApiKeyInfo } from "./types/apiKeyType.js";
5
4
  import { IOAuthAppInfo } from "./types/oAuthType.js";
6
- /**
7
- * @internal
8
- * Used to check privileges validity.
9
- */
10
- export declare const arePrivilegesValid: (privileges: Array<Privileges | `${Privileges}`>) => boolean;
11
5
  /**
12
6
  * @internal
13
7
  * Encode special params value (e.g. array type...) in advance in order to make {@linkcode encodeParam} works correctly. Usage is case by case.
@@ -38,3 +32,44 @@ export declare function extractBaseRequestOptions<T extends IRequestOptions>(opt
38
32
  * Used to create a new object including only specified keys from another object.
39
33
  */
40
34
  export declare function filterKeys<T extends object>(object: T, includedKeys: Array<keyof T>): any;
35
+ /**
36
+ * Used to determine if a generated key is in slot 1 or slot 2 key. The full API key should be passed. `undefined` will be returned if the proper slot could not be identified.
37
+ */
38
+ export declare function slotForKey(key: string): 1 | 2;
39
+ /**
40
+ * @internal
41
+ * Used to determine which slot to invalidate a key in given a number or a full or patial key.
42
+ */
43
+ export declare function slotForInvalidationKey(param: string | 1 | 2): 2 | 1;
44
+ interface IGenerateApiKeyTokenOptions extends IRequestOptions {
45
+ authentication: IAuthenticationManager;
46
+ }
47
+ /**
48
+ * @internal
49
+ * Used to generate tokens in slot 1 and/or 2 of an API key.
50
+ */
51
+ export declare function generateApiKeyTokens(itemId: string, slots: Array<1 | 2>, requestOptions: IGenerateApiKeyTokenOptions): Promise<{
52
+ [key: string]: string;
53
+ }>;
54
+ /**
55
+ * @internal
56
+ * Convert boolean flags to an array of slots for {@linkcode generateApiKeyTokens}.
57
+ */
58
+ export declare function generateOptionsToSlots(generateToken1: boolean, generateToken2: boolean): Array<1 | 2>;
59
+ declare type expirationDateParams = {
60
+ apiToken1ExpirationDate: Date | -1;
61
+ apiToken2ExpirationDate: Date | -1;
62
+ } | {
63
+ apiToken1ExpirationDate: Date | -1;
64
+ } | {
65
+ apiToken2ExpirationDate: Date | -1;
66
+ } | {};
67
+ /**
68
+ * @internal
69
+ * Build params for updating expiration dates
70
+ */
71
+ export declare function buildExpirationDateParams(requestOptions: {
72
+ apiToken1ExpirationDate?: Date;
73
+ apiToken2ExpirationDate?: Date;
74
+ }, fillDefaults?: boolean): expirationDateParams;
75
+ export {};