xref: /plugin/oauth/vendor/lusitanian/oauth/src/OAuth/OAuth2/Service/Ustream.php (revision 98a3611675f696f14f2d12205fb81f7db0cf7b25)
1*98a36116SAndreas Gohr<?php
2*98a36116SAndreas Gohr
3*98a36116SAndreas Gohrnamespace OAuth\OAuth2\Service;
4*98a36116SAndreas Gohr
5*98a36116SAndreas Gohruse OAuth\OAuth2\Token\StdOAuth2Token;
6*98a36116SAndreas Gohruse OAuth\Common\Http\Exception\TokenResponseException;
7*98a36116SAndreas Gohruse OAuth\Common\Http\Uri\Uri;
8*98a36116SAndreas Gohruse OAuth\Common\Consumer\CredentialsInterface;
9*98a36116SAndreas Gohruse OAuth\Common\Http\Client\ClientInterface;
10*98a36116SAndreas Gohruse OAuth\Common\Storage\TokenStorageInterface;
11*98a36116SAndreas Gohruse OAuth\Common\Http\Uri\UriInterface;
12*98a36116SAndreas Gohr
13*98a36116SAndreas Gohrclass Ustream extends AbstractService
14*98a36116SAndreas Gohr{
15*98a36116SAndreas Gohr    /**
16*98a36116SAndreas Gohr     * Scopes
17*98a36116SAndreas Gohr     *
18*98a36116SAndreas Gohr     * @var string
19*98a36116SAndreas Gohr     */
20*98a36116SAndreas Gohr    const SCOPE_OFFLINE     = 'offline';
21*98a36116SAndreas Gohr    const SCOPE_BROADCASTER = 'broadcaster';
22*98a36116SAndreas Gohr
23*98a36116SAndreas Gohr    public function __construct(
24*98a36116SAndreas Gohr        CredentialsInterface $credentials,
25*98a36116SAndreas Gohr        ClientInterface $httpClient,
26*98a36116SAndreas Gohr        TokenStorageInterface $storage,
27*98a36116SAndreas Gohr        $scopes = array(),
28*98a36116SAndreas Gohr        UriInterface $baseApiUri = null
29*98a36116SAndreas Gohr    ) {
30*98a36116SAndreas Gohr        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
31*98a36116SAndreas Gohr
32*98a36116SAndreas Gohr        if (null === $baseApiUri) {
33*98a36116SAndreas Gohr            $this->baseApiUri = new Uri('https://api.ustream.tv/');
34*98a36116SAndreas Gohr        }
35*98a36116SAndreas Gohr    }
36*98a36116SAndreas Gohr
37*98a36116SAndreas Gohr    /**
38*98a36116SAndreas Gohr     * {@inheritdoc}
39*98a36116SAndreas Gohr     */
40*98a36116SAndreas Gohr    public function getAuthorizationEndpoint()
41*98a36116SAndreas Gohr    {
42*98a36116SAndreas Gohr        return new Uri('https://www.ustream.tv/oauth2/authorize');
43*98a36116SAndreas Gohr    }
44*98a36116SAndreas Gohr
45*98a36116SAndreas Gohr    /**
46*98a36116SAndreas Gohr     * {@inheritdoc}
47*98a36116SAndreas Gohr     */
48*98a36116SAndreas Gohr    public function getAccessTokenEndpoint()
49*98a36116SAndreas Gohr    {
50*98a36116SAndreas Gohr        return new Uri('https://www.ustream.tv/oauth2/token');
51*98a36116SAndreas Gohr    }
52*98a36116SAndreas Gohr
53*98a36116SAndreas Gohr    /**
54*98a36116SAndreas Gohr     * {@inheritdoc}
55*98a36116SAndreas Gohr     */
56*98a36116SAndreas Gohr    protected function getAuthorizationMethod()
57*98a36116SAndreas Gohr    {
58*98a36116SAndreas Gohr        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
59*98a36116SAndreas Gohr    }
60*98a36116SAndreas Gohr
61*98a36116SAndreas Gohr    /**
62*98a36116SAndreas Gohr     * {@inheritdoc}
63*98a36116SAndreas Gohr     */
64*98a36116SAndreas Gohr    protected function parseAccessTokenResponse($responseBody)
65*98a36116SAndreas Gohr    {
66*98a36116SAndreas Gohr        $data = json_decode($responseBody, true);
67*98a36116SAndreas Gohr
68*98a36116SAndreas Gohr        if (null === $data || !is_array($data)) {
69*98a36116SAndreas Gohr            throw new TokenResponseException('Unable to parse response.');
70*98a36116SAndreas Gohr        } elseif (isset($data['error'])) {
71*98a36116SAndreas Gohr            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
72*98a36116SAndreas Gohr        }
73*98a36116SAndreas Gohr
74*98a36116SAndreas Gohr        $token = new StdOAuth2Token();
75*98a36116SAndreas Gohr        $token->setAccessToken($data['access_token']);
76*98a36116SAndreas Gohr        $token->setLifeTime($data['expires_in']);
77*98a36116SAndreas Gohr
78*98a36116SAndreas Gohr        if (isset($data['refresh_token'])) {
79*98a36116SAndreas Gohr            $token->setRefreshToken($data['refresh_token']);
80*98a36116SAndreas Gohr            unset($data['refresh_token']);
81*98a36116SAndreas Gohr        }
82*98a36116SAndreas Gohr
83*98a36116SAndreas Gohr        unset($data['access_token']);
84*98a36116SAndreas Gohr        unset($data['expires_in']);
85*98a36116SAndreas Gohr
86*98a36116SAndreas Gohr        $token->setExtraParams($data);
87*98a36116SAndreas Gohr
88*98a36116SAndreas Gohr        return $token;
89*98a36116SAndreas Gohr    }
90*98a36116SAndreas Gohr
91*98a36116SAndreas Gohr    /**
92*98a36116SAndreas Gohr     * {@inheritdoc}
93*98a36116SAndreas Gohr     */
94*98a36116SAndreas Gohr    protected function getExtraOAuthHeaders()
95*98a36116SAndreas Gohr    {
96*98a36116SAndreas Gohr        return array('Authorization' => 'Basic ' . $this->credentials->getConsumerSecret());
97*98a36116SAndreas Gohr    }
98*98a36116SAndreas Gohr}
99