1 | <?php |
||
2 | |||
3 | namespace Stevenmaguire\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\ArrayAccessorTrait; |
||
9 | use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
||
10 | use Psr\Http\Message\ResponseInterface; |
||
11 | |||
12 | class Basecamp extends AbstractProvider |
||
13 | { |
||
14 | use ArrayAccessorTrait, |
||
15 | BearerAuthorizationTrait; |
||
16 | |||
17 | /** |
||
18 | * Basecamp environment specific host; defaults to api |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $host = 'https://launchpad.37signals.com'; |
||
23 | |||
24 | /** |
||
25 | * Type of flow |
||
26 | * |
||
27 | * Basecamp supports the web_server and user_agent flows, not the |
||
28 | * client_credentials or device flows. |
||
29 | * |
||
30 | * @var string |
||
31 | * @see https://github.com/basecamp/api/blob/master/sections/authentication.md |
||
32 | */ |
||
33 | protected $type = 'web_server'; |
||
34 | |||
35 | /** |
||
36 | * Returns authorization parameters based on provided options. |
||
37 | * |
||
38 | * @param array $options |
||
39 | * @return array Authorization parameters |
||
40 | */ |
||
41 | 2 | protected function getAuthorizationParameters(array $options) |
|
42 | { |
||
43 | 2 | $options = parent::getAuthorizationParameters($options); |
|
44 | |||
45 | 2 | $options['type'] = $this->type; |
|
46 | |||
47 | 2 | return $options; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Get authorization url to begin OAuth flow |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | 4 | public function getBaseAuthorizationUrl() |
|
56 | { |
||
57 | 4 | return $this->getHost() . '/authorization/new'; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get access token url to retrieve token |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | 2 | public function getBaseAccessTokenUrl(array $params) |
|
66 | { |
||
67 | 2 | $params = array_merge([ |
|
68 | 2 | 'type' => $this->type, |
|
69 | 2 | ], $params); |
|
70 | |||
71 | 2 | $query = array_filter($params, function ($key) { |
|
72 | 2 | return in_array($key, ['type', 'refresh_token']); |
|
73 | 2 | }, ARRAY_FILTER_USE_KEY); |
|
74 | |||
75 | 2 | return $this->getHost() . '/authorization/token?'.http_build_query($query); |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Get provider url to fetch user details |
||
80 | * |
||
81 | * @param AccessToken $token |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | 4 | public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
86 | { |
||
87 | 4 | return $this->getHost() . '/authorization.json'; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get the default scopes used by this provider. |
||
92 | * |
||
93 | * This should not be a complete list of all scopes, but the minimum |
||
94 | * required for the provider user interface! |
||
95 | * |
||
96 | * @return array |
||
97 | */ |
||
98 | 2 | protected function getDefaultScopes() |
|
99 | { |
||
100 | 2 | return []; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns a cleaned host. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 10 | public function getHost() |
|
109 | { |
||
110 | 10 | return rtrim($this->host, '/'); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Returns the string that should be used to separate scopes when building |
||
115 | * the URL for requesting an access token. |
||
116 | * |
||
117 | * @return string Scope separator, defaults to ' ' |
||
118 | */ |
||
119 | 2 | protected function getScopeSeparator() |
|
120 | { |
||
121 | 2 | return ' '; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Check a provider response for errors. |
||
126 | * |
||
127 | * @throws IdentityProviderException |
||
128 | * @param ResponseInterface $response |
||
129 | * @param string $data Parsed response data |
||
130 | * @return void |
||
131 | */ |
||
132 | 2 | protected function checkResponse(ResponseInterface $response, $data) |
|
133 | { |
||
134 | // At the time of initial implementation the possible error payloads returned |
||
135 | // by Basecamp were not very well documented. This method will need some |
||
136 | // improvement as the API continues to mature. |
||
137 | 2 | if ($response->getStatusCode() != 200) { |
|
138 | 2 | throw new IdentityProviderException('Unexpected response code', $response->getStatusCode(), $response); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Generate a user object from a successful user details request. |
||
144 | * |
||
145 | * @param object $response |
||
146 | * @param AccessToken $token |
||
147 | * @return BasecampResourceOwner |
||
148 | */ |
||
149 | 2 | protected function createResourceOwner(array $response, AccessToken $token) |
|
150 | { |
||
151 | 2 | return new BasecampResourceOwner($response); |
|
152 | } |
||
153 | } |
||
154 |