Skip to content

Commit 1191234

Browse files
add api key tracking
1 parent f482945 commit 1191234

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

indexers/api/src/app.module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ApiDailyUsage } from './entities/users/api-daily-usage.entity';
1515
import { ApiKey } from './entities/users/api-key.entity';
1616
import { ApiMonthlyUsage } from './entities/users/api-monthly-usage.entity';
1717
import { Profile } from './entities/users/profile.entity';
18+
import { ApiUsageService } from './services/api-usage.service';
1819

1920
@Module({
2021
imports: [
@@ -57,6 +58,6 @@ import { Profile } from './entities/users/profile.entity';
5758
ExtrinsicsController,
5859
AccountsController,
5960
],
60-
providers: [AppService, ApiKeyStrategy],
61+
providers: [AppService, ApiKeyStrategy, ApiUsageService],
6162
})
6263
export class AppModule {}

indexers/api/src/auth/api-key.strategy.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { InjectRepository } from '@nestjs/typeorm';
44
import { HeaderAPIKeyStrategy } from 'passport-headerapikey';
55
import { Repository } from 'typeorm';
66
import { ApiKey } from '../entities/users/api-key.entity';
7+
import { ApiUsageService } from '../services/api-usage.service';
78

89
@Injectable()
910
export class ApiKeyStrategy extends PassportStrategy(
@@ -13,10 +14,9 @@ export class ApiKeyStrategy extends PassportStrategy(
1314
constructor(
1415
@InjectRepository(ApiKey)
1516
private apiKeyRepository: Repository<ApiKey>,
17+
private apiUsageService: ApiUsageService,
1618
) {
1719
super({ header: 'X-API-KEY', prefix: '' }, true, async (apiKey, done) => {
18-
console.log('apiKey', apiKey);
19-
console.log('done', done);
2020
return this.validate(apiKey, done);
2121
});
2222
}
@@ -25,10 +25,7 @@ export class ApiKeyStrategy extends PassportStrategy(
2525
apiKey: string,
2626
done: (error: Error | null, data: ApiKey | boolean) => void,
2727
) {
28-
console.log('Received API Key:', apiKey);
29-
3028
if (!apiKey) {
31-
console.log('No API Key provided');
3229
return done(new UnauthorizedException('Missing API Key'), false);
3330
}
3431

@@ -37,16 +34,15 @@ export class ApiKeyStrategy extends PassportStrategy(
3734
where: { id: apiKey },
3835
});
3936

40-
console.log('Database query result:', key);
41-
4237
if (!key || key.total_requests_remaining <= 0) {
43-
console.log('Invalid or expired API Key');
4438
return done(new UnauthorizedException('Invalid API Key'), false);
4539
}
4640

41+
// Track API usage after successful validation
42+
await this.apiUsageService.trackUsage(apiKey);
43+
4744
return done(null, key);
4845
} catch (error) {
49-
console.log('Error during validation:', error);
5046
return done(new UnauthorizedException('Invalid API Key'), false);
5147
}
5248
}

indexers/api/src/controllers/extrinsics.controller.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ export class ExtrinsicsController {
2727
) {}
2828

2929
@Get(':hash')
30-
@ApiOperation({ summary: 'Get extrinsic by hash' })
30+
@ApiOperation({
31+
operationId: 'getExtrinsicByHash',
32+
summary: 'Get extrinsic by hash',
33+
})
3134
@ApiResponse({
3235
status: 200,
3336
description: 'Returns the extrinsic details',

0 commit comments

Comments
 (0)