Skip to content

Commit

Permalink
use ApiScopes names instead of numbers (#51)
Browse files Browse the repository at this point in the history
- [ ] Update ExApps used in tests to provide textual scopes (ApiScope
names)

---------

Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
Co-authored-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
andrey18106 and bigcat88 authored Aug 22, 2023
1 parent 21a8bd5 commit efa55ea
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests-special.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ jobs:
sleep 5s
php occ app_ecosystem_v2:daemon:register manual_install "Manual Install" manual-install 0 0 0
php occ app_ecosystem_v2:app:register nc_py_api manual_install --json-info \
"{\"appid\":\"$APP_ID\",\"name\":\"$APP_ID\",\"daemon_config_name\":\"manual_install\",\"version\":\"$APP_VERSION\",\"secret\":\"$APP_SECRET\",\"host\":\"localhost\",\"port\":$APP_PORT,\"scopes\":{\"required\":[2, 10, 11],\"optional\":[30, 31, 32, 33]},\"protocol\":\"http\",\"system_app\":1}" \
"{\"appid\":\"$APP_ID\",\"name\":\"$APP_ID\",\"daemon_config_name\":\"manual_install\",\"version\":\"$APP_VERSION\",\"secret\":\"$APP_SECRET\",\"host\":\"localhost\",\"port\":$APP_PORT,\"scopes\":{\"required\":[\"SYSTEM\", \"FILES\", \"FILES_SHARING\"],\"optional\":[\"USER_INFO\", \"USER_STATUS\", \"NOTIFICATIONS\", \"WEATHER_STATUS\", \"TALK\"]},\"protocol\":\"http\",\"system_app\":1}" \
-e --force-scopes
kill -15 $(cat /tmp/_install.pid)
timeout 3m tail --pid=$(cat /tmp/_install.pid) -f /dev/null
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ jobs:
sleep 5s
php occ app_ecosystem_v2:daemon:register manual_install "Manual Install" manual-install 0 0 0
php occ app_ecosystem_v2:app:register nc_py_api manual_install --json-info \
"{\"appid\":\"$APP_ID\",\"name\":\"$APP_ID\",\"daemon_config_name\":\"manual_install\",\"version\":\"$APP_VERSION\",\"secret\":\"$APP_SECRET\",\"host\":\"localhost\",\"port\":$APP_PORT,\"scopes\":{\"required\":[2, 10, 11],\"optional\":[30, 31, 32, 33]},\"protocol\":\"http\",\"system_app\":1}" \
"{\"appid\":\"$APP_ID\",\"name\":\"$APP_ID\",\"daemon_config_name\":\"manual_install\",\"version\":\"$APP_VERSION\",\"secret\":\"$APP_SECRET\",\"host\":\"localhost\",\"port\":$APP_PORT,\"scopes\":{\"required\":[\"SYSTEM\", \"FILES\", \"FILES_SHARING\"],\"optional\":[\"USER_INFO\", \"USER_STATUS\", \"NOTIFICATIONS\", \"WEATHER_STATUS\", \"TALK\"]},\"protocol\":\"http\",\"system_app\":1}" \
-e --force-scopes
kill -15 $(cat /tmp/_install.pid)
timeout 3m tail --pid=$(cat /tmp/_install.pid) -f /dev/null
Expand Down
3 changes: 2 additions & 1 deletion docs/tech_details/ApiScopes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ The following API groups are currently supported:
* ``31`` USER_STATUS
* ``32`` NOTIFICATIONS
* ``33`` WEATHER_STATUS
* ``50`` TALK

These groups are identified numerically, as opposed to using names. As time progresses,
These groups are identified using names. As time progresses,
the list will steadily expand, comprehensively encompassing all potential APIs provided by Nextcloud.

There is a CLI command to list registered scopes: ``occ app_ecosystem_v2:scopes:list``.
10 changes: 5 additions & 5 deletions lib/Command/ExApp/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Prompt to approve required ExApp scopes
if (count($requestedExAppScopeGroups['required']) > 0) {
$output->writeln(sprintf('ExApp %s requested required scopes: %s', $appId, implode(', ',
$this->exAppApiScopeService->mapScopeGroupsToNames($requestedExAppScopeGroups['required']))));
$output->writeln(sprintf('ExApp %s requested required scopes: %s', $appId, implode(', ', $requestedExAppScopeGroups['required'])));
$question = new ConfirmationQuestion('Do you want to approve it? [y/N] ', false);
$confirmRequiredScopes = $helper->ask($input, $output, $question);
} else {
$confirmRequiredScopes = true;
}

// Prompt to approve optional ExApp scopes
if ($confirmRequiredScopes && count($requestedExAppScopeGroups['optional']) > 0) {
$output->writeln(sprintf('ExApp %s requested optional scopes: %s', $appId, implode(', ',
$this->exAppApiScopeService->mapScopeGroupsToNames($requestedExAppScopeGroups['optional']))));
$output->writeln(sprintf('ExApp %s requested optional scopes: %s', $appId, implode(', ', $requestedExAppScopeGroups['optional'])));
$question = new ConfirmationQuestion('Do you want to approve it? [y/N] ', false);
$confirmOptionalScopes = $helper->ask($input, $output, $question);
}
Expand Down Expand Up @@ -199,7 +199,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function registerExAppScopes($output, ExApp $exApp, array $requestedExAppScopeGroups, string $scopeType): void {
$registeredScopeGroups = [];
foreach ($requestedExAppScopeGroups as $scopeGroup) {
foreach ($this->exAppApiScopeService->mapScopeNamesToNumbers($requestedExAppScopeGroups) as $scopeGroup) {
if ($this->exAppScopesService->setExAppScopeGroup($exApp, $scopeGroup)) {
$registeredScopeGroups[] = $scopeGroup;
} else {
Expand Down
9 changes: 7 additions & 2 deletions lib/Command/ExApp/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->exAppApiScopeService->mapScopeGroupsToNames($requiredScopes))));
$question = new ConfirmationQuestion('Do you want to approve it? [y/N] ', false);
$confirmRequiredScopes = $helper->ask($input, $output, $question);
} else {
$confirmRequiredScopes = true;
}

if ($confirmRequiredScopes && count($optionalScopes) > 0) {
Expand All @@ -216,7 +218,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$newExAppScopes['optional'] = [];
}

$newExAppScopes = array_merge($newExAppScopes['required'], $newExAppScopes['optional']);
$newExAppScopes = array_merge(
$this->exAppApiScopeService->mapScopeNamesToNumbers($newExAppScopes['required']),
$this->exAppApiScopeService->mapScopeNamesToNumbers($newExAppScopes['optional'])
);
if (!$this->exAppScopeService->updateExAppScopes($exApp, $newExAppScopes)) {
$output->writeln(sprintf('Failed to update ExApp %s scopes', $appId));
return 1;
Expand Down Expand Up @@ -244,6 +249,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* @return array
*/
private function compareExAppScopes(array $currentExAppScopes, array $newExAppScopes, string $type): array {
return array_values(array_diff($newExAppScopes[$type], $currentExAppScopes));
return array_values(array_diff($this->exAppApiScopeService->mapScopeNamesToNumbers($newExAppScopes[$type]), $currentExAppScopes));
}
}
4 changes: 2 additions & 2 deletions lib/Service/AppEcosystemV2Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ public function getExAppRequestedScopes(ExApp $exApp, ?\SimpleXMLElement $infoXm
if ($scopes !== false) {
$scopes = (array) $scopes[0];
$required = array_map(function (string $scopeGroup) {
return intval($scopeGroup);
return $scopeGroup;
}, (array) $scopes['required']->value);
$optional = array_map(function (string $scopeGroup) {
return intval($scopeGroup);
return $scopeGroup;
}, (array) $scopes['optional']->value);
return [
'required' => array_values($required),
Expand Down
13 changes: 11 additions & 2 deletions lib/Service/ExAppApiScopeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function registerInitScopes(): bool {
['api_route' => '/cloud/capabilities', 'scope_group' => 1, 'name' => 'BASIC'],
['api_route' => '/cloud/apps', 'scope_group' => 2, 'name' => 'SYSTEM'],
['api_route' => '/apps/provisioning_api/api/', 'scope_group' => 2, 'name' => 'SYSTEM'],
['api_route' => '/dav/', 'scope_group' => 10, 'name' => 'DAV'],
['api_route' => '/apps/files/ajax/', 'scope_group' => 10, 'name' => 'DAV'],
['api_route' => '/dav/', 'scope_group' => 10, 'name' => 'FILES'],
['api_route' => '/apps/files/ajax/', 'scope_group' => 10, 'name' => 'FILES'],
['api_route' => '/apps/files_sharing/api/', 'scope_group' => 11, 'name' => 'FILES_SHARING'],
['api_route' => '/cloud/users', 'scope_group' => 30, 'name' => 'USER_INFO'],
['api_route' => '/cloud/groups', 'scope_group' => 30, 'name' => 'USER_INFO'],
Expand Down Expand Up @@ -159,4 +159,13 @@ public function mapScopeGroupsToNames(array $scopeGroups): array {
return $apiScope->getName();
}, $apiScopes));
}

public function mapScopeNamesToNumbers(array $scopeNames): array {
$apiScopes = array_values(array_filter($this->getExAppApiScopes(), function (ExAppApiScope $apiScope) use ($scopeNames) {
return in_array($apiScope->getName(), $scopeNames);
}));
return array_unique(array_map(function (ExAppApiScope $apiScope) {
return $apiScope->getScopeGroup();
}, $apiScopes));
}
}

0 comments on commit efa55ea

Please sign in to comment.