-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e468fea
Showing
3 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# CitadelClient | ||
|
||
CitadelClient is a PHP library for interacting with the Citadel API. | ||
|
||
## Installation | ||
|
||
You can install this library via Composer: | ||
|
||
```bash | ||
composer require everluitonsk/citadel-client | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
<?php | ||
|
||
require_once __DIR__ . '/vendor/autoload.php'; | ||
|
||
use CitadelClient\HttpClient; | ||
use CitadelClient\SessionResolveRequest; | ||
|
||
// Initialize the HTTP client | ||
$client = new HttpClient('https://api.citadel.example', 'your-pre-shared-key'); | ||
|
||
// Prepare the session resolve request | ||
$request = new SessionResolveRequest(); | ||
$request->cookieHeader = 'your-cookie-header'; | ||
$request->clientId = 'your-client-id'; | ||
$request->clientSecret = 'your-client-secret'; | ||
|
||
try { | ||
// Resolve the session | ||
$response = $client->sessionResolve($request); | ||
|
||
// Handle the response | ||
if ($response->session) { | ||
echo "Session resolved successfully:\n"; | ||
echo "Session ID: " . $response->session->id . "\n"; | ||
// Other session details... | ||
} else { | ||
echo "No session resolved.\n"; | ||
echo "Recommended action: " . $response->recommended->action . "\n"; | ||
// Other recommendations... | ||
} | ||
} catch (\Exception $e) { | ||
// Handle errors | ||
echo "Error: " . $e->getMessage() . "\n"; | ||
} | ||
``` | ||
|
||
Replace 'https://api.citadel.example', 'your-pre-shared-key', 'your-cookie-header', 'your-client-id', and 'your-client-secret' with your actual Citadel API endpoint, pre-shared key, cookie header, client ID, and client secret respectively. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "everluitonsk/citadel-client", | ||
"description": "Client library for interacting with the Citadel API", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Ivan Barlog", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.0", | ||
"guzzlehttp/guzzle": "^7.0" | ||
}, | ||
"repositories": [ | ||
{ | ||
"type": "vcs", | ||
"url": "https://github.com/BeeSolve/citadel-php" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"CitadelClient\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace CitadelClient; | ||
|
||
class ResolveSessionResponse { | ||
public ?ResolvedSession $session; | ||
public Recommended $recommended; | ||
} | ||
|
||
class ResolvedSession { | ||
public string $id; | ||
public string $sid; | ||
public array $identities; | ||
public string $audience; | ||
public \DateTime $issuedAt; | ||
public \DateTime $refreshedAt; | ||
public \DateTime $expiresAt; | ||
public \DateTime $resolvedAt; | ||
} | ||
|
||
class ResolvedIdentity { | ||
public string $id; | ||
public \DateTime $assignedAt; | ||
public string $user; | ||
public array $data; | ||
} | ||
|
||
class ResolvedValue { | ||
public string $name; | ||
public $value; // It can be string, number, boolean, or null | ||
public string $from; | ||
} | ||
|
||
class MultiValueHeaders { | ||
public array $headers; | ||
} | ||
|
||
class Recommended { | ||
public string $action; | ||
public MultiValueHeaders $responseHeaders; | ||
public string $reason; | ||
} | ||
|
||
class SessionResolveRequest { | ||
public string $cookieHeader; | ||
public string $clientId; | ||
public string $clientSecret; | ||
} | ||
|
||
class SessionResolveResponse { | ||
public ?ResolvedSession $session; | ||
public Recommended $recommended; | ||
} | ||
|
||
class SessionRevokeRequest { | ||
public string $cookieHeader; | ||
public string $clientId; | ||
public array $clientSecret; | ||
} | ||
|
||
class SessionRevokeResponse { | ||
public array $responseHeaders; | ||
} | ||
|
||
class SessionResolveBearerRequest { | ||
public string $token; | ||
} | ||
|
||
class SessionResolveBearerResponse { | ||
public ?ResolvedSession $session; | ||
} | ||
|
||
interface Client { | ||
public function sessionResolve(SessionResolveRequest $request): ResolveSessionResponse; | ||
public function sessionRevoke(SessionRevokeRequest $request): SessionRevokeResponse; | ||
public function sessionResolveBearer(SessionResolveBearerRequest $request): SessionResolveBearerResponse; | ||
} | ||
|
||
class HttpClient implements Client { | ||
private string $baseUrl; | ||
private \GuzzleHttp\Client $client; | ||
private string $preSharedKey; | ||
|
||
public function __construct(string $baseUrl, string $preSharedKey) { | ||
$this->baseUrl = $baseUrl; | ||
$this->client = new \GuzzleHttp\Client(); | ||
$this->preSharedKey = $preSharedKey; | ||
} | ||
|
||
public function sessionResolve(SessionResolveRequest $request): ResolveSessionResponse { | ||
$response = $this->sendRequest('/sessions.resolve', $request); | ||
return $response; | ||
} | ||
|
||
public function sessionRevoke(SessionRevokeRequest $request): SessionRevokeResponse { | ||
$response = $this->sendRequest('/sessions.revoke', $request); | ||
return $response; | ||
} | ||
|
||
public function sessionResolveBearer(SessionResolveBearerRequest $request): SessionResolveBearerResponse { | ||
$response = $this->sendRequest('/sessions.resolveBearer', $request); | ||
return $response; | ||
} | ||
|
||
private function sendRequest(string $action, $request) { | ||
$requestBody = json_encode($request); | ||
$response = $this->client->post($this->baseUrl . $action, [ | ||
'headers' => ['Content-Type' => 'application/json'], | ||
'body' => $requestBody | ||
]); | ||
|
||
$statusCode = $response->getStatusCode(); | ||
$body = $response->getBody()->getContents(); | ||
|
||
if ($statusCode === 400) { | ||
$errorResponse = json_decode($body); | ||
throw new \Exception("API error ({$errorResponse->errorId}): {$errorResponse->error}"); | ||
} | ||
|
||
return json_decode($body); | ||
} | ||
} | ||
|