Skip to content

Commit

Permalink
Merge pull request #13 from sitegeist/task/requireSigningOfAdditional…
Browse files Browse the repository at this point in the history
…Instructions

TASK: Require an Hmac for additional instructions
  • Loading branch information
mficzel authored Apr 3, 2024
2 parents f2b69a7 + a346a86 commit 04a11eb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Classes/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Neos\Cache\Frontend\VariableFrontend;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\Security\Cryptography\HashService;
use Sitegeist\Chatterbox\Domain\MessageRecord;
use Sitegeist\Chatterbox\Domain\OrganizationRepository;

Expand All @@ -25,6 +26,7 @@ class ChatController extends ActionController

public function __construct(
private readonly OrganizationRepository $organizationRepository,
private readonly HashService $hashService,
) {
}

Expand All @@ -35,6 +37,10 @@ public function injectMetaDataCache(VariableFrontend $metaDataCache): void

public function startAction(string $organizationId, string $assistantId, string $message, ?string $additionalInstructions = null): string
{
if ($additionalInstructions) {
$additionalInstructions = $this->hashService->validateAndStripHmac($additionalInstructions);
}

$organization = $this->organizationRepository->findById($organizationId);
$assistant = $organization->assistantDepartment->findAssistantById($assistantId);
$threadId = $assistant->startThread();
Expand Down Expand Up @@ -90,6 +96,10 @@ function (MessageRecord $message) use ($cachedMetadata, $assistantId, $threadId)

public function postAction(string $organizationId, string $assistantId, string $threadId, string $message, ?string $additionalInstructions = null): string
{
if ($additionalInstructions) {
$additionalInstructions = $this->hashService->validateAndStripHmac($additionalInstructions);
}

$organization = $this->organizationRepository->findById($organizationId);
$assistant = $organization->assistantDepartment->findAssistantById($assistantId);
$assistant->continueThread($threadId, $message, $additionalInstructions);
Expand Down
26 changes: 26 additions & 0 deletions Classes/Helper/InstructionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Sitegeist\Chatterbox\Helper;

use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Security\Cryptography\HashService;

class InstructionHelper implements ProtectedContextAwareInterface
{
public function __construct(
private readonly HashService $hashService
) {
}

public function signInstructionsWithHmac(string $instructions): string
{
return $this->hashService->appendHmac($instructions);
}

public function allowsCallOfMethod($methodName)
{
return true;
}
}
3 changes: 3 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ Neos:
icon: 'icon-robot'
controller: 'Sitegeist\Chatterbox\Controller\AssistantModuleController'
privilegeTarget: 'Sitegeist.Chatterbox:ManageAssistants'
Fusion:
defaultContext:
Chatterbox.Instruction: Sitegeist\Chatterbox\Helper\InstructionHelper
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
}
},
"scripts": {
"fix": ["phpcbf --standard=PSR12 Classes"],
"test:style-fix": ["phpcbf --standard=PSR12 Classes"],
"test:style": ["phpcs --standard=PSR12 -n Classes"],
"test:stan": ["phpstan analyse --level 8 Classes"],
Expand Down

0 comments on commit 04a11eb

Please sign in to comment.