@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
@@ -1,9 +1,4 @@
1
- import { Privileges } from "./enum/privileges.js";
2
- /**
3
- * @internal
4
- * Used to check privileges validity.
5
- */
6
- export const arePrivilegesValid = (privileges) => privileges.every((element) => Object.values(Privileges).includes(element));
1
+ import { generateApiKeyToken } from "./generateApiKeyToken.js";
7
2
  /**
8
3
  * @internal
9
4
  * Encode special params value (e.g. array type...) in advance in order to make {@linkcode encodeParam} works correctly. Usage is case by case.
@@ -25,7 +20,8 @@ export function registeredAppResponseToApp(response) {
25
20
  "apnsProdCert",
26
21
  "apnsSandboxCert",
27
22
  "gcmApiKey",
28
- "isBeta"
23
+ "isBeta",
24
+ "customAppLoginShowTriage"
29
25
  ];
30
26
  const dateKeys = ["modified", "registered"];
31
27
  return Object.keys(response)
@@ -45,13 +41,11 @@ export function registeredAppResponseToApp(response) {
45
41
  * Used to convert {@linkcode IApp} to {@linkcode IApiKeyInfo} only if `appType` is "apikey".
46
42
  */
47
43
  export function appToApiKeyProperties(response) {
48
- if (response.appType !== "apikey" || !("apiKey" in response)) {
49
- throw new Error("Item is not an API key.");
50
- }
51
- delete response.client_id;
52
44
  delete response.client_secret;
53
45
  delete response.redirect_uris;
54
46
  delete response.appType;
47
+ delete response.customAppLoginShowTriage;
48
+ delete response.apiKey;
55
49
  return response;
56
50
  }
57
51
  /**
@@ -59,12 +53,14 @@ export function appToApiKeyProperties(response) {
59
53
  * Used to convert {@linkcode IApp} to {@linkcode IOAuthAppInfo}.
60
54
  */
61
55
  export function appToOAuthAppProperties(response) {
62
- if (response.appType === "apikey") {
63
- throw new Error("Item is not an OAuth 2.0 app.");
64
- }
65
56
  delete response.appType;
66
57
  delete response.httpReferrers;
67
58
  delete response.privileges;
59
+ delete response.apiKey;
60
+ delete response.customAppLoginShowTriage;
61
+ delete response.isPersonalAPIToken;
62
+ delete response.apiToken1Active;
63
+ delete response.apiToken2Active;
68
64
  return response;
69
65
  }
70
66
  /**
@@ -97,4 +93,82 @@ export function filterKeys(object, includedKeys) {
97
93
  return obj;
98
94
  }, {});
99
95
  }
96
+ /**
97
+ * 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.
98
+ */
99
+ export function slotForKey(key) {
100
+ const slot = parseInt(key.substring(key.length - 10, key.length - 9));
101
+ if (slot === 1 || slot === 2) {
102
+ return slot;
103
+ }
104
+ return undefined;
105
+ }
106
+ /**
107
+ * @internal
108
+ * Used to determine which slot to invalidate a key in given a number or a full or patial key.
109
+ */
110
+ export function slotForInvalidationKey(param) {
111
+ if (param === 1 || param === 2) {
112
+ return param;
113
+ }
114
+ if (typeof param !== "string") {
115
+ return undefined;
116
+ }
117
+ const fullKeySlot = slotForKey(param);
118
+ if (fullKeySlot) {
119
+ return fullKeySlot;
120
+ }
121
+ }
122
+ /**
123
+ * @internal
124
+ * Used to generate tokens in slot 1 and/or 2 of an API key.
125
+ */
126
+ export function generateApiKeyTokens(itemId, slots, requestOptions) {
127
+ return Promise.all(slots.map((slot) => {
128
+ return generateApiKeyToken(Object.assign({ itemId, apiKey: slot }, requestOptions));
129
+ })).then((responses) => {
130
+ return responses
131
+ .map((responses) => responses.access_token)
132
+ .reduce((obj, token, index) => {
133
+ obj[`accessToken${slotForKey(token)}`] = token;
134
+ return obj;
135
+ }, {});
136
+ });
137
+ }
138
+ /**
139
+ * @internal
140
+ * Convert boolean flags to an array of slots for {@linkcode generateApiKeyTokens}.
141
+ */
142
+ export function generateOptionsToSlots(generateToken1, generateToken2) {
143
+ const slots = [];
144
+ if (generateToken1) {
145
+ slots.push(1);
146
+ }
147
+ if (generateToken2) {
148
+ slots.push(2);
149
+ }
150
+ return slots;
151
+ }
152
+ /**
153
+ * @internal
154
+ * Build params for updating expiration dates
155
+ */
156
+ export function buildExpirationDateParams(requestOptions, fillDefaults) {
157
+ const updateparams = {};
158
+ if (requestOptions.apiToken1ExpirationDate) {
159
+ updateparams.apiToken1ExpirationDate =
160
+ requestOptions.apiToken1ExpirationDate;
161
+ }
162
+ if (requestOptions.apiToken2ExpirationDate) {
163
+ updateparams.apiToken2ExpirationDate =
164
+ requestOptions.apiToken2ExpirationDate;
165
+ }
166
+ if (fillDefaults && !updateparams.apiToken1ExpirationDate) {
167
+ updateparams.apiToken1ExpirationDate = -1;
168
+ }
169
+ if (fillDefaults && !updateparams.apiToken2ExpirationDate) {
170
+ updateparams.apiToken2ExpirationDate = -1;
171
+ }
172
+ return updateparams;
173
+ }
100
174
  //# sourceMappingURL=helpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/shared/helpers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAKlD;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,UAA+C,EACtC,EAAE,CACX,UAAU,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,EAAE,CAC3B,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,QAAQ,CAAC,OAAc,CAAC,CACnD,CAAC;AAEJ;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,cAA+B,EAAE,EAAE;IACjE,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QACtD,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,KAAK,CAAC;QAC3B,IAAI,KAAK,CAAC,WAAW,CAAC,IAAI,KAAK,OAAO,EAAE;YACtC,cAAc,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;SACpD;IACH,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,0BAA0B,CACxC,QAAgC;IAEhC,MAAM,WAAW,GAAG;QAClB,cAAc;QACd,iBAAiB;QACjB,WAAW;QACX,QAAQ;KACT,CAAC;IACF,MAAM,QAAQ,GAAG,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;IAE5C,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;SACzB,MAAM,CAAC,CAAC,GAA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;QACT,WAAW;QACX,YAAY;QACZ,cAAc;QACd,QAAQ;QACR,aAAa;QACb,QAAQ;QACR,kBAAkB;KACnB,CAAC;IAEF,OAAO,UAAU,CAAC,OAAO,EAAE,wBAAwB,CAAC,CAAC;AACvD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CACxB,MAAS,EACT,YAA4B;IAE5B,OAAO,YAAY,CAAC,MAAM,CACxB,CAAC,GAA6C,EAAE,GAAG,EAAE,EAAE;QACrD,IAAI,GAAG,IAAI,MAAM,EAAE;YACjB,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;SACxB;QACD,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import { IRequestOptions } from \"@esri/arcgis-rest-request\";\nimport { Privileges } from \"./enum/privileges.js\";\nimport { IRegisteredAppResponse, IApp } from \"./types/appType.js\";\nimport { IApiKeyInfo } from \"./types/apiKeyType.js\";\nimport { IOAuthAppInfo } from \"./types/oAuthType.js\";\n\n/**\n * @internal\n * Used to check privileges validity.\n */\nexport const arePrivilegesValid = (\n privileges: Array<Privileges | `${Privileges}`>\n): boolean =>\n privileges.every((element) =>\n Object.values(Privileges).includes(element as any)\n );\n\n/**\n * @internal\n * Encode special params value (e.g. array type...) in advance in order to make {@linkcode encodeParam} works correctly. Usage is case by case.\n */\nexport const stringifyArrays = (requestOptions: IRequestOptions) => {\n Object.entries(requestOptions.params).forEach((entry) => {\n const [key, value] = entry;\n if (value.constructor.name === \"Array\") {\n requestOptions.params[key] = JSON.stringify(value);\n }\n });\n};\n\n/**\n * @internal\n * Used to convert {@linkcode IRegisteredAppResponse} to {@linkcode IApp}.\n */\nexport function registeredAppResponseToApp(\n response: IRegisteredAppResponse\n): IApp {\n const omittedKeys = [\n \"apnsProdCert\",\n \"apnsSandboxCert\",\n \"gcmApiKey\",\n \"isBeta\"\n ];\n const dateKeys = [\"modified\", \"registered\"];\n\n return Object.keys(response)\n .filter((key) => !omittedKeys.includes(key))\n .reduce((obj: any, key) => {\n if (dateKeys.includes(key)) {\n obj[key] = new Date((response as any)[key]);\n } else {\n obj[key] = (response as any)[key];\n }\n return obj;\n }, {});\n}\n\n/**\n * @internal\n * Used to convert {@linkcode IApp} to {@linkcode IApiKeyInfo} only if `appType` is \"apikey\".\n */\nexport function appToApiKeyProperties(response: IApp): IApiKeyInfo {\n if (response.appType !== \"apikey\" || !(\"apiKey\" in response)) {\n throw new Error(\"Item is not an API key.\");\n }\n\n delete response.client_id;\n delete response.client_secret;\n delete response.redirect_uris;\n delete response.appType;\n\n return response as IApiKeyInfo;\n}\n\n/**\n * @internal\n * Used to convert {@linkcode IApp} to {@linkcode IOAuthAppInfo}.\n */\nexport function appToOAuthAppProperties(response: IApp): IOAuthAppInfo {\n if (response.appType === \"apikey\") {\n throw new Error(\"Item is not an OAuth 2.0 app.\");\n }\n\n delete response.appType;\n delete response.httpReferrers;\n delete response.privileges;\n\n return response as IOAuthAppInfo;\n}\n\n/**\n * @internal\n * Used to extract base request options from a hybrid option and exclude `params` and `authentication`.\n */\nexport function extractBaseRequestOptions<T extends IRequestOptions>(\n options: T\n): Partial<IRequestOptions> {\n const requestOptionsProperties: Array<keyof T> = [\n \"credentials\",\n \"headers\",\n \"hideToken\",\n \"httpMethod\",\n \"maxUrlLength\",\n \"portal\",\n \"rawResponse\",\n \"signal\",\n \"suppressWarnings\"\n ];\n\n return filterKeys(options, requestOptionsProperties);\n}\n\n/**\n * @internal\n * Used to create a new object including only specified keys from another object.\n */\nexport function filterKeys<T extends object>(\n object: T,\n includedKeys: Array<keyof T>\n): any {\n return includedKeys.reduce(\n (obj: { [key: string | number | symbol]: any }, ele) => {\n if (ele in object) {\n obj[ele] = object[ele];\n }\n return obj;\n },\n {}\n );\n}\n"]}
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../../src/shared/helpers.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,mBAAmB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C,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CACpC,cAAuB,EACvB,cAAuB;IAEvB,MAAM,KAAK,GAAiB,EAAE,CAAC;IAC/B,IAAI,cAAc,EAAE;QAClB,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACf;IACD,IAAI,cAAc,EAAE;QAClB,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;KACf;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAeD;;;GAGG;AACH,MAAM,UAAU,yBAAyB,CACvC,cAGC,EACD,YAAsB;IAEtB,MAAM,YAAY,GAAQ,EAAE,CAAC;IAC7B,IAAI,cAAc,CAAC,uBAAuB,EAAE;QAC1C,YAAY,CAAC,uBAAuB;YAClC,cAAc,CAAC,uBAAuB,CAAC;KAC1C;IAED,IAAI,cAAc,CAAC,uBAAuB,EAAE;QAC1C,YAAY,CAAC,uBAAuB;YAClC,cAAc,CAAC,uBAAuB,CAAC;KAC1C;IAED,IAAI,YAAY,IAAI,CAAC,YAAY,CAAC,uBAAuB,EAAE;QACzD,YAAY,CAAC,uBAAuB,GAAG,CAAC,CAAC,CAAC;KAC3C;IAED,IAAI,YAAY,IAAI,CAAC,YAAY,CAAC,uBAAuB,EAAE;QACzD,YAAY,CAAC,uBAAuB,GAAG,CAAC,CAAC,CAAC;KAC3C;IACD,OAAO,YAAY,CAAC;AACtB,CAAC","sourcesContent":["import {\n IRequestOptions,\n IAuthenticationManager\n} from \"@esri/arcgis-rest-request\";\nimport { IRegisteredAppResponse, IApp } from \"./types/appType.js\";\nimport { IApiKeyInfo } from \"./types/apiKeyType.js\";\nimport { IOAuthAppInfo } from \"./types/oAuthType.js\";\nimport { generateApiKeyToken } from \"./generateApiKeyToken.js\";\n\n/**\n * @internal\n * Encode special params value (e.g. array type...) in advance in order to make {@linkcode encodeParam} works correctly. Usage is case by case.\n */\nexport const stringifyArrays = (requestOptions: IRequestOptions) => {\n Object.entries(requestOptions.params).forEach((entry) => {\n const [key, value] = entry;\n if (value.constructor.name === \"Array\") {\n requestOptions.params[key] = JSON.stringify(value);\n }\n });\n};\n\n/**\n * @internal\n * Used to convert {@linkcode IRegisteredAppResponse} to {@linkcode IApp}.\n */\nexport function registeredAppResponseToApp(\n response: IRegisteredAppResponse\n): IApp {\n const omittedKeys = [\n \"apnsProdCert\",\n \"apnsSandboxCert\",\n \"gcmApiKey\",\n \"isBeta\",\n \"customAppLoginShowTriage\"\n ];\n const dateKeys = [\"modified\", \"registered\"];\n\n return Object.keys(response)\n .filter((key) => !omittedKeys.includes(key))\n .reduce((obj: any, key) => {\n if (dateKeys.includes(key)) {\n obj[key] = new Date((response as any)[key]);\n } else {\n obj[key] = (response as any)[key];\n }\n return obj;\n }, {});\n}\n\n/**\n * @internal\n * Used to convert {@linkcode IApp} to {@linkcode IApiKeyInfo} only if `appType` is \"apikey\".\n */\nexport function appToApiKeyProperties(response: IApp): IApiKeyInfo {\n delete response.client_secret;\n delete response.redirect_uris;\n delete response.appType;\n delete (response as any).customAppLoginShowTriage;\n delete response.apiKey;\n\n return response as IApiKeyInfo;\n}\n\n/**\n * @internal\n * Used to convert {@linkcode IApp} to {@linkcode IOAuthAppInfo}.\n */\nexport function appToOAuthAppProperties(response: IApp): IOAuthAppInfo {\n delete response.appType;\n delete response.httpReferrers;\n delete response.privileges;\n delete response.apiKey;\n delete (response as any).customAppLoginShowTriage;\n delete response.isPersonalAPIToken;\n delete response.apiToken1Active;\n delete response.apiToken2Active;\n\n return response as IOAuthAppInfo;\n}\n\n/**\n * @internal\n * Used to extract base request options from a hybrid option and exclude `params` and `authentication`.\n */\nexport function extractBaseRequestOptions<T extends IRequestOptions>(\n options: T\n): Partial<IRequestOptions> {\n const requestOptionsProperties: Array<keyof T> = [\n \"credentials\",\n \"headers\",\n \"hideToken\",\n \"httpMethod\",\n \"maxUrlLength\",\n \"portal\",\n \"rawResponse\",\n \"signal\",\n \"suppressWarnings\"\n ];\n\n return filterKeys(options, requestOptionsProperties);\n}\n\n/**\n * @internal\n * Used to create a new object including only specified keys from another object.\n */\nexport function filterKeys<T extends object>(\n object: T,\n includedKeys: Array<keyof T>\n): any {\n return includedKeys.reduce(\n (obj: { [key: string | number | symbol]: any }, ele) => {\n if (ele in object) {\n obj[ele] = object[ele];\n }\n return obj;\n },\n {}\n );\n}\n\n/**\n * 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.\n */\nexport function slotForKey(key: string) {\n const slot = parseInt(key.substring(key.length - 10, key.length - 9));\n\n if (slot === 1 || slot === 2) {\n return slot;\n }\n\n return undefined;\n}\n\n/**\n * @internal\n * Used to determine which slot to invalidate a key in given a number or a full or patial key.\n */\nexport function slotForInvalidationKey(param: string | 1 | 2) {\n if (param === 1 || param === 2) {\n return param;\n }\n\n if (typeof param !== \"string\") {\n return undefined;\n }\n\n const fullKeySlot = slotForKey(param);\n if (fullKeySlot) {\n return fullKeySlot;\n }\n}\n\ninterface IGenerateApiKeyTokenOptions extends IRequestOptions {\n authentication: IAuthenticationManager;\n}\n\n/**\n * @internal\n * Used to generate tokens in slot 1 and/or 2 of an API key.\n */\nexport function generateApiKeyTokens(\n itemId: string,\n slots: Array<1 | 2>,\n requestOptions: IGenerateApiKeyTokenOptions\n) {\n return Promise.all(\n slots.map((slot) => {\n return generateApiKeyToken({\n itemId,\n apiKey: slot,\n ...requestOptions\n });\n })\n ).then((responses) => {\n return responses\n .map((responses) => responses.access_token)\n .reduce((obj, token, index) => {\n obj[`accessToken${slotForKey(token)}`] = token;\n return obj;\n }, {} as { [key: string]: string });\n });\n}\n\n/**\n * @internal\n * Convert boolean flags to an array of slots for {@linkcode generateApiKeyTokens}.\n */\nexport function generateOptionsToSlots(\n generateToken1: boolean,\n generateToken2: boolean\n): Array<1 | 2> {\n const slots: Array<1 | 2> = [];\n if (generateToken1) {\n slots.push(1);\n }\n if (generateToken2) {\n slots.push(2);\n }\n return slots;\n}\n\ntype expirationDateParams =\n | {\n apiToken1ExpirationDate: Date | -1;\n apiToken2ExpirationDate: Date | -1;\n }\n | {\n apiToken1ExpirationDate: Date | -1;\n }\n | {\n apiToken2ExpirationDate: Date | -1;\n }\n | {};\n\n/**\n * @internal\n * Build params for updating expiration dates\n */\nexport function buildExpirationDateParams(\n requestOptions: {\n apiToken1ExpirationDate?: Date;\n apiToken2ExpirationDate?: Date;\n },\n fillDefaults?: boolean\n): expirationDateParams {\n const updateparams: any = {};\n if (requestOptions.apiToken1ExpirationDate) {\n updateparams.apiToken1ExpirationDate =\n requestOptions.apiToken1ExpirationDate;\n }\n\n if (requestOptions.apiToken2ExpirationDate) {\n updateparams.apiToken2ExpirationDate =\n requestOptions.apiToken2ExpirationDate;\n }\n\n if (fillDefaults && !updateparams.apiToken1ExpirationDate) {\n updateparams.apiToken1ExpirationDate = -1;\n }\n\n if (fillDefaults && !updateparams.apiToken2ExpirationDate) {\n updateparams.apiToken2ExpirationDate = -1;\n }\n return updateparams;\n}\n"]}
@@ -23,7 +23,7 @@ import { IApp, IRegisterAppOptions } from "./types/appType.js";
23
23
  * appType: "multiple",
24
24
  * redirect_uris: ["http://localhost:3000/"],
25
25
  * httpReferrers: ["http://localhost:3000/"],
26
- * privileges: [Privileges.Geocode, Privileges.FeatureReport],
26
+ * privileges: ["premium:user:geocode:temporary", Privileges.FeatureReport],
27
27
  * authentication: authSession
28
28
  * }).then((registeredApp: IApp) => {
29
29
  * // => {client_id: "xyz_id", client_secret: "xyz_secret", ...}
@@ -2,7 +2,7 @@
2
2
  * Apache-2.0 */
3
3
  import { request, appendCustomParams } from "@esri/arcgis-rest-request";
4
4
  import { getPortalUrl } from "@esri/arcgis-rest-portal";
5
- import { arePrivilegesValid, stringifyArrays, registeredAppResponseToApp } from "./helpers.js";
5
+ import { stringifyArrays, registeredAppResponseToApp } from "./helpers.js";
6
6
  /**
7
7
  * Used to register an app. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/register-app.htm) for more information.
8
8
  *
@@ -27,7 +27,7 @@ import { arePrivilegesValid, stringifyArrays, registeredAppResponseToApp } from
27
27
  * appType: "multiple",
28
28
  * redirect_uris: ["http://localhost:3000/"],
29
29
  * httpReferrers: ["http://localhost:3000/"],
30
- * privileges: [Privileges.Geocode, Privileges.FeatureReport],
30
+ * privileges: ["premium:user:geocode:temporary", Privileges.FeatureReport],
31
31
  * authentication: authSession
32
32
  * }).then((registeredApp: IApp) => {
33
33
  * // => {client_id: "xyz_id", client_secret: "xyz_secret", ...}
@@ -40,10 +40,6 @@ import { arePrivilegesValid, stringifyArrays, registeredAppResponseToApp } from
40
40
  * @returns A Promise that will resolve to an {@linkcode IApp} object representing the newly registered app.
41
41
  */
42
42
  export async function registerApp(requestOptions) {
43
- // privileges validation
44
- if (!arePrivilegesValid(requestOptions.privileges)) {
45
- throw new Error("The `privileges` option contains invalid privileges.");
46
- }
47
43
  // build params
48
44
  const options = appendCustomParams(requestOptions, [
49
45
  "itemId",
@@ -1 +1 @@
1
- {"version":3,"file":"registerApp.js","sourceRoot":"","sources":["../../../src/shared/registerApp.ts"],"names":[],"mappings":"AAAA;gBACgB;AAEhB,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAMxD,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,0BAA0B,EAC3B,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,cAAmC;IAEnC,wBAAwB;IACxB,IAAI,CAAC,kBAAkB,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE;QAClD,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;KACzE;IAED,eAAe;IACf,MAAM,OAAO,GAAG,kBAAkB,CAAC,cAAc,EAAE;QACjD,QAAQ;QACR,SAAS;QACT,eAAe;QACf,eAAe;QACf,YAAY;KACb,CAAC,CAAC;IACH,mHAAmH;IACnH,eAAe,CAAC,OAAO,CAAC,CAAC;IAEzB,MAAM,GAAG,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG,qBAAqB,CAAC;IAC1D,OAAO,CAAC,UAAU,GAAG,MAAM,CAAC;IAC5B,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC;IAE1B,MAAM,qBAAqB,GAA2B,MAAM,OAAO,CACjE,GAAG,EACH,OAAO,CACR,CAAC;IAEF,OAAO,0BAA0B,CAAC,qBAAqB,CAAC,CAAC;AAC3D,CAAC","sourcesContent":["/* Copyright (c) 2023 Environmental Systems Research Institute, Inc.\n * Apache-2.0 */\n\nimport { request, appendCustomParams } from \"@esri/arcgis-rest-request\";\nimport { getPortalUrl } from \"@esri/arcgis-rest-portal\";\nimport {\n IApp,\n IRegisterAppOptions,\n IRegisteredAppResponse\n} from \"./types/appType.js\";\nimport {\n arePrivilegesValid,\n stringifyArrays,\n registeredAppResponseToApp\n} from \"./helpers.js\";\n\n/**\n * Used to register an app. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/register-app.htm) for more information.\n *\n * Accepted app types:\n * - apikey\n * - multiple\n * - browser\n * - server\n * - native\n *\n * ```js\n * import { registerApp, IApp } 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 * registerApp({\n * itemId: \"xyz_itemId\",\n * appType: \"multiple\",\n * redirect_uris: [\"http://localhost:3000/\"],\n * httpReferrers: [\"http://localhost:3000/\"],\n * privileges: [Privileges.Geocode, Privileges.FeatureReport],\n * authentication: authSession\n * }).then((registeredApp: IApp) => {\n * // => {client_id: \"xyz_id\", client_secret: \"xyz_secret\", ...}\n * }).catch(e => {\n * // => an exception object\n * });\n * ```\n *\n * @param requestOptions - Options for {@linkcode registerApp | registerApp()}, including necessary params to register an app and an {@linkcode ArcGISIdentityManager} authentication session.\n * @returns A Promise that will resolve to an {@linkcode IApp} object representing the newly registered app.\n */\nexport async function registerApp(\n requestOptions: IRegisterAppOptions\n): Promise<IApp> {\n // privileges validation\n if (!arePrivilegesValid(requestOptions.privileges)) {\n throw new Error(\"The `privileges` option contains invalid privileges.\");\n }\n\n // build params\n const options = appendCustomParams(requestOptions, [\n \"itemId\",\n \"appType\",\n \"redirect_uris\",\n \"httpReferrers\",\n \"privileges\"\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/registerApp\";\n options.httpMethod = \"POST\";\n options.params.f = \"json\";\n\n const registeredAppResponse: IRegisteredAppResponse = await request(\n url,\n options\n );\n\n return registeredAppResponseToApp(registeredAppResponse);\n}\n"]}
1
+ {"version":3,"file":"registerApp.js","sourceRoot":"","sources":["../../../src/shared/registerApp.ts"],"names":[],"mappings":"AAAA;gBACgB;AAEhB,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAMxD,OAAO,EAAE,eAAe,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAE3E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,cAAmC;IAEnC,eAAe;IACf,MAAM,OAAO,GAAG,kBAAkB,CAAC,cAAc,EAAE;QACjD,QAAQ;QACR,SAAS;QACT,eAAe;QACf,eAAe;QACf,YAAY;KACb,CAAC,CAAC;IACH,mHAAmH;IACnH,eAAe,CAAC,OAAO,CAAC,CAAC;IAEzB,MAAM,GAAG,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG,qBAAqB,CAAC;IAC1D,OAAO,CAAC,UAAU,GAAG,MAAM,CAAC;IAC5B,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,MAAM,CAAC;IAE1B,MAAM,qBAAqB,GAA2B,MAAM,OAAO,CACjE,GAAG,EACH,OAAO,CACR,CAAC;IAEF,OAAO,0BAA0B,CAAC,qBAAqB,CAAC,CAAC;AAC3D,CAAC","sourcesContent":["/* Copyright (c) 2023 Environmental Systems Research Institute, Inc.\n * Apache-2.0 */\n\nimport { request, appendCustomParams } from \"@esri/arcgis-rest-request\";\nimport { getPortalUrl } from \"@esri/arcgis-rest-portal\";\nimport {\n IApp,\n IRegisterAppOptions,\n IRegisteredAppResponse\n} from \"./types/appType.js\";\nimport { stringifyArrays, registeredAppResponseToApp } from \"./helpers.js\";\n\n/**\n * Used to register an app. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/register-app.htm) for more information.\n *\n * Accepted app types:\n * - apikey\n * - multiple\n * - browser\n * - server\n * - native\n *\n * ```js\n * import { registerApp, IApp } 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 * registerApp({\n * itemId: \"xyz_itemId\",\n * appType: \"multiple\",\n * redirect_uris: [\"http://localhost:3000/\"],\n * httpReferrers: [\"http://localhost:3000/\"],\n * privileges: [\"premium:user:geocode:temporary\", Privileges.FeatureReport],\n * authentication: authSession\n * }).then((registeredApp: IApp) => {\n * // => {client_id: \"xyz_id\", client_secret: \"xyz_secret\", ...}\n * }).catch(e => {\n * // => an exception object\n * });\n * ```\n *\n * @param requestOptions - Options for {@linkcode registerApp | registerApp()}, including necessary params to register an app and an {@linkcode ArcGISIdentityManager} authentication session.\n * @returns A Promise that will resolve to an {@linkcode IApp} object representing the newly registered app.\n */\nexport async function registerApp(\n requestOptions: IRegisterAppOptions\n): Promise<IApp> {\n // build params\n const options = appendCustomParams(requestOptions, [\n \"itemId\",\n \"appType\",\n \"redirect_uris\",\n \"httpReferrers\",\n \"privileges\"\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/registerApp\";\n options.httpMethod = \"POST\";\n options.params.f = \"json\";\n\n const registeredAppResponse: IRegisteredAppResponse = await request(\n url,\n options\n );\n\n return registeredAppResponseToApp(registeredAppResponse);\n}\n"]}
@@ -1,6 +1,5 @@
1
1
  import { IAuthenticationManager, IRequestOptions, ISpatialReference } from "@esri/arcgis-rest-request";
2
2
  import { IRegisterAppOptions, IApp } from "./appType.js";
3
- import { Privileges } from "../enum/privileges.js";
4
3
  import { IItem } from "@esri/arcgis-rest-portal";
5
4
  /**
6
5
  * Options to register an API Key.
@@ -20,6 +19,22 @@ export interface ICreateApiKeyOptions extends Omit<IRegisterAppOptions, "itemId"
20
19
  properties?: any;
21
20
  url?: string;
22
21
  tags?: string[];
22
+ /**
23
+ * Expiration date of the access token in slot 1 of this API Key.
24
+ */
25
+ apiToken1ExpirationDate?: Date;
26
+ /**
27
+ * Expiration date of the access token in slot 2 of this API Key.
28
+ */
29
+ apiToken2ExpirationDate?: Date;
30
+ /**
31
+ * Generate a new access token in slot 1 of this API. Will override and invalidate any existing token.
32
+ */
33
+ generateToken1?: boolean;
34
+ /**
35
+ * Generate a new access token in slot 2 of this API. Will override and invalidate any existing token.
36
+ */
37
+ generateToken2?: boolean;
23
38
  }
24
39
  /**
25
40
  * Options to retrieve an API Key.
@@ -37,17 +52,18 @@ export interface IGetApiKeyOptions extends Omit<IRequestOptions, "params"> {
37
52
  /**
38
53
  * Represent only the API key related properties from {@linkcode IApp}.
39
54
  */
40
- export interface IApiKeyInfo extends Omit<IApp, "client_id" | "client_secret" | "redirect_uris" | "appType"> {
41
- apiKey: string;
55
+ export interface IApiKeyInfo extends Omit<IApp, "client_secret" | "redirect_uris" | "appType"> {
42
56
  }
43
57
  /**
44
- * Return value of {@linkcode createApiKey}, {@linkcode getApiKey}, {@linkcode updateApiKey} representing an API Key entity.
58
+ * Return value of {@linkcode createApiKey}, {@linkcode getApiKey}, {@linkcode updateApiKey} representing an API Key entity. `accessToken1`/`accessToken2` properties are only available when `generateToken1`/`generateToken2` are `true`.
45
59
  */
46
60
  export interface IApiKeyResponse extends IApiKeyInfo {
47
61
  /**
48
62
  * Represent item info attached to this API Key.
49
63
  */
50
64
  item: IItem;
65
+ accessToken1?: null | string;
66
+ accessToken2?: null | string;
51
67
  }
52
68
  /**
53
69
  * Options to update an API Key.
@@ -68,7 +84,23 @@ export interface IUpdateApiKeyOptions extends Omit<IRequestOptions, "params"> {
68
84
  /**
69
85
  * Override previous `privileges` if this value is provided.
70
86
  */
71
- privileges?: Array<Privileges | `${Privileges}`>;
87
+ privileges?: string[];
88
+ /**
89
+ * Expiration date of the access token in slot 1 of this API Key.
90
+ */
91
+ apiToken1ExpirationDate?: Date;
92
+ /**
93
+ * Expiration date of the access token in slot 2 of this API Key.
94
+ */
95
+ apiToken2ExpirationDate?: Date;
96
+ /**
97
+ * Generate a new access token in slot 1 of this API. Will override and invalidate any existing token.
98
+ */
99
+ generateToken1?: boolean;
100
+ /**
101
+ * Generate a new access token in slot 2 of this API. Will override and invalidate any existing token.
102
+ */
103
+ generateToken2?: boolean;
72
104
  }
73
105
  /**
74
106
  * Options to delete an API key.
@@ -93,3 +125,20 @@ export interface IDeleteApiKeyResponse {
93
125
  itemId: string;
94
126
  success: boolean;
95
127
  }
128
+ export interface IInvalidateApiKeyOptions extends Omit<IRequestOptions, "params"> {
129
+ /**
130
+ * {@linkcode IAuthenticationManager} authentication.
131
+ */
132
+ authentication: IAuthenticationManager;
133
+ /**
134
+ * itemId of the item of the API key to be revoked.
135
+ */
136
+ itemId: string;
137
+ /**
138
+ * The API key to be revoked. The full or partial API key or the slot number (1 or 2) can be provided.
139
+ */
140
+ apiKey?: string | 1 | 2;
141
+ }
142
+ export interface IInvalidateApiKeyResponse {
143
+ success: boolean;
144
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"apiKeyType.js","sourceRoot":"","sources":["../../../../src/shared/types/apiKeyType.ts"],"names":[],"mappings":"","sourcesContent":["import {\n IAuthenticationManager,\n IRequestOptions,\n ISpatialReference\n} from \"@esri/arcgis-rest-request\";\nimport { IRegisterAppOptions, IApp } from \"./appType.js\";\nimport { Privileges } from \"../enum/privileges.js\";\nimport { IItem } from \"@esri/arcgis-rest-portal\";\n\n/**\n * Options to register an API Key.\n */\nexport interface ICreateApiKeyOptions\n extends Omit<\n IRegisterAppOptions,\n \"itemId\" | \"redirect_uris\" | \"appType\" | \"httpReferrers\"\n > {\n httpReferrers?: string[];\n title: string;\n owner?: string;\n typeKeywords?: string[];\n description?: string;\n snippet?: string;\n documentation?: string;\n extent?: number[][];\n categories?: string[];\n spatialReference?: ISpatialReference;\n culture?: string;\n properties?: any;\n url?: string;\n tags?: string[];\n}\n\n/**\n * Options to retrieve an API Key.\n */\nexport interface IGetApiKeyOptions extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of which API key to be retrieved.\n */\n itemId: string;\n}\n\n/**\n * Represent only the API key related properties from {@linkcode IApp}.\n */\nexport interface IApiKeyInfo\n extends Omit<\n IApp,\n \"client_id\" | \"client_secret\" | \"redirect_uris\" | \"appType\"\n > {\n apiKey: string;\n}\n\n/**\n * Return value of {@linkcode createApiKey}, {@linkcode getApiKey}, {@linkcode updateApiKey} representing an API Key entity.\n */\nexport interface IApiKeyResponse extends IApiKeyInfo {\n /**\n * Represent item info attached to this API Key.\n */\n item: IItem;\n}\n\n/**\n * Options to update an API Key.\n */\nexport interface IUpdateApiKeyOptions extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of which API key will be updated.\n */\n itemId: string;\n /**\n * Override previous `httpReferrers` if this value is provided.\n */\n httpReferrers?: string[];\n /**\n * Override previous `privileges` if this value is provided.\n */\n privileges?: Array<Privileges | `${Privileges}`>;\n}\n\n/**\n * Options to delete an API key.\n */\nexport interface IDeleteApiKeyOption extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of which API key to be removed.\n */\n itemId: string;\n}\n\n/**\n * Response of API key deletion.\n */\nexport interface IDeleteApiKeyResponse {\n /**\n * itemId of which API key has been removed.\n */\n itemId: string;\n success: boolean;\n}\n"]}
1
+ {"version":3,"file":"apiKeyType.js","sourceRoot":"","sources":["../../../../src/shared/types/apiKeyType.ts"],"names":[],"mappings":"","sourcesContent":["import {\n IAuthenticationManager,\n IRequestOptions,\n ISpatialReference\n} from \"@esri/arcgis-rest-request\";\nimport { IRegisterAppOptions, IApp } from \"./appType.js\";\nimport { Privileges } from \"../enum/privileges.js\";\nimport { IItem } from \"@esri/arcgis-rest-portal\";\n\n/**\n * Options to register an API Key.\n */\nexport interface ICreateApiKeyOptions\n extends Omit<\n IRegisterAppOptions,\n \"itemId\" | \"redirect_uris\" | \"appType\" | \"httpReferrers\"\n > {\n httpReferrers?: string[];\n title: string;\n owner?: string;\n typeKeywords?: string[];\n description?: string;\n snippet?: string;\n documentation?: string;\n extent?: number[][];\n categories?: string[];\n spatialReference?: ISpatialReference;\n culture?: string;\n properties?: any;\n url?: string;\n tags?: string[];\n /**\n * Expiration date of the access token in slot 1 of this API Key.\n */\n apiToken1ExpirationDate?: Date;\n /**\n * Expiration date of the access token in slot 2 of this API Key.\n */\n apiToken2ExpirationDate?: Date;\n /**\n * Generate a new access token in slot 1 of this API. Will override and invalidate any existing token.\n */\n generateToken1?: boolean;\n /**\n * Generate a new access token in slot 2 of this API. Will override and invalidate any existing token.\n */\n generateToken2?: boolean;\n}\n\n/**\n * Options to retrieve an API Key.\n */\nexport interface IGetApiKeyOptions extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of which API key to be retrieved.\n */\n itemId: string;\n}\n\n/**\n * Represent only the API key related properties from {@linkcode IApp}.\n */\nexport interface IApiKeyInfo\n extends Omit<IApp, \"client_secret\" | \"redirect_uris\" | \"appType\"> {}\n\n/**\n * Return value of {@linkcode createApiKey}, {@linkcode getApiKey}, {@linkcode updateApiKey} representing an API Key entity. `accessToken1`/`accessToken2` properties are only available when `generateToken1`/`generateToken2` are `true`.\n */\nexport interface IApiKeyResponse extends IApiKeyInfo {\n /**\n * Represent item info attached to this API Key.\n */\n item: IItem;\n accessToken1?: null | string;\n accessToken2?: null | string;\n}\n\n/**\n * Options to update an API Key.\n */\nexport interface IUpdateApiKeyOptions extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of which API key will be updated.\n */\n itemId: string;\n /**\n * Override previous `httpReferrers` if this value is provided.\n */\n httpReferrers?: string[];\n /**\n * Override previous `privileges` if this value is provided.\n */\n privileges?: string[];\n /**\n * Expiration date of the access token in slot 1 of this API Key.\n */\n apiToken1ExpirationDate?: Date;\n /**\n * Expiration date of the access token in slot 2 of this API Key.\n */\n apiToken2ExpirationDate?: Date;\n /**\n * Generate a new access token in slot 1 of this API. Will override and invalidate any existing token.\n */\n generateToken1?: boolean;\n /**\n * Generate a new access token in slot 2 of this API. Will override and invalidate any existing token.\n */\n generateToken2?: boolean;\n}\n\n/**\n * Options to delete an API key.\n */\nexport interface IDeleteApiKeyOption extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of which API key to be removed.\n */\n itemId: string;\n}\n\n/**\n * Response of API key deletion.\n */\nexport interface IDeleteApiKeyResponse {\n /**\n * itemId of which API key has been removed.\n */\n itemId: string;\n success: boolean;\n}\n\nexport interface IInvalidateApiKeyOptions\n extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of the item of the API key to be revoked.\n */\n itemId: string;\n /**\n * The API key to be revoked. The full or partial API key or the slot number (1 or 2) can be provided.\n */\n apiKey?: string | 1 | 2;\n}\n\nexport interface IInvalidateApiKeyResponse {\n success: boolean;\n}\n"]}
@@ -28,7 +28,7 @@ export interface IRegisterAppOptions extends Omit<IRequestOptions, "params"> {
28
28
  /**
29
29
  * Privilege lists. For more info, refer to {@linkcode Privileges}.
30
30
  */
31
- privileges: Array<Privileges | `${Privileges}`>;
31
+ privileges: Privileges;
32
32
  /**
33
33
  * {@linkcode IAuthenticationManager} authentication.
34
34
  */
@@ -61,18 +61,22 @@ export interface IRegisteredAppResponse {
61
61
  client_secret: string;
62
62
  httpReferrers: string[];
63
63
  redirect_uris: string[];
64
- privileges: Array<Privileges | `${Privileges}`>;
64
+ privileges: string[];
65
65
  registered: UnixTime;
66
66
  modified: UnixTime;
67
67
  apnsProdCert: any;
68
68
  apnsSandboxCert: any;
69
69
  gcmApiKey: any;
70
70
  isBeta: boolean;
71
+ customAppLoginShowTriage: boolean;
72
+ isPersonalAPIToken: boolean;
73
+ apiToken1Active: boolean;
74
+ apiToken2Active: boolean;
71
75
  }
72
76
  /**
73
77
  * Return value of {@linkcode registerApp} and {@linkcode getRegisteredAppInfo} representing an app entity.
74
78
  */
75
- export interface IApp extends Omit<IRegisteredAppResponse, "registered" | "modified" | "apnsProdCert" | "apnsSandboxCert" | "gcmApiKey" | "isBeta"> {
79
+ export interface IApp extends Omit<IRegisteredAppResponse, "registered" | "modified" | "apnsProdCert" | "apnsSandboxCert" | "gcmApiKey" | "isBeta" | "customAppLoginShowTriage"> {
76
80
  registered: Date;
77
81
  modified: Date;
78
82
  }
@@ -1 +1 @@
1
- {"version":3,"file":"appType.js","sourceRoot":"","sources":["../../../../src/shared/types/appType.ts"],"names":[],"mappings":"","sourcesContent":["import {\n IRequestOptions,\n IAuthenticationManager\n} from \"@esri/arcgis-rest-request\";\nimport { UnixTime } from \"@esri/arcgis-rest-portal\";\nimport { Privileges } from \"../enum/privileges.js\";\n\n/**\n * Accepted app types.\n */\nexport type AppType = \"apikey\" | \"browser\" | \"native\" | \"server\" | \"multiple\";\n\n/**\n * Options to register an app.\n */\nexport interface IRegisterAppOptions extends Omit<IRequestOptions, \"params\"> {\n /**\n * itemId that the newly registered app will be based on.\n */\n itemId: string;\n /**\n * App types. For more info, refer to {@linkcode AppType}.\n */\n appType: AppType;\n /**\n * Redirect URIs set for this app.\n */\n redirect_uris: string[];\n /**\n * Http Referrers set for this app.\n */\n httpReferrers: string[];\n /**\n * Privilege lists. For more info, refer to {@linkcode Privileges}.\n */\n privileges: Array<Privileges | `${Privileges}`>;\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n}\n\n/**\n * Options to retrieve an app.\n */\nexport interface IGetAppInfoOptions extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of which app to be retrieved.\n */\n itemId: string;\n}\n\n/**\n * Raw response of app related endpoints calls.\n */\nexport interface IRegisteredAppResponse {\n itemId: string;\n /**\n * Only to be present if appType is apikey\n */\n apiKey?: string;\n appType: AppType;\n client_id: string;\n client_secret: string;\n httpReferrers: string[];\n redirect_uris: string[];\n privileges: Array<Privileges | `${Privileges}`>;\n registered: UnixTime;\n modified: UnixTime;\n apnsProdCert: any;\n apnsSandboxCert: any;\n gcmApiKey: any;\n isBeta: boolean;\n}\n\n/**\n * Return value of {@linkcode registerApp} and {@linkcode getRegisteredAppInfo} representing an app entity.\n */\nexport interface IApp\n extends Omit<\n IRegisteredAppResponse,\n | \"registered\"\n | \"modified\"\n | \"apnsProdCert\"\n | \"apnsSandboxCert\"\n | \"gcmApiKey\"\n | \"isBeta\"\n > {\n registered: Date;\n modified: Date;\n}\n\n/**\n * Options to unregister an app.\n */\nexport interface IUnregisterAppOptions extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of which app to be unregistered.\n */\n itemId: string;\n}\n\n/**\n * Response of app un-registration.\n */\nexport interface IUnregisterAppResponse {\n success: true | false;\n /**\n * itemId of which app has been unregistered.\n */\n itemId: string;\n}\n"]}
1
+ {"version":3,"file":"appType.js","sourceRoot":"","sources":["../../../../src/shared/types/appType.ts"],"names":[],"mappings":"","sourcesContent":["import {\n IRequestOptions,\n IAuthenticationManager\n} from \"@esri/arcgis-rest-request\";\nimport { UnixTime } from \"@esri/arcgis-rest-portal\";\nimport { Privileges } from \"../enum/privileges.js\";\n\n/**\n * Accepted app types.\n */\nexport type AppType = \"apikey\" | \"browser\" | \"native\" | \"server\" | \"multiple\";\n\n/**\n * Options to register an app.\n */\nexport interface IRegisterAppOptions extends Omit<IRequestOptions, \"params\"> {\n /**\n * itemId that the newly registered app will be based on.\n */\n itemId: string;\n /**\n * App types. For more info, refer to {@linkcode AppType}.\n */\n appType: AppType;\n /**\n * Redirect URIs set for this app.\n */\n redirect_uris: string[];\n /**\n * Http Referrers set for this app.\n */\n httpReferrers: string[];\n /**\n * Privilege lists. For more info, refer to {@linkcode Privileges}.\n */\n privileges: Privileges;\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n}\n\n/**\n * Options to retrieve an app.\n */\nexport interface IGetAppInfoOptions extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of which app to be retrieved.\n */\n itemId: string;\n}\n\n/**\n * Raw response of app related endpoints calls.\n */\nexport interface IRegisteredAppResponse {\n itemId: string;\n /**\n * Only to be present if appType is apikey\n */\n apiKey?: string;\n appType: AppType;\n client_id: string;\n client_secret: string;\n httpReferrers: string[];\n redirect_uris: string[];\n privileges: string[];\n registered: UnixTime;\n modified: UnixTime;\n apnsProdCert: any;\n apnsSandboxCert: any;\n gcmApiKey: any;\n isBeta: boolean;\n customAppLoginShowTriage: boolean;\n isPersonalAPIToken: boolean;\n apiToken1Active: boolean;\n apiToken2Active: boolean;\n}\n\n/**\n * Return value of {@linkcode registerApp} and {@linkcode getRegisteredAppInfo} representing an app entity.\n */\nexport interface IApp\n extends Omit<\n IRegisteredAppResponse,\n | \"registered\"\n | \"modified\"\n | \"apnsProdCert\"\n | \"apnsSandboxCert\"\n | \"gcmApiKey\"\n | \"isBeta\"\n | \"customAppLoginShowTriage\"\n > {\n registered: Date;\n modified: Date;\n}\n\n/**\n * Options to unregister an app.\n */\nexport interface IUnregisterAppOptions extends Omit<IRequestOptions, \"params\"> {\n /**\n * {@linkcode IAuthenticationManager} authentication.\n */\n authentication: IAuthenticationManager;\n /**\n * itemId of which app to be unregistered.\n */\n itemId: string;\n}\n\n/**\n * Response of app un-registration.\n */\nexport interface IUnregisterAppResponse {\n success: true | false;\n /**\n * itemId of which app has been unregistered.\n */\n itemId: string;\n}\n"]}
@@ -15,13 +15,19 @@ import { IApiKeyResponse, IUpdateApiKeyOptions } from "./shared/types/apiKeyType
15
15
  * password: "xyz_pw"
16
16
  * });
17
17
  *
18
+ * const threeDaysFromToday = new Date();
19
+ * threeDaysFromToday.setDate(threeDaysFromToday.getDate() + 3);
20
+ * threeDaysFromToday.setHours(23, 59, 59, 999);
21
+ *
18
22
  * updateApiKey({
19
23
  * itemId: "xyz_itemId",
20
- * privileges: [Privileges.Geocode],
24
+ * privileges: ["premium:user:geocode:temporary"],
21
25
  * httpReferrers: [], // httpReferrers will be set to be empty
22
26
  * authentication: authSession
27
+ * generateToken1: true, // optional,generate a new token
28
+ * apiToken1ExpirationDate: threeDaysFromToday // optional, update expiration date
23
29
  * }).then((updatedAPIKey: IApiKeyResponse) => {
24
- * // => {apiKey: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
30
+ * // => {accessToken1: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
25
31
  * }).catch(e => {
26
32
  * // => an exception object
27
33
  * });
@@ -1,8 +1,8 @@
1
1
  /* Copyright (c) 2023 Environmental Systems Research Institute, Inc.
2
2
  * Apache-2.0 */
3
3
  import { getRegisteredAppInfo } from "./shared/getRegisteredAppInfo.js";
4
- import { appToApiKeyProperties, extractBaseRequestOptions, arePrivilegesValid, stringifyArrays, registeredAppResponseToApp } from "./shared/helpers.js";
5
- import { getItem, getPortalUrl } from "@esri/arcgis-rest-portal";
4
+ import { appToApiKeyProperties, extractBaseRequestOptions, stringifyArrays, generateApiKeyTokens, generateOptionsToSlots, buildExpirationDateParams } from "./shared/helpers.js";
5
+ import { getItem, getPortalUrl, updateItem } from "@esri/arcgis-rest-portal";
6
6
  import { appendCustomParams, request } from "@esri/arcgis-rest-request";
7
7
  /**
8
8
  * Used to update an API key.
@@ -20,13 +20,19 @@ import { appendCustomParams, request } from "@esri/arcgis-rest-request";
20
20
  * password: "xyz_pw"
21
21
  * });
22
22
  *
23
+ * const threeDaysFromToday = new Date();
24
+ * threeDaysFromToday.setDate(threeDaysFromToday.getDate() + 3);
25
+ * threeDaysFromToday.setHours(23, 59, 59, 999);
26
+ *
23
27
  * updateApiKey({
24
28
  * itemId: "xyz_itemId",
25
- * privileges: [Privileges.Geocode],
29
+ * privileges: ["premium:user:geocode:temporary"],
26
30
  * httpReferrers: [], // httpReferrers will be set to be empty
27
31
  * authentication: authSession
32
+ * generateToken1: true, // optional,generate a new token
33
+ * apiToken1ExpirationDate: threeDaysFromToday // optional, update expiration date
28
34
  * }).then((updatedAPIKey: IApiKeyResponse) => {
29
- * // => {apiKey: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
35
+ * // => {accessToken1: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
30
36
  * }).catch(e => {
31
37
  * // => an exception object
32
38
  * });
@@ -36,32 +42,45 @@ import { appendCustomParams, request } from "@esri/arcgis-rest-request";
36
42
  * @returns A Promise that will resolve to an {@linkcode IApiKeyResponse} object representing updated API key.
37
43
  */
38
44
  export async function updateApiKey(requestOptions) {
39
- // privileges validation
40
- if (requestOptions.privileges &&
41
- !arePrivilegesValid(requestOptions.privileges)) {
42
- throw new Error("The `privileges` option contains invalid privileges.");
43
- }
44
45
  requestOptions.httpMethod = "POST";
45
- // get app
46
46
  const baseRequestOptions = extractBaseRequestOptions(requestOptions); // get base requestOptions snapshot
47
- const getAppOption = Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication, itemId: requestOptions.itemId });
48
- const appResponse = await getRegisteredAppInfo(getAppOption);
49
- // appType must be APIKey to continue
50
- if (appResponse.appType !== "apikey" || !("apiKey" in appResponse)) {
51
- throw new Error("Item is not an API key.");
47
+ /**
48
+ * step 1: update expiration dates if provided. Build the object up to avoid overwriting any existing properties.
49
+ */
50
+ if (requestOptions.apiToken1ExpirationDate ||
51
+ requestOptions.apiToken2ExpirationDate) {
52
+ const updateParams = buildExpirationDateParams(requestOptions);
53
+ await updateItem(Object.assign(Object.assign({}, baseRequestOptions), { item: Object.assign({ id: requestOptions.itemId }, updateParams), authentication: requestOptions.authentication }));
54
+ }
55
+ /**
56
+ * step 2: update privileges and httpReferrers if provided. Build the object up to avoid overwriting any existing properties.
57
+ */
58
+ if (requestOptions.privileges || requestOptions.httpReferrers) {
59
+ const getAppOption = Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication, itemId: requestOptions.itemId });
60
+ const appResponse = await getRegisteredAppInfo(getAppOption);
61
+ const clientId = appResponse.client_id;
62
+ const options = appendCustomParams(Object.assign(Object.assign({}, appResponse), requestOptions), // object with the custom params to look in
63
+ ["privileges", "httpReferrers"] // keys you want copied to the params object
64
+ );
65
+ options.params.f = "json";
66
+ // encode special params value (e.g. array type...) in advance in order to make encodeQueryString() works correctly
67
+ stringifyArrays(options);
68
+ const url = getPortalUrl(options) + `/oauth2/apps/${clientId}/update`;
69
+ // Raw response from `/oauth2/apps/${clientId}/update`, apiKey not included because key is same.
70
+ const updateResponse = await request(url, Object.assign(Object.assign({}, options), { authentication: requestOptions.authentication }));
52
71
  }
53
- const clientId = appResponse.client_id;
54
- const options = appendCustomParams(Object.assign(Object.assign({}, appResponse), requestOptions), // object with the custom params to look in
55
- ["privileges", "httpReferrers"] // keys you want copied to the params object
56
- );
57
- options.params.f = "json";
58
- // encode special params value (e.g. array type...) in advance in order to make encodeQueryString() works correctly
59
- stringifyArrays(options);
60
- const url = getPortalUrl(options) + `/oauth2/apps/${clientId}/update`;
61
- // Raw response from `/oauth2/apps/${clientId}/update`, apiKey not included because key is same.
62
- const updateResponse = await request(url, Object.assign(Object.assign({}, options), { authentication: requestOptions.authentication }));
63
- const app = registeredAppResponseToApp(Object.assign(Object.assign({}, updateResponse), { apiKey: appResponse.apiKey }));
64
- const itemInfo = await getItem(requestOptions.itemId, Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication, params: { f: "json" } }));
65
- return Object.assign(Object.assign({}, appToApiKeyProperties(app)), { item: itemInfo });
72
+ /**
73
+ * step 3: get the updated item info to return to the user.
74
+ */
75
+ const updatedItemInfo = await getItem(requestOptions.itemId, Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication, params: { f: "json" } }));
76
+ /**
77
+ * step 4: generate tokens if requested
78
+ */
79
+ const generatedTokens = await generateApiKeyTokens(requestOptions.itemId, generateOptionsToSlots(requestOptions.generateToken1, requestOptions.generateToken2), Object.assign(Object.assign({}, baseRequestOptions), { authentication: requestOptions.authentication }));
80
+ /**
81
+ * step 5: get updated registered app info
82
+ */
83
+ const updatedRegisteredAppResponse = await getRegisteredAppInfo(Object.assign(Object.assign({}, baseRequestOptions), { itemId: requestOptions.itemId, authentication: requestOptions.authentication }));
84
+ return Object.assign(Object.assign(Object.assign({}, generatedTokens), appToApiKeyProperties(updatedRegisteredAppResponse)), { item: updatedItemInfo });
66
85
  }
67
86
  //# sourceMappingURL=updateApiKey.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"updateApiKey.js","sourceRoot":"","sources":["../../src/updateApiKey.ts"],"names":[],"mappings":"AAAA;gBACgB;AAMhB,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AACxE,OAAO,EACL,qBAAqB,EACrB,yBAAyB,EACzB,kBAAkB,EAClB,eAAe,EACf,0BAA0B,EAC3B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AAOxE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,cAAoC;IAEpC,wBAAwB;IACxB,IACE,cAAc,CAAC,UAAU;QACzB,CAAC,kBAAkB,CAAC,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,yBAAyB,CAAC,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,oBAAoB,CAAC,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,kBAAkB,iCAC3B,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,eAAe,CAAC,OAAO,CAAC,CAAC;IAEzB,MAAM,GAAG,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG,gBAAgB,QAAQ,SAAS,CAAC;IAEtE,gGAAgG;IAChG,MAAM,cAAc,GAA2B,MAAM,OAAO,CAAC,GAAG,kCAC3D,OAAO,KACV,cAAc,EAAE,cAAc,CAAC,cAAc,IAC7C,CAAC;IAEH,MAAM,GAAG,GAAS,0BAA0B,iCACvC,cAAc,KACjB,MAAM,EAAE,WAAW,CAAC,MAAM,IAC1B,CAAC;IAEH,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,cAAc,CAAC,MAAM,kCAC/C,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,EAC7C,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,IACrB,CAAC;IAEH,uCACK,qBAAqB,CAAC,GAAG,CAAC,KAC7B,IAAI,EAAE,QAAQ,IACd;AACJ,CAAC","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,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AACxE,OAAO,EACL,qBAAqB,EACrB,yBAAyB,EACzB,eAAe,EAEf,oBAAoB,EACpB,sBAAsB,EACtB,yBAAyB,EAC1B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAC7E,OAAO,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AAOxE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,cAAoC;IAEpC,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC;IACnC,MAAM,kBAAkB,GAAG,yBAAyB,CAAC,cAAc,CAAC,CAAC,CAAC,mCAAmC;IAEzG;;OAEG;IACH,IACE,cAAc,CAAC,uBAAuB;QACtC,cAAc,CAAC,uBAAuB,EACtC;QACA,MAAM,YAAY,GAAG,yBAAyB,CAAC,cAAc,CAAC,CAAC;QAC/D,M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,kCAEI,kBAAkB,KACrB,cAAc,EAAE,cAAc,CAAC,cAAc,IAEhD,CAAC;IAEF;;OAEG;IACH,MAAM,4BAA4B,GAAG,MAAM,oBAAoB,iCAC1D,kBAAkB,KACrB,MAAM,EAAE,cAAc,CAAC,MAAM,EAC7B,cAAc,EAAE,cAAc,CAAC,cAAc,IAC7C,CAAC;IAEH,qDACK,eAAe,GACf,qBAAqB,CAAC,4BAA4B,CAAC,KACtD,IAAI,EAAE,eAAe,IACrB;AACJ,CAAC","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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@esri/arcgis-rest-developer-credentials",
3
- "version": "1.1.0",
3
+ "version": "2.1.0",
4
4
  "description": "Developer Credentials for @esri/arcgis-rest-js",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -1,43 +0,0 @@
1
- "use strict";
2
- /* Copyright (c) 2023 Environmental Systems Research Institute, Inc.
3
- * Apache-2.0 */
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.deleteApiKey = void 0;
6
- const arcgis_rest_portal_1 = require("@esri/arcgis-rest-portal");
7
- const helpers_js_1 = require("./shared/helpers.js");
8
- const getApiKey_js_1 = require("./getApiKey.js");
9
- /**
10
- * Used to delete the API Key with given `itemId`.
11
- *
12
- * ```js
13
- * import { deleteApiKey, IDeleteApiKeyResponse } from '@esri/arcgis-rest-developer-credentials';
14
- * import { ArcGISIdentityManager } from "@esri/arcgis-rest-request";
15
- *
16
- * const authSession: ArcGISIdentityManager = await ArcGISIdentityManager.signIn({
17
- * username: "xyz_usrName",
18
- * password: "xyz_pw"
19
- * });
20
- *
21
- * deleteApiKey({
22
- * itemId: "xyz_itemId",
23
- * authentication: authSession
24
- * }).then((deletedApiKey: IDeleteApiKeyResponse) => {
25
- * // => {itemId: "xyz_itemId", success: true}
26
- * }).catch(e => {
27
- * // => an exception object
28
- * });
29
- * ```
30
- *
31
- * @param requestOptions - Options for {@linkcode deleteApiKey | deleteApiKey()}, including `itemId` of which API key to be deleted and an {@linkcode ArcGISIdentityManager} authentication session.
32
- * @returns A Promise that will resolve to an {@linkcode IDeleteApiKeyResponse} object representing deletion status.
33
- */
34
- async function deleteApiKey(requestOptions) {
35
- requestOptions.httpMethod = "POST";
36
- const baseRequestOptions = (0, helpers_js_1.extractBaseRequestOptions)(requestOptions);
37
- // validate provided itemId associates with API Key
38
- await (0, getApiKey_js_1.getApiKey)(Object.assign(Object.assign({}, baseRequestOptions), { itemId: requestOptions.itemId, authentication: requestOptions.authentication }));
39
- const removeItemResponse = await (0, arcgis_rest_portal_1.removeItem)(Object.assign(Object.assign({}, baseRequestOptions), { id: requestOptions.itemId, authentication: requestOptions.authentication, params: { f: "json" } }));
40
- return removeItemResponse;
41
- }
42
- exports.deleteApiKey = deleteApiKey;
43
- //# sourceMappingURL=deleteApiKey.js.map