SocialConnect /
auth
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * SocialConnect project |
||
| 4 | * @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
||
| 5 | */ |
||
| 6 | declare(strict_types=1); |
||
| 7 | |||
| 8 | namespace SocialConnect\OAuth1\Provider; |
||
| 9 | |||
| 10 | use SocialConnect\Common\ArrayHydrator; |
||
| 11 | use SocialConnect\Common\HttpStack; |
||
| 12 | use SocialConnect\OAuth1\Signature\MethodRSASHA1; |
||
| 13 | use SocialConnect\Provider\AccessTokenInterface; |
||
| 14 | use SocialConnect\Provider\Exception\InvalidResponse; |
||
| 15 | use SocialConnect\OAuth1\AbstractProvider; |
||
| 16 | use SocialConnect\Common\Entity\User; |
||
| 17 | use SocialConnect\Provider\Session\SessionInterface; |
||
| 18 | |||
| 19 | class Atlassian extends AbstractProvider |
||
| 20 | { |
||
| 21 | const NAME = 'atlassian'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string The Base-URI of the Atlassian instance |
||
| 25 | */ |
||
| 26 | private $baseUri; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | 5 | public function getBaseUri() |
|
| 32 | { |
||
| 33 | 5 | return $this->baseUri; |
|
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | 1 | public function getAuthorizeUri() |
|
| 40 | { |
||
| 41 | 1 | return $this->getBaseUri() . '/plugins/servlet/oauth/authorize'; |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | 1 | public function getRequestTokenUri() |
|
| 48 | { |
||
| 49 | 1 | return $this->getBaseUri() . '/plugins/servlet/oauth/request-token'; |
|
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | 1 | public function getRequestTokenAccessUri() |
|
| 56 | { |
||
| 57 | 1 | return $this->getBaseUri() . '/plugins/servlet/oauth/access-token'; |
|
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | 1 | public function getName() |
|
| 65 | { |
||
| 66 | 1 | return self::NAME; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Atlassian constructor. |
||
| 71 | * |
||
| 72 | * Required configuration parameters are: |
||
| 73 | * |
||
| 74 | * * baseUri The base URI of your self-hosted atlassian product |
||
| 75 | * * applicationSecret The path to the private key file used for signing. |
||
| 76 | * * applicationId The ID shared with your Atlassian instance |
||
| 77 | * |
||
| 78 | * @param HttpStack $httpStack |
||
| 79 | * @param \SocialConnect\Provider\Session\SessionInterface $session |
||
| 80 | * @param array $parameters |
||
| 81 | * @throws \SocialConnect\Provider\Exception\InvalidProviderConfiguration |
||
| 82 | */ |
||
| 83 | 9 | public function __construct(HttpStack $httpStack, SessionInterface $session, array $parameters) |
|
| 84 | { |
||
| 85 | 9 | if (!isset($parameters['baseUri'])) { |
|
| 86 | 1 | throw new \InvalidArgumentException('There is no "baseUri" given in the configuration'); |
|
| 87 | } |
||
| 88 | |||
| 89 | 8 | $this->baseUri = $parameters['baseUri']; |
|
| 90 | |||
| 91 | 8 | if (($lastSlash = strrpos($this->baseUri, '/')) == strlen($this->baseUri) - 1) { |
|
| 92 | 7 | $this->baseUri = substr($this->baseUri, 0, $lastSlash); |
|
| 93 | } |
||
| 94 | |||
| 95 | 8 | parent::__construct($httpStack, $session, $parameters); |
|
| 96 | |||
| 97 | // needs to be set after calling the parent constructor as there the |
||
| 98 | // signature is set as well. |
||
| 99 | 8 | $this->signature = new MethodRSASHA1($this->consumer->getSecret()); |
|
| 100 | 8 | } |
|
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | 1 | public function getIdentity(AccessTokenInterface $accessToken) |
|
| 106 | { |
||
| 107 | 1 | $this->consumerToken = $accessToken; |
|
|
0 ignored issues
–
show
|
|||
| 108 | |||
| 109 | $parameters = [ |
||
| 110 | 1 | 'oauth_consumer_key' => $this->consumer->getKey(), |
|
| 111 | 1 | 'oauth_token' => $this->consumerToken->getToken(), |
|
| 112 | ]; |
||
| 113 | |||
| 114 | 1 | $response = $this->oauthRequest( |
|
| 115 | 1 | $this->getBaseUri() . '/rest/prototype/1/user/current', |
|
| 116 | 1 | 'GET', |
|
| 117 | $parameters |
||
| 118 | ); |
||
| 119 | |||
| 120 | 1 | $redirectMax = 30; |
|
| 121 | 1 | $redirectCount = 0; |
|
| 122 | |||
| 123 | 1 | while ($response->hasHeader('Location')) { |
|
| 124 | if ($redirectMax < $redirectCount++) { |
||
| 125 | throw new \RangeException('Too many redirects'); |
||
| 126 | } |
||
| 127 | |||
| 128 | $response = $this->oauthRequest( |
||
| 129 | $response->getHeaderLine('Location'), |
||
| 130 | 'GET', |
||
| 131 | $parameters |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | 1 | $result = $this->hydrateResponse($response); |
|
| 136 | |||
| 137 | 1 | if (!isset($result['name']) || !$result['name']) { |
|
| 138 | throw new InvalidResponse( |
||
| 139 | 'API response without user inside JSON', |
||
| 140 | $response |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | 1 | $hydrator = new ArrayHydrator([ |
|
| 145 | 1 | 'name' => 'username', |
|
| 146 | 'displayName' => 'fullname', |
||
| 147 | 'displayableEmail' => 'email', |
||
| 148 | ]); |
||
| 149 | |||
| 150 | 1 | return $hydrator->hydrate(new User(), $result); |
|
| 151 | } |
||
| 152 | } |
||
| 153 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..