|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PayPing\OAuth2\Client\Provider; |
| 4 | + |
| 5 | +use League\OAuth2\Client\Provider\AbstractProvider; |
| 6 | +use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
| 7 | +use League\OAuth2\Client\Token\AccessToken; |
| 8 | +use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
| 9 | +use Psr\Http\Message\ResponseInterface; |
| 10 | + |
| 11 | +class PayPing extends AbstractProvider |
| 12 | +{ |
| 13 | + use BearerAuthorizationTrait; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var string Key used in the access token response to identify the resource owner. |
| 17 | + */ |
| 18 | + const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'account_id'; |
| 19 | + |
| 20 | + /** |
| 21 | + * Get authorization url to begin OAuth flow |
| 22 | + * |
| 23 | + * @return string |
| 24 | + */ |
| 25 | + public function getBaseAuthorizationUrl() |
| 26 | + { |
| 27 | + return 'https://oauth.payping.ir/connect/authorize'; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Get access token url to retrieve token |
| 32 | + * |
| 33 | + * @return string |
| 34 | + */ |
| 35 | + public function getBaseAccessTokenUrl(array $params) |
| 36 | + { |
| 37 | + return 'https://oauth.payping.ir/connect/token'; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Get provider url to fetch user details |
| 42 | + * |
| 43 | + * @param AccessToken $token |
| 44 | + * |
| 45 | + * @return string |
| 46 | + */ |
| 47 | + public function getResourceOwnerDetailsUrl(AccessToken $token) |
| 48 | + { |
| 49 | + return 'https://oauth.payping.ir/connect/userinfo'; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the default scopes used by this provider. |
| 54 | + * |
| 55 | + * This should not be a complete list of all scopes, but the minimum |
| 56 | + * required for the provider user interface! |
| 57 | + * |
| 58 | + * @return array |
| 59 | + */ |
| 60 | + protected function getDefaultScopes() |
| 61 | + { |
| 62 | + return ['openid']; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Check a provider response for errors. |
| 67 | + * |
| 68 | + * @link https://www.dropbox.com/developers/core/docs |
| 69 | + * @throws IdentityProviderException |
| 70 | + * @param ResponseInterface $response |
| 71 | + * @param string $data Parsed response data |
| 72 | + * @return void |
| 73 | + */ |
| 74 | + protected function checkResponse(ResponseInterface $response, $data) |
| 75 | + { |
| 76 | + if (isset($data['error'])) { |
| 77 | + throw new IdentityProviderException( |
| 78 | + $data['error'] ?: $response->getReasonPhrase(), |
| 79 | + $response->getStatusCode(), |
| 80 | + $response |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Generate a user object from a successful user details request. |
| 87 | + * |
| 88 | + * @param object $response |
| 89 | + * @param AccessToken $token |
| 90 | + * @return DropboxResourceOwner |
| 91 | + */ |
| 92 | + protected function createResourceOwner(array $response, AccessToken $token) |
| 93 | + { |
| 94 | + return new DropboxResourceOwner($response); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Requests resource owner details. |
| 99 | + * |
| 100 | + * @param AccessToken $token |
| 101 | + * @return mixed |
| 102 | + */ |
| 103 | + protected function fetchResourceOwnerDetails(AccessToken $token) |
| 104 | + { |
| 105 | + $url = $this->getResourceOwnerDetailsUrl($token); |
| 106 | + |
| 107 | + $request = $this->getAuthenticatedRequest(self::METHOD_POST, $url, $token); |
| 108 | + |
| 109 | + return $this->getParsedResponse($request); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Builds the authorization URL. |
| 114 | + * |
| 115 | + * @param array $options |
| 116 | + * @return string Authorization URL |
| 117 | + */ |
| 118 | + public function getAuthorizationUrl(array $options = []) |
| 119 | + { |
| 120 | + return parent::getAuthorizationUrl(array_merge([ |
| 121 | + 'approval_prompt' => [] |
| 122 | + ], $options)); |
| 123 | + } |
| 124 | +} |
0 commit comments