Skip to content

Commit

Permalink
Adding the ability to grab a token from a policy and to conclude whet…
Browse files Browse the repository at this point in the history
…her it is valid for processing.
  • Loading branch information
mattsmithies committed Oct 7, 2024
1 parent 4eaf705 commit 3f3b82d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 90 deletions.
36 changes: 36 additions & 0 deletions src/Domain/GuardianToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Dovu\GuardianPhpSdk\Domain;

class GuardianToken
{
public function __construct(public object $token)
{
}

public static function none(): self
{
return new self((object) [
"adminId" => null,
"tokenId" => ""
]);
}


/**
* When "adminId" from parent obj is falsely then ignore "id()" fn in client.
*
* Usually this would indicate a "published" policy over a "dry run" state.
*
* @return bool
*/
public function hasValidToken(): bool
{
return !!! $this->token->adminId;
}

public function id(): string
{
return $this->token->tokenId;
}
}
103 changes: 13 additions & 90 deletions src/Service/PolicyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dovu\GuardianPhpSdk\Service;

use Dovu\GuardianPhpSdk\Domain\GuardianToken;
use Exception;

class PolicyService extends AbstractService
Expand Down Expand Up @@ -34,105 +35,27 @@ public function assign(string $username, string $policy_id, bool $assign = true)

/**
*
* @param string $policyId
* @param string $document
* @return array|\Exception
*/
public function createProject(string $policyId, string $document)
{
if (! is_array($document)) {
$document = json_decode($document, true);
}

return $this->httpClient->post(uri: "policies/{$policyId}/projects", payload: $document, jsonRequest: true);
}

/**
*
* @param string $policyId
* @param string $entityId
* @return array|\Exception
*/
public function approveProject(string $policyId, string $entityId)
{
return $this->httpClient->put("policies/{$policyId}/approval/projects/{$entityId}");
}

/**
*
* @param string $policyId
* @param string $projectId
* @param string|array $document
* @return array|\Exception
*/
public function createSite(string $policyId, string $projectId, string|array $document)
{
if (! is_array($document)) {
$document = json_decode($document, true);
}

return $this->httpClient->post(uri: "policies/{$policyId}/projects/{$projectId}/sites", payload: $document, jsonRequest: true);
}

/**
*
* @param string $policyId
* @param string $entityId
* @return array|Exception
*/
public function approveSite(string $policyId, string $entityId): array|Exception
{
return $this->httpClient->put("policies/{$policyId}/approval/sites/{$entityId}");
}

/**
* Going to make an assumption for this version of the SDK that there is only one token that is
* published, might need to revisit.
*
* @param string $policyId
* @param string $siteId
* @param string|array $document
* @return array|\Exception
* @return GuardianToken
*/
public function createClaim(string $policyId, string $siteId, string|array $document)
public function token(string $policyId): GuardianToken
{
if (! is_array($document)) {
$document = json_decode($document, true);
}

return $this->httpClient->post(uri: "policies/{$policyId}/sites/{$siteId}/claims", payload: $document, jsonRequest: true);
}
$tokens = $this->httpClient->get("tokens?policyId={$policyId}&status=All")->data();

/**
*
* @param string $policyId
* @param string $entityId
* @return array|\Exception
*/
public function approveClaim(string $policyId, string $entityId): array|Exception
{
return $this->httpClient->put("policies/{$policyId}/approval/claims/{$entityId}");
}
// object-ify Hack.
$as_list = json_decode(json_encode($tokens));

/**
*
* @param string $policyId
* @return array|\Exception
*/
public function trustChain(?string $policyId): array|Exception
{
if ($policyId === null) {
return [];
if (empty($as_list)) {
return GuardianToken::none();
}

return $this->httpClient->get("policies/{$policyId}/trustchains");
}
// Get first entry for tokens (might be improved later).
$token = $as_list[0];

/**
*
* @param string $policyId
* @return void
*/
public function token(string $policyId): array|Exception
{
return $this->httpClient->get("policies/{$policyId}/token");
return new GuardianToken($token);
}
}

0 comments on commit 3f3b82d

Please sign in to comment.