Passed
Push — master ( a53d55...43aca1 )
by Maximilian
03:50
created
src/Validation/RequestValidator.php 2 patches
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -13,8 +13,7 @@
 block discarded – undo
13 13
 /**
14 14
  * This is a validator for amazon echo requests. It validates the timestamp of the request and the request signature.
15 15
  */
16
-class RequestValidator
17
-{
16
+class RequestValidator {
18 17
     /**
19 18
      * Basic value for timestamp validation. 150 seconds is suggested by amazon.
20 19
      */
Please login to merge, or discard this patch.
Switch Indentation   +115 added lines, -115 removed lines patch added patch discarded remove patch
@@ -1,73 +1,73 @@  discard block
 block discarded – undo
1
-<?php
1
+    <?php
2 2
 
3
-declare(strict_types=1);
3
+    declare(strict_types=1);
4 4
 
5
-namespace MaxBeckers\AmazonAlexa\Validation;
5
+    namespace MaxBeckers\AmazonAlexa\Validation;
6 6
 
7
-use GuzzleHttp\Client;
8
-use MaxBeckers\AmazonAlexa\Exception\OutdatedCertExceptionException;
9
-use MaxBeckers\AmazonAlexa\Exception\RequestInvalidSignatureException;
10
-use MaxBeckers\AmazonAlexa\Exception\RequestInvalidTimestampException;
11
-use MaxBeckers\AmazonAlexa\Request\Request;
7
+    use GuzzleHttp\Client;
8
+    use MaxBeckers\AmazonAlexa\Exception\OutdatedCertExceptionException;
9
+    use MaxBeckers\AmazonAlexa\Exception\RequestInvalidSignatureException;
10
+    use MaxBeckers\AmazonAlexa\Exception\RequestInvalidTimestampException;
11
+    use MaxBeckers\AmazonAlexa\Request\Request;
12 12
 
13 13
 /**
14 14
  * This is a validator for amazon echo requests. It validates the timestamp of the request and the request signature.
15 15
  */
16
-class RequestValidator
17
-{
18
-    /**
16
+    class RequestValidator
17
+    {
18
+        /**
19 19
      * Basic value for timestamp validation. 150 seconds is suggested by amazon.
20 20
      */
21
-    public const TIMESTAMP_VALID_TOLERANCE_SECONDS = 150;
21
+        public const TIMESTAMP_VALID_TOLERANCE_SECONDS = 150;
22 22
 
23
-    /**
23
+        /**
24 24
      * @param int $timestampTolerance Timestamp tolerance in seconds
25 25
      * @param Client $client HTTP client for fetching certificates
26 26
      */
27
-    public function __construct(
28
-        protected int $timestampTolerance = self::TIMESTAMP_VALID_TOLERANCE_SECONDS,
29
-        public Client $client = new Client(),
30
-    ) {
31
-    }
27
+        public function __construct(
28
+            protected int $timestampTolerance = self::TIMESTAMP_VALID_TOLERANCE_SECONDS,
29
+            public Client $client = new Client(),
30
+        ) {
31
+        }
32 32
 
33
-    /**
33
+        /**
34 34
      * Validate request data.
35 35
      *
36 36
      * @throws OutdatedCertExceptionException
37 37
      * @throws RequestInvalidSignatureException
38 38
      * @throws RequestInvalidTimestampException
39 39
      */
40
-    public function validate(Request $request): void
41
-    {
42
-        $this->validateTimestamp($request);
43
-        try {
44
-            $this->validateSignature($request);
45
-        } catch (OutdatedCertExceptionException $e) {
46
-            // load cert again and validate because temp file was outdated.
47
-            $this->validateSignature($request);
40
+        public function validate(Request $request): void
41
+        {
42
+            $this->validateTimestamp($request);
43
+            try {
44
+                $this->validateSignature($request);
45
+            } catch (OutdatedCertExceptionException $e) {
46
+                // load cert again and validate because temp file was outdated.
47
+                $this->validateSignature($request);
48
+            }
48 49
         }
49
-    }
50 50
 
51
-    /**
51
+        /**
52 52
      * Validate request timestamp. Request tolerance should be 150 seconds.
53 53
      * For more details @see https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/developing-an-alexa-skill-as-a-web-service#timestamp.
54 54
      *
55 55
      * @throws RequestInvalidTimestampException
56 56
      */
57
-    private function validateTimestamp(Request $request): void
58
-    {
59
-        if (null === $request->request || !$request->request->validateTimestamp()) {
60
-            return;
61
-        }
57
+        private function validateTimestamp(Request $request): void
58
+        {
59
+            if (null === $request->request || !$request->request->validateTimestamp()) {
60
+                return;
61
+            }
62 62
 
63
-        $differenceInSeconds = time() - $request->request->timestamp?->getTimestamp();
63
+            $differenceInSeconds = time() - $request->request->timestamp?->getTimestamp();
64 64
 
65
-        if ($differenceInSeconds > $this->timestampTolerance) {
66
-            throw new RequestInvalidTimestampException('Invalid timestamp.');
65
+            if ($differenceInSeconds > $this->timestampTolerance) {
66
+                throw new RequestInvalidTimestampException('Invalid timestamp.');
67
+            }
67 68
         }
68
-    }
69 69
 
70
-    /**
70
+        /**
71 71
      * Validate request signature. The steps for signature validation are described at developer page.
72 72
      *
73 73
      * @see https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/developing-an-alexa-skill-as-a-web-service#checking-the-signature-of-the-request
@@ -75,117 +75,117 @@  discard block
 block discarded – undo
75 75
      * @throws OutdatedCertExceptionException
76 76
      * @throws RequestInvalidSignatureException
77 77
      */
78
-    private function validateSignature(Request $request): void
79
-    {
80
-        if (null === $request->request || !$request->request->validateSignature()) {
81
-            return;
82
-        }
78
+        private function validateSignature(Request $request): void
79
+        {
80
+            if (null === $request->request || !$request->request->validateSignature()) {
81
+                return;
82
+            }
83 83
 
84
-        // validate cert url
85
-        $this->validateCertUrl($request);
84
+            // validate cert url
85
+            $this->validateCertUrl($request);
86 86
 
87
-        // generate local cert path
88
-        $localCertPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . md5($request->signatureCertChainUrl) . '.pem';
87
+            // generate local cert path
88
+            $localCertPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . md5($request->signatureCertChainUrl) . '.pem';
89 89
 
90
-        // check if pem file is already downloaded to temp or download.
91
-        $certData = $this->fetchCertData($request, $localCertPath);
90
+            // check if pem file is already downloaded to temp or download.
91
+            $certData = $this->fetchCertData($request, $localCertPath);
92 92
 
93
-        // openssl cert validation
94
-        $this->verifyCert($request, $certData);
93
+            // openssl cert validation
94
+            $this->verifyCert($request, $certData);
95 95
 
96
-        // parse cert
97
-        $certContent = $this->parseCertData($certData);
96
+            // parse cert
97
+            $certContent = $this->parseCertData($certData);
98 98
 
99
-        // validate cert
100
-        $this->validateCertContent($certContent, $localCertPath);
101
-    }
99
+            // validate cert
100
+            $this->validateCertContent($certContent, $localCertPath);
101
+        }
102 102
 
103
-    /**
103
+        /**
104 104
      * @throws RequestInvalidSignatureException
105 105
      */
106
-    private function validateCertUrl(Request $request): void
107
-    {
108
-        if (false === (bool) preg_match("/https:\/\/s3.amazonaws.com(\:443)?\/echo.api\/*/i", $request->signatureCertChainUrl)) {
109
-            throw new RequestInvalidSignatureException('Invalid cert url.');
106
+        private function validateCertUrl(Request $request): void
107
+        {
108
+            if (false === (bool) preg_match("/https:\/\/s3.amazonaws.com(\:443)?\/echo.api\/*/i", $request->signatureCertChainUrl)) {
109
+                throw new RequestInvalidSignatureException('Invalid cert url.');
110
+            }
110 111
         }
111
-    }
112 112
 
113
-    /**
113
+        /**
114 114
      * @throws RequestInvalidSignatureException
115 115
      */
116
-    private function fetchCertData(Request $request, string $localCertPath): string
117
-    {
118
-        if (!file_exists($localCertPath)) {
119
-            $response = $this->client->request('GET', $request->signatureCertChainUrl);
120
-
121
-            if ($response->getStatusCode() !== 200) {
122
-                throw new RequestInvalidSignatureException('Can\'t fetch cert from URL.');
116
+        private function fetchCertData(Request $request, string $localCertPath): string
117
+        {
118
+            if (!file_exists($localCertPath)) {
119
+                $response = $this->client->request('GET', $request->signatureCertChainUrl);
120
+
121
+                if ($response->getStatusCode() !== 200) {
122
+                    throw new RequestInvalidSignatureException('Can\'t fetch cert from URL.');
123
+                }
124
+
125
+                $certData = $response->getBody()->getContents();
126
+                @file_put_contents($localCertPath, $certData);
127
+            } else {
128
+                $certData = @file_get_contents($localCertPath);
123 129
             }
124 130
 
125
-            $certData = $response->getBody()->getContents();
126
-            @file_put_contents($localCertPath, $certData);
127
-        } else {
128
-            $certData = @file_get_contents($localCertPath);
131
+            return $certData;
129 132
         }
130 133
 
131
-        return $certData;
132
-    }
133
-
134
-    /**
134
+        /**
135 135
      * @throws RequestInvalidSignatureException
136 136
      */
137
-    private function verifyCert(Request $request, string $certData): void
138
-    {
139
-        if (1 !== @openssl_verify($request->amazonRequestBody, base64_decode($request->signature, true), $certData, 'sha1')) {
140
-            throw new RequestInvalidSignatureException('Cert ssl verification failed.');
137
+        private function verifyCert(Request $request, string $certData): void
138
+        {
139
+            if (1 !== @openssl_verify($request->amazonRequestBody, base64_decode($request->signature, true), $certData, 'sha1')) {
140
+                throw new RequestInvalidSignatureException('Cert ssl verification failed.');
141
+            }
141 142
         }
142
-    }
143 143
 
144
-    /**
144
+        /**
145 145
      * @throws RequestInvalidSignatureException
146 146
      */
147
-    private function parseCertData(string $certData): array
148
-    {
149
-        $certContent = @openssl_x509_parse($certData);
150
-        if (empty($certContent)) {
151
-            throw new RequestInvalidSignatureException('Parse cert failed.');
152
-        }
147
+        private function parseCertData(string $certData): array
148
+        {
149
+            $certContent = @openssl_x509_parse($certData);
150
+            if (empty($certContent)) {
151
+                throw new RequestInvalidSignatureException('Parse cert failed.');
152
+            }
153 153
 
154
-        return $certContent;
155
-    }
154
+            return $certContent;
155
+        }
156 156
 
157
-    /**
157
+        /**
158 158
      * @throws OutdatedCertExceptionException
159 159
      * @throws RequestInvalidSignatureException
160 160
      */
161
-    private function validateCertContent(array $cert, string $localCertPath): void
162
-    {
163
-        $this->validateCertSubject($cert);
164
-        $this->validateCertValidTime($cert, $localCertPath);
165
-    }
161
+        private function validateCertContent(array $cert, string $localCertPath): void
162
+        {
163
+            $this->validateCertSubject($cert);
164
+            $this->validateCertValidTime($cert, $localCertPath);
165
+        }
166 166
 
167
-    /**
167
+        /**
168 168
      * @throws RequestInvalidSignatureException
169 169
      */
170
-    private function validateCertSubject(array $cert): void
171
-    {
172
-        if (false === isset($cert['extensions']['subjectAltName']) ||
173
-            false === stristr($cert['extensions']['subjectAltName'], 'echo-api.amazon.com')
174
-        ) {
175
-            throw new RequestInvalidSignatureException('Cert subject error.');
170
+        private function validateCertSubject(array $cert): void
171
+        {
172
+            if (false === isset($cert['extensions']['subjectAltName']) ||
173
+                false === stristr($cert['extensions']['subjectAltName'], 'echo-api.amazon.com')
174
+            ) {
175
+                throw new RequestInvalidSignatureException('Cert subject error.');
176
+            }
176 177
         }
177
-    }
178 178
 
179
-    /**
179
+        /**
180 180
      * @throws OutdatedCertExceptionException
181 181
      */
182
-    private function validateCertValidTime(array $cert, string $localCertPath): void
183
-    {
184
-        if (false === isset($cert['validTo_time_t']) || time() > $cert['validTo_time_t'] || false === isset($cert['validFrom_time_t']) || time() < $cert['validFrom_time_t']) {
185
-            if (file_exists($localCertPath)) {
186
-                /* @scrutinizer ignore-unhandled */ @unlink($localCertPath);
182
+        private function validateCertValidTime(array $cert, string $localCertPath): void
183
+        {
184
+            if (false === isset($cert['validTo_time_t']) || time() > $cert['validTo_time_t'] || false === isset($cert['validFrom_time_t']) || time() < $cert['validFrom_time_t']) {
185
+                if (file_exists($localCertPath)) {
186
+                    /* @scrutinizer ignore-unhandled */ @unlink($localCertPath);
187
+                }
188
+                throw new OutdatedCertExceptionException('Cert is outdated.');
187 189
             }
188
-            throw new OutdatedCertExceptionException('Cert is outdated.');
189 190
         }
190
-    }
191 191
 }
Please login to merge, or discard this patch.
src/Response/Directives/APL/Document/Settings.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@
 block discarded – undo
7 7
 class Settings implements \JsonSerializable
8 8
 {
9 9
     public function __construct(
10
-        public int|float|null $idleTimeout = null,
10
+        public int | float | null $idleTimeout = null,
11 11
         public ?PseudoLocalization $pseudoLocalization = null,
12 12
         public bool $supportsResizing = false,
13 13
     ) {
Please login to merge, or discard this patch.
src/Response/Directives/APL/Document/Role.php 1 patch
Switch Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -29,9 +29,9 @@
 block discarded – undo
29 29
     case SPINBUTTON = 'spinbutton';
30 30
     case SUMMARY = 'summary';
31 31
     case SWITCH = 'switch';
32
-    case TAB = 'tab';
33
-    case TABLIST = 'tablist';
34
-    case TEXT = 'text';
35
-    case TIMER = 'timer';
36
-    case TOOLBAR = 'toolbar';
32
+        case TAB = 'tab';
33
+        case TABLIST = 'tablist';
34
+        case TEXT = 'text';
35
+        case TIMER = 'timer';
36
+        case TOOLBAR = 'toolbar';
37 37
 }
Please login to merge, or discard this patch.
src/Response/Directives/APL/Document/SwipeAction.php 1 patch
Switch Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -1,12 +1,12 @@
 block discarded – undo
1
-<?php
1
+    <?php
2 2
 
3
-declare(strict_types=1);
3
+    declare(strict_types=1);
4 4
 
5
-namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
5
+    namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
6 6
 
7
-enum SwipeAction: string
8
-{
9
-    case REVEAL = 'reveal';
10
-    case SLIDE = 'slide';
11
-    case COVER = 'cover';
7
+    enum SwipeAction: string
8
+    {
9
+        case REVEAL = 'reveal';
10
+        case SLIDE = 'slide';
11
+        case COVER = 'cover';
12 12
 }
Please login to merge, or discard this patch.
src/Response/Directives/APL/Document/Value.php 2 patches
Switch Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -1,35 +1,35 @@
 block discarded – undo
1
-<?php
1
+    <?php
2 2
 
3
-declare(strict_types=1);
3
+    declare(strict_types=1);
4 4
 
5
-namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
5
+    namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
6 6
 
7
-class Value implements \JsonSerializable
8
-{
9
-    /**
7
+    class Value implements \JsonSerializable
8
+    {
9
+        /**
10 10
      * @param string $property The property to animate
11 11
      * @param int|float $to Target value for the animation
12 12
      * @param int|float|null $from Starting value (optional). Null means: use current runtime value as implicit start.
13 13
      */
14
-    public function __construct(
15
-        public string $property,
16
-        public int|float $to,
17
-        public int|float|null $from = null,
18
-    ) {
19
-    }
14
+        public function __construct(
15
+            public string $property,
16
+            public int|float $to,
17
+            public int|float|null $from = null,
18
+        ) {
19
+        }
20 20
 
21
-    public function jsonSerialize(): array
22
-    {
23
-        $out = [
24
-            'property' => $this->property,
25
-        ];
21
+        public function jsonSerialize(): array
22
+        {
23
+            $out = [
24
+                'property' => $this->property,
25
+            ];
26 26
 
27
-        if ($this->from !== null) {
28
-            $out['from'] = $this->from;
29
-        }
27
+            if ($this->from !== null) {
28
+                $out['from'] = $this->from;
29
+            }
30 30
 
31
-        $out['to'] = $this->to;
31
+            $out['to'] = $this->to;
32 32
 
33
-        return $out;
34
-    }
33
+            return $out;
34
+        }
35 35
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -13,8 +13,8 @@
 block discarded – undo
13 13
      */
14 14
     public function __construct(
15 15
         public string $property,
16
-        public int|float $to,
17
-        public int|float|null $from = null,
16
+        public int | float $to,
17
+        public int | float | null $from = null,
18 18
     ) {
19 19
     }
20 20
 
Please login to merge, or discard this patch.
src/Response/Directives/APL/Document/Environment.php 1 patch
Switch Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -1,36 +1,36 @@
 block discarded – undo
1
-<?php
1
+    <?php
2 2
 
3
-declare(strict_types=1);
3
+    declare(strict_types=1);
4 4
 
5
-namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
5
+    namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
6 6
 
7
-class Environment implements \JsonSerializable
8
-{
9
-    /**
7
+    class Environment implements \JsonSerializable
8
+    {
9
+        /**
10 10
      * @param string|null $lang Language setting
11 11
      * @param LayoutDirection|null $layoutDirection Layout direction setting
12 12
      * @param string[]|null $parameters Array of parameter strings
13 13
      */
14
-    public function __construct(
15
-        public ?string $lang = null,
16
-        public ?LayoutDirection $layoutDirection = null,
17
-        public ?array $parameters = null,
18
-    ) {
19
-    }
20
-
21
-    public function jsonSerialize(): array
22
-    {
23
-        $data = [];
24
-        if ($this->lang !== null) {
25
-            $data['lang'] = $this->lang;
26
-        }
27
-        if ($this->layoutDirection !== null) {
28
-            $data['layoutDirection'] = $this->layoutDirection->value;
29
-        }
30
-        if ($this->parameters !== null && count($this->parameters) > 0) {
31
-            $data['parameters'] = $this->parameters;
14
+        public function __construct(
15
+            public ?string $lang = null,
16
+            public ?LayoutDirection $layoutDirection = null,
17
+            public ?array $parameters = null,
18
+        ) {
32 19
         }
33 20
 
34
-        return $data;
35
-    }
21
+        public function jsonSerialize(): array
22
+        {
23
+            $data = [];
24
+            if ($this->lang !== null) {
25
+                $data['lang'] = $this->lang;
26
+            }
27
+            if ($this->layoutDirection !== null) {
28
+                $data['layoutDirection'] = $this->layoutDirection->value;
29
+            }
30
+            if ($this->parameters !== null && count($this->parameters) > 0) {
31
+                $data['parameters'] = $this->parameters;
32
+            }
33
+
34
+            return $data;
35
+        }
36 36
 }
Please login to merge, or discard this patch.
src/Response/Directives/APL/Document/Scale.php 1 patch
Switch Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -1,14 +1,14 @@
 block discarded – undo
1
-<?php
1
+    <?php
2 2
 
3
-declare(strict_types=1);
3
+    declare(strict_types=1);
4 4
 
5
-namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
5
+    namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
6 6
 
7
-enum Scale: string
8
-{
9
-    case BEST_FIT = 'best-fit';
10
-    case BEST_FILL = 'best-fill';
11
-    case BEST_FIT_DOWN = 'best-fit-down';
12
-    case FILL = 'fill';
13
-    case NONE = 'none';
7
+    enum Scale: string
8
+    {
9
+        case BEST_FIT = 'best-fit';
10
+        case BEST_FILL = 'best-fill';
11
+        case BEST_FIT_DOWN = 'best-fit-down';
12
+        case FILL = 'fill';
13
+        case NONE = 'none';
14 14
 }
Please login to merge, or discard this patch.
src/Response/Directives/APL/Document/FontStyle.php 1 patch
Switch Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -1,11 +1,11 @@
 block discarded – undo
1
-<?php
1
+    <?php
2 2
 
3
-declare(strict_types=1);
3
+    declare(strict_types=1);
4 4
 
5
-namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
5
+    namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
6 6
 
7
-enum FontStyle: string
8
-{
9
-    case NORMAL = 'normal';
10
-    case ITALIC = 'italic';
7
+    enum FontStyle: string
8
+    {
9
+        case NORMAL = 'normal';
10
+        case ITALIC = 'italic';
11 11
 }
Please login to merge, or discard this patch.
src/Response/Directives/APL/Document/SubmitKeyType.php 1 patch
Switch Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -1,14 +1,14 @@
 block discarded – undo
1
-<?php
1
+    <?php
2 2
 
3
-declare(strict_types=1);
3
+    declare(strict_types=1);
4 4
 
5
-namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
5
+    namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Document;
6 6
 
7
-enum SubmitKeyType: string
8
-{
9
-    case DONE = 'done';
10
-    case GO = 'go';
11
-    case NEXT = 'next';
12
-    case SEARCH = 'search';
13
-    case SEND = 'send';
7
+    enum SubmitKeyType: string
8
+    {
9
+        case DONE = 'done';
10
+        case GO = 'go';
11
+        case NEXT = 'next';
12
+        case SEARCH = 'search';
13
+        case SEND = 'send';
14 14
 }
Please login to merge, or discard this patch.