Skip to content

Commit

Permalink
Merge branch 'master' into release-0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasquale Tripodi authored Nov 1, 2023
2 parents ef4c458 + e22a92e commit 93eae8f
Show file tree
Hide file tree
Showing 14 changed files with 5,714 additions and 5,421 deletions.
9 changes: 7 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"presets": [
["env", {"modules": false}]
[
"@babel/preset-env",
{
"modules": false
}
]
]
}
}
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ all: $(js_deps) js/market.bundle.js

.PHONY: clean
clean: clean-composer-deps clean-js-deps clean-dist clean-build
rm js/market.bundle.js

#
# ownCloud market PHP dependencies
Expand Down
2 changes: 0 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
['name' => 'market#getConfig', 'url' => '/config', 'verb' => 'GET'],
['name' => 'market#requestDemoLicenseKeyFromMarket', 'url' => '/request-license-key-from-market', 'verb' => 'GET'],
['name' => 'market#invalidateCache', 'url' => '/cache/invalidate', 'verb' => 'POST'],
['name' => 'market#receiveMarketplaceLoginToken', 'url' => '/check-marketplace-login-token', 'verb' => 'POST'],
['name' => 'market#startMarketplaceLogin', 'url' => '/generate-login-challenge', 'verb' => 'POST'],
['name' => 'localApps#index', 'url' => '/installed-apps/{state}', 'verb' => 'GET', 'defaults' => ['state' => 'enabled']],
],
'resources' => []
Expand Down
46 changes: 0 additions & 46 deletions lib/Controller/MarketController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ class MarketController extends Controller {
/** @var IL10N */
private $l10n;

/** @var IURLGenerator */
private $urlGenerator;

/** @var IConfig */
private $config;

Expand All @@ -51,13 +48,11 @@ public function __construct(
IRequest $request,
MarketService $marketService,
IL10N $l10n,
IURLGenerator $urlGenerator,
IConfig $config
) {
parent::__construct($appName, $request);
$this->marketService = $marketService;
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
$this->config = $config;
}

Expand Down Expand Up @@ -358,47 +353,6 @@ public function requestDemoLicenseKeyFromMarket() {
}
}

/**
* @NoCSRFRequired
*/
public function startMarketplaceLogin() {
$codeChallenge = $this->marketService->startMarketplaceLogin();
$callbackUrl = $this->urlGenerator->linkToRouteAbsolute('market.page.indexHash');
$appStoreUrl = $this->config->getSystemValue('appstoreurl', 'https://marketplace.owncloud.com');

return new DataResponse([
'loginUrl' => "$appStoreUrl/login?callbackUrl=$callbackUrl&codeChallenge=$codeChallenge"
]);
}

/**
* Redirect from marketplace with login-token in the query
*
* @NoCSRFRequired
*
* @param string $token
* @return DataResponse
*/
public function receiveMarketplaceLoginToken($token) {
if (!$token) {
return new DataResponse([
'message' => $this->l10n->t('Token is missing')
], Http::STATUS_BAD_REQUEST);
}

try {
$apiKey = $this->marketService->loginViaMarketplace($token);
} catch (\Exception $ex) {
return new DataResponse(
[
'message' => $this->l10n->t('Could not login via marketplace')],
Http::STATUS_UNAUTHORIZED
);
}

return new DataResponse(['apiKey' => $apiKey]);
}

public function invalidateCache() {
$this->marketService->invalidateCache();
return new DataResponse(
Expand Down
56 changes: 0 additions & 56 deletions lib/HttpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,6 @@ public function validateKey($apiKey) {
return $this->httpGet($url, [], $apiKey);
}

/**
*
* Exchange login token for api key
*
* @param string $loginToken
* @param string $codeVerifier
* @return string
* @throws AppManagerException
*/
public function exchangeLoginTokenForApiKey($loginToken, $codeVerifier) {
$url = $this->getAbsoluteUrl('/api/v1/authorize');
$result = $this->httpPost($url, [
'body' => [
'loginToken' => $loginToken,
'codeVerifier' => $codeVerifier
]
]);

$body = \json_decode($result->getBody(), true);
return $body['apiKey'];
}

/**
* @return void
*/
Expand Down Expand Up @@ -295,40 +273,6 @@ private function httpGet($path, $options, $apiKey) {
return $response;
}

/**
* @param string $path
* @param array $options
* @return \OCP\Http\Client\IResponse
* @throws AppManagerException
*/
private function httpPost($path, $options) {
$ca = $this->config->getSystemValue('marketplace.ca', null);
if ($ca !== null) {
$options = \array_merge(
[
'verify' => $ca
],
$options
);
}
$client = $this->httpClientService->newClient();

try {
$response = $client->post($path, $options);
} catch (TransferException $e) {
throw new AppManagerException(
$this->l10n->t(
'No marketplace connection: %s',
[$e->getMessage()]
),
0,
$e
);
}

return $response;
}

/**
* @param string $code
*
Expand Down
32 changes: 1 addition & 31 deletions lib/MarketService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class MarketService {
private $categories;
/** @var array */
private $bundles;
/** @var ISecureRandom */
private $rng;

/**
* Service constructor.
Expand All @@ -70,15 +68,13 @@ public function __construct(
VersionHelper $versionHelper,
IAppManager $appManager,
IConfig $config,
IL10N $l10n,
ISecureRandom $rng
IL10N $l10n
) {
$this->httpService = $httpService;
$this->versionHelper = $versionHelper;
$this->appManager = $appManager;
$this->config = $config;
$this->l10n = $l10n;
$this->rng = $rng;
}

/**
Expand Down Expand Up @@ -433,32 +429,6 @@ public function getApiKey() {
return $this->httpService->getApiKey();
}

public function startMarketplaceLogin() {
$codeVerify = $this->rng->generate(
64,
ISecureRandom::CHAR_DIGITS .
ISecureRandom::CHAR_LOWER .
ISecureRandom::CHAR_UPPER
);

$codeChallenge = \base64_encode(\hash('sha256', $codeVerify));
$this->config->setAppValue('market', 'code_verify', $codeVerify);
$this->config->setAppValue('market', 'code_challenge', $codeChallenge);

return $codeChallenge;
}

public function loginViaMarketplace($loginToken) {
$codeVerify = $this->config->getAppValue('market', 'code_verify');
$apiKey = $this->httpService->exchangeLoginTokenForApiKey($loginToken, $codeVerify);

$this->setApiKey($apiKey);
$this->config->deleteAppValue('market', 'code_verify');
$this->config->deleteAppValue('market', 'code_challenge');

return $apiKey;
}

/**
* Set api key
*
Expand Down
Loading

0 comments on commit 93eae8f

Please sign in to comment.