These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Chadicus\Slim\OAuth2\Http; |
||
3 | |||
4 | use Psr\Http\Message\ServerRequestInterface; |
||
5 | use OAuth2; |
||
6 | |||
7 | /** |
||
8 | * Static utility class for bridging Psr-7 Requests to OAuth2 Requests. |
||
9 | */ |
||
10 | class RequestBridge |
||
11 | { |
||
12 | /** |
||
13 | * Returns a new instance of \OAuth2\Request based on the given \Slim\Http\Request |
||
14 | * |
||
15 | * @param ServerRequestInterface $request The psr-7 request. |
||
16 | * |
||
17 | * @return OAuth2\Request |
||
18 | */ |
||
19 | final public static function toOAuth2(ServerRequestInterface $request) |
||
20 | { |
||
21 | return new OAuth2\Request( |
||
22 | (array)$request->getQueryParams(), |
||
23 | $request->isPost() ? (array)$request->getParsedBody() : [], |
||
24 | $request->getAttributes(), |
||
25 | $request->getCookieParams(), |
||
26 | self::convertUploadedFiles($request->getUploadedFiles()), |
||
27 | $request->getServerParams(), |
||
28 | (string)$request->getBody(), |
||
29 | self::cleanupHeaders($request->getHeaders()) |
||
30 | ); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Helper method to clean header keys and values. |
||
35 | * |
||
36 | * Slim will convert all headers to Camel-Case style. There are certain headers such as PHP_AUTH_USER that the |
||
37 | * OAuth2 library requires CAPS_CASE format. This method will adjust those headers as needed. The OAuth2 library |
||
38 | * also does not expect arrays for header values, this method will implode the multiple values with a ', ' |
||
39 | * |
||
40 | * @param array $uncleanHeaders The headers to be cleaned. |
||
41 | * |
||
42 | * @return array The cleaned headers |
||
43 | */ |
||
44 | private static function cleanupHeaders(array $uncleanHeaders = []) |
||
45 | { |
||
46 | $cleanHeaders = []; |
||
47 | $headerMap = [ |
||
48 | 'Php-Auth-User' => 'PHP_AUTH_USER', |
||
49 | 'Php-Auth-Pw' => 'PHP_AUTH_PW', |
||
50 | 'Php-Auth-Digest' => 'PHP_AUTH_DIGEST', |
||
51 | 'Auth-Type' => 'AUTH_TYPE', |
||
52 | ]; |
||
53 | |||
54 | foreach ($uncleanHeaders as $key => $value) { |
||
55 | if (array_key_exists($key, $headerMap)) { |
||
56 | $key = $headerMap[$key]; |
||
57 | } |
||
58 | |||
59 | $cleanHeaders[$key] = is_array($value) ? implode(', ', $value) : $value; |
||
60 | } |
||
61 | |||
62 | return $cleanHeaders; |
||
63 | } |
||
64 | |||
65 | /** |
||
0 ignored issues
–
show
Coding Style
Documentation
introduced
by
![]() |
|||
66 | * Convert a PSR-7 uploaded files structure to a $_FILES structure |
||
67 | * |
||
68 | * @param \Psr\Http\Message\UploadedFileInterface[] |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | private static function convertUploadedFiles(array $uploadedFiles) |
||
73 | { |
||
74 | $files = []; |
||
75 | foreach ($uploadedFiles as $name => $upload) { |
||
76 | $files[$name] = [ |
||
77 | 'name' => $upload->getClientFilename(), |
||
78 | 'type' => $upload->getClientMediaType(), |
||
79 | 'size' => $upload->getSize(), |
||
80 | 'tmp_name' => $upload->getStream()->getMetadata('uri'), |
||
81 | 'error' => $upload->getError(), |
||
82 | ]; |
||
83 | } |
||
84 | |||
85 | return $files; |
||
86 | } |
||
87 | } |
||
88 |