1 | <?php |
||
38 | class Authentication |
||
39 | { |
||
40 | /** |
||
41 | * @var \Akamai\Open\EdgeGrid\Authentication |
||
42 | */ |
||
43 | protected $signer; |
||
44 | |||
45 | /** |
||
46 | * Inject signer object |
||
47 | * |
||
48 | * @param Signer|null $auth |
||
49 | */ |
||
50 | 150 | public function setSigner(\Akamai\Open\EdgeGrid\Authentication $auth = null) |
|
51 | { |
||
52 | 150 | $this->signer = $auth; |
|
53 | 150 | if ($this->signer === null) { |
|
54 | 14 | $this->signer = new Signer(); |
|
55 | } |
||
56 | 150 | } |
|
57 | |||
58 | /** |
||
59 | * Handler magic invoker |
||
60 | * |
||
61 | * @param callable $handler The next handler in the stack |
||
62 | * @return \Closure |
||
63 | * @throws \Akamai\Open\EdgeGrid\Exception\HandlerException |
||
64 | */ |
||
65 | public function __invoke(callable $handler) |
||
66 | { |
||
67 | 121 | return function ( |
|
68 | RequestInterface $request, |
||
69 | array $config |
||
70 | ) use ($handler) { |
||
71 | 121 | if ($request->getUri()->getScheme() !== 'https' || |
|
72 | 121 | strpos($request->getUri()->getHost(), 'akamaiapis.net') === false |
|
73 | ) { |
||
74 | 64 | return $handler($request, $config); |
|
75 | } |
||
76 | |||
77 | 57 | if (!$this->signer) { |
|
78 | 1 | throw new HandlerException('Signer not set, make sure to call setSigner first'); |
|
79 | } |
||
80 | |||
81 | 56 | $request->getBody()->rewind(); |
|
82 | |||
83 | 56 | $this->signer->setHttpMethod($request->getMethod()) |
|
84 | 56 | ->setHost($request->getUri()->getHost()) |
|
85 | 56 | ->setPath($request->getUri()->getPath()) |
|
86 | 56 | ->setQuery($request->getUri()->getQuery()) |
|
87 | 56 | ->setHeaders($request->getHeaders()) |
|
88 | 56 | ->setBody($request->getBody()->getContents()); |
|
89 | |||
90 | 56 | $request = $request->withHeader('Authorization', $this->signer->createAuthHeader()); |
|
91 | |||
92 | 56 | return $handler($request, $config); |
|
93 | 121 | }; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Proxy to the underlying signer object |
||
98 | * |
||
99 | * @param $method |
||
100 | * @param $args |
||
101 | * @return mixed |
||
102 | * @throws \Akamai\Open\EdgeGrid\Exception\HandlerException |
||
103 | */ |
||
104 | 16 | public function __call($method, $args) |
|
117 | |||
118 | /** |
||
119 | * Create Handler using an .edgerc file |
||
120 | * |
||
121 | * Automatically create a valid authentication handler using |
||
122 | * an .edgerc file |
||
123 | * |
||
124 | * @param string $section |
||
125 | * @param null $file |
||
126 | * |
||
127 | * @return static |
||
128 | */ |
||
129 | 4 | public static function createFromEdgeRcFile($section = 'default', $file = null) |
|
137 | } |
||
138 |