nomagick commited on
Commit
bd629a8
·
unverified ·
1 Parent(s): 5d86565

search now requires authentication

Browse files
backend/functions/src/cloud-functions/searcher.ts CHANGED
@@ -93,61 +93,50 @@ export class SearcherHost extends RPCHost {
93
  const noSlashPath = decodeURIComponent(ctx.req.path).slice(1);
94
  if (!noSlashPath && !q) {
95
  const latestUser = uid ? await auth.assertUser() : undefined;
 
 
 
 
96
  if (!ctx.req.accepts('text/plain') && (ctx.req.accepts('text/json') || ctx.req.accepts('application/json'))) {
97
 
98
- return this.crawler.getIndex(latestUser);
99
  }
100
 
101
- return assignTransferProtocolMeta(`${this.crawler.getIndex(latestUser)}`,
102
  { contentType: 'text/plain', envelope: null }
103
  );
104
  }
105
 
106
- if (uid) {
107
- const user = await auth.assertUser();
108
- if (!(user.wallet.total_balance > 0)) {
109
- throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
110
- }
111
 
112
- const rateLimitPolicy = auth.getRateLimits(rpcReflect.name.toUpperCase()) || [
113
- parseInt(user.metadata?.speed_level) >= 2 ?
114
- RateLimitDesc.from({
115
- occurrence: 100,
116
- periodSeconds: 60
117
- }) :
118
- RateLimitDesc.from({
119
- occurrence: 40,
120
- periodSeconds: 60
121
- })
122
- ];
123
-
124
- const apiRoll = await this.rateLimitControl.simpleRPCUidBasedLimit(
125
- rpcReflect, uid, [rpcReflect.name.toUpperCase()],
126
- ...rateLimitPolicy
127
- );
128
 
129
- rpcReflect.finally(() => {
130
- if (chargeAmount) {
131
- auth.reportUsage(chargeAmount, `reader-${rpcReflect.name}`).catch((err) => {
132
- this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
133
- });
134
- apiRoll.chargeAmount = chargeAmount;
135
- }
136
- });
137
- } else if (ctx.req.ip) {
138
- this.threadLocal.set('ip', ctx.req.ip);
139
- const apiRoll = await this.rateLimitControl.simpleRpcIPBasedLimit(rpcReflect, ctx.req.ip, [rpcReflect.name.toUpperCase()],
140
- [
141
- // 5 requests per minute
142
- new Date(Date.now() - 60 * 1000), 5
143
- ]
144
- );
145
- rpcReflect.finally(() => {
146
- if (chargeAmount) {
147
- apiRoll.chargeAmount = chargeAmount;
148
- }
149
- });
150
- }
151
 
152
  delete crawlerOptions.html;
153
 
 
93
  const noSlashPath = decodeURIComponent(ctx.req.path).slice(1);
94
  if (!noSlashPath && !q) {
95
  const latestUser = uid ? await auth.assertUser() : undefined;
96
+ const index = this.crawler.getIndex(latestUser);
97
+ if (!uid) {
98
+ index.note = 'Authentication is required to use this endpoint. Please provide a valid API key via Authorization header.';
99
+ }
100
  if (!ctx.req.accepts('text/plain') && (ctx.req.accepts('text/json') || ctx.req.accepts('application/json'))) {
101
 
102
+ return index;
103
  }
104
 
105
+ return assignTransferProtocolMeta(`${index}`,
106
  { contentType: 'text/plain', envelope: null }
107
  );
108
  }
109
 
110
+ const user = await auth.assertUser();
111
+ if (!(user.wallet.total_balance > 0)) {
112
+ throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
113
+ }
 
114
 
115
+ const rateLimitPolicy = auth.getRateLimits(rpcReflect.name.toUpperCase()) || [
116
+ parseInt(user.metadata?.speed_level) >= 2 ?
117
+ RateLimitDesc.from({
118
+ occurrence: 100,
119
+ periodSeconds: 60
120
+ }) :
121
+ RateLimitDesc.from({
122
+ occurrence: 40,
123
+ periodSeconds: 60
124
+ })
125
+ ];
126
+
127
+ const apiRoll = await this.rateLimitControl.simpleRPCUidBasedLimit(
128
+ rpcReflect, uid!, [rpcReflect.name.toUpperCase()],
129
+ ...rateLimitPolicy
130
+ );
131
 
132
+ rpcReflect.finally(() => {
133
+ if (chargeAmount) {
134
+ auth.reportUsage(chargeAmount, `reader-${rpcReflect.name}`).catch((err) => {
135
+ this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
136
+ });
137
+ apiRoll.chargeAmount = chargeAmount;
138
+ }
139
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
  delete crawlerOptions.html;
142