Files

106 lines
2.4 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Grocy\Services;
class ApiKeyService extends BaseService
{
const API_KEY_TYPE_DEFAULT = 'default';
2020-08-31 20:40:31 +02:00
const API_KEY_TYPE_SPECIAL_PURPOSE_CALENDAR_ICAL = 'special-purpose-calendar-ical';
2026-03-02 20:58:11 +01:00
public function CreateApiKey(string $keyType = self::API_KEY_TYPE_DEFAULT, ?string $description = null)
{
$newApiKey = $this->GenerateApiKey();
2026-04-20 22:46:47 +02:00
$apiKeyRow = $this->DB->api_keys()->createRow([
'api_key' => $newApiKey,
2018-07-25 19:28:15 +02:00
'user_id' => GROCY_USER_ID,
2023-05-23 20:31:51 +02:00
'expires' => '2999-12-31 23:59:59', // Default is that API keys never expire
'key_type' => $keyType,
'description' => $description
2020-08-31 20:40:31 +02:00
]);
$apiKeyRow->save();
return $newApiKey;
}
public function GetApiKeyId($apiKey)
{
2026-04-20 22:46:47 +02:00
$apiKey = $this->DB->api_keys()->where('api_key', $apiKey)->fetch();
return $apiKey->id;
}
2020-09-01 21:29:47 +02:00
// Returns any valid key for $keyType,
2020-08-31 20:40:31 +02:00
// not allowed for key type "default"
public function GetOrCreateApiKey($keyType)
{
if ($keyType === self::API_KEY_TYPE_DEFAULT)
{
return null;
}
else
{
2026-04-20 22:46:47 +02:00
$apiKeyRow = $this->DB->api_keys()->where('key_type = :1 AND expires > :2', $keyType, date('Y-m-d H:i:s', time()))->fetch();
2020-08-31 20:40:31 +02:00
if ($apiKeyRow !== null)
{
return $apiKeyRow->api_key;
}
else
{
return $this->CreateApiKey($keyType);
}
}
}
2018-07-25 19:28:15 +02:00
public function GetUserByApiKey($apiKey)
{
2026-04-20 22:46:47 +02:00
$apiKeyRow = $this->DB->api_keys()->where('api_key', $apiKey)->fetch();
2020-08-31 20:40:31 +02:00
2018-07-25 19:28:15 +02:00
if ($apiKeyRow !== null)
{
2026-04-20 22:46:47 +02:00
return $this->DB->users($apiKeyRow->user_id);
2018-07-25 19:28:15 +02:00
}
2020-08-31 20:40:31 +02:00
2018-07-25 19:28:15 +02:00
return null;
}
2020-08-31 20:40:31 +02:00
public function IsValidApiKey($apiKey, $keyType = self::API_KEY_TYPE_DEFAULT)
{
2020-08-31 20:40:31 +02:00
if ($apiKey === null || empty($apiKey))
{
2020-08-31 20:40:31 +02:00
return false;
}
else
{
2026-04-20 22:46:47 +02:00
$apiKeyRow = $this->DB->api_keys()->where('api_key = :1 AND expires > :2 AND key_type = :3', $apiKey, date('Y-m-d H:i:s', time()), $keyType)->fetch();
2020-08-31 20:40:31 +02:00
if ($apiKeyRow !== null)
{
2020-09-01 21:29:47 +02:00
// This should not change the database file modification time as this is used
2020-08-31 20:40:31 +02:00
// to determine if REALLY something has changed
2026-04-20 22:46:47 +02:00
$dbModTime = DatabaseService::GetInstance()->GetDbChangedTime();
2020-08-31 20:40:31 +02:00
$apiKeyRow->update([
'last_used' => date('Y-m-d H:i:s', time())
]);
2026-04-20 22:46:47 +02:00
DatabaseService::GetInstance()->SetDbChangedTime($dbModTime);
2020-08-31 20:40:31 +02:00
return true;
}
else
{
2020-08-31 20:40:31 +02:00
return false;
}
}
2020-08-31 20:40:31 +02:00
}
public function RemoveApiKey($apiKey)
{
2026-04-20 22:46:47 +02:00
$this->DB->api_keys()->where('api_key', $apiKey)->delete();
}
private function GenerateApiKey()
{
return RandomString(50);
}
}