zend-blog-3-backend
125 строк · 3.1 Кб
1<?php
2/**
3* Created by PhpStorm.
4* User: morontt
5* Date: 05.10.17
6* Time: 22:55
7*/
8
9namespace App\OAuth2\Client;
10
11use League\OAuth2\Client\Provider\AbstractProvider;
12use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
13use League\OAuth2\Client\Provider\ResourceOwnerInterface;
14use League\OAuth2\Client\Token\AccessToken;
15use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
16use Psr\Http\Message\ResponseInterface;
17
18class DropboxProvider extends AbstractProvider
19{
20use BearerAuthorizationTrait;
21
22/**
23* @var string Key used in the access token response to identify the resource owner
24*/
25const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'account_id';
26
27/**
28* Returns the base URL for authorizing a client.
29*
30* Eg. https://oauth.service.com/authorize
31*
32* @return string
33*/
34public function getBaseAuthorizationUrl()
35{
36return 'https://api.dropbox.com/oauth2/authorize';
37}
38
39/**
40* Returns the base URL for requesting an access token.
41*
42* Eg. https://oauth.service.com/token
43*
44* @param array $params
45*
46* @return string
47*/
48public function getBaseAccessTokenUrl(array $params)
49{
50return 'https://api.dropbox.com/oauth2/token';
51}
52
53/**
54* Returns the URL for requesting the resource owner's details.
55*
56* @param AccessToken $token
57*
58* @return string
59*/
60public function getResourceOwnerDetailsUrl(AccessToken $token)
61{
62return 'https://api.dropbox.com/2/users/get_current_account';
63}
64
65/**
66* Builds the authorization URL.
67*
68* @param array $options
69*
70* @return string Authorization URL
71*/
72public function getAuthorizationUrl(array $options = [])
73{
74return parent::getAuthorizationUrl(
75array_merge(
76[
77'approval_prompt' => [],
78],
79$options
80)
81);
82}
83
84/**
85* Returns the default scopes used by this provider.
86*
87* This should only be the scopes that are required to request the details
88* of the resource owner, rather than all the available scopes.
89*
90* @return array
91*/
92protected function getDefaultScopes()
93{
94return [];
95}
96
97/**
98* Checks a provider response for errors.
99*
100* @param ResponseInterface $response
101* @param array|string $data Parsed response data
102*
103* @throws IdentityProviderException
104*/
105protected function checkResponse(ResponseInterface $response, $data)
106{
107if (isset($data['error'])) {
108throw new IdentityProviderException($data['error'] ?: $response->getReasonPhrase(), $response->getStatusCode(), $response);
109}
110}
111
112/**
113* Generates a resource owner object from a successful resource owner
114* details request.
115*
116* @param array $response
117* @param AccessToken $token
118*
119* @return ResourceOwnerInterface
120*/
121protected function createResourceOwner(array $response, AccessToken $token)
122{
123return new DropboxResourceOwner($response);
124}
125}
126