5
5
ICreateItemOptions ,
6
6
createItem ,
7
7
getItem ,
8
- IItemAdd
8
+ IItemAdd ,
9
+ updateItem
9
10
} from "@esri/arcgis-rest-portal" ;
10
11
import {
11
12
IApiKeyResponse ,
@@ -18,8 +19,11 @@ import {
18
19
appToApiKeyProperties ,
19
20
filterKeys ,
20
21
extractBaseRequestOptions ,
21
- arePrivilegesValid
22
+ generateApiKeyTokens ,
23
+ generateOptionsToSlots ,
24
+ buildExpirationDateParams
22
25
} from "./shared/helpers.js" ;
26
+ import { getRegisteredAppInfo } from "./shared/getRegisteredAppInfo.js" ;
23
27
24
28
/**
25
29
* 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.
@@ -37,7 +41,7 @@ import {
37
41
* title: "xyz_title",
38
42
* description: "xyz_desc",
39
43
* tags: ["xyz_tag1", "xyz_tag2"],
40
- * privileges: [Privileges.Geocode, Privileges.FeatureReport ],
44
+ * privileges: ["premium:user:networkanalysis:routing" ],
41
45
* authentication: authSession
42
46
* }).then((registeredAPIKey: IApiKeyResponse) => {
43
47
* // => {apiKey: "xyz_key", item: {tags: ["xyz_tag1", "xyz_tag2"], ...}, ...}
@@ -52,14 +56,9 @@ import {
52
56
export async function createApiKey (
53
57
requestOptions : ICreateApiKeyOptions
54
58
) : Promise < IApiKeyResponse > {
55
- if ( ! arePrivilegesValid ( requestOptions . privileges ) ) {
56
- throw new Error ( "The `privileges` option contains invalid privileges." ) ;
57
- }
58
-
59
59
requestOptions . httpMethod = "POST" ;
60
60
61
61
// filter param buckets:
62
-
63
62
const baseRequestOptions = extractBaseRequestOptions ( requestOptions ) ; // snapshot of basic IRequestOptions before customized params being built into it
64
63
65
64
const itemAddProperties : Array < keyof IItemAdd > = [
@@ -79,11 +78,13 @@ export async function createApiKey(
79
78
"url"
80
79
] ;
81
80
82
- // step 1: add item
81
+ /**
82
+ * step 1: create item
83
+ */
83
84
const createItemOption : ICreateItemOptions = {
84
85
item : {
85
86
...filterKeys ( requestOptions as any , itemAddProperties ) ,
86
- type : "API Key "
87
+ type : "Application "
87
88
} ,
88
89
...baseRequestOptions ,
89
90
authentication : requestOptions . authentication ,
@@ -94,26 +95,71 @@ export async function createApiKey(
94
95
95
96
const createItemResponse = await createItem ( createItemOption ) ;
96
97
97
- // step 2: register app
98
- const registerAppOption : IRegisterAppOptions = {
98
+ /**
99
+ * getRegisteredAppInfoRoute
100
+ */
101
+ const registerAppOptions : IRegisterAppOptions = {
99
102
itemId : createItemResponse . id ,
100
- appType : "apikey " ,
101
- redirect_uris : [ ] ,
103
+ appType : "multiple " ,
104
+ redirect_uris : [ "urn:ietf:wg:oauth:2.0:oob" ] ,
102
105
httpReferrers : requestOptions . httpReferrers || [ ] ,
103
106
privileges : requestOptions . privileges ,
104
107
...baseRequestOptions ,
105
108
authentication : requestOptions . authentication
106
109
} ;
107
110
108
- const registeredAppResponse = await registerApp ( registerAppOption ) ;
111
+ const registeredAppResponse = await registerApp ( registerAppOptions ) ;
112
+
113
+ /**
114
+ * step 3: update item with desired expiration dates
115
+ * you cannot set the expiration date propierties until you
116
+ * regiester the app so this has to be a seperate step
117
+ */
118
+ await updateItem ( {
119
+ ...baseRequestOptions ,
120
+ item : {
121
+ id : createItemResponse . id ,
122
+ ...buildExpirationDateParams ( requestOptions , true )
123
+ } ,
124
+ authentication : requestOptions . authentication
125
+ } ) ;
126
+
127
+ /*
128
+ * step 4: get item info
129
+ */
109
130
const itemInfo = await getItem ( registeredAppResponse . itemId , {
110
131
...baseRequestOptions ,
111
132
authentication : requestOptions . authentication ,
112
133
params : { f : "json" }
113
134
} ) ;
114
135
136
+ /**
137
+ * step 5: generate tokens if requested
138
+ */
139
+ const generatedTokens = await generateApiKeyTokens (
140
+ itemInfo . id ,
141
+ generateOptionsToSlots (
142
+ requestOptions . generateToken1 ,
143
+ requestOptions . generateToken2
144
+ ) ,
145
+ {
146
+ ...baseRequestOptions ,
147
+ authentication : requestOptions . authentication
148
+ }
149
+ ) ;
150
+
151
+ /**
152
+ * step 6: get registered app info to get updated active key status
153
+ */
154
+ const updatedRegisteredAppResponse = await getRegisteredAppInfo ( {
155
+ ...baseRequestOptions ,
156
+ itemId : itemInfo . id ,
157
+ authentication : requestOptions . authentication
158
+ } ) ;
159
+
115
160
return {
116
- ...appToApiKeyProperties ( registeredAppResponse ) ,
161
+ ...generatedTokens ,
162
+ ...appToApiKeyProperties ( updatedRegisteredAppResponse ) ,
117
163
item : itemInfo
118
164
} ;
119
165
}
0 commit comments