|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\OAuth\Storage; |
|
20
|
|
|
|
|
21
|
|
|
use fkooman\RemoteStorage\OAuth\Client; |
|
22
|
|
|
use fkooman\RemoteStorage\OAuth\ClientStorageInterface; |
|
23
|
|
|
use RuntimeException; |
|
24
|
|
|
|
|
25
|
|
|
class JsonClientStorage implements ClientStorageInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $jsonFile; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct($jsonFile) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->jsonFile = $jsonFile; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getClient($clientId, $responseType = null, $redirectUri = null, $scope = null) |
|
36
|
|
|
{ |
|
37
|
|
|
if (false === $fileData = @file_get_contents($this->jsonFile)) { |
|
38
|
|
|
throw new RuntimeException('error reading file'); |
|
39
|
|
|
} |
|
40
|
|
|
$data = json_decode($fileData, true); |
|
41
|
|
|
if (!array_key_exists($clientId, $data)) { |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// secret is not always needed |
|
46
|
|
|
$clientSecret = array_key_exists('secret', $data[$clientId]) ? $data[$clientId]['secret'] : null; |
|
47
|
|
|
|
|
48
|
|
|
return new Client( |
|
49
|
|
|
$clientId, |
|
50
|
|
|
$data[$clientId]['response_type'], |
|
51
|
|
|
$data[$clientId]['redirect_uri'], |
|
52
|
|
|
$data[$clientId]['scope'], |
|
53
|
|
|
$clientSecret |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|