Skip to content

Commit 5ba8fd7

Browse files
#28561: GraphQL added CORS headers (static test fix)
1 parent b79c484 commit 5ba8fd7

File tree

9 files changed

+165
-4
lines changed

9 files changed

+165
-4
lines changed

app/code/Magento/GraphQl/Controller/HttpResponse/Cors/CorsAllowCredentialsHeaderProvider.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
class CorsAllowCredentialsHeaderProvider implements HeaderProviderInterface
1717
{
18+
/**
19+
* @var string
20+
*/
1821
private $headerName;
1922

2023
/**
@@ -24,6 +27,10 @@ class CorsAllowCredentialsHeaderProvider implements HeaderProviderInterface
2427
*/
2528
private $corsConfiguration;
2629

30+
/**
31+
* @param ConfigurationInterface $corsConfiguration
32+
* @param string $headerName
33+
*/
2734
public function __construct(
2835
ConfigurationInterface $corsConfiguration,
2936
string $headerName
@@ -32,16 +39,31 @@ public function __construct(
3239
$this->headerName = $headerName;
3340
}
3441

42+
/**
43+
* Get name of header
44+
*
45+
* @return string
46+
*/
3547
public function getName()
3648
{
3749
return $this->headerName;
3850
}
3951

52+
/**
53+
* Get value for header
54+
*
55+
* @return string
56+
*/
4057
public function getValue()
4158
{
42-
return true;
59+
return "1";
4360
}
4461

62+
/**
63+
* Check if header can be applied
64+
*
65+
* @return bool
66+
*/
4567
public function canApply() : bool
4668
{
4769
return $this->corsConfiguration->isEnabled() && $this->corsConfiguration->isCredentialsAllowed();

app/code/Magento/GraphQl/Controller/HttpResponse/Cors/CorsAllowHeadersHeaderProvider.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
class CorsAllowHeadersHeaderProvider implements HeaderProviderInterface
1717
{
18+
/**
19+
* @var string
20+
*/
1821
private $headerName;
1922

2023
/**
@@ -24,6 +27,10 @@ class CorsAllowHeadersHeaderProvider implements HeaderProviderInterface
2427
*/
2528
private $corsConfiguration;
2629

30+
/**
31+
* @param ConfigurationInterface $corsConfiguration
32+
* @param string $headerName
33+
*/
2734
public function __construct(
2835
ConfigurationInterface $corsConfiguration,
2936
string $headerName
@@ -32,16 +39,31 @@ public function __construct(
3239
$this->headerName = $headerName;
3340
}
3441

42+
/**
43+
* Get name of header
44+
*
45+
* @return string
46+
*/
3547
public function getName()
3648
{
3749
return $this->headerName;
3850
}
3951

52+
/**
53+
* Check if header can be applied
54+
*
55+
* @return bool
56+
*/
4057
public function canApply() : bool
4158
{
4259
return $this->corsConfiguration->isEnabled() && $this->getValue();
4360
}
4461

62+
/**
63+
* Get value for header
64+
*
65+
* @return string
66+
*/
4567
public function getValue()
4668
{
4769
return $this->corsConfiguration->getAllowedHeaders();

app/code/Magento/GraphQl/Controller/HttpResponse/Cors/CorsAllowMethodsHeaderProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,31 @@ public function __construct(
3232
$this->headerName = $headerName;
3333
}
3434

35+
/**
36+
* Get name of header
37+
*
38+
* @return string
39+
*/
3540
public function getName()
3641
{
3742
return $this->headerName;
3843
}
3944

45+
/**
46+
* Check if header can be applied
47+
*
48+
* @return bool
49+
*/
4050
public function canApply() : bool
4151
{
4252
return $this->corsConfiguration->isEnabled() && $this->getValue();
4353
}
4454

55+
/**
56+
* Get value for header
57+
*
58+
* @return string
59+
*/
4560
public function getValue()
4661
{
4762
return $this->corsConfiguration->getAllowedMethods();

app/code/Magento/GraphQl/Controller/HttpResponse/Cors/CorsAllowOriginHeaderProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,31 @@ public function __construct(
3232
$this->headerName = $headerName;
3333
}
3434

35+
/**
36+
* Get name of header
37+
*
38+
* @return string
39+
*/
3540
public function getName()
3641
{
3742
return $this->headerName;
3843
}
3944

45+
/**
46+
* Check if header can be applied
47+
*
48+
* @return bool
49+
*/
4050
public function canApply() : bool
4151
{
4252
return $this->corsConfiguration->isEnabled() && $this->getValue();
4353
}
4454

55+
/**
56+
* Get value for header
57+
*
58+
* @return string
59+
*/
4560
public function getValue()
4661
{
4762
return $this->corsConfiguration->getAllowedOrigins();

app/code/Magento/GraphQl/Controller/HttpResponse/Cors/CorsMaxAgeHeaderProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,31 @@ public function __construct(
3232
$this->headerName = $headerName;
3333
}
3434

35+
/**
36+
* Get name of header
37+
*
38+
* @return string
39+
*/
3540
public function getName()
3641
{
3742
return $this->headerName;
3843
}
3944

45+
/**
46+
* Check if header can be applied
47+
*
48+
* @return bool
49+
*/
4050
public function canApply()
4151
{
4252
return $this->corsConfiguration->isEnabled() && $this->getValue();
4353
}
4454

55+
/**
56+
* Get value for header
57+
*
58+
* @return string
59+
*/
4560
public function getValue()
4661
{
4762
return $this->corsConfiguration->getMaxAge();

app/code/Magento/GraphQl/Model/Cors/Configuration.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,73 @@ class Configuration implements ConfigurationInterface
2424
/**
2525
* @var ScopeConfigInterface
2626
*/
27-
protected $scopeConfig;
27+
private $scopeConfig;
2828

29+
/**
30+
* @param ScopeConfigInterface $scopeConfig
31+
*/
2932
public function __construct(ScopeConfigInterface $scopeConfig)
3033
{
3134
$this->scopeConfig = $scopeConfig;
3235
}
3336

37+
/**
38+
* Are CORS headers enabled
39+
*
40+
* @return bool
41+
*/
3442
public function isEnabled(): bool
3543
{
3644
return $this->scopeConfig->isSetFlag(self::XML_PATH_CORS_HEADERS_ENABLED);
3745
}
3846

47+
/**
48+
* Get allowed origins or null if stored configuration is empty
49+
*
50+
* @return string|null
51+
*/
3952
public function getAllowedOrigins(): ?string
4053
{
4154
return $this->scopeConfig->getValue(self::XML_PATH_CORS_ALLOWED_ORIGINS);
4255
}
4356

57+
/**
58+
* Get allowed headers or null if stored configuration is empty
59+
*
60+
* @return string|null
61+
*/
4462
public function getAllowedHeaders(): ?string
4563
{
4664
return $this->scopeConfig->getValue(self::XML_PATH_CORS_ALLOWED_HEADERS);
4765
}
4866

67+
/**
68+
* Get allowed methods or null if stored configuration is empty
69+
*
70+
* @return string|null
71+
*/
4972
public function getAllowedMethods(): ?string
5073
{
5174
return $this->scopeConfig->getValue(self::XML_PATH_CORS_ALLOWED_METHODS);
5275
}
5376

77+
/**
78+
* Get max age header value
79+
*
80+
* @return int
81+
*/
5482
public function getMaxAge(): int
5583
{
5684
return (int) $this->scopeConfig->getValue(self::XML_PATH_CORS_MAX_AGE);
5785
}
5886

87+
/**
88+
* Are credentials allowed
89+
*
90+
* @return bool
91+
*/
5992
public function isCredentialsAllowed(): bool
6093
{
6194
return $this->scopeConfig->isSetFlag(self::XML_PATH_CORS_ALLOW_CREDENTIALS);
6295
}
63-
6496
}

app/code/Magento/GraphQl/Model/Cors/ConfigurationInterface.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,45 @@
1212
*/
1313
interface ConfigurationInterface
1414
{
15+
/**
16+
* Are CORS headers enabled
17+
*
18+
* @return bool
19+
*/
1520
public function isEnabled() : bool;
1621

22+
/**
23+
* Get allowed origins or null if stored configuration is empty
24+
*
25+
* @return string|null
26+
*/
1727
public function getAllowedOrigins() : ?string;
1828

29+
/**
30+
* Get allowed headers or null if stored configuration is empty
31+
*
32+
* @return string|null
33+
*/
1934
public function getAllowedHeaders() : ?string;
2035

36+
/**
37+
* Get allowed methods or null if stored configuration is empty
38+
*
39+
* @return string|null
40+
*/
2141
public function getAllowedMethods() : ?string;
2242

43+
/**
44+
* Get max age header value
45+
*
46+
* @return int
47+
*/
2348
public function getMaxAge() : int;
2449

50+
/**
51+
* Are credentials allowed
52+
*
53+
* @return bool
54+
*/
2555
public function isCredentialsAllowed() : bool;
2656
}

app/code/Magento/GraphQl/etc/config.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
28
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
39
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
410
<default>

dev/tests/api-functional/testsuite/Magento/GraphQl/CorsHeadersTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
27

38
namespace Magento\GraphQl;
49

@@ -15,7 +20,6 @@ class CorsHeadersTest extends GraphQlAbstract
1520
* @var Config $config
1621
*/
1722
private $resourceConfig;
18-
1923
/**
2024
* @var ReinitableConfigInterface
2125
*/

0 commit comments

Comments
 (0)