o2system /
security
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * This file is part of the O2System Framework package. |
||||||
| 4 | * |
||||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||||
| 6 | * file that was distributed with this source code. |
||||||
| 7 | * |
||||||
| 8 | * @author Steeve Andrian Salim |
||||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||||
| 10 | */ |
||||||
| 11 | |||||||
| 12 | // ------------------------------------------------------------------------ |
||||||
| 13 | |||||||
| 14 | namespace O2System\Security\Authentication\Oauth; |
||||||
| 15 | |||||||
| 16 | // ------------------------------------------------------------------------ |
||||||
| 17 | |||||||
| 18 | use O2System\Psr\Http\Server\MethodInterface; |
||||||
| 19 | use O2System\Security\Encoders\Base64; |
||||||
| 20 | use O2System\Security\Encoders\Json; |
||||||
| 21 | use O2System\Security\Generators; |
||||||
| 22 | use O2System\Spl\Traits\Collectors\ErrorCollectorTrait; |
||||||
| 23 | |||||||
| 24 | /** |
||||||
| 25 | * Class Consumer |
||||||
| 26 | * @package O2System\Security\Authentication\Oauth |
||||||
| 27 | */ |
||||||
| 28 | class Consumer implements MethodInterface |
||||||
| 29 | { |
||||||
| 30 | use ErrorCollectorTrait; |
||||||
| 31 | |||||||
| 32 | public $version = '1.0'; |
||||||
| 33 | |||||||
| 34 | /** |
||||||
| 35 | * Consumer::$key |
||||||
| 36 | * |
||||||
| 37 | * String of OAuth Consumer Key (oauth_consumer_key). |
||||||
| 38 | * |
||||||
| 39 | * @var string |
||||||
| 40 | */ |
||||||
| 41 | public $key; |
||||||
| 42 | |||||||
| 43 | /** |
||||||
| 44 | * Consumer::$secret |
||||||
| 45 | * |
||||||
| 46 | * String of OAuth Consumer Secret (oauth_consumer_secret). |
||||||
| 47 | * |
||||||
| 48 | * @var string |
||||||
| 49 | */ |
||||||
| 50 | public $secret; |
||||||
| 51 | |||||||
| 52 | /** |
||||||
| 53 | * Consumer::__construct |
||||||
| 54 | * |
||||||
| 55 | * @param string $key oauth_consumer_key. |
||||||
| 56 | * @param string $secret oauth_consumer_secret. |
||||||
| 57 | */ |
||||||
| 58 | public function __construct($key, $secret) |
||||||
| 59 | { |
||||||
| 60 | $this->setKey($key); |
||||||
| 61 | $this->setSecret($secret); |
||||||
| 62 | } |
||||||
| 63 | |||||||
| 64 | // ------------------------------------------------------------------------ |
||||||
| 65 | |||||||
| 66 | /** |
||||||
| 67 | * Consumer::setKey |
||||||
| 68 | * |
||||||
| 69 | * Sets oauth_consumer_key. |
||||||
| 70 | * |
||||||
| 71 | * @param string $key oauth_consumer_key. |
||||||
| 72 | * |
||||||
| 73 | * @return static |
||||||
| 74 | */ |
||||||
| 75 | public function setKey($key) |
||||||
| 76 | { |
||||||
| 77 | $this->key = $key; |
||||||
| 78 | |||||||
| 79 | return $this; |
||||||
| 80 | } |
||||||
| 81 | |||||||
| 82 | // ------------------------------------------------------------------------ |
||||||
| 83 | |||||||
| 84 | /** |
||||||
| 85 | * Consumer::setSecret |
||||||
| 86 | * |
||||||
| 87 | * Sets oauth_consumer_secret. |
||||||
| 88 | * |
||||||
| 89 | * @param string $secret oauth_consumer_secret. |
||||||
| 90 | * |
||||||
| 91 | * @return static |
||||||
| 92 | */ |
||||||
| 93 | public function setSecret($secret) |
||||||
| 94 | { |
||||||
| 95 | $this->secret = $secret; |
||||||
| 96 | |||||||
| 97 | return $this; |
||||||
| 98 | } |
||||||
| 99 | |||||||
| 100 | // ------------------------------------------------------------------------ |
||||||
| 101 | |||||||
| 102 | /** |
||||||
| 103 | * Consumer::generate |
||||||
| 104 | * |
||||||
| 105 | * Generate consumer secret and key base on payload. |
||||||
| 106 | * |
||||||
| 107 | * @param array $payload |
||||||
| 108 | * |
||||||
| 109 | * @return array |
||||||
| 110 | */ |
||||||
| 111 | public static function generate(array $payload, $algorithm = 'HMAC-SHA1') |
||||||
| 112 | { |
||||||
| 113 | $token = new Generators\Token(); |
||||||
| 114 | $token->setAlgorithm($algorithm); |
||||||
| 115 | $token->addHeader('timestamp', time()); |
||||||
| 116 | |||||||
| 117 | $tokenString = $token->encode($payload); |
||||||
| 118 | $tokenParts = explode('.', $tokenString); |
||||||
| 119 | $tokenParts = array_map('trim', $tokenParts); |
||||||
| 120 | |||||||
| 121 | return [ |
||||||
| 122 | 'oauth_consumer_key' => $tokenParts[ 1 ], |
||||||
| 123 | 'oauth_consumer_secret' => $tokenParts[ 0 ], |
||||||
| 124 | ]; |
||||||
| 125 | } |
||||||
| 126 | |||||||
| 127 | // ------------------------------------------------------------------------ |
||||||
| 128 | |||||||
| 129 | /** |
||||||
| 130 | * Consumer::setVersion |
||||||
| 131 | * |
||||||
| 132 | * Sets oauth_version to 1.0 or 2.0 |
||||||
| 133 | * |
||||||
| 134 | * @param string $version |
||||||
| 135 | * |
||||||
| 136 | * @return static |
||||||
| 137 | */ |
||||||
| 138 | public function setVersion($version) |
||||||
| 139 | { |
||||||
| 140 | $this->version = in_array($version, ['1.0', '2.0']) ? $version : '1.0'; |
||||||
| 141 | |||||||
| 142 | return $this; |
||||||
| 143 | } |
||||||
| 144 | |||||||
| 145 | // ------------------------------------------------------------------------ |
||||||
| 146 | |||||||
| 147 | /** |
||||||
| 148 | * Consumer::getRequestToken |
||||||
| 149 | * |
||||||
| 150 | * Fetch a request token, secret and any additional response parameters from the service provider. |
||||||
| 151 | * |
||||||
| 152 | * @param \O2System\Security\Authentication\Oauth\Consumer $consumer |
||||||
| 153 | * @param string $callbackUrl |
||||||
| 154 | * @param string $httpMethod |
||||||
| 155 | * |
||||||
| 156 | * @return array|bool Returns FALSE if failed. |
||||||
| 157 | */ |
||||||
| 158 | public function getRequestToken($callbackUrl, $httpMethod = self::HTTP_POST) |
||||||
| 159 | { |
||||||
| 160 | $token = new Token($this); |
||||||
| 161 | |||||||
| 162 | return $token->getRequest($callbackUrl, $httpMethod); |
||||||
| 163 | } |
||||||
| 164 | |||||||
| 165 | // ------------------------------------------------------------------------ |
||||||
| 166 | |||||||
| 167 | /** |
||||||
| 168 | * Consumer::getAuthorizationHeader |
||||||
| 169 | * |
||||||
| 170 | * Gets OAuth HTTP_AUTHORIZATION header parameters. |
||||||
| 171 | * |
||||||
| 172 | * @param string|null $callbackUrl |
||||||
| 173 | * @param string $httpMethod |
||||||
| 174 | * |
||||||
| 175 | * @return string|bool Returns FALSE if failed |
||||||
| 176 | */ |
||||||
| 177 | public function getAuthorizationHeader($callbackUrl, $httpMethod = self::HTTP_GET) |
||||||
| 178 | { |
||||||
| 179 | $algorithm = 'HMAC-SHA1'; |
||||||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||||||
| 180 | if (false === ($signature = Base64::decode($this->secret))) { |
||||||
| 181 | $this->addError(400, 'Invalid Consumer Secret'); |
||||||
| 182 | |||||||
| 183 | return false; |
||||||
| 184 | } |
||||||
| 185 | |||||||
| 186 | if (false === ($signature = Json::decode($signature))) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 187 | $this->addError(400, 'Invalid Consumer Secret'); |
||||||
| 188 | |||||||
| 189 | return false; |
||||||
| 190 | } |
||||||
| 191 | |||||||
| 192 | $algorithm = $signature->algorithm; |
||||||
| 193 | |||||||
| 194 | $oauth = new \OAuth($this->key, $this->secret, $algorithm, OAUTH_AUTH_TYPE_AUTHORIZATION); |
||||||
| 195 | |||||||
| 196 | $parameters = [ |
||||||
| 197 | 'oauth_nonce' => Generators\Nonce::generate($algorithm), |
||||||
| 198 | 'oauth_callback' => $callbackUrl, |
||||||
| 199 | 'oauth_signature_method' => $algorithm, |
||||||
| 200 | 'oauth_timestamp' => time(), |
||||||
| 201 | 'oauth_consumer_key' => $this->key, |
||||||
| 202 | ]; |
||||||
| 203 | |||||||
| 204 | $parameters[ 'oauth_signature' ] = $oauth->generateSignature($httpMethod, $callbackUrl, $parameters); |
||||||
|
0 ignored issues
–
show
The method
generateSignature() does not exist on OAuth.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 205 | $parameters[ 'oauth_version' ] = $this->version; |
||||||
| 206 | |||||||
| 207 | $parts = []; |
||||||
| 208 | foreach ($parameters as $key => $value) { |
||||||
| 209 | $parts[] = $key . '="' . $value . '"'; |
||||||
| 210 | } |
||||||
| 211 | |||||||
| 212 | return 'OAuth ' . implode(', ', $parts); |
||||||
| 213 | } |
||||||
| 214 | |||||||
| 215 | // ------------------------------------------------------------------------ |
||||||
| 216 | |||||||
| 217 | /** |
||||||
| 218 | * Consumer::getAuthorizationBasic |
||||||
| 219 | * |
||||||
| 220 | * Gets Consumer HTTP_AUTHORIZATION Bearer code. |
||||||
| 221 | * |
||||||
| 222 | * @return bool|string |
||||||
| 223 | */ |
||||||
| 224 | public function getAuthorizationBasic() |
||||||
| 225 | { |
||||||
| 226 | if ( ! empty($this->key) && ! empty($this->secret)) { |
||||||
| 227 | $key = rawurlencode($this->key); |
||||||
| 228 | $secret = rawurlencode($this->secret); |
||||||
| 229 | |||||||
| 230 | return 'Basic ' . base64_encode($key . ':' . $secret); |
||||||
| 231 | } |
||||||
| 232 | |||||||
| 233 | return false; |
||||||
| 234 | } |
||||||
| 235 | |||||||
| 236 | // ------------------------------------------------------------------------ |
||||||
| 237 | |||||||
| 238 | /** |
||||||
| 239 | * Consumer::getAuthorizationBearer |
||||||
| 240 | * |
||||||
| 241 | * Gets Consumer HTTP_AUTHORIZATION Bearer code. |
||||||
| 242 | * |
||||||
| 243 | * @return bool|string |
||||||
| 244 | */ |
||||||
| 245 | public function getAuthorizationBearer() |
||||||
| 246 | { |
||||||
| 247 | if ( ! empty($this->key) && ! empty($this->secret)) { |
||||||
| 248 | $key = rawurlencode($this->key); |
||||||
| 249 | $secret = rawurlencode($this->secret); |
||||||
| 250 | |||||||
| 251 | return 'Bearer ' . base64_encode($key . ':' . $secret . ':' . md5($key . $secret)); |
||||||
| 252 | } |
||||||
| 253 | |||||||
| 254 | return false; |
||||||
| 255 | } |
||||||
| 256 | } |