@thirdweb-dev/service-utils 0.5.0-nightly-f5aed34d-20230829001006 → 0.5.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.
- package/dist/cjs/cf-worker/index.js +164 -0
- package/dist/cjs/cf-worker/index.js.map +1 -0
- package/dist/cjs/cf-worker/usage.js +55 -0
- package/dist/cjs/cf-worker/usage.js.map +1 -0
- package/dist/cjs/core/api.js +42 -0
- package/dist/cjs/core/api.js.map +1 -0
- package/dist/cjs/core/authorize/client.js +98 -0
- package/dist/cjs/core/authorize/client.js.map +1 -0
- package/dist/cjs/core/authorize/index.js +109 -0
- package/dist/cjs/core/authorize/index.js.map +1 -0
- package/dist/cjs/core/authorize/service.js +50 -0
- package/dist/cjs/core/authorize/service.js.map +1 -0
- package/dist/cjs/core/authorize/types.js +3 -0
- package/dist/cjs/core/authorize/types.js.map +1 -0
- package/dist/cjs/core/rateLimit/index.js +60 -0
- package/dist/cjs/core/rateLimit/index.js.map +1 -0
- package/dist/cjs/core/rateLimit/types.js +3 -0
- package/dist/cjs/core/rateLimit/types.js.map +1 -0
- package/dist/cjs/core/services.js +85 -0
- package/dist/cjs/core/services.js.map +1 -0
- package/dist/cjs/core/types.js +3 -0
- package/dist/cjs/core/types.js.map +1 -0
- package/dist/cjs/core/usage.js +85 -0
- package/dist/cjs/core/usage.js.map +1 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mocks.js +60 -0
- package/dist/cjs/mocks.js.map +1 -0
- package/dist/cjs/node/index.js +182 -0
- package/dist/cjs/node/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/cf-worker/index.js +156 -0
- package/dist/esm/cf-worker/index.js.map +1 -0
- package/dist/esm/cf-worker/usage.js +53 -0
- package/dist/esm/cf-worker/usage.js.map +1 -0
- package/dist/esm/core/api.js +38 -0
- package/dist/esm/core/api.js.map +1 -0
- package/dist/esm/core/authorize/client.js +93 -0
- package/dist/esm/core/authorize/client.js.map +1 -0
- package/dist/esm/core/authorize/index.js +106 -0
- package/dist/esm/core/authorize/index.js.map +1 -0
- package/dist/esm/core/authorize/service.js +47 -0
- package/dist/esm/core/authorize/service.js.map +1 -0
- package/dist/esm/core/authorize/types.js +2 -0
- package/dist/esm/core/authorize/types.js.map +1 -0
- package/dist/esm/core/rateLimit/index.js +57 -0
- package/dist/esm/core/rateLimit/index.js.map +1 -0
- package/dist/esm/core/rateLimit/types.js +2 -0
- package/dist/esm/core/rateLimit/types.js.map +1 -0
- package/dist/esm/core/services.js +81 -0
- package/dist/esm/core/services.js.map +1 -0
- package/dist/esm/core/types.js +2 -0
- package/dist/esm/core/types.js.map +1 -0
- package/dist/esm/core/usage.js +82 -0
- package/dist/esm/core/usage.js.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/mocks.js +57 -0
- package/dist/esm/mocks.js.map +1 -0
- package/dist/esm/node/index.js +174 -0
- package/dist/esm/node/index.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/{declarations/src → types}/cf-worker/index.d.ts +10 -8
- package/dist/types/cf-worker/index.d.ts.map +1 -0
- package/dist/types/cf-worker/usage.d.ts +24 -0
- package/dist/types/cf-worker/usage.d.ts.map +1 -0
- package/dist/types/core/api.d.ts +121 -0
- package/dist/types/core/api.d.ts.map +1 -0
- package/dist/types/core/authorize/client.d.ts +17 -0
- package/dist/types/core/authorize/client.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/authorize/index.d.ts +6 -5
- package/dist/types/core/authorize/index.d.ts.map +1 -0
- package/dist/types/core/authorize/service.d.ts +4 -0
- package/dist/types/core/authorize/service.d.ts.map +1 -0
- package/dist/types/core/authorize/types.d.ts +10 -0
- package/dist/types/core/authorize/types.d.ts.map +1 -0
- package/dist/types/core/rateLimit/index.d.ts +20 -0
- package/dist/types/core/rateLimit/index.d.ts.map +1 -0
- package/dist/types/core/rateLimit/types.d.ts +13 -0
- package/dist/types/core/rateLimit/types.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/services.d.ts +84 -4
- package/dist/types/core/services.d.ts.map +1 -0
- package/dist/types/core/types.d.ts.map +1 -0
- package/dist/types/core/usage.d.ts +196 -0
- package/dist/types/core/usage.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/mocks.d.ts +7 -0
- package/dist/types/mocks.d.ts.map +1 -0
- package/dist/types/node/index.d.ts +41 -0
- package/dist/types/node/index.d.ts.map +1 -0
- package/package.json +42 -44
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.d.ts +0 -2
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.d.ts.map +0 -1
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.dev.js +0 -240
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.js +0 -7
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.prod.js +0 -240
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.esm.js +0 -227
- package/cf-worker/package.json +0 -4
- package/dist/declarations/src/cf-worker/index.d.ts.map +0 -1
- package/dist/declarations/src/cf-worker/usage.d.ts +0 -79
- package/dist/declarations/src/cf-worker/usage.d.ts.map +0 -1
- package/dist/declarations/src/core/api.d.ts +0 -48
- package/dist/declarations/src/core/api.d.ts.map +0 -1
- package/dist/declarations/src/core/authorize/client.d.ts +0 -9
- package/dist/declarations/src/core/authorize/client.d.ts.map +0 -1
- package/dist/declarations/src/core/authorize/index.d.ts.map +0 -1
- package/dist/declarations/src/core/authorize/service.d.ts +0 -7
- package/dist/declarations/src/core/authorize/service.d.ts.map +0 -1
- package/dist/declarations/src/core/authorize/types.d.ts +0 -12
- package/dist/declarations/src/core/authorize/types.d.ts.map +0 -1
- package/dist/declarations/src/core/services.d.ts.map +0 -1
- package/dist/declarations/src/core/types.d.ts.map +0 -1
- package/dist/declarations/src/index.d.ts +0 -2
- package/dist/declarations/src/index.d.ts.map +0 -1
- package/dist/declarations/src/node/index.d.ts +0 -23
- package/dist/declarations/src/node/index.d.ts.map +0 -1
- package/dist/index-6e0ecc5f.cjs.prod.js +0 -424
- package/dist/index-cd4f96ef.cjs.dev.js +0 -424
- package/dist/index-ffddf746.esm.js +0 -422
- package/dist/services-86283509.esm.js +0 -44
- package/dist/services-9e185105.cjs.prod.js +0 -49
- package/dist/services-a3f36057.cjs.dev.js +0 -49
- package/dist/thirdweb-dev-service-utils.cjs.d.ts +0 -2
- package/dist/thirdweb-dev-service-utils.cjs.d.ts.map +0 -1
- package/dist/thirdweb-dev-service-utils.cjs.dev.js +0 -12
- package/dist/thirdweb-dev-service-utils.cjs.js +0 -7
- package/dist/thirdweb-dev-service-utils.cjs.prod.js +0 -12
- package/dist/thirdweb-dev-service-utils.esm.js +0 -1
- package/node/dist/thirdweb-dev-service-utils-node.cjs.d.ts +0 -2
- package/node/dist/thirdweb-dev-service-utils-node.cjs.d.ts.map +0 -1
- package/node/dist/thirdweb-dev-service-utils-node.cjs.dev.js +0 -167
- package/node/dist/thirdweb-dev-service-utils-node.cjs.js +0 -7
- package/node/dist/thirdweb-dev-service-utils-node.cjs.prod.js +0 -167
- package/node/dist/thirdweb-dev-service-utils-node.esm.js +0 -155
- package/node/package.json +0 -4
- /package/dist/{declarations/src → types}/core/types.d.ts +0 -0
package/cf-worker/package.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"../../../../src/cf-worker","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,WAAW,EACX,QAAQ,EACT,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAGV,iBAAiB,EAClB,MAAM,aAAa,CAAC;AAGrB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,cAAc,kBAAkB,CAAC;AACjC,cAAc,SAAS,CAAC;AAExB,KAAK,mBAAmB,GAAG,iBAAiB,GAAG;IAC7C,OAAO,EAAE,WAAW,CAAC;IACrB,GAAG,EAAE,gBAAgB,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAIF,KAAK,SAAS,GAAG,aAAa,GAAG;IAC/B,GAAG,EAAE,OAAO,CAAC;CACd,CAAC;AAEF,wBAAsB,eAAe,CACnC,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,mBAAmB,GACjC,OAAO,CAAC,mBAAmB,CAAC,CA0C9B;AAED,wBAAsB,wBAAwB,CAC5C,SAAS,EAAE,SAAS,GACnB,OAAO,CAAC,kBAAkB,CAAC,CA2E7B;AAED,wBAAsB,aAAa,CAAC,SAAS,EAAE,MAAM,mBAIpD;AAED,wBAAgB,+BAA+B,CAAC,aAAa,EAAE,MAAM,UAEpE;AAQD,wBAAsB,cAAc,CAAC,EACnC,MAAM,EACN,QAAQ,EACR,GAAG,EACH,GAAG,EACH,QAAQ,EACR,aAAa,GACd,EAAE,SAAS,GAAG;IACb,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,QAAQ,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,aAAa,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;CAChC,iBAoBA"}
|
@@ -1,79 +0,0 @@
|
|
1
|
-
import { z } from "zod";
|
2
|
-
/**
|
3
|
-
* Types
|
4
|
-
*/
|
5
|
-
declare const usageEventSchema: z.ZodObject<{
|
6
|
-
source: z.ZodEnum<["wallet", "rpc", "storage", "bundler", "paymaster", "relayer"]>;
|
7
|
-
action: z.ZodString;
|
8
|
-
accountId: z.ZodString;
|
9
|
-
apiKeyId: z.ZodOptional<z.ZodString>;
|
10
|
-
creatorWalletAddress: z.ZodOptional<z.ZodString>;
|
11
|
-
clientId: z.ZodOptional<z.ZodString>;
|
12
|
-
walletAddress: z.ZodOptional<z.ZodString>;
|
13
|
-
chainId: z.ZodOptional<z.ZodNumber>;
|
14
|
-
provider: z.ZodOptional<z.ZodString>;
|
15
|
-
mimeType: z.ZodOptional<z.ZodString>;
|
16
|
-
fileSize: z.ZodOptional<z.ZodNumber>;
|
17
|
-
fileCid: z.ZodOptional<z.ZodString>;
|
18
|
-
transactionHash: z.ZodOptional<z.ZodString>;
|
19
|
-
gasLimit: z.ZodOptional<z.ZodNumber>;
|
20
|
-
gasPricePerUnit: z.ZodOptional<z.ZodString>;
|
21
|
-
userOpHash: z.ZodOptional<z.ZodString>;
|
22
|
-
}, "strip", z.ZodTypeAny, {
|
23
|
-
source: "storage" | "rpc" | "bundler" | "relayer" | "wallet" | "paymaster";
|
24
|
-
action: string;
|
25
|
-
accountId: string;
|
26
|
-
apiKeyId?: string | undefined;
|
27
|
-
creatorWalletAddress?: string | undefined;
|
28
|
-
clientId?: string | undefined;
|
29
|
-
walletAddress?: string | undefined;
|
30
|
-
chainId?: number | undefined;
|
31
|
-
provider?: string | undefined;
|
32
|
-
mimeType?: string | undefined;
|
33
|
-
fileSize?: number | undefined;
|
34
|
-
fileCid?: string | undefined;
|
35
|
-
transactionHash?: string | undefined;
|
36
|
-
gasLimit?: number | undefined;
|
37
|
-
gasPricePerUnit?: string | undefined;
|
38
|
-
userOpHash?: string | undefined;
|
39
|
-
}, {
|
40
|
-
source: "storage" | "rpc" | "bundler" | "relayer" | "wallet" | "paymaster";
|
41
|
-
action: string;
|
42
|
-
accountId: string;
|
43
|
-
apiKeyId?: string | undefined;
|
44
|
-
creatorWalletAddress?: string | undefined;
|
45
|
-
clientId?: string | undefined;
|
46
|
-
walletAddress?: string | undefined;
|
47
|
-
chainId?: number | undefined;
|
48
|
-
provider?: string | undefined;
|
49
|
-
mimeType?: string | undefined;
|
50
|
-
fileSize?: number | undefined;
|
51
|
-
fileCid?: string | undefined;
|
52
|
-
transactionHash?: string | undefined;
|
53
|
-
gasLimit?: number | undefined;
|
54
|
-
gasPricePerUnit?: string | undefined;
|
55
|
-
userOpHash?: string | undefined;
|
56
|
-
}>;
|
57
|
-
export type UsageEvent = z.infer<typeof usageEventSchema>;
|
58
|
-
/**
|
59
|
-
* Publish usage events. Provide the relevant fields for your application.
|
60
|
-
*
|
61
|
-
* Usage in Cloudflare Workers:
|
62
|
-
* ctx.waitUntil(
|
63
|
-
* publishUsageEvents(
|
64
|
-
* [event1, event2],
|
65
|
-
* { queueUrl, accessKeyId, secretAccessKey },
|
66
|
-
* )
|
67
|
-
* )
|
68
|
-
*
|
69
|
-
* @param usageEvents
|
70
|
-
* @param config
|
71
|
-
*/
|
72
|
-
export declare function publishUsageEvents(usageEvents: UsageEvent[], config: {
|
73
|
-
queueUrl: string;
|
74
|
-
accessKeyId: string;
|
75
|
-
secretAccessKey: string;
|
76
|
-
region?: string;
|
77
|
-
}): Promise<void>;
|
78
|
-
export {};
|
79
|
-
//# sourceMappingURL=usage.d.ts.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"usage.d.ts","sourceRoot":"../../../../src/cf-worker","sources":["usage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAWxB;;GAEG;AACH,QAAA,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA0BpB,CAAC;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAE1D;;;;;;;;;;;;;GAaG;AACH,wBAAsB,kBAAkB,CACtC,WAAW,EAAE,UAAU,EAAE,EACzB,MAAM,EAAE;IACN,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GACA,OAAO,CAAC,IAAI,CAAC,CAiCf"}
|
@@ -1,48 +0,0 @@
|
|
1
|
-
import type { ServiceName } from "./services";
|
2
|
-
export type CoreServiceConfig = {
|
3
|
-
enforceAuth: boolean;
|
4
|
-
apiUrl: string;
|
5
|
-
serviceScope: ServiceName;
|
6
|
-
serviceApiKey: string;
|
7
|
-
serviceAction?: string;
|
8
|
-
useWalletAuth?: boolean;
|
9
|
-
};
|
10
|
-
export type ApiKeyMetadata = {
|
11
|
-
id: string;
|
12
|
-
key: string;
|
13
|
-
accountId: string;
|
14
|
-
creatorWalletAddress: string;
|
15
|
-
secretHash: string;
|
16
|
-
walletAddresses: string[];
|
17
|
-
domains: string[];
|
18
|
-
bundleIds: string[];
|
19
|
-
services: {
|
20
|
-
name: string;
|
21
|
-
targetAddresses: string[];
|
22
|
-
actions: string[];
|
23
|
-
}[];
|
24
|
-
};
|
25
|
-
export type AccountMetadata = {
|
26
|
-
id: string;
|
27
|
-
name: string;
|
28
|
-
creatorWalletAddress: string;
|
29
|
-
};
|
30
|
-
export type ApiResponse = {
|
31
|
-
data: ApiKeyMetadata | null;
|
32
|
-
error: {
|
33
|
-
code: string;
|
34
|
-
statusCode: number;
|
35
|
-
message: string;
|
36
|
-
};
|
37
|
-
};
|
38
|
-
export type ApiAccountResponse = {
|
39
|
-
data: AccountMetadata | null;
|
40
|
-
error: {
|
41
|
-
code: string;
|
42
|
-
statusCode: number;
|
43
|
-
message: string;
|
44
|
-
};
|
45
|
-
};
|
46
|
-
export declare function fetchKeyMetadataFromApi(clientId: string, config: CoreServiceConfig): Promise<ApiResponse>;
|
47
|
-
export declare function fetchAccountFromApi(jwt: string, config: CoreServiceConfig, useWalletAuth: boolean): Promise<ApiAccountResponse>;
|
48
|
-
//# sourceMappingURL=api.d.ts.map
|
@@ -1 +0,0 @@
|
|
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,MAAM,CAAC;IACf,YAAY,EAAE,WAAW,CAAC;IAC1B,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,UAAU,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,QAAQ,EAAE;QACR,IAAI,EAAE,MAAM,CAAC;QACb,eAAe,EAAE,MAAM,EAAE,CAAC;QAC1B,OAAO,EAAE,MAAM,EAAE,CAAC;KACnB,EAAE,CAAC;CACL,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,cAAc,GAAG,IAAI,CAAC;IAC5B,KAAK,EAAE;QACL,IAAI,EAAE,MAAM,CAAC;QACb,UAAU,EAAE,MAAM,CAAC;QACnB,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,IAAI,EAAE,eAAe,GAAG,IAAI,CAAC;IAC7B,KAAK,EAAE;QACL,IAAI,EAAE,MAAM,CAAC;QACb,UAAU,EAAE,MAAM,CAAC;QACnB,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;CACH,CAAC;AAEF,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,iBAAiB,GACxB,OAAO,CAAC,WAAW,CAAC,CAiBtB;AAED,wBAAsB,mBAAmB,CACvC,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,iBAAiB,EACzB,aAAa,EAAE,OAAO,GACrB,OAAO,CAAC,kBAAkB,CAAC,CAkB7B"}
|
@@ -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,CAwGrB"}
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,cAAc,EACd,iBAAiB,EAGlB,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,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACnB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAElC,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAClD,GAAG,EAAE,CACH,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,cAAc,GAAG,eAAe,KACnC,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC1B,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC;AAgBF,wBAAsB,SAAS,CAC7B,QAAQ,EAAE,kBAAkB,EAC5B,aAAa,EAAE,iBAAiB,EAChC,YAAY,CAAC,EAAE,YAAY,GAC1B,OAAO,CAAC,mBAAmB,CAAC,CAmN9B"}
|
@@ -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,CA6DrB"}
|
@@ -1,12 +0,0 @@
|
|
1
|
-
import { AccountMetadata, ApiKeyMetadata } from "../api";
|
2
|
-
export type AuthorizationResult = {
|
3
|
-
authorized: true;
|
4
|
-
apiKeyMeta: ApiKeyMetadata | null;
|
5
|
-
accountMeta: AccountMetadata | null;
|
6
|
-
} | {
|
7
|
-
authorized: false;
|
8
|
-
status: number;
|
9
|
-
errorMessage: string;
|
10
|
-
errorCode: string;
|
11
|
-
};
|
12
|
-
//# sourceMappingURL=types.d.ts.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"../../../../../src/core/authorize","sources":["types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;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 +0,0 @@
|
|
1
|
-
{"version":3,"file":"services.d.ts","sourceRoot":"../../../../src/core","sources":["services.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuCtB,CAAC;AAEX,eAAO,MAAM,aAAa,+CAEe,CAAC;AAE1C,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAAqC,CAAC;AAE3D,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AAEzD,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,OAAO,GACjB,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,OAAO,mBAAmB,CAAC,CAAC;AAEjE,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAEjD"}
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"../../../../src/core","sources":["types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG;IAE1B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CACnC,CAAC"}
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,cAAc,iBAAiB,CAAC"}
|
@@ -1,23 +0,0 @@
|
|
1
|
-
/// <reference types="node" />
|
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
|
-
import type { ServerResponse } from "http";
|
8
|
-
export * from "../core/services";
|
9
|
-
type NodeServiceConfig = CoreServiceConfig;
|
10
|
-
export type AuthInput = CoreAuthInput & {
|
11
|
-
req: IncomingMessage;
|
12
|
-
};
|
13
|
-
export declare function authorizeNode(authInput: AuthInput, serviceConfig: NodeServiceConfig): Promise<AuthorizationResult>;
|
14
|
-
export declare function extractAuthorizationData(authInput: AuthInput): AuthorizationInput;
|
15
|
-
export declare function hashSecretKey(secretKey: string): string;
|
16
|
-
export declare function deriveClientIdFromSecretKeyHash(secretKeyHash: string): string;
|
17
|
-
export declare function logHttpRequest({ source, clientId, req, res, isAuthed, statusMessage, }: AuthInput & {
|
18
|
-
source: string;
|
19
|
-
res: ServerResponse;
|
20
|
-
isAuthed?: boolean;
|
21
|
-
statusMessage?: Error | string;
|
22
|
-
}): void;
|
23
|
-
//# sourceMappingURL=index.d.ts.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"../../../../src/node","sources":["index.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAuB,eAAe,EAAE,MAAM,WAAW,CAAC;AACtE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAE3C,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,CA2FpB;AAED,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,UAE9C;AAED,wBAAgB,+BAA+B,CAAC,aAAa,EAAE,MAAM,UAEpE;AAED,wBAAgB,cAAc,CAAC,EAC7B,MAAM,EACN,QAAQ,EACR,GAAG,EACH,GAAG,EACH,QAAQ,EACR,aAAa,GACd,EAAE,SAAS,GAAG;IACb,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,cAAc,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,aAAa,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;CAChC,QAsBA"}
|
@@ -1,424 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
async function fetchKeyMetadataFromApi(clientId, config) {
|
4
|
-
const {
|
5
|
-
apiUrl,
|
6
|
-
serviceScope,
|
7
|
-
serviceApiKey
|
8
|
-
} = config;
|
9
|
-
const url = `${apiUrl}/v1/keys/use?clientId=${clientId}&scope=${serviceScope}`;
|
10
|
-
const response = await fetch(url, {
|
11
|
-
method: "GET",
|
12
|
-
headers: {
|
13
|
-
"x-service-api-key": serviceApiKey,
|
14
|
-
"content-type": "application/json"
|
15
|
-
}
|
16
|
-
});
|
17
|
-
let json;
|
18
|
-
try {
|
19
|
-
json = await response.json();
|
20
|
-
} catch (e) {
|
21
|
-
throw new Error(`Error fetching key metadata from API: ${response.status} - ${response.statusText} - ${await response.text()}`);
|
22
|
-
}
|
23
|
-
return json;
|
24
|
-
}
|
25
|
-
async function fetchAccountFromApi(jwt, config, useWalletAuth) {
|
26
|
-
const {
|
27
|
-
apiUrl,
|
28
|
-
serviceApiKey
|
29
|
-
} = config;
|
30
|
-
const url = useWalletAuth ? `${apiUrl}/v1/wallet/me` : `${apiUrl}/v1/account/me`;
|
31
|
-
const response = await fetch(url, {
|
32
|
-
method: "GET",
|
33
|
-
headers: {
|
34
|
-
"x-service-api-key": serviceApiKey,
|
35
|
-
"content-type": "application/json",
|
36
|
-
authorization: `Bearer ${jwt}`
|
37
|
-
}
|
38
|
-
});
|
39
|
-
let json;
|
40
|
-
try {
|
41
|
-
json = await response.json();
|
42
|
-
} catch (e) {
|
43
|
-
throw new Error(`Error fetching account from API: ${response.status} - ${response.statusText} - ${await response.text()}`);
|
44
|
-
}
|
45
|
-
return json;
|
46
|
-
}
|
47
|
-
|
48
|
-
function authorizeClient(authOptions, apiKeyMeta) {
|
49
|
-
const {
|
50
|
-
origin,
|
51
|
-
bundleId,
|
52
|
-
secretKeyHash: providedSecretHash
|
53
|
-
} = authOptions;
|
54
|
-
const {
|
55
|
-
domains,
|
56
|
-
bundleIds,
|
57
|
-
secretHash
|
58
|
-
} = apiKeyMeta;
|
59
|
-
const authResult = {
|
60
|
-
authorized: true,
|
61
|
-
apiKeyMeta,
|
62
|
-
accountMeta: {
|
63
|
-
id: apiKeyMeta.accountId,
|
64
|
-
// TODO update this later
|
65
|
-
name: "",
|
66
|
-
creatorWalletAddress: apiKeyMeta.creatorWalletAddress
|
67
|
-
}
|
68
|
-
};
|
69
|
-
|
70
|
-
// check for public restrictions
|
71
|
-
if (domains.includes("*")) {
|
72
|
-
return authResult;
|
73
|
-
}
|
74
|
-
|
75
|
-
// check for secretHash
|
76
|
-
if (providedSecretHash) {
|
77
|
-
if (secretHash !== providedSecretHash) {
|
78
|
-
return {
|
79
|
-
authorized: false,
|
80
|
-
errorMessage: "Incorrect key provided. You can view your active API keys at https://thirdweb.com/dashboard/settings",
|
81
|
-
errorCode: "SECRET_INVALID",
|
82
|
-
status: 401
|
83
|
-
};
|
84
|
-
}
|
85
|
-
return authResult;
|
86
|
-
}
|
87
|
-
|
88
|
-
// validate domains
|
89
|
-
if (origin) {
|
90
|
-
if (
|
91
|
-
// find matching domain, or if all domains allowed
|
92
|
-
domains.find(d => {
|
93
|
-
// if any domain is allowed, we'll return true
|
94
|
-
if (d === "*") {
|
95
|
-
return true;
|
96
|
-
}
|
97
|
-
|
98
|
-
// special rule for `localhost`
|
99
|
-
// if the domain is localhost, we'll allow any origin that starts with localhost
|
100
|
-
if (d === "localhost" && origin.startsWith("localhost")) {
|
101
|
-
return true;
|
102
|
-
}
|
103
|
-
|
104
|
-
// If the allowedDomain has a wildcard,
|
105
|
-
// we'll check that the ending of our domain matches the wildcard
|
106
|
-
if (d.startsWith("*.")) {
|
107
|
-
// get rid of the * and check if it ends with the `.<domain>.<tld>`
|
108
|
-
const domainRoot = d.slice(1);
|
109
|
-
return origin.endsWith(domainRoot);
|
110
|
-
}
|
111
|
-
|
112
|
-
// If there's no wildcard, we'll check for an exact match
|
113
|
-
return d === origin;
|
114
|
-
})) {
|
115
|
-
return authResult;
|
116
|
-
}
|
117
|
-
return {
|
118
|
-
authorized: false,
|
119
|
-
errorMessage: `Invalid request: Unauthorized domain: ${origin}. You can view the restrictions on this API key at https://thirdweb.com/create-api-key`,
|
120
|
-
errorCode: "ORIGIN_UNAUTHORIZED",
|
121
|
-
status: 401
|
122
|
-
};
|
123
|
-
}
|
124
|
-
|
125
|
-
// validate bundleId
|
126
|
-
if (bundleId) {
|
127
|
-
if (
|
128
|
-
// find matching bundle id, or if all bundles allowed
|
129
|
-
bundleIds.find(b => {
|
130
|
-
if (b === "*") {
|
131
|
-
return true;
|
132
|
-
}
|
133
|
-
return b === bundleId;
|
134
|
-
})) {
|
135
|
-
return authResult;
|
136
|
-
}
|
137
|
-
return {
|
138
|
-
authorized: false,
|
139
|
-
errorMessage: `Invalid request: Unauthorized Bundle ID: ${bundleId}. You can view the restrictions on this API key at https://thirdweb.com/create-api-key`,
|
140
|
-
errorCode: "BUNDLE_UNAUTHORIZED",
|
141
|
-
status: 401
|
142
|
-
};
|
143
|
-
}
|
144
|
-
return {
|
145
|
-
authorized: false,
|
146
|
-
errorMessage: "The keys are invalid. Please check the secret-key/clientId and try again.",
|
147
|
-
errorCode: "UNAUTHORIZED",
|
148
|
-
status: 401
|
149
|
-
};
|
150
|
-
}
|
151
|
-
|
152
|
-
function authorizeService(apiKeyMetadata, serviceConfig, authorizationPayload) {
|
153
|
-
const {
|
154
|
-
services
|
155
|
-
} = apiKeyMetadata;
|
156
|
-
// validate services
|
157
|
-
const service = services.find(srv => srv.name === serviceConfig.serviceScope);
|
158
|
-
if (!service) {
|
159
|
-
return {
|
160
|
-
authorized: false,
|
161
|
-
errorMessage: `Invalid request: Unauthorized service: ${serviceConfig.serviceScope}. You can view the restrictions on this API key in your dashboard: https://thirdweb.com/create-api-key`,
|
162
|
-
errorCode: "SERVICE_UNAUTHORIZED",
|
163
|
-
status: 403
|
164
|
-
};
|
165
|
-
}
|
166
|
-
|
167
|
-
// validate service actions
|
168
|
-
if (serviceConfig.serviceAction) {
|
169
|
-
const isActionAllowed = service.actions.includes(serviceConfig.serviceAction);
|
170
|
-
if (!isActionAllowed) {
|
171
|
-
return {
|
172
|
-
authorized: false,
|
173
|
-
errorMessage: `Invalid request: Unauthorized action: ${serviceConfig.serviceScope} ${serviceConfig.serviceAction}. You can view the restrictions on this API key in your dashboard: https://thirdweb.com/create-api-key`,
|
174
|
-
errorCode: "SERVICE_ACTION_UNAUTHORIZED",
|
175
|
-
status: 403
|
176
|
-
};
|
177
|
-
}
|
178
|
-
}
|
179
|
-
|
180
|
-
// validate service target addresses
|
181
|
-
// the service has to pass in the target address for this to be validated
|
182
|
-
if (authorizationPayload?.targetAddress) {
|
183
|
-
const checkedAddresses = Array.isArray(authorizationPayload.targetAddress) ? authorizationPayload.targetAddress : [authorizationPayload.targetAddress];
|
184
|
-
const allAllowed = service.targetAddresses.includes("*");
|
185
|
-
if (!allAllowed && checkedAddresses.some(ta => !service.targetAddresses.includes(ta))) {
|
186
|
-
return {
|
187
|
-
authorized: false,
|
188
|
-
errorMessage: `Invalid request: Unauthorized address: ${serviceConfig.serviceScope} ${checkedAddresses}. You can view the restrictions on this API key in your dashboard: https://thirdweb.com/create-api-key`,
|
189
|
-
errorCode: "SERVICE_TARGET_ADDRESS_UNAUTHORIZED",
|
190
|
-
status: 403
|
191
|
-
};
|
192
|
-
}
|
193
|
-
}
|
194
|
-
return {
|
195
|
-
authorized: true,
|
196
|
-
accountMeta: {
|
197
|
-
id: apiKeyMetadata.accountId,
|
198
|
-
name: "",
|
199
|
-
creatorWalletAddress: apiKeyMetadata.creatorWalletAddress
|
200
|
-
},
|
201
|
-
apiKeyMeta: apiKeyMetadata
|
202
|
-
};
|
203
|
-
}
|
204
|
-
|
205
|
-
async function authorize(authData, serviceConfig, cacheOptions) {
|
206
|
-
const {
|
207
|
-
clientId,
|
208
|
-
targetAddress,
|
209
|
-
secretKeyHash,
|
210
|
-
jwt,
|
211
|
-
hashedJWT,
|
212
|
-
useWalletAuth
|
213
|
-
} = authData;
|
214
|
-
const {
|
215
|
-
enforceAuth
|
216
|
-
} = serviceConfig;
|
217
|
-
|
218
|
-
// BACKWARDS COMPAT: if auth not enforced and we don't have auth credentials bypass
|
219
|
-
if (!enforceAuth && !clientId && !secretKeyHash) {
|
220
|
-
return {
|
221
|
-
authorized: true,
|
222
|
-
apiKeyMeta: null,
|
223
|
-
accountMeta: null
|
224
|
-
};
|
225
|
-
}
|
226
|
-
// if we come in with a JWT then we only check the account is valid
|
227
|
-
if (jwt && hashedJWT) {
|
228
|
-
let accountMeta = null;
|
229
|
-
if (cacheOptions) {
|
230
|
-
try {
|
231
|
-
const cachedAccountInfo = await cacheOptions.get(hashedJWT);
|
232
|
-
if (cachedAccountInfo) {
|
233
|
-
const parsed = JSON.parse(cachedAccountInfo);
|
234
|
-
if ("updatedAt" in parsed) {
|
235
|
-
// we want to compare the updatedAt time to the current time
|
236
|
-
// if the difference is greater than the cacheTtl we want to ignore the cached data
|
237
|
-
const now = Date.now();
|
238
|
-
const diff = now - parsed.updatedAt;
|
239
|
-
const cacheTtl = cacheOptions.cacheTtlSeconds * 1000;
|
240
|
-
// only if the diff is less than the cacheTtl do we want to use the cached key
|
241
|
-
if (diff < cacheTtl * 1000) {
|
242
|
-
accountMeta = parsed.apiKeyMeta;
|
243
|
-
}
|
244
|
-
} else {
|
245
|
-
accountMeta = parsed;
|
246
|
-
}
|
247
|
-
}
|
248
|
-
} catch (err) {
|
249
|
-
// ignore errors, proceed as if not in cache
|
250
|
-
}
|
251
|
-
}
|
252
|
-
if (!accountMeta) {
|
253
|
-
try {
|
254
|
-
const {
|
255
|
-
data,
|
256
|
-
error
|
257
|
-
} = await fetchAccountFromApi(jwt, serviceConfig, useWalletAuth?.toLowerCase() === "true");
|
258
|
-
if (error) {
|
259
|
-
return {
|
260
|
-
authorized: false,
|
261
|
-
errorCode: error.code,
|
262
|
-
errorMessage: error.message,
|
263
|
-
status: error.statusCode
|
264
|
-
};
|
265
|
-
} else if (!data) {
|
266
|
-
return {
|
267
|
-
authorized: false,
|
268
|
-
errorCode: "NO_ACCOUNT",
|
269
|
-
errorMessage: "No error but also no account returned.",
|
270
|
-
status: 500
|
271
|
-
};
|
272
|
-
}
|
273
|
-
accountMeta = data;
|
274
|
-
if (cacheOptions) {
|
275
|
-
await cacheOptions.put(hashedJWT, accountMeta);
|
276
|
-
}
|
277
|
-
} catch (err) {
|
278
|
-
console.warn("failed to fetch account from api", err);
|
279
|
-
return {
|
280
|
-
authorized: false,
|
281
|
-
status: 500,
|
282
|
-
errorMessage: "Failed to get account information.",
|
283
|
-
errorCode: "FAILED_TO_LOAD_ACCOUNT"
|
284
|
-
};
|
285
|
-
}
|
286
|
-
}
|
287
|
-
// if we still don't have an accountMeta at this point we can't authorize
|
288
|
-
if (!accountMeta) {
|
289
|
-
return {
|
290
|
-
authorized: false,
|
291
|
-
status: 401,
|
292
|
-
errorMessage: "Missing account information.",
|
293
|
-
errorCode: "MISSING_ACCOUNT"
|
294
|
-
};
|
295
|
-
}
|
296
|
-
// otherwise we want to return early with the accountMeta
|
297
|
-
return {
|
298
|
-
authorized: true,
|
299
|
-
apiKeyMeta: null,
|
300
|
-
accountMeta
|
301
|
-
};
|
302
|
-
}
|
303
|
-
|
304
|
-
// if we don't have a client id at this point we can't authorize
|
305
|
-
if (!clientId) {
|
306
|
-
return {
|
307
|
-
authorized: false,
|
308
|
-
status: 401,
|
309
|
-
errorMessage: "Missing clientId or secretKey.",
|
310
|
-
errorCode: "MISSING_KEY"
|
311
|
-
};
|
312
|
-
}
|
313
|
-
let apiKeyMeta = null;
|
314
|
-
// if we have cache options we want to check the cache first
|
315
|
-
if (cacheOptions) {
|
316
|
-
try {
|
317
|
-
const cachedKey = await cacheOptions.get(clientId);
|
318
|
-
if (cachedKey) {
|
319
|
-
const parsed = JSON.parse(cachedKey);
|
320
|
-
if ("updatedAt" in parsed) {
|
321
|
-
// we want to compare the updatedAt time to the current time
|
322
|
-
// if the difference is greater than the cacheTtl we want to ignore the cached data
|
323
|
-
const now = Date.now();
|
324
|
-
const diff = now - parsed.updatedAt;
|
325
|
-
const cacheTtl = cacheOptions.cacheTtlSeconds * 1000;
|
326
|
-
// only if the diff is less than the cacheTtl do we want to use the cached key
|
327
|
-
if (diff < cacheTtl * 1000) {
|
328
|
-
apiKeyMeta = parsed.apiKeyMeta;
|
329
|
-
}
|
330
|
-
} else {
|
331
|
-
apiKeyMeta = parsed;
|
332
|
-
}
|
333
|
-
}
|
334
|
-
} catch (err) {
|
335
|
-
// ignore errors, proceed as if not in cache
|
336
|
-
}
|
337
|
-
}
|
338
|
-
|
339
|
-
// if we don't have a cached key, fetch from the API
|
340
|
-
if (!apiKeyMeta) {
|
341
|
-
try {
|
342
|
-
const {
|
343
|
-
data,
|
344
|
-
error
|
345
|
-
} = await fetchKeyMetadataFromApi(clientId, serviceConfig);
|
346
|
-
if (error) {
|
347
|
-
return {
|
348
|
-
authorized: false,
|
349
|
-
errorCode: error.code,
|
350
|
-
errorMessage: error.message,
|
351
|
-
status: error.statusCode
|
352
|
-
};
|
353
|
-
} else if (!data) {
|
354
|
-
return {
|
355
|
-
authorized: false,
|
356
|
-
errorCode: "NO_KEY",
|
357
|
-
errorMessage: "No error but also no key returned.",
|
358
|
-
status: 500
|
359
|
-
};
|
360
|
-
}
|
361
|
-
// if we have a key for sure then assign it
|
362
|
-
apiKeyMeta = data;
|
363
|
-
|
364
|
-
// cache the retrieved key if we have cache options
|
365
|
-
if (cacheOptions) {
|
366
|
-
// we await this always because it can be a promise or not
|
367
|
-
await cacheOptions.put(clientId, data);
|
368
|
-
}
|
369
|
-
} catch (err) {
|
370
|
-
console.warn("failed to fetch key metadata from api", err);
|
371
|
-
return {
|
372
|
-
authorized: false,
|
373
|
-
status: 500,
|
374
|
-
errorMessage: "Failed to fetch key metadata. Please check your secret-key/clientId.",
|
375
|
-
errorCode: "FAILED_TO_FETCH_KEY"
|
376
|
-
};
|
377
|
-
}
|
378
|
-
}
|
379
|
-
if (!apiKeyMeta) {
|
380
|
-
return {
|
381
|
-
authorized: false,
|
382
|
-
status: 401,
|
383
|
-
errorMessage: "Key is invalid. Please check your secret-key/clientId.",
|
384
|
-
errorCode: "INVALID_KEY"
|
385
|
-
};
|
386
|
-
}
|
387
|
-
// now we can validate the key itself
|
388
|
-
const clientAuth = authorizeClient(authData, apiKeyMeta);
|
389
|
-
if (!clientAuth.authorized) {
|
390
|
-
return {
|
391
|
-
errorCode: clientAuth.errorCode,
|
392
|
-
authorized: false,
|
393
|
-
status: 401,
|
394
|
-
errorMessage: clientAuth.errorMessage
|
395
|
-
};
|
396
|
-
}
|
397
|
-
|
398
|
-
// if we've made it this far we need to check service specific authorization
|
399
|
-
const serviceAuth = authorizeService(apiKeyMeta, serviceConfig, {
|
400
|
-
targetAddress
|
401
|
-
});
|
402
|
-
if (!serviceAuth.authorized) {
|
403
|
-
return {
|
404
|
-
errorCode: serviceAuth.errorCode,
|
405
|
-
authorized: false,
|
406
|
-
status: 403,
|
407
|
-
errorMessage: serviceAuth.errorMessage
|
408
|
-
};
|
409
|
-
}
|
410
|
-
|
411
|
-
// if we reach this point we are authorized!
|
412
|
-
return {
|
413
|
-
authorized: true,
|
414
|
-
apiKeyMeta,
|
415
|
-
accountMeta: {
|
416
|
-
id: apiKeyMeta.accountId,
|
417
|
-
// TODO update this later
|
418
|
-
name: "",
|
419
|
-
creatorWalletAddress: apiKeyMeta.creatorWalletAddress
|
420
|
-
}
|
421
|
-
};
|
422
|
-
}
|
423
|
-
|
424
|
-
exports.authorize = authorize;
|