ferrero-opentext/src/OAuth2Handler.php
DJP 716f0be068 Initial commit: Ferrero OpenText Content Scaling Application
Complete PHP-based workflow application for Ferrero DAM system:
- OAuth2 authentication with automatic token management
- Campaign discovery and filtering
- Folder structure navigation
- Asset download (individual and bulk)
- Metadata extraction and display
- Clean step-by-step web interface

Status: Fully functional and production-ready

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-17 14:29:20 -04:00

161 lines
No EOL
4.4 KiB
PHP

<?php
class OAuth2Handler
{
private $clientId;
private $clientSecret;
private $tokenUrl;
private $accessToken;
private $tokenExpiry;
private $grantType;
public function __construct($clientId, $clientSecret, $tokenUrl, $grantType = 'client_credentials')
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->tokenUrl = $tokenUrl;
$this->grantType = $grantType;
$this->accessToken = null;
$this->tokenExpiry = 0;
}
public function getAccessToken()
{
if ($this->isTokenValid()) {
return $this->accessToken;
}
return $this->requestNewToken();
}
private function isTokenValid()
{
return $this->accessToken && time() < $this->tokenExpiry;
}
private function requestNewToken()
{
$postData = [
'grant_type' => $this->grantType,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->tokenUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($postData),
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json'
],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 30
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
throw new Exception("OAuth2 cURL Error: " . $error);
}
if ($httpCode !== 200) {
throw new Exception("OAuth2 Token Request Failed: HTTP {$httpCode} - " . $response);
}
$tokenData = json_decode($response, true);
if (!$tokenData || !isset($tokenData['access_token'])) {
throw new Exception("Invalid OAuth2 token response: " . $response);
}
$this->accessToken = $tokenData['access_token'];
$expiresIn = $tokenData['expires_in'] ?? 3600;
$this->tokenExpiry = time() + $expiresIn - 60; // 1 minute buffer
return $this->accessToken;
}
public function getAuthHeader()
{
$token = $this->getAccessToken();
return 'Bearer ' . $token;
}
public function refreshToken()
{
$this->accessToken = null;
$this->tokenExpiry = 0;
return $this->getAccessToken();
}
public function getTokenInfo()
{
return [
'has_token' => !empty($this->accessToken),
'expires_at' => date('Y-m-d H:i:s', $this->tokenExpiry),
'expires_in' => max(0, $this->tokenExpiry - time()),
'is_valid' => $this->isTokenValid()
];
}
}
class CredentialsManager
{
private $credsFile;
private $credentials;
public function __construct($credsFile = 'Creds.txt')
{
$this->credsFile = $credsFile;
$this->loadCredentials();
}
private function loadCredentials()
{
if (!file_exists($this->credsFile)) {
throw new Exception("Credentials file not found: " . $this->credsFile);
}
$content = file_get_contents($this->credsFile);
$lines = explode("\n", trim($content));
$this->credentials = [];
foreach ($lines as $line) {
$line = trim($line);
if (empty($line)) continue;
if (preg_match('/^Secret Key\s+(.+)$/i', $line, $matches)) {
$this->credentials['client_secret'] = trim($matches[1]);
} elseif (preg_match('/^Client ID\s+(.+)$/i', $line, $matches)) {
$this->credentials['client_id'] = trim($matches[1]);
} elseif (preg_match('/^([^:]+)[:=]\s*(.+)$/', $line, $matches)) {
$key = strtolower(str_replace(' ', '_', trim($matches[1])));
$this->credentials[$key] = trim($matches[2]);
}
}
}
public function get($key)
{
return $this->credentials[$key] ?? null;
}
public function getAll()
{
return $this->credentials;
}
public function has($key)
{
return isset($this->credentials[$key]);
}
}