|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class OAuthStorageAbstract |
|
4
|
|
|
* |
|
5
|
|
|
* @created 09.07.2017 |
|
6
|
|
|
* @author Smiley <[email protected]> |
|
7
|
|
|
* @copyright 2017 Smiley |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace chillerlan\OAuth\Storage; |
|
12
|
|
|
|
|
13
|
|
|
use chillerlan\OAuth\OAuthOptions; |
|
14
|
|
|
use chillerlan\OAuth\Core\AccessToken; |
|
15
|
|
|
use chillerlan\Settings\SettingsContainerInterface; |
|
16
|
|
|
use Psr\Log\{LoggerAwareTrait, LoggerInterface, NullLogger}; |
|
17
|
|
|
use function is_string; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Implements an abstract OAuth storage adapter |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class OAuthStorageAbstract implements OAuthStorageInterface{ |
|
23
|
|
|
use LoggerAwareTrait; |
|
24
|
|
|
|
|
25
|
|
|
protected OAuthOptions|SettingsContainerInterface $options; |
|
26
|
|
|
protected string $serviceName; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* OAuthStorageAbstract constructor. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(OAuthOptions|SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
|
32
|
|
|
$this->options = ($options ?? new OAuthOptions); |
|
33
|
|
|
$this->logger = ($logger ?? new NullLogger); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritDoc |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setServiceName(string $service):OAuthStorageInterface{ |
|
40
|
|
|
$service = trim($service); |
|
41
|
|
|
|
|
42
|
|
|
if(empty($service)){ |
|
43
|
|
|
throw new OAuthStorageException('service name must not be empty'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->serviceName = $service; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritDoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getServiceName(string $service = null):string{ |
|
55
|
|
|
|
|
56
|
|
|
if($service === null && !isset($this->serviceName)){ |
|
57
|
|
|
throw new OAuthStorageException('invalid service'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$name = trim($service ?? $this->serviceName); |
|
61
|
|
|
|
|
62
|
|
|
if(empty($name)){ |
|
63
|
|
|
throw new OAuthStorageException('service name must not be empty'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $name; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritDoc |
|
71
|
|
|
*/ |
|
72
|
|
|
public function toStorage(AccessToken $token):string{ |
|
73
|
|
|
return $token->toJSON(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritDoc |
|
78
|
|
|
* @phan-suppress PhanTypeMismatchReturnSuperType |
|
79
|
|
|
*/ |
|
80
|
|
|
public function fromStorage(mixed $data):AccessToken{ |
|
81
|
|
|
|
|
82
|
|
|
if(!is_string($data)){ |
|
83
|
|
|
throw new OAuthStorageException('invalid data'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
87
|
|
|
return (new AccessToken)->fromJSON($data); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|