Skip to content

Commit 51cf6c8

Browse files
ENGCOM-7751: GraphQL CORS Headers #28713
- Merge Pull Request #28713 from michalderlatka/magento2:28561_graphql_cors_requests - Merged commits: 1. 50635d9 2. b79c484 3. 69cb58c 4. 5ba8fd7 5. c3def13 6. e82cca4 7. 64b4228 8. 88840a7
2 parents 2efa700 + 88840a7 commit 51cf6c8

File tree

12 files changed

+758
-0
lines changed

12 files changed

+758
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Controller\HttpResponse\Cors;
9+
10+
use Magento\Framework\App\Response\HeaderProvider\HeaderProviderInterface;
11+
use Magento\GraphQl\Model\Cors\ConfigurationInterface;
12+
13+
/**
14+
* Provides value for Access-Control-Allow-Credentials header if CORS is enabled
15+
*/
16+
class CorsAllowCredentialsHeaderProvider implements HeaderProviderInterface
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $headerName;
22+
23+
/**
24+
* CORS configuration provider
25+
*
26+
* @var \Magento\GraphQl\Model\Cors\ConfigurationInterface
27+
*/
28+
private $corsConfiguration;
29+
30+
/**
31+
* @param ConfigurationInterface $corsConfiguration
32+
* @param string $headerName
33+
*/
34+
public function __construct(
35+
ConfigurationInterface $corsConfiguration,
36+
string $headerName
37+
) {
38+
$this->corsConfiguration = $corsConfiguration;
39+
$this->headerName = $headerName;
40+
}
41+
42+
/**
43+
* Get name of header
44+
*
45+
* @return string
46+
*/
47+
public function getName(): string
48+
{
49+
return $this->headerName;
50+
}
51+
52+
/**
53+
* Get value for header
54+
*
55+
* @return string
56+
*/
57+
public function getValue(): string
58+
{
59+
return "1";
60+
}
61+
62+
/**
63+
* Check if header can be applied
64+
*
65+
* @return bool
66+
*/
67+
public function canApply(): bool
68+
{
69+
return $this->corsConfiguration->isEnabled() && $this->corsConfiguration->isCredentialsAllowed();
70+
}
71+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Controller\HttpResponse\Cors;
9+
10+
use Magento\Framework\App\Response\HeaderProvider\HeaderProviderInterface;
11+
use Magento\GraphQl\Model\Cors\ConfigurationInterface;
12+
13+
/**
14+
* Provides value for Access-Control-Allow-Headers header if CORS is enabled
15+
*/
16+
class CorsAllowHeadersHeaderProvider implements HeaderProviderInterface
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $headerName;
22+
23+
/**
24+
* CORS configuration provider
25+
*
26+
* @var \Magento\GraphQl\Model\Cors\ConfigurationInterface
27+
*/
28+
private $corsConfiguration;
29+
30+
/**
31+
* @param ConfigurationInterface $corsConfiguration
32+
* @param string $headerName
33+
*/
34+
public function __construct(
35+
ConfigurationInterface $corsConfiguration,
36+
string $headerName
37+
) {
38+
$this->corsConfiguration = $corsConfiguration;
39+
$this->headerName = $headerName;
40+
}
41+
42+
/**
43+
* Get name of header
44+
*
45+
* @return string
46+
*/
47+
public function getName(): string
48+
{
49+
return $this->headerName;
50+
}
51+
52+
/**
53+
* Check if header can be applied
54+
*
55+
* @return bool
56+
*/
57+
public function canApply(): bool
58+
{
59+
return $this->corsConfiguration->isEnabled() && $this->getValue();
60+
}
61+
62+
/**
63+
* Get value for header
64+
*
65+
* @return string|null
66+
*/
67+
public function getValue(): ?string
68+
{
69+
return $this->corsConfiguration->getAllowedHeaders();
70+
}
71+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Controller\HttpResponse\Cors;
9+
10+
use Magento\Framework\App\Response\HeaderProvider\HeaderProviderInterface;
11+
use Magento\GraphQl\Model\Cors\ConfigurationInterface;
12+
13+
/**
14+
* Provides value for Access-Control-Allow-Methods header if CORS is enabled
15+
*/
16+
class CorsAllowMethodsHeaderProvider implements HeaderProviderInterface
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $headerName;
22+
23+
/**
24+
* CORS configuration provider
25+
*
26+
* @var \Magento\GraphQl\Model\Cors\ConfigurationInterface
27+
*/
28+
private $corsConfiguration;
29+
30+
/**
31+
* @param ConfigurationInterface $corsConfiguration
32+
* @param string $headerName
33+
*/
34+
public function __construct(
35+
ConfigurationInterface $corsConfiguration,
36+
string $headerName
37+
) {
38+
$this->corsConfiguration = $corsConfiguration;
39+
$this->headerName = $headerName;
40+
}
41+
42+
/**
43+
* Get name of header
44+
*
45+
* @return string
46+
*/
47+
public function getName(): string
48+
{
49+
return $this->headerName;
50+
}
51+
52+
/**
53+
* Check if header can be applied
54+
*
55+
* @return bool
56+
*/
57+
public function canApply(): bool
58+
{
59+
return $this->corsConfiguration->isEnabled() && $this->getValue();
60+
}
61+
62+
/**
63+
* Get value for header
64+
*
65+
* @return string|null
66+
*/
67+
public function getValue(): ?string
68+
{
69+
return $this->corsConfiguration->getAllowedMethods();
70+
}
71+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Controller\HttpResponse\Cors;
9+
10+
use Magento\Framework\App\Response\HeaderProvider\HeaderProviderInterface;
11+
use Magento\GraphQl\Model\Cors\ConfigurationInterface;
12+
13+
/**
14+
* Provides value for Access-Control-Allow-Origin header if CORS is enabled
15+
*/
16+
class CorsAllowOriginHeaderProvider implements HeaderProviderInterface
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $headerName;
22+
23+
/**
24+
* CORS configuration provider
25+
*
26+
* @var \Magento\GraphQl\Model\Cors\ConfigurationInterface
27+
*/
28+
private $corsConfiguration;
29+
30+
/**
31+
* @param ConfigurationInterface $corsConfiguration
32+
* @param string $headerName
33+
*/
34+
public function __construct(
35+
ConfigurationInterface $corsConfiguration,
36+
string $headerName
37+
) {
38+
$this->corsConfiguration = $corsConfiguration;
39+
$this->headerName = $headerName;
40+
}
41+
42+
/**
43+
* Get name of header
44+
*
45+
* @return string
46+
*/
47+
public function getName(): string
48+
{
49+
return $this->headerName;
50+
}
51+
52+
/**
53+
* Check if header can be applied
54+
*
55+
* @return bool
56+
*/
57+
public function canApply(): bool
58+
{
59+
return $this->corsConfiguration->isEnabled() && $this->getValue();
60+
}
61+
62+
/**
63+
* Get value for header
64+
*
65+
* @return string|null
66+
*/
67+
public function getValue(): ?string
68+
{
69+
return $this->corsConfiguration->getAllowedOrigins();
70+
}
71+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Controller\HttpResponse\Cors;
9+
10+
use Magento\Framework\App\Response\HeaderProvider\HeaderProviderInterface;
11+
use Magento\GraphQl\Model\Cors\ConfigurationInterface;
12+
13+
/**
14+
* Provides value for Access-Control-Max-Age header if CORS is enabled
15+
*/
16+
class CorsMaxAgeHeaderProvider implements HeaderProviderInterface
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $headerName;
22+
23+
/**
24+
* CORS configuration provider
25+
*
26+
* @var \Magento\GraphQl\Model\Cors\ConfigurationInterface
27+
*/
28+
private $corsConfiguration;
29+
30+
/**
31+
* @param ConfigurationInterface $corsConfiguration
32+
* @param string $headerName
33+
*/
34+
public function __construct(
35+
ConfigurationInterface $corsConfiguration,
36+
string $headerName
37+
) {
38+
$this->corsConfiguration = $corsConfiguration;
39+
$this->headerName = $headerName;
40+
}
41+
42+
/**
43+
* Get name of header
44+
*
45+
* @return string
46+
*/
47+
public function getName(): string
48+
{
49+
return $this->headerName;
50+
}
51+
52+
/**
53+
* Check if header can be applied
54+
*
55+
* @return bool
56+
*/
57+
public function canApply(): bool
58+
{
59+
return $this->corsConfiguration->isEnabled() && $this->getValue();
60+
}
61+
62+
/**
63+
* Get value for header
64+
*
65+
* @return string|null
66+
*/
67+
public function getValue(): ?string
68+
{
69+
return (string) $this->corsConfiguration->getMaxAge();
70+
}
71+
}

0 commit comments

Comments
 (0)