diff --git a/docs/PolicyServer.md b/docs/PolicyServer.md index 20c4ea956..be49539c3 100644 --- a/docs/PolicyServer.md +++ b/docs/PolicyServer.md @@ -52,18 +52,31 @@ Called whenever a DDO is updated by indexer } ``` +### validateDDO + +Called whenever a DDO is validated + +```json +{ + "action":"validateDDO", + "rawDDO": {..}, + "publisherAddress": '0x001', + "policyServer": {} +} +``` + ### initialize Called whenever a new initialize command is received by Ocean Node ```json { - "action":"initialize", - "documentId": "did:op:123", - "ddo": {}, - "serviceId": "0x123", - "consumerAddress": "0x123" - "policyServer": {} + "action": "initialize", + "documentId": "did:op:123", + "ddo": {}, + "serviceId": "0x123", + "consumerAddress": "0x123", + "policyServer": {} } ``` diff --git a/src/@types/commands.ts b/src/@types/commands.ts index 4a51ff2e6..a3060b725 100644 --- a/src/@types/commands.ts +++ b/src/@types/commands.ts @@ -88,6 +88,7 @@ export interface ValidateDDOCommand extends Command { nonce?: string signature?: string message?: string + policyServer?: any // object to pass to policy server } export interface StatusCommand extends Command { diff --git a/src/components/core/handler/ddoHandler.ts b/src/components/core/handler/ddoHandler.ts index 3438fcfae..97a21217c 100644 --- a/src/components/core/handler/ddoHandler.ts +++ b/src/components/core/handler/ddoHandler.ts @@ -19,7 +19,12 @@ import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templat // import lzma from 'lzma-native' import lzmajs from 'lzma-purejs-requirejs' import { getValidationSignature } from '../utils/validateDdoHandler.js' -import { getConfiguration, hasP2PInterface } from '../../../utils/config.js' +import { + getConfiguration, + hasP2PInterface, + isPolicyServerConfigured +} from '../../../utils/config.js' +import { PolicyServer } from '../../policyServer/index.js' import { GetDdoCommand, FindDDOCommand, @@ -839,6 +844,27 @@ export class ValidateDDOHandler extends CommandHandler { status: { httpStatus: 400, error: `Validation error: ${validation[1]}` } } } + if (isPolicyServerConfigured()) { + const policyServer = new PolicyServer() + const response = await policyServer.validateDDO( + task.ddo, + task.publisherAddress, + task.policyServer + ) + if (!response) { + CORE_LOGGER.logMessage( + `Error: Validation for ${task.publisherAddress} was denied`, + true + ) + return { + stream: null, + status: { + httpStatus: 403, + error: `Error: Validation for ${task.publisherAddress} was denied` + } + } + } + } return { stream: shouldSign ? Readable.from( diff --git a/src/components/policyServer/index.ts b/src/components/policyServer/index.ts index 87deb51ea..0ff9eafc2 100644 --- a/src/components/policyServer/index.ts +++ b/src/components/policyServer/index.ts @@ -70,6 +70,20 @@ export class PolicyServer { return await this.askServer(command) } + async validateDDO( + rawDDO: DDO, + publisherAddress: string, + policyServer: any + ): Promise { + const command = { + action: 'validateDDO', + rawDDO, + publisherAddress, + policyServer + } + return await this.askServer(command) + } + async checkEncrypt( consumerAddress: string, policyServer: any