Skip to content

Commit

Permalink
fix(app/user/worker): remove chain id validation (#114)
Browse files Browse the repository at this point in the history
* fix(app/user/worker): remove chain id validation

* Test fix and JWT User data align

* Updates README.md: Adds Caching section, Changes in signature type

* Removes commented code

* Changes after hcaptcha api check

* Adds interceptor for axios requests

* Fix/signature (#126)

* Updates README.md (#125)

* Updates README.md: Adds Caching section, Changes in signature type

* README.md updates

* README.md updates

* fix(app/worker): add signature field to req body

* update(app): revert readme

* fix(app/register-address): fix test

---------

Co-authored-by: macnablocky <[email protected]>
Co-authored-by: Kacper Koza <[email protected]>

* Fixes undefined in POST data, fixes unit tests

* Adds Resign job feature, small changes in the gateway config (#134)

* Fixes

* Fixes in dependencies

---------

Co-authored-by: Kacper Koza <[email protected]>
Co-authored-by: maciej.nabialek <[email protected]>
Co-authored-by: macnablocky <[email protected]>
  • Loading branch information
4 people authored Jun 20, 2024
1 parent 7701d7f commit 6c1a082
Show file tree
Hide file tree
Showing 73 changed files with 4,074 additions and 4,007 deletions.
17 changes: 11 additions & 6 deletions packages/apps/human-app/server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ REPUTATION_ORACLE_URL= # string
REPUTATION_ORACLE_ADDRESS= # string
REDIS_HOST= # string, example: localhost
REDIS_PORT= # number, example: 6379
RPC_URL= # string
CHAIN_IDS_ENABLED= # number array, example: 80002,80001
HCAPTCHA_LABELING_STATS_API_URL= # string
HCAPTCHA_LABELING_VERIFY_API_URL= # string
HCAPTCHA_LABELING_API_KEY= # string
DAILY_HMT_SPENT_KEY= # string
IS_AXIOS_REQUEST_LOGGING_ENABLED= #string, true if enabled, disabled otherwise
# CACHE TTL VALUES
CACHE_TTL_ORACLE_DISCOVERY= # number, example: 43200
CACHE_TTL_ORACLE_STATS= # number, example: 900
CACHE_TTL_USER_STATS= # number, example: 86400
CACHE_TTL_EXCHANGE_ORACLE_URL= # number: example 86400
CACHE_TTL_HCAPTCHA_USER_STATS= # number: example 86400
CACHE_TTL_DAILY_HMT_SPENT= # number: example 86400
# E2E TESTING
E2E_TESTING_EMAIL_ADDRESS= # string
E2E_TESTING_PASSWORD= # string
E2E_TESTING_EXCHANGE_ORACLE_URL= # string
E2E_TESTING_ESCROW_ADDRESS= # string
E2E_TESTING_ESCROW_CHAIN_ID= # number
RPC_URL= # string
CORS_ENABLED= # boolean, example: true
# CORS
CORS_ALLOWED_ORIGIN= # string example: http://localhost:5173
CORS_ALLOWED_HEADERS= # string, example: 'Content-Type,Accept'
CHAIN_IDS_ENABLED= # number array, example: 80002,80001
HCAPTCHA_LABELING_API_URL= # string
HCAPTCHA_LABELING_API_KEY= # string
DAILY_HMT_SPENT_KEY= # string
CORS_ENABLED= # boolean, example: true
5 changes: 4 additions & 1 deletion packages/apps/human-app/server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import Joi from 'joi';
import { ChainId } from '@human-protocol/sdk';
import { RegisterAddressController } from './modules/register-address/register-address.controller';
import { RegisterAddressModule } from './modules/register-address/register-address.module';
import { InterceptorModule } from './common/interceptors/interceptor.module';

@Module({
imports: [
Expand All @@ -49,7 +50,8 @@ import { RegisterAddressModule } from './modules/register-address/register-addre
REDIS_PORT: Joi.number().required(),
REDIS_HOST: Joi.string().required(),
RPC_URL: Joi.string().required(),
HCAPTCHA_LABELING_API_URL: Joi.string().required(),
HCAPTCHA_LABELING_STATS_API_URL: Joi.string().required(),
HCAPTCHA_LABELING_VERIFY_API_URL: Joi.string().required(),
HCAPTCHA_LABELING_API_KEY: Joi.string().required(),
CHAIN_IDS_ENABLED: Joi.string()
.custom((value) => {
Expand Down Expand Up @@ -90,6 +92,7 @@ import { RegisterAddressModule } from './modules/register-address/register-addre
HCaptchaLabelingModule,
EscrowUtilsModule,
RegisterAddressModule,
InterceptorModule,
],
controllers: [
AppController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ export class EnvironmentConfigService {
return this.configService.getOrThrow<string>('REPUTATION_ORACLE_URL');
}
get reputationOracleAddress(): string {
return this.configService.getOrThrow<string>('REPUTATION_ORACLE_ADDRESS');
return this.configService
.getOrThrow<string>('REPUTATION_ORACLE_ADDRESS')
.toLowerCase();
}
get axiosRequestLoggingEnabled(): boolean {
return (
this.configService.get('IS_AXIOS_REQUEST_LOGGING_ENABLED') === 'true'
);
}
get cachePort(): number {
return this.configService.getOrThrow<number>('REDIS_PORT');
Expand Down Expand Up @@ -87,8 +94,15 @@ export class EnvironmentConfigService {
DEFAULT_CACHE_TTL_EXCHANGE_ORACLE_URL,
);
}
get hcaptchaLabelingApiUrl(): string {
return this.configService.getOrThrow<string>('HCAPTCHA_LABELING_API_URL');
get hcaptchaLabelingStatsApiUrl(): string {
return this.configService.getOrThrow<string>(
'HCAPTCHA_LABELING_STATS_API_URL',
);
}
get hcaptchaLabelingVerifyApiUrl(): string {
return this.configService.getOrThrow<string>(
'HCAPTCHA_LABELING_VERIFY_API_URL',
);
}
get hcaptchaLabelingApiKey(): string {
return this.configService.getOrThrow<string>('HCAPTCHA_LABELING_API_KEY');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { ExternalApiName } from '../enums/external-api-name';
import {
HCaptchaLabelingEndpoints,
HCaptchaLabelingStatsEndpoints, HCaptchaLabelingVerifyEndpoints,
ReputationOracleEndpoints,
} from '../enums/reputation-oracle-endpoints';
import {
Expand All @@ -17,9 +17,6 @@ export class GatewayConfigService {
JSON_HEADER = {
'Content-Type': 'application/json',
};
HCAPTCHA_API_KEY: Record<string, string> = {
api_key: this.envConfig.hcaptchaLabelingApiKey,
};
constructor(private envConfig: EnvironmentConfigService) {}

private getGatewaysConfig(): Gateways {
Expand Down Expand Up @@ -79,9 +76,11 @@ export class GatewayConfigService {
headers: this.JSON_HEADER,
},
[ReputationOracleEndpoints.ENABLE_LABELING]: {
endpoint: '/labeler/register',
endpoint: '/user/register-labeler',
method: HttpMethod.POST,
params: this.HCAPTCHA_API_KEY,
params: {
api_key: this.envConfig.hcaptchaLabelingApiKey,
},
},
[ReputationOracleEndpoints.OPERATOR_SIGNIN]: {
endpoint: '/auth/web3/signin',
Expand All @@ -95,28 +94,35 @@ export class GatewayConfigService {
},
} as Record<ReputationOracleEndpoints, GatewayEndpointConfig>,
},
[ExternalApiName.HCAPTCHA_LABELING]: {
url: this.envConfig.hcaptchaLabelingApiUrl,
[ExternalApiName.HCAPTCHA_LABELING_STATS]: {
url: this.envConfig.hcaptchaLabelingStatsApiUrl,
endpoints: {
[HCaptchaLabelingEndpoints.USER_STATS]: {
[HCaptchaLabelingStatsEndpoints.USER_STATS]: {
endpoint: '/support/labeler/', // email to append as url param
method: HttpMethod.GET,
params: this.HCAPTCHA_API_KEY,
params: {
api_key: this.envConfig.hcaptchaLabelingApiKey,
},
},
[HCaptchaLabelingEndpoints.DAILY_HMT_SPENT]: {
[HCaptchaLabelingStatsEndpoints.DAILY_HMT_SPENT]: {
endpoint: '/requester/daily_hmt_spend',
method: HttpMethod.GET,
params: {
...this.HCAPTCHA_API_KEY,
api_key: this.envConfig.hcaptchaLabelingApiKey,
actual: false,
},
},
[HCaptchaLabelingEndpoints.TOKEN_VERIFY]: {
} as Record<HCaptchaLabelingStatsEndpoints, GatewayEndpointConfig>,
},
[ExternalApiName.HCAPTCHA_LABELING_VERIFY]: {
url: this.envConfig.hcaptchaLabelingVerifyApiUrl,
endpoints: {
[HCaptchaLabelingVerifyEndpoints.TOKEN_VERIFY]: {
endpoint: '/siteverify',
method: HttpMethod.POST,
// params in this method are dynamic
},
} as Record<HCaptchaLabelingEndpoints, GatewayEndpointConfig>,
} as Record<HCaptchaLabelingVerifyEndpoints, GatewayEndpointConfig>,
},
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {
HCaptchaLabelingStatsEndpoints,
HCaptchaLabelingVerifyEndpoints,
ReputationOracleEndpoints,
} from '../enums/reputation-oracle-endpoints';

export type GatewayEndpoints =
| HCaptchaLabelingStatsEndpoints
| ReputationOracleEndpoints
| HCaptchaLabelingVerifyEndpoints;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import * as jwt from 'jsonwebtoken';
import { jwtDecode } from 'jwt-decode';
import { JwtUserData } from '../interfaces/jwt-token.model';

export const Authorization = createParamDecorator(
Expand All @@ -13,11 +13,11 @@ export const JwtPayload = createParamDecorator(
const request = ctx.switchToHttp().getRequest();
const token = request.headers['authorization']?.split(' ')[1];
if (!token) return null;

try {
const decoded = jwt.decode(token);
const decoded = jwtDecode(token);
return decoded as JwtUserData;
} catch (error) {
console.error('Error in decoding token: ', error);
return null;
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum ExternalApiName {
REPUTATION_ORACLE = 'REPUTATION_ORACLE',
HCAPTCHA_LABELING = 'HCAPTCHA_LABELING',
HCAPTCHA_LABELING_STATS = 'HCAPTCHA_LABELING_STATS',
HCAPTCHA_LABELING_VERIFY = 'HCAPTCHA_LABELING_VERIFY',
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export enum AssignmentSortField {
CREATED_AT = 'created_at',
EXPIRES_AT = 'expires_at',
}
export enum PrepareSignatureType {
export enum SignatureType {
SIGNUP = 'SIGNUP',
DISABLE_OPERATOR = 'DISABLE_OPERATOR',
SIGNIN = 'SIGNIN',
DISABLE_OPERATOR = 'DISABLE_OPERATOR',
CERTIFICATE_AUTHENTICATION = 'CERTIFICATE_AUTHENTICATION',
REGISTER_ADDRESS = 'REGISTER_ADDRESS',
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export enum ReputationOracleEndpoints {
ENABLE_LABELING = 'ENABLE_LABELING',
REGISTER_ADDRESS = 'REGISTER_ADDRESS',
}
export enum HCaptchaLabelingEndpoints {
export enum HCaptchaLabelingStatsEndpoints {
USER_STATS = 'USER_STATS',
DAILY_HMT_SPENT = 'DAILY_HMT_SPENT',
}
export enum HCaptchaLabelingVerifyEndpoints {
TOKEN_VERIFY = 'TOKEN_VERIFY',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import axios from 'axios';
import { Injectable, Logger } from '@nestjs/common';
import { EnvironmentConfigService } from '../config/environment-config.service';

@Injectable()
export class AxiosRequestInterceptor {
private readonly logger = new Logger(AxiosRequestInterceptor.name);

constructor(private configService: EnvironmentConfigService) {
this.initializeRequestInterceptor();
}

private initializeRequestInterceptor() {
if (!this.configService.axiosRequestLoggingEnabled) return;
axios.interceptors.request.use(
(config) => {
const { url, method, params, data, headers } = config;
this.logger.log('NEW REQUEST:');
this.logger.log(`Request URL: ${url}`);
this.logger.log(`Method: ${method}`);
this.logger.log(`Params: ${JSON.stringify(params ?? {})}`);
this.logger.log(`Body: ${JSON.stringify(data ?? {})}`);
this.logger.log(`Headers: ${JSON.stringify(headers ?? {})}`);
return config;
},
(error) => {
this.logger.error(`Request error: ${error.message}`);
return Promise.reject(error);
},
);

axios.interceptors.response.use(
(response) => response,
(error) => {
this.logger.error(`Response error: ${error.message}`);
return Promise.reject(error);
},
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module, Global } from '@nestjs/common';
import { AxiosRequestInterceptor } from './axios-request.interceptor';

@Global()
@Module({
providers: [AxiosRequestInterceptor],
exports: [AxiosRequestInterceptor],
})
export class InterceptorModule {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
HCaptchaLabelingEndpoints,
HCaptchaLabelingStatsEndpoints,
ReputationOracleEndpoints,
} from '../enums/reputation-oracle-endpoints';
import { ExternalApiName } from '../enums/external-api-name';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import { AutoMap } from '@automapper/classes';

export class JwtUserData {
@AutoMap()
_id: string;
userId: string;
@AutoMap()
country: string;
@AutoMap()
eth_addr: string;
address: string;
@AutoMap()
email: string;
@AutoMap()
hcaptchaSiteKey: string;
kyc_status: 'APPROVED' | 'NONE';
@AutoMap()
reputation_network: string;
@AutoMap()
site_key: string;
@AutoMap()
polygonWalletAddr: string;
iat: number;
@AutoMap()
isKYCed: boolean;
exp: number;
}
Loading

0 comments on commit 6c1a082

Please sign in to comment.