|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace HF\OAuth2\Client\Provider; |
| 6 | + |
| 7 | +use League\OAuth2\Client\Provider\AbstractProvider; |
| 8 | +use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
| 9 | +use League\OAuth2\Client\Provider\GenericResourceOwner; |
| 10 | +use League\OAuth2\Client\Token\AccessToken; |
| 11 | +use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | + |
| 14 | +class PLHWProvider extends AbstractProvider |
| 15 | +{ |
| 16 | + use BearerAuthorizationTrait; |
| 17 | + |
| 18 | + private $env = 'production'; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var array|null |
| 22 | + */ |
| 23 | + private $defaultScopes = null; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + private $scopeSeparator = ' '; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + private $responseError = 'error'; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + private $responseCode; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + private $responseResourceOwnerId = 'user_id'; |
| 44 | + |
| 45 | + public function __construct(array $options = [], array $collaborators = [], $env = 'production') |
| 46 | + { |
| 47 | + $this->env = $env; |
| 48 | + |
| 49 | + if (! in_array($this->env, ['production', 'testing', 'development'])) { |
| 50 | + throw new \InvalidArgumentException(sprintf("Not a valid environment '%s' given", $this->env)); |
| 51 | + } |
| 52 | + |
| 53 | + parent::__construct($options, $collaborators); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get authorization url to begin OAuth flow |
| 58 | + * |
| 59 | + * @return string |
| 60 | + */ |
| 61 | + public function getBaseAuthorizationUrl() |
| 62 | + { |
| 63 | + return sprintf('https://api%s.plhw.nl/oauth2/authorize', $this->env === 'production' ? '' : '-' . $this->env); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get access token url to retrieve token |
| 68 | + * |
| 69 | + * @return string |
| 70 | + */ |
| 71 | + public function getBaseAccessTokenUrl(array $params) |
| 72 | + { |
| 73 | + return sprintf('https://api%s.plhw.nl/oauth2/token', $this->env === 'production' ? '' : '-' . $this->env); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get provider url to fetch user details |
| 78 | + * |
| 79 | + * @param AccessToken $token |
| 80 | + * |
| 81 | + * @return string |
| 82 | + */ |
| 83 | + public function getResourceOwnerDetailsUrl(AccessToken $token) |
| 84 | + { |
| 85 | + return sprintf('https://api%s.plhw.nl/identity/me', $this->env === 'production' ? '' : '-' . $this->env); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * {@inheritdoc} |
| 90 | + */ |
| 91 | + protected function checkResponse(ResponseInterface $response, $data) |
| 92 | + { |
| 93 | + if (! empty($data[$this->responseError])) { |
| 94 | + $error = $data[$this->responseError]; |
| 95 | + $code = $this->responseCode ? $data[$this->responseCode] : 0; |
| 96 | + throw new IdentityProviderException($error, $code, $data); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * {@inheritdoc} |
| 102 | + */ |
| 103 | + protected function createResourceOwner(array $response, AccessToken $token) |
| 104 | + { |
| 105 | + return new GenericResourceOwner($response, $this->responseResourceOwnerId); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns the default scopes used by this provider. |
| 110 | + * |
| 111 | + * This should only be the scopes that are required to request the details |
| 112 | + * of the resource owner, rather than all the available scopes. |
| 113 | + * |
| 114 | + * @return array|null |
| 115 | + */ |
| 116 | + protected function getDefaultScopes() |
| 117 | + { |
| 118 | + return $this->defaultScopes; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Returns the string that should be used to separate scopes when building |
| 123 | + * the URL for requesting an access token. |
| 124 | + * |
| 125 | + * @return string Scope separator, defaults to ',' |
| 126 | + */ |
| 127 | + protected function getScopeSeparator() |
| 128 | + { |
| 129 | + return $this->scopeSeparator; |
| 130 | + } |
| 131 | +} |
0 commit comments