@thirdweb-dev/service-utils 0.0.0-dev-786a4b3-20230729072030 → 0.0.0-dev-8e57e1d-20230731190300

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 (26) hide show
  1. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.dev.js +13 -1
  2. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.prod.js +13 -1
  3. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.esm.js +13 -1
  4. package/dist/declarations/src/cf-worker/index.d.ts +4 -4
  5. package/dist/declarations/src/cf-worker/index.d.ts.map +1 -1
  6. package/dist/declarations/src/core/api.d.ts +16 -1
  7. package/dist/declarations/src/core/api.d.ts.map +1 -1
  8. package/dist/declarations/src/core/authorize/index.d.ts +5 -3
  9. package/dist/declarations/src/core/authorize/index.d.ts.map +1 -1
  10. package/dist/declarations/src/core/authorize/types.d.ts +2 -1
  11. package/dist/declarations/src/core/authorize/types.d.ts.map +1 -1
  12. package/dist/declarations/src/index.d.ts +1 -1
  13. package/dist/declarations/src/index.d.ts.map +1 -1
  14. package/dist/declarations/src/node/index.d.ts +5 -5
  15. package/dist/declarations/src/node/index.d.ts.map +1 -1
  16. package/dist/{index-4496b57e.cjs.dev.js → index-794f8b68.cjs.prod.js} +144 -9
  17. package/dist/{index-d752cff8.esm.js → index-80604116.esm.js} +144 -9
  18. package/dist/{index-7c6b47cd.cjs.prod.js → index-ca7a4739.cjs.dev.js} +144 -9
  19. package/node/dist/thirdweb-dev-service-utils-node.cjs.dev.js +12 -1
  20. package/node/dist/thirdweb-dev-service-utils-node.cjs.prod.js +12 -1
  21. package/node/dist/thirdweb-dev-service-utils-node.esm.js +12 -1
  22. package/package.json +3 -3
  23. package/dist/declarations/src/core/authorize/client.d.ts +0 -9
  24. package/dist/declarations/src/core/authorize/client.d.ts.map +0 -1
  25. package/dist/declarations/src/core/authorize/service.d.ts +0 -7
  26. package/dist/declarations/src/core/authorize/service.d.ts.map +0 -1
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('../../dist/index-4496b57e.cjs.dev.js');
5
+ var index = require('../../dist/index-ca7a4739.cjs.dev.js');
6
6
  var services = require('../../dist/services-a3f36057.cjs.dev.js');
7
7
 
8
8
  const DEFAULT_CACHE_TTL_SECONDS = 60;
@@ -88,7 +88,19 @@ async function extractAuthorizationData(authInput) {
88
88
  // otherwise set the client id to the derived client id (client id based off of secret key)
89
89
  clientId = derivedClientId;
90
90
  }
91
+ let jwt = null;
92
+ if (headers.has("authorization")) {
93
+ const authHeader = headers.get("authorization");
94
+ if (authHeader) {
95
+ const [type, token] = authHeader.split(" ");
96
+ if (type.toLowerCase() === "bearer") {
97
+ jwt = token;
98
+ }
99
+ }
100
+ }
91
101
  return {
102
+ jwt,
103
+ hashedJWT: jwt ? await hashSecretKey(jwt) : null,
92
104
  secretKey,
93
105
  clientId,
94
106
  origin,
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('../../dist/index-7c6b47cd.cjs.prod.js');
5
+ var index = require('../../dist/index-794f8b68.cjs.prod.js');
6
6
  var services = require('../../dist/services-9e185105.cjs.prod.js');
7
7
 
8
8
  const DEFAULT_CACHE_TTL_SECONDS = 60;
@@ -88,7 +88,19 @@ async function extractAuthorizationData(authInput) {
88
88
  // otherwise set the client id to the derived client id (client id based off of secret key)
89
89
  clientId = derivedClientId;
90
90
  }
91
+ let jwt = null;
92
+ if (headers.has("authorization")) {
93
+ const authHeader = headers.get("authorization");
94
+ if (authHeader) {
95
+ const [type, token] = authHeader.split(" ");
96
+ if (type.toLowerCase() === "bearer") {
97
+ jwt = token;
98
+ }
99
+ }
100
+ }
91
101
  return {
102
+ jwt,
103
+ hashedJWT: jwt ? await hashSecretKey(jwt) : null,
92
104
  secretKey,
93
105
  clientId,
94
106
  origin,
@@ -1,4 +1,4 @@
1
- import { a as authorize } from '../../dist/index-d752cff8.esm.js';
1
+ import { a as authorize } from '../../dist/index-80604116.esm.js';
2
2
  export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from '../../dist/services-86283509.esm.js';
3
3
 
4
4
  const DEFAULT_CACHE_TTL_SECONDS = 60;
@@ -84,7 +84,19 @@ async function extractAuthorizationData(authInput) {
84
84
  // otherwise set the client id to the derived client id (client id based off of secret key)
85
85
  clientId = derivedClientId;
86
86
  }
87
+ let jwt = null;
88
+ if (headers.has("authorization")) {
89
+ const authHeader = headers.get("authorization");
90
+ if (authHeader) {
91
+ const [type, token] = authHeader.split(" ");
92
+ if (type.toLowerCase() === "bearer") {
93
+ jwt = token;
94
+ }
95
+ }
96
+ }
87
97
  return {
98
+ jwt,
99
+ hashedJWT: jwt ? await hashSecretKey(jwt) : null,
88
100
  secretKey,
89
101
  clientId,
90
102
  origin,
@@ -1,9 +1,9 @@
1
1
  import type { ExecutionContext, KVNamespace } from "@cloudflare/workers-types";
2
- import type { CoreServiceConfig } from "../core/api";
2
+ import type { CoreServiceConfig } from "../core/api.js";
3
3
  import type { Request } from "@cloudflare/workers-types";
4
- import type { AuthorizationResult } from "../core/authorize/types";
5
- import type { CoreAuthInput } from "../core/types";
6
- export * from "../core/services";
4
+ import type { AuthorizationResult } from "../core/authorize/types.js";
5
+ import type { CoreAuthInput } from "../core/types.js";
6
+ export * from "../core/services.js";
7
7
  type WorkerServiceConfig = CoreServiceConfig & {
8
8
  kvStore: KVNamespace;
9
9
  ctx: ExecutionContext;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../../src/cf-worker","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC/E,OAAO,KAAK,EAAkB,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGrE,OAAO,KAAK,EA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BAAsB,eAAe,CACnC,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,mBAAmB,GACjC,OAAO,CAAC,mBAAmB,CAAC,CA0C9B;AAiFD,wBAAsB,aAAa,CAAC,SAAS,EAAE,MAAM,mBAIpD;AAED,wBAAgB,+BAA+B,CAAC,aAAa,EAAE,MAAM,UAEpE"}
@@ -1,4 +1,4 @@
1
- import type { ServiceName } from "./services";
1
+ import type { ServiceName } from "./services.js";
2
2
  export type CoreServiceConfig = {
3
3
  enforceAuth: boolean;
4
4
  apiUrl: string;
@@ -9,6 +9,7 @@ export type CoreServiceConfig = {
9
9
  export type ApiKeyMetadata = {
10
10
  id: string;
11
11
  key: string;
12
+ accountId: string;
12
13
  creatorWalletAddress: string;
13
14
  secretHash: string;
14
15
  walletAddresses: string[];
@@ -20,6 +21,11 @@ export type ApiKeyMetadata = {
20
21
  actions: string[];
21
22
  }[];
22
23
  };
24
+ export type AccountMetadata = {
25
+ id: string;
26
+ name: string;
27
+ creatorWalletAddress: string;
28
+ };
23
29
  export type ApiResponse = {
24
30
  data: ApiKeyMetadata | null;
25
31
  error: {
@@ -28,5 +34,14 @@ export type ApiResponse = {
28
34
  message: string;
29
35
  };
30
36
  };
37
+ export type ApiAccountResponse = {
38
+ data: AccountMetadata | null;
39
+ error: {
40
+ code: string;
41
+ statusCode: number;
42
+ message: string;
43
+ };
44
+ };
31
45
  export declare function fetchKeyMetadataFromApi(clientId: string, config: CoreServiceConfig): Promise<ApiResponse>;
46
+ export declare function fetchAccountFromApi(jwt: string, config: CoreServiceConfig): Promise<ApiAccountResponse>;
32
47
  //# sourceMappingURL=api.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"../../../../src/core","sources":["api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,MAAM,iBAAiB,GAAG;IAC9B,WAAW,EAAE,OAAO,CAAC;IACrB,MAAM,EAAE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
1
+ import { AccountMetadata, ApiKeyMetadata, CoreServiceConfig } from "../api.js";
2
+ import { AuthorizationResult } from "./types.js";
3
3
  export type AuthorizationInput = {
4
4
  secretKey: string | null;
5
5
  clientId: string | null;
6
6
  origin: string | null;
7
7
  bundleId: string | null;
8
8
  secretKeyHash: string | null;
9
+ jwt: string | null;
10
+ hashedJWT: string | null;
9
11
  targetAddress?: string | string[];
10
12
  };
11
13
  type CacheOptions = {
12
14
  get: (clientId: string) => Promise<string | null>;
13
- put: (clientId: string, data: ApiKeyMetadata) => Promise<void> | void;
15
+ put: (clientId: string, data: ApiKeyMetadata | AccountMetadata) => Promise<void> | void;
14
16
  cacheTtlSeconds: number;
15
17
  };
16
18
  export declare function authorize(authData: AuthorizationInput, serviceConfig: CoreServiceConfig, cacheOptions?: CacheOptions): Promise<AuthorizationResult>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,iBAAiB,EAElB,MAAM,QAAQ,CAAC;AAGhB,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C,MAAM,MAAM,kBAAkB,GAAG;IAC/B,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CACnC,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAClD,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,KAAK,OAAO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AC,CAmN9B"}
@@ -1,7 +1,8 @@
1
- import { ApiKeyMetadata } from "../api";
1
+ import { AccountMetadata, ApiKeyMetadata } from "../api.js";
2
2
  export type AuthorizationResult = {
3
3
  authorized: true;
4
4
  apiKeyMeta: ApiKeyMetadata | null;
5
+ accountMeta: AccountMetadata | null;
5
6
  } | {
6
7
  authorized: false;
7
8
  status: number;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAExC,MAAM,MAAM,mBAAmB,GAC3B;IACE,UAAU,EAAE,IAAI,CAAC;IACjB,UAAU,EAAE,cAAc,GAAG,IAAI,CAAC;CACnC,GACD;IACE,UAAU,EAAE,KAAK,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,kBAAe;AAEzD,MAAM,MAAM,mBAAmB,GAC3B;IACE,UAAU,EAAE,IAAI,CAAC;IACjB,UAAU,EAAE,cAAc,GAAG,IAAI,CAAC;IAClC,WAAW,EAAE,eAAe,GAAG,IAAI,CAAC;CACrC,GACD;IACE,UAAU,EAAE,KAAK,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC"}
@@ -1,2 +1,2 @@
1
- export * from "./core/services";
1
+ export * from "./core/services.js";
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,cAAc,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,mCAAgC"}
@@ -1,10 +1,10 @@
1
1
  /// <reference types="node" />
2
2
  import type { IncomingMessage } from "node:http";
3
- import type { AuthorizationInput } from "../core/authorize";
4
- import type { CoreServiceConfig } from "../core/api";
5
- import type { AuthorizationResult } from "../core/authorize/types";
6
- import type { CoreAuthInput } from "../core/types";
7
- export * from "../core/services";
3
+ import type { AuthorizationInput } from "../core/authorize/index.js";
4
+ import type { CoreServiceConfig } from "../core/api.js";
5
+ import type { AuthorizationResult } from "../core/authorize/types.js";
6
+ import type { CoreAuthInput } from "../core/types.js";
7
+ export * from "../core/services.js";
8
8
  type NodeServiceConfig = CoreServiceConfig;
9
9
  export type AuthInput = CoreAuthInput & {
10
10
  req: IncomingMessage;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../../src/node","sources":["index.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAuB,eAAe,EAAE,MAAM,WAAW,CAAC;AAEtE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAErD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,cAAc,kBAAkB,CAAC;AAEjC,KAAK,iBAAiB,GAAG,iBAAiB,CAAC;AAE3C,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG;IACtC,GAAG,EAAE,eAAe,CAAC;CACtB,CAAC;AAEF,wBAAsB,aAAa,CACjC,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,iBAAiB,GAC/B,OAAO,CAAC,mBAAmB,CAAC,CAsB9B;AAaD,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,SAAS,GACnB,kBAAkB,CAwEpB;AAED,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,UAE9C;AAED,wBAAgB,+BAA+B,CAAC,aAAa,EAAE,MAAM,UAEpE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"../../../../src/node","sources":["index.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAuB,eAAe,EAAE,MAAM,WAAW,CAAC;AAEtE,OAAO,KAAK,EAAE,kBAAkB,EAAE,mCAA0B;AAC5D,OAAO,KAAK,EAAE,iBAAiB,EAAE,uBAAoB;AAErD,OAAO,KAAK,EAAE,mBAAmB,EAAE,mCAAgC;AACnE,OAAO,KAAK,EAAE,aAAa,EAAE,yBAAsB;AAEnD,oCAAiC;AAEjC,KAAK,iBAAiB,GAAG,iBAAiB,CAAC;AAE3C,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG;IACtC,GAAG,EAAE,eAAe,CAAC;CACtB,CAAC;AAEF,wBAAsB,aAAa,CACjC,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,iBAAiB,GAC/B,OAAO,CAAC,mBAAmB,CAAC,CAsB9B;AAaD,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,SAAS,GACnB,kBAAkB,CAoFpB;AAED,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,UAE9C;AAED,wBAAgB,+BAA+B,CAAC,aAAa,EAAE,MAAM,UAEpE"}
@@ -19,6 +19,25 @@ async function fetchKeyMetadataFromApi(clientId, config) {
19
19
  }
20
20
  return await response.json();
21
21
  }
22
+ async function fetchAccountFromApi(jwt, config) {
23
+ const {
24
+ apiUrl,
25
+ serviceApiKey
26
+ } = config;
27
+ const url = `${apiUrl}/v1/account/me`;
28
+ const response = await fetch(url, {
29
+ method: "GET",
30
+ headers: {
31
+ "x-service-api-key": serviceApiKey,
32
+ "content-type": "application/json",
33
+ authorization: `Bearer ${jwt}`
34
+ }
35
+ });
36
+ if (!response.ok) {
37
+ throw new Error(`Error fetching account from API: ${response.statusText}`);
38
+ }
39
+ return await response.json();
40
+ }
22
41
 
23
42
  function authorizeClient(authOptions, apiKeyMeta) {
24
43
  const {
@@ -42,7 +61,13 @@ function authorizeClient(authOptions, apiKeyMeta) {
42
61
  }
43
62
  return {
44
63
  authorized: true,
45
- apiKeyMeta
64
+ apiKeyMeta,
65
+ accountMeta: {
66
+ id: apiKeyMeta.accountId,
67
+ // TODO update this later
68
+ name: "",
69
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
70
+ }
46
71
  };
47
72
  }
48
73
 
@@ -51,14 +76,22 @@ function authorizeClient(authOptions, apiKeyMeta) {
51
76
  if (
52
77
  // find matching domain, or if all domains allowed
53
78
  domains.find(d => {
79
+ // if any domain is allowed, we'll return true
54
80
  if (d === "*") {
55
81
  return true;
56
82
  }
57
83
 
84
+ // special rule for `localhost`
85
+ // if the domain is localhost, we'll allow any origin that starts with localhost
86
+ if (d === "localhost" && origin.startsWith("localhost")) {
87
+ return true;
88
+ }
89
+
58
90
  // If the allowedDomain has a wildcard,
59
91
  // we'll check that the ending of our domain matches the wildcard
60
92
  if (d.startsWith("*.")) {
61
- const domainRoot = d.slice(2);
93
+ // get rid of the * and check if it ends with the `.<domain>.<tld>`
94
+ const domainRoot = d.slice(1);
62
95
  return origin.endsWith(domainRoot);
63
96
  }
64
97
 
@@ -67,7 +100,13 @@ function authorizeClient(authOptions, apiKeyMeta) {
67
100
  })) {
68
101
  return {
69
102
  authorized: true,
70
- apiKeyMeta
103
+ apiKeyMeta,
104
+ accountMeta: {
105
+ id: apiKeyMeta.accountId,
106
+ // TODO update this later
107
+ name: "",
108
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
109
+ }
71
110
  };
72
111
  }
73
112
  return {
@@ -90,7 +129,13 @@ function authorizeClient(authOptions, apiKeyMeta) {
90
129
  })) {
91
130
  return {
92
131
  authorized: true,
93
- apiKeyMeta
132
+ apiKeyMeta,
133
+ accountMeta: {
134
+ id: apiKeyMeta.accountId,
135
+ // TODO update this later
136
+ name: "",
137
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
138
+ }
94
139
  };
95
140
  }
96
141
  return {
@@ -152,6 +197,11 @@ function authorizeService(apiKeyMetadata, serviceConfig, authorizationPayload) {
152
197
  }
153
198
  return {
154
199
  authorized: true,
200
+ accountMeta: {
201
+ id: apiKeyMetadata.accountId,
202
+ name: "",
203
+ creatorWalletAddress: apiKeyMetadata.creatorWalletAddress
204
+ },
155
205
  apiKeyMeta: apiKeyMetadata
156
206
  };
157
207
  }
@@ -160,18 +210,97 @@ async function authorize(authData, serviceConfig, cacheOptions) {
160
210
  const {
161
211
  clientId,
162
212
  targetAddress,
163
- secretKeyHash
213
+ secretKeyHash,
214
+ jwt,
215
+ hashedJWT
164
216
  } = authData;
165
217
  const {
166
218
  enforceAuth
167
219
  } = serviceConfig;
168
220
 
169
- // BACKWARDS COMPAT: if auth not enforced and
170
- // we don't have auth credentials bypass
221
+ // BACKWARDS COMPAT: if auth not enforced and we don't have auth credentials bypass
171
222
  if (!enforceAuth && !clientId && !secretKeyHash) {
172
223
  return {
173
224
  authorized: true,
174
- apiKeyMeta: null
225
+ apiKeyMeta: null,
226
+ accountMeta: null
227
+ };
228
+ }
229
+ // if we come in with a JWT then we only check the account is valid
230
+ if (jwt && hashedJWT) {
231
+ let accountMeta = null;
232
+ if (cacheOptions) {
233
+ try {
234
+ const cachedAccountInfo = await cacheOptions.get(hashedJWT);
235
+ if (cachedAccountInfo) {
236
+ const parsed = JSON.parse(cachedAccountInfo);
237
+ if ("updatedAt" in parsed) {
238
+ // we want to compare the updatedAt time to the current time
239
+ // if the difference is greater than the cacheTtl we want to ignore the cached data
240
+ const now = Date.now();
241
+ const diff = now - parsed.updatedAt;
242
+ const cacheTtl = cacheOptions.cacheTtlSeconds * 1000;
243
+ // only if the diff is less than the cacheTtl do we want to use the cached key
244
+ if (diff < cacheTtl * 1000) {
245
+ accountMeta = parsed.apiKeyMeta;
246
+ }
247
+ } else {
248
+ accountMeta = parsed;
249
+ }
250
+ }
251
+ } catch (err) {
252
+ // ignore errors, proceed as if not in cache
253
+ }
254
+ }
255
+ if (!accountMeta) {
256
+ try {
257
+ const {
258
+ data,
259
+ error
260
+ } = await fetchAccountFromApi(jwt, serviceConfig);
261
+ if (error) {
262
+ return {
263
+ authorized: false,
264
+ errorCode: error.code,
265
+ errorMessage: error.message,
266
+ status: error.statusCode
267
+ };
268
+ } else if (!data) {
269
+ return {
270
+ authorized: false,
271
+ errorCode: "NO_ACCOUNT",
272
+ errorMessage: "No error but also no account returned.",
273
+ status: 500
274
+ };
275
+ }
276
+ accountMeta = data;
277
+ if (cacheOptions) {
278
+ await cacheOptions.put(hashedJWT, accountMeta);
279
+ }
280
+ } catch (err) {
281
+ console.warn("failed to fetch account from api", err);
282
+ return {
283
+ authorized: false,
284
+ status: 500,
285
+ errorMessage: "Failed to account information.",
286
+ errorCode: "FAILED_TO_ACCOUNT"
287
+ };
288
+ }
289
+ }
290
+ // if we still don't have an accountMeta at this point we can't authorize
291
+ if (!accountMeta) {
292
+ return {
293
+ authorized: false,
294
+ status: 401,
295
+ errorMessage: "Missing account information.",
296
+ errorCode: "MISSING_ACCOUNT"
297
+ };
298
+ }
299
+ // otherwise we want to return early with the accountMeta
300
+ return {
301
+ authorized: true,
302
+ apiKeyMeta: null,
303
+ accountMeta
175
304
  };
176
305
  }
177
306
 
@@ -285,7 +414,13 @@ async function authorize(authData, serviceConfig, cacheOptions) {
285
414
  // if we reach this point we are authorized!
286
415
  return {
287
416
  authorized: true,
288
- apiKeyMeta
417
+ apiKeyMeta,
418
+ accountMeta: {
419
+ id: apiKeyMeta.accountId,
420
+ // TODO update this later
421
+ name: "",
422
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
423
+ }
289
424
  };
290
425
  }
291
426
 
@@ -17,6 +17,25 @@ async function fetchKeyMetadataFromApi(clientId, config) {
17
17
  }
18
18
  return await response.json();
19
19
  }
20
+ async function fetchAccountFromApi(jwt, config) {
21
+ const {
22
+ apiUrl,
23
+ serviceApiKey
24
+ } = config;
25
+ const url = `${apiUrl}/v1/account/me`;
26
+ const response = await fetch(url, {
27
+ method: "GET",
28
+ headers: {
29
+ "x-service-api-key": serviceApiKey,
30
+ "content-type": "application/json",
31
+ authorization: `Bearer ${jwt}`
32
+ }
33
+ });
34
+ if (!response.ok) {
35
+ throw new Error(`Error fetching account from API: ${response.statusText}`);
36
+ }
37
+ return await response.json();
38
+ }
20
39
 
21
40
  function authorizeClient(authOptions, apiKeyMeta) {
22
41
  const {
@@ -40,7 +59,13 @@ function authorizeClient(authOptions, apiKeyMeta) {
40
59
  }
41
60
  return {
42
61
  authorized: true,
43
- apiKeyMeta
62
+ apiKeyMeta,
63
+ accountMeta: {
64
+ id: apiKeyMeta.accountId,
65
+ // TODO update this later
66
+ name: "",
67
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
68
+ }
44
69
  };
45
70
  }
46
71
 
@@ -49,14 +74,22 @@ function authorizeClient(authOptions, apiKeyMeta) {
49
74
  if (
50
75
  // find matching domain, or if all domains allowed
51
76
  domains.find(d => {
77
+ // if any domain is allowed, we'll return true
52
78
  if (d === "*") {
53
79
  return true;
54
80
  }
55
81
 
82
+ // special rule for `localhost`
83
+ // if the domain is localhost, we'll allow any origin that starts with localhost
84
+ if (d === "localhost" && origin.startsWith("localhost")) {
85
+ return true;
86
+ }
87
+
56
88
  // If the allowedDomain has a wildcard,
57
89
  // we'll check that the ending of our domain matches the wildcard
58
90
  if (d.startsWith("*.")) {
59
- const domainRoot = d.slice(2);
91
+ // get rid of the * and check if it ends with the `.<domain>.<tld>`
92
+ const domainRoot = d.slice(1);
60
93
  return origin.endsWith(domainRoot);
61
94
  }
62
95
 
@@ -65,7 +98,13 @@ function authorizeClient(authOptions, apiKeyMeta) {
65
98
  })) {
66
99
  return {
67
100
  authorized: true,
68
- apiKeyMeta
101
+ apiKeyMeta,
102
+ accountMeta: {
103
+ id: apiKeyMeta.accountId,
104
+ // TODO update this later
105
+ name: "",
106
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
107
+ }
69
108
  };
70
109
  }
71
110
  return {
@@ -88,7 +127,13 @@ function authorizeClient(authOptions, apiKeyMeta) {
88
127
  })) {
89
128
  return {
90
129
  authorized: true,
91
- apiKeyMeta
130
+ apiKeyMeta,
131
+ accountMeta: {
132
+ id: apiKeyMeta.accountId,
133
+ // TODO update this later
134
+ name: "",
135
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
136
+ }
92
137
  };
93
138
  }
94
139
  return {
@@ -150,6 +195,11 @@ function authorizeService(apiKeyMetadata, serviceConfig, authorizationPayload) {
150
195
  }
151
196
  return {
152
197
  authorized: true,
198
+ accountMeta: {
199
+ id: apiKeyMetadata.accountId,
200
+ name: "",
201
+ creatorWalletAddress: apiKeyMetadata.creatorWalletAddress
202
+ },
153
203
  apiKeyMeta: apiKeyMetadata
154
204
  };
155
205
  }
@@ -158,18 +208,97 @@ async function authorize(authData, serviceConfig, cacheOptions) {
158
208
  const {
159
209
  clientId,
160
210
  targetAddress,
161
- secretKeyHash
211
+ secretKeyHash,
212
+ jwt,
213
+ hashedJWT
162
214
  } = authData;
163
215
  const {
164
216
  enforceAuth
165
217
  } = serviceConfig;
166
218
 
167
- // BACKWARDS COMPAT: if auth not enforced and
168
- // we don't have auth credentials bypass
219
+ // BACKWARDS COMPAT: if auth not enforced and we don't have auth credentials bypass
169
220
  if (!enforceAuth && !clientId && !secretKeyHash) {
170
221
  return {
171
222
  authorized: true,
172
- apiKeyMeta: null
223
+ apiKeyMeta: null,
224
+ accountMeta: null
225
+ };
226
+ }
227
+ // if we come in with a JWT then we only check the account is valid
228
+ if (jwt && hashedJWT) {
229
+ let accountMeta = null;
230
+ if (cacheOptions) {
231
+ try {
232
+ const cachedAccountInfo = await cacheOptions.get(hashedJWT);
233
+ if (cachedAccountInfo) {
234
+ const parsed = JSON.parse(cachedAccountInfo);
235
+ if ("updatedAt" in parsed) {
236
+ // we want to compare the updatedAt time to the current time
237
+ // if the difference is greater than the cacheTtl we want to ignore the cached data
238
+ const now = Date.now();
239
+ const diff = now - parsed.updatedAt;
240
+ const cacheTtl = cacheOptions.cacheTtlSeconds * 1000;
241
+ // only if the diff is less than the cacheTtl do we want to use the cached key
242
+ if (diff < cacheTtl * 1000) {
243
+ accountMeta = parsed.apiKeyMeta;
244
+ }
245
+ } else {
246
+ accountMeta = parsed;
247
+ }
248
+ }
249
+ } catch (err) {
250
+ // ignore errors, proceed as if not in cache
251
+ }
252
+ }
253
+ if (!accountMeta) {
254
+ try {
255
+ const {
256
+ data,
257
+ error
258
+ } = await fetchAccountFromApi(jwt, serviceConfig);
259
+ if (error) {
260
+ return {
261
+ authorized: false,
262
+ errorCode: error.code,
263
+ errorMessage: error.message,
264
+ status: error.statusCode
265
+ };
266
+ } else if (!data) {
267
+ return {
268
+ authorized: false,
269
+ errorCode: "NO_ACCOUNT",
270
+ errorMessage: "No error but also no account returned.",
271
+ status: 500
272
+ };
273
+ }
274
+ accountMeta = data;
275
+ if (cacheOptions) {
276
+ await cacheOptions.put(hashedJWT, accountMeta);
277
+ }
278
+ } catch (err) {
279
+ console.warn("failed to fetch account from api", err);
280
+ return {
281
+ authorized: false,
282
+ status: 500,
283
+ errorMessage: "Failed to account information.",
284
+ errorCode: "FAILED_TO_ACCOUNT"
285
+ };
286
+ }
287
+ }
288
+ // if we still don't have an accountMeta at this point we can't authorize
289
+ if (!accountMeta) {
290
+ return {
291
+ authorized: false,
292
+ status: 401,
293
+ errorMessage: "Missing account information.",
294
+ errorCode: "MISSING_ACCOUNT"
295
+ };
296
+ }
297
+ // otherwise we want to return early with the accountMeta
298
+ return {
299
+ authorized: true,
300
+ apiKeyMeta: null,
301
+ accountMeta
173
302
  };
174
303
  }
175
304
 
@@ -283,7 +412,13 @@ async function authorize(authData, serviceConfig, cacheOptions) {
283
412
  // if we reach this point we are authorized!
284
413
  return {
285
414
  authorized: true,
286
- apiKeyMeta
415
+ apiKeyMeta,
416
+ accountMeta: {
417
+ id: apiKeyMeta.accountId,
418
+ // TODO update this later
419
+ name: "",
420
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
421
+ }
287
422
  };
288
423
  }
289
424
 
@@ -19,6 +19,25 @@ async function fetchKeyMetadataFromApi(clientId, config) {
19
19
  }
20
20
  return await response.json();
21
21
  }
22
+ async function fetchAccountFromApi(jwt, config) {
23
+ const {
24
+ apiUrl,
25
+ serviceApiKey
26
+ } = config;
27
+ const url = `${apiUrl}/v1/account/me`;
28
+ const response = await fetch(url, {
29
+ method: "GET",
30
+ headers: {
31
+ "x-service-api-key": serviceApiKey,
32
+ "content-type": "application/json",
33
+ authorization: `Bearer ${jwt}`
34
+ }
35
+ });
36
+ if (!response.ok) {
37
+ throw new Error(`Error fetching account from API: ${response.statusText}`);
38
+ }
39
+ return await response.json();
40
+ }
22
41
 
23
42
  function authorizeClient(authOptions, apiKeyMeta) {
24
43
  const {
@@ -42,7 +61,13 @@ function authorizeClient(authOptions, apiKeyMeta) {
42
61
  }
43
62
  return {
44
63
  authorized: true,
45
- apiKeyMeta
64
+ apiKeyMeta,
65
+ accountMeta: {
66
+ id: apiKeyMeta.accountId,
67
+ // TODO update this later
68
+ name: "",
69
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
70
+ }
46
71
  };
47
72
  }
48
73
 
@@ -51,14 +76,22 @@ function authorizeClient(authOptions, apiKeyMeta) {
51
76
  if (
52
77
  // find matching domain, or if all domains allowed
53
78
  domains.find(d => {
79
+ // if any domain is allowed, we'll return true
54
80
  if (d === "*") {
55
81
  return true;
56
82
  }
57
83
 
84
+ // special rule for `localhost`
85
+ // if the domain is localhost, we'll allow any origin that starts with localhost
86
+ if (d === "localhost" && origin.startsWith("localhost")) {
87
+ return true;
88
+ }
89
+
58
90
  // If the allowedDomain has a wildcard,
59
91
  // we'll check that the ending of our domain matches the wildcard
60
92
  if (d.startsWith("*.")) {
61
- const domainRoot = d.slice(2);
93
+ // get rid of the * and check if it ends with the `.<domain>.<tld>`
94
+ const domainRoot = d.slice(1);
62
95
  return origin.endsWith(domainRoot);
63
96
  }
64
97
 
@@ -67,7 +100,13 @@ function authorizeClient(authOptions, apiKeyMeta) {
67
100
  })) {
68
101
  return {
69
102
  authorized: true,
70
- apiKeyMeta
103
+ apiKeyMeta,
104
+ accountMeta: {
105
+ id: apiKeyMeta.accountId,
106
+ // TODO update this later
107
+ name: "",
108
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
109
+ }
71
110
  };
72
111
  }
73
112
  return {
@@ -90,7 +129,13 @@ function authorizeClient(authOptions, apiKeyMeta) {
90
129
  })) {
91
130
  return {
92
131
  authorized: true,
93
- apiKeyMeta
132
+ apiKeyMeta,
133
+ accountMeta: {
134
+ id: apiKeyMeta.accountId,
135
+ // TODO update this later
136
+ name: "",
137
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
138
+ }
94
139
  };
95
140
  }
96
141
  return {
@@ -152,6 +197,11 @@ function authorizeService(apiKeyMetadata, serviceConfig, authorizationPayload) {
152
197
  }
153
198
  return {
154
199
  authorized: true,
200
+ accountMeta: {
201
+ id: apiKeyMetadata.accountId,
202
+ name: "",
203
+ creatorWalletAddress: apiKeyMetadata.creatorWalletAddress
204
+ },
155
205
  apiKeyMeta: apiKeyMetadata
156
206
  };
157
207
  }
@@ -160,18 +210,97 @@ async function authorize(authData, serviceConfig, cacheOptions) {
160
210
  const {
161
211
  clientId,
162
212
  targetAddress,
163
- secretKeyHash
213
+ secretKeyHash,
214
+ jwt,
215
+ hashedJWT
164
216
  } = authData;
165
217
  const {
166
218
  enforceAuth
167
219
  } = serviceConfig;
168
220
 
169
- // BACKWARDS COMPAT: if auth not enforced and
170
- // we don't have auth credentials bypass
221
+ // BACKWARDS COMPAT: if auth not enforced and we don't have auth credentials bypass
171
222
  if (!enforceAuth && !clientId && !secretKeyHash) {
172
223
  return {
173
224
  authorized: true,
174
- apiKeyMeta: null
225
+ apiKeyMeta: null,
226
+ accountMeta: null
227
+ };
228
+ }
229
+ // if we come in with a JWT then we only check the account is valid
230
+ if (jwt && hashedJWT) {
231
+ let accountMeta = null;
232
+ if (cacheOptions) {
233
+ try {
234
+ const cachedAccountInfo = await cacheOptions.get(hashedJWT);
235
+ if (cachedAccountInfo) {
236
+ const parsed = JSON.parse(cachedAccountInfo);
237
+ if ("updatedAt" in parsed) {
238
+ // we want to compare the updatedAt time to the current time
239
+ // if the difference is greater than the cacheTtl we want to ignore the cached data
240
+ const now = Date.now();
241
+ const diff = now - parsed.updatedAt;
242
+ const cacheTtl = cacheOptions.cacheTtlSeconds * 1000;
243
+ // only if the diff is less than the cacheTtl do we want to use the cached key
244
+ if (diff < cacheTtl * 1000) {
245
+ accountMeta = parsed.apiKeyMeta;
246
+ }
247
+ } else {
248
+ accountMeta = parsed;
249
+ }
250
+ }
251
+ } catch (err) {
252
+ // ignore errors, proceed as if not in cache
253
+ }
254
+ }
255
+ if (!accountMeta) {
256
+ try {
257
+ const {
258
+ data,
259
+ error
260
+ } = await fetchAccountFromApi(jwt, serviceConfig);
261
+ if (error) {
262
+ return {
263
+ authorized: false,
264
+ errorCode: error.code,
265
+ errorMessage: error.message,
266
+ status: error.statusCode
267
+ };
268
+ } else if (!data) {
269
+ return {
270
+ authorized: false,
271
+ errorCode: "NO_ACCOUNT",
272
+ errorMessage: "No error but also no account returned.",
273
+ status: 500
274
+ };
275
+ }
276
+ accountMeta = data;
277
+ if (cacheOptions) {
278
+ await cacheOptions.put(hashedJWT, accountMeta);
279
+ }
280
+ } catch (err) {
281
+ console.warn("failed to fetch account from api", err);
282
+ return {
283
+ authorized: false,
284
+ status: 500,
285
+ errorMessage: "Failed to account information.",
286
+ errorCode: "FAILED_TO_ACCOUNT"
287
+ };
288
+ }
289
+ }
290
+ // if we still don't have an accountMeta at this point we can't authorize
291
+ if (!accountMeta) {
292
+ return {
293
+ authorized: false,
294
+ status: 401,
295
+ errorMessage: "Missing account information.",
296
+ errorCode: "MISSING_ACCOUNT"
297
+ };
298
+ }
299
+ // otherwise we want to return early with the accountMeta
300
+ return {
301
+ authorized: true,
302
+ apiKeyMeta: null,
303
+ accountMeta
175
304
  };
176
305
  }
177
306
 
@@ -285,7 +414,13 @@ async function authorize(authData, serviceConfig, cacheOptions) {
285
414
  // if we reach this point we are authorized!
286
415
  return {
287
416
  authorized: true,
288
- apiKeyMeta
417
+ apiKeyMeta,
418
+ accountMeta: {
419
+ id: apiKeyMeta.accountId,
420
+ // TODO update this later
421
+ name: "",
422
+ creatorWalletAddress: apiKeyMeta.creatorWalletAddress
423
+ }
289
424
  };
290
425
  }
291
426
 
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var node_crypto = require('node:crypto');
6
- var index = require('../../dist/index-4496b57e.cjs.dev.js');
6
+ var index = require('../../dist/index-ca7a4739.cjs.dev.js');
7
7
  var services = require('../../dist/services-a3f36057.cjs.dev.js');
8
8
 
9
9
  async function authorizeNode(authInput, serviceConfig) {
@@ -91,7 +91,18 @@ function extractAuthorizationData(authInput) {
91
91
  // otherwise set the client id to the derived client id (client id based off of secret key)
92
92
  clientId = derivedClientId;
93
93
  }
94
+ let jwt = null;
95
+ // check for authorization header on the request
96
+ const authorizationHeader = getHeader(headers, "authorization");
97
+ if (authorizationHeader) {
98
+ const [type, token] = authorizationHeader.split(" ");
99
+ if (type.toLowerCase() === "bearer") {
100
+ jwt = token;
101
+ }
102
+ }
94
103
  return {
104
+ jwt,
105
+ hashedJWT: jwt ? hashSecretKey(jwt) : null,
95
106
  secretKeyHash,
96
107
  secretKey,
97
108
  clientId,
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var node_crypto = require('node:crypto');
6
- var index = require('../../dist/index-7c6b47cd.cjs.prod.js');
6
+ var index = require('../../dist/index-794f8b68.cjs.prod.js');
7
7
  var services = require('../../dist/services-9e185105.cjs.prod.js');
8
8
 
9
9
  async function authorizeNode(authInput, serviceConfig) {
@@ -91,7 +91,18 @@ function extractAuthorizationData(authInput) {
91
91
  // otherwise set the client id to the derived client id (client id based off of secret key)
92
92
  clientId = derivedClientId;
93
93
  }
94
+ let jwt = null;
95
+ // check for authorization header on the request
96
+ const authorizationHeader = getHeader(headers, "authorization");
97
+ if (authorizationHeader) {
98
+ const [type, token] = authorizationHeader.split(" ");
99
+ if (type.toLowerCase() === "bearer") {
100
+ jwt = token;
101
+ }
102
+ }
94
103
  return {
104
+ jwt,
105
+ hashedJWT: jwt ? hashSecretKey(jwt) : null,
95
106
  secretKeyHash,
96
107
  secretKey,
97
108
  clientId,
@@ -1,5 +1,5 @@
1
1
  import { createHash } from 'node:crypto';
2
- import { a as authorize } from '../../dist/index-d752cff8.esm.js';
2
+ import { a as authorize } from '../../dist/index-80604116.esm.js';
3
3
  export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from '../../dist/services-86283509.esm.js';
4
4
 
5
5
  async function authorizeNode(authInput, serviceConfig) {
@@ -87,7 +87,18 @@ function extractAuthorizationData(authInput) {
87
87
  // otherwise set the client id to the derived client id (client id based off of secret key)
88
88
  clientId = derivedClientId;
89
89
  }
90
+ let jwt = null;
91
+ // check for authorization header on the request
92
+ const authorizationHeader = getHeader(headers, "authorization");
93
+ if (authorizationHeader) {
94
+ const [type, token] = authorizationHeader.split(" ");
95
+ if (type.toLowerCase() === "bearer") {
96
+ jwt = token;
97
+ }
98
+ }
90
99
  return {
100
+ jwt,
101
+ hashedJWT: jwt ? hashSecretKey(jwt) : null,
91
102
  secretKeyHash,
92
103
  secretKey,
93
104
  clientId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thirdweb-dev/service-utils",
3
- "version": "0.0.0-dev-786a4b3-20230729072030",
3
+ "version": "0.0.0-dev-8e57e1d-20230731190300",
4
4
  "main": "dist/thirdweb-dev-service-utils.cjs.js",
5
5
  "module": "dist/thirdweb-dev-service-utils.esm.js",
6
6
  "exports": {
@@ -40,7 +40,7 @@
40
40
  "sideEffects": false,
41
41
  "devDependencies": {
42
42
  "@cloudflare/workers-types": "^4.20230724.0",
43
- "@preconstruct/cli": "2.7.0",
43
+ "@preconstruct/cli": "^2.8.1",
44
44
  "@thirdweb-dev/tsconfig": "^0.1.7",
45
45
  "@types/jest": "^29.5.3",
46
46
  "@types/node": "^18.17.1",
@@ -48,7 +48,7 @@
48
48
  "@typescript-eslint/parser": "^6.2.0",
49
49
  "eslint": "^8.45.0",
50
50
  "eslint-config-prettier": "^8.9.0",
51
- "eslint-config-thirdweb": "^0.1.6",
51
+ "eslint-config-thirdweb": "^0.0.0-dev-8e57e1d-20230731190300",
52
52
  "jest": "^29.6.2",
53
53
  "prettier": "^3.0.0",
54
54
  "typescript": "^5.1.6"
@@ -1,9 +0,0 @@
1
- import type { ApiKeyMetadata } from "../api";
2
- import type { AuthorizationResult } from "./types";
3
- export type ClientAuthorizationPayload = {
4
- secretKeyHash: string | null;
5
- bundleId: string | null;
6
- origin: string | null;
7
- };
8
- export declare function authorizeClient(authOptions: ClientAuthorizationPayload, apiKeyMeta: ApiKeyMetadata): AuthorizationResult;
9
- //# sourceMappingURL=client.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD,MAAM,MAAM,0BAA0B,GAAG;IACvC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB,CAAC;AAEF,wBAAgB,eAAe,CAC7B,WAAW,EAAE,0BAA0B,EACvC,UAAU,EAAE,cAAc,GACzB,mBAAmB,CAsFrB"}
@@ -1,7 +0,0 @@
1
- import { ApiKeyMetadata, CoreServiceConfig } from "../api";
2
- import { AuthorizationResult } from "./types";
3
- export type ServiceAuthorizationPayload = {
4
- targetAddress?: string | string[];
5
- };
6
- export declare function authorizeService(apiKeyMetadata: ApiKeyMetadata, serviceConfig: CoreServiceConfig, authorizationPayload?: ServiceAuthorizationPayload): AuthorizationResult;
7
- //# sourceMappingURL=service.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"service.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C,MAAM,MAAM,2BAA2B,GAAG;IAAE,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CAAE,CAAC;AAEhF,wBAAgB,gBAAgB,CAC9B,cAAc,EAAE,cAAc,EAC9B,aAAa,EAAE,iBAAiB,EAChC,oBAAoB,CAAC,EAAE,2BAA2B,GACjD,mBAAmB,CAwDrB"}