Skip to content

Commit

Permalink
Add support for user specific api key management. (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeet authored Mar 9, 2023
1 parent d6f176f commit 041fc08
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 10 deletions.
29 changes: 25 additions & 4 deletions examples/api_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// Fill in with your BTCPay Server data.
$apiKey = '';
$host = ''; // e.g. https://your.btcpay-server.tld
$storeId = '';
$invoiceId = '';
$userEmail = '';
$userId = '';

// Get information about store on BTCPay Server.
try {
Expand All @@ -17,11 +17,32 @@
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}

// Create a new api key. Needs server modify permission of used api.
/*
print("\nCreate a new api key (needs server modify permission of used api).\n");
try {
$client = new Apikey($host, $apiKey);
var_dump($client->createApiKey('api generated', ['btcpay.store.canmodifystoresettings']));
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
*/
print("\nCreate a new api key for different user. Needs unrestricted access\n");

try {
$client = new Apikey($host, $apiKey);
$uKey = $client->createApiKeyForUser($userEmail, 'api generated to be deleted', ['btcpay.store.canmodifystoresettings']);
var_dump($uKey);
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}


print("\nRevoke api key for different user.\n");

try {
$client = new Apikey($host, $apiKey);
$uKey = $client->revokeApiKeyForUser($userEmail, $uKey->getData()['apiKey']);
var_dump($uKey);
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
95 changes: 93 additions & 2 deletions src/Client/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function getAuthorizeUrl(string $baseUrl, array $permissions, ?str
}

/**
* Get the current API Key information
* Get the current API key information
*/
public function getCurrent(): ResultApiKey
{
Expand All @@ -79,7 +79,11 @@ public function getCurrent(): ResultApiKey
}

/**
* Create a new API Key.
* Create a new API key for current user.
*
* @param string $label Visible label on API key overview
* @param array $permissions The permissions array can contain specific store id
* e.g. btcpay.server.canmanageusers:2KxSpc9V5zDWfUbvgYiZuAfka4wUhGF96F75Ao8y4zHP
*/
public function createApikey(?string $label = null, ?array $permissions = null): ResultApiKey
{
Expand All @@ -103,4 +107,91 @@ public function createApikey(?string $label = null, ?array $permissions = null):
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}

/**
* Create a new API key for a user.
*
* @param string $userId Can be user id or email.
* @param string $label Visible label on API key overview
* @param array $permissions The permissions array can contain specific store id
* e.g. btcpay.server.canmanageusers:2KxSpc9V5zDWfUbvgYiZuAfka4wUhGF96F75Ao8y4zHP
*/
public function createApiKeyForUser(
string $idOrMail,
?string $label = null,
?array $permissions = null
): ResultApiKey {
$url = $this->getApiUrl() . 'users/' . urlencode($idOrMail) . '/api-keys';
$headers = $this->getRequestHeaders();
$method = 'POST';

$body = json_encode(
[
'label' => $label,
'permissions' => $permissions
],
JSON_THROW_ON_ERROR
);

$response = $this->getHttpClient()->request($method, $url, $headers, $body);

if ($response->getStatus() === 200) {
return new ResultApiKey(json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR));
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}


/**
* Revokes the current API key.
*/
public function revokeCurrentApiKey(): bool
{
$url = $this->getApiUrl() . 'api-keys/current';
$headers = $this->getRequestHeaders();
$method = 'DELETE';
$response = $this->getHttpClient()->request($method, $url, $headers);

if ($response->getStatus() === 200) {
return true;
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}

/**
* Revokes an API key for current user.
*/
public function revokeApiKey(string $apiKey): bool
{
$url = $this->getApiUrl() . 'api-keys/' . urlencode($apiKey);
$headers = $this->getRequestHeaders();
$method = 'DELETE';
$response = $this->getHttpClient()->request($method, $url, $headers);

if ($response->getStatus() === 200) {
return true;
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}


/**
* Revokes the API key of target user.
*/
public function revokeApiKeyForUser(string $idOrMail, string $apiKey): bool
{
$url = $this->getApiUrl() . 'users/' . urlencode($idOrMail) . '/api-keys/' . urlencode($apiKey) ;
$headers = $this->getRequestHeaders();
$method = 'DELETE';
$response = $this->getHttpClient()->request($method, $url, $headers);

if ($response->getStatus() === 200) {
return true;
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}
}
8 changes: 4 additions & 4 deletions src/Client/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public function createUser(
}
}

public function deleteUser(string $userId): bool
public function deleteUser(string $idOrMail): bool
{
$url = $this->getApiUrl() . 'users/' . urlencode($userId);
$url = $this->getApiUrl() . 'users/' . urlencode($idOrMail);
$headers = $this->getRequestHeaders();
$method = 'DELETE';
$response = $this->getHttpClient()->request($method, $url, $headers);
Expand All @@ -82,9 +82,9 @@ public function deleteUser(string $userId): bool
}
}

public function setUserLock(string $userId, bool $locked): bool
public function setUserLock(string $idOrMail, bool $locked): bool
{
$url = $this->getApiUrl() . 'users/' . urlencode($userId) . '/lock';
$url = $this->getApiUrl() . 'users/' . urlencode($idOrMail) . '/lock';
$headers = $this->getRequestHeaders();
$method = 'POST';

Expand Down

0 comments on commit 041fc08

Please sign in to comment.