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\Signature; |
||
| 22 | use O2System\Spl\Traits\Collectors\ErrorCollectorTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Class Token |
||
| 26 | * @package O2System\Security\Authentication\Oauth |
||
| 27 | */ |
||
| 28 | class Token implements MethodInterface |
||
| 29 | { |
||
| 30 | use ErrorCollectorTrait; |
||
| 31 | |||
| 32 | protected $consumer; |
||
| 33 | |||
| 34 | public function __construct(Consumer $consumer) |
||
| 35 | { |
||
| 36 | $this->consumer = $consumer; |
||
| 37 | } |
||
| 38 | |||
| 39 | // ------------------------------------------------------------------------ |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Token::getVerifier |
||
| 43 | * |
||
| 44 | * Gets Token oauth_verifier code. |
||
| 45 | * |
||
| 46 | * @return bool|string |
||
| 47 | */ |
||
| 48 | public function getVerifier() |
||
| 49 | { |
||
| 50 | if ( ! empty($this->key) && ! empty($this->secret)) { |
||
| 51 | $key = rawurlencode($this->key); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 52 | $secret = rawurlencode($this->secret); |
||
|
0 ignored issues
–
show
|
|||
| 53 | |||
| 54 | return base64_encode($key . ':' . $secret); |
||
| 55 | } |
||
| 56 | |||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | // ------------------------------------------------------------------------ |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Token::getRequest |
||
| 64 | * |
||
| 65 | * Gets OAuth Request Token. |
||
| 66 | * |
||
| 67 | * @return array|bool Returns FALSE if failed. |
||
| 68 | */ |
||
| 69 | public function getRequest($callbackUrl, $httpMethod = self::HTTP_POST) |
||
| 70 | { |
||
| 71 | $algorithm = 'HMAC-SHA1'; |
||
|
0 ignored issues
–
show
|
|||
| 72 | if (false === ($signature = Base64::decode($this->consumer->secret))) { |
||
| 73 | $this->addError(400, 'Invalid Consumer Secret'); |
||
| 74 | |||
| 75 | return false; |
||
| 76 | } |
||
| 77 | |||
| 78 | if (false === ($signature = Json::decode($signature))) { |
||
|
0 ignored issues
–
show
|
|||
| 79 | $this->addError(400, 'Invalid Consumer Secret'); |
||
| 80 | |||
| 81 | return false; |
||
| 82 | } |
||
| 83 | |||
| 84 | $signature->callbackUrl = $callbackUrl; |
||
|
0 ignored issues
–
show
|
|||
| 85 | $signature->httpMethod = $httpMethod; |
||
|
0 ignored issues
–
show
|
|||
| 86 | $algorithm = $signature->algorithm; |
||
| 87 | |||
| 88 | if (false !== ($payload = Base64::decode($this->consumer->key))) { |
||
| 89 | $payload = Json::decode($payload)->getArrayCopy(); |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($payload) { |
||
| 93 | $payload[ 'timestamp' ] = time(); |
||
| 94 | |||
| 95 | $segments[] = Base64::encode(Json::encode($signature)); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 96 | $segments[] = $token = Base64::encode(Signature::generate([ |
||
| 97 | 'payload' => Base64::encode(Json::encode($payload)), |
||
| 98 | 'token' => \OAuthProvider::generateToken(strlen($this->consumer->secret), true), |
||
| 99 | ], $this->consumer->key, $algorithm)); |
||
| 100 | |||
| 101 | $secret = Base64::encode(Signature::generate($segments, $this->consumer->key, $algorithm)); |
||
| 102 | |||
| 103 | return [ |
||
| 104 | 'oauth_token' => $token, |
||
| 105 | 'oauth_token_secret' => $secret, |
||
| 106 | ]; |
||
| 107 | } |
||
| 108 | |||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | // ------------------------------------------------------------------------ |
||
| 113 | } |