|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DMS\Service\Meetup\Config; |
| 4 | + |
| 5 | +use DMS\Service\Meetup\Exception\MissingPackageException; |
| 6 | +use GuzzleHttp\Subscriber\Oauth\Oauth1; |
| 7 | + |
| 8 | +final class OAuth1Config implements ClientConfig |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var string |
| 12 | + */ |
| 13 | + protected $consumerKey; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $consumerSecret; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $token; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $tokenSecret; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + protected $privateKeyFile; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + protected $privateKeyPassphrase; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + protected $signatureMethod; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var bool |
| 47 | + */ |
| 48 | + protected $shouldSignAllRequests = true; |
| 49 | + |
| 50 | + /** |
| 51 | + * @param string $consumerKey |
| 52 | + * @param string $consumerSecret |
| 53 | + */ |
| 54 | + private function __construct($consumerKey, $consumerSecret) |
| 55 | + { |
| 56 | + if (!class_exists(Oauth1::class)) { |
| 57 | + MissingPackageException::forOAuth1(); |
| 58 | + } |
| 59 | + |
| 60 | + $this->consumerKey = $consumerKey; |
| 61 | + $this->consumerSecret = $consumerSecret; |
| 62 | + } |
| 63 | + |
| 64 | + public static function withTwoLeggedOauth(string $consumerKey, string $consumerSecret): self |
| 65 | + { |
| 66 | + $instance = new static($consumerKey, $consumerSecret); |
| 67 | + $instance->tokenSecret = false; |
| 68 | + return $instance; |
| 69 | + } |
| 70 | + |
| 71 | + public static function withToken( |
| 72 | + string $consumerKey, |
| 73 | + string $consumerSecret, |
| 74 | + string $token, |
| 75 | + string $tokenSecret |
| 76 | + ): self { |
| 77 | + $instance = new static($consumerKey, $consumerSecret); |
| 78 | + $instance->token = $token; |
| 79 | + $instance->tokenSecret = $tokenSecret; |
| 80 | + return $instance; |
| 81 | + } |
| 82 | + |
| 83 | + public static function withRSASH1( |
| 84 | + string $consumerKey, |
| 85 | + string $consumerSecret, |
| 86 | + string $privateKeyFile, |
| 87 | + string $privateKeyPassphrase |
| 88 | + ): self { |
| 89 | + $instance = new static($consumerKey, $consumerSecret); |
| 90 | + $instance->privateKeyFile = $privateKeyFile; |
| 91 | + $instance->privateKeyPassphrase = $privateKeyPassphrase; |
| 92 | + $instance->signatureMethod = Oauth1::SIGNATURE_METHOD_RSA; |
| 93 | + return $instance; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @return callable[] |
| 98 | + */ |
| 99 | + public function getMiddleware(): array |
| 100 | + { |
| 101 | + return [ |
| 102 | + 'OAuth1' => new Oauth1($this->buildConfig()), |
| 103 | + ]; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return string[] |
| 108 | + */ |
| 109 | + public function getClientConfig(): array |
| 110 | + { |
| 111 | + $config = []; |
| 112 | + |
| 113 | + if ($this->shouldSignAllRequests) { |
| 114 | + $config['auth'] = 'oauth'; |
| 115 | + } |
| 116 | + |
| 117 | + return $config; |
| 118 | + } |
| 119 | + |
| 120 | + private function buildConfig(): array |
| 121 | + { |
| 122 | + $config = [ |
| 123 | + 'consumer_key' => $this->consumerKey, |
| 124 | + 'consumer_secret' => $this->consumerSecret, |
| 125 | + ]; |
| 126 | + |
| 127 | + if ($this->signatureMethod !== null) { |
| 128 | + $config['signature_method'] = $this->signatureMethod; |
| 129 | + } |
| 130 | + |
| 131 | + if ($this->signatureMethod === Oauth1::SIGNATURE_METHOD_RSA) { |
| 132 | + $config['private_key_file'] = $this->privateKeyFile; |
| 133 | + $config['private_key_passphrase'] = $this->privateKeyPassphrase; |
| 134 | + } |
| 135 | + |
| 136 | + if ($this->token !== null) { |
| 137 | + $config['token'] = $this->token; |
| 138 | + } |
| 139 | + |
| 140 | + if ($this->tokenSecret !== null) { |
| 141 | + $config['token_secret'] = $this->tokenSecret; |
| 142 | + } |
| 143 | + |
| 144 | + return $config; |
| 145 | + } |
| 146 | +} |
0 commit comments