1<?php
2/**
3 * Horde_Service_Twitter_Auth class to abstract all auth related tasks
4 *
5 * Basically implements Horde_Oauth_Client and passes the calls along to the
6 * protected oauth object.
7 *
8 * Copyright 2009-2017 Horde LLC (http://www.horde.org/)
9 *
10 * @author Michael J. Rubinsky <mrubinsk@horde.org>
11 * @license  http://www.horde.org/licenses/bsd BSD
12 * @category Horde
13 * @package Service_Twitter
14 */
15class Horde_Service_Twitter_Auth_Oauth extends Horde_Service_Twitter_Auth
16{
17    /**
18     *
19     * @var Horde_OAuth_Token
20     */
21    protected $_token;
22
23    public function __construct(Horde_OAuth_Consumer $oauth)
24    {
25        $this->_config['oauth'] = $oauth;
26    }
27
28    /**
29     * Obtain the URL used to get an authorization token.
30     *
31     * @param Horde_Oauth_Token $requestToken The request token
32     *
33     * @return string  The Url
34     */
35    public function getUserAuthorizationUrl($requestToken)
36    {
37        return $this->oauth->getUserAuthorizationUrl($requestToken);
38    }
39
40    /**
41     * Set the access token
42     *
43     * @param Horde_OAuth_Token $token
44     */
45    public function setToken(Horde_OAuth_Token $token)
46    {
47        $this->_token = $token;
48    }
49
50    /**
51     * Obtain the access token. This is the token that should be persisted to
52     * storage.
53     *
54     * @param Horde_Controller_Request_Http     Http request object
55     * @param string $requestSecret             The token secret returned by
56     *                                          Twitter after the user authorizes
57     *                                          the application.
58     * @return Horde_Oauth_Token
59     * @throws Horde_Service_Twitter_Exception
60     */
61    public function getAccessToken(Horde_Controller_Request_Http $request, $requestSecret = null)
62    {
63        if (!empty($this->_token)) {
64            return $this->_token;
65        }
66
67        $params = $request->getGetVars();
68        if (empty($params['oauth_token'])) {
69            return false;
70        }
71        $token = new Horde_Oauth_Token($params['oauth_token'], $requestSecret);
72        try {
73            return $this->oauth->getAccessToken($token, array('oauth_verifier' => $requestSecret));
74        } catch (Horde_Oauth_Exception $e) {
75            throw new Horde_Service_Twitter_Exception($e->getMessage());
76        }
77    }
78
79    /**
80     * Obtain the OAuth request token
81     *
82     * @param array $params
83     *
84     * @return  Horde_OAuth_Token  The request token
85     * @throws Horde_Service_Twitter_Exception
86     */
87    public function getRequestToken($params = array())
88    {
89        try {
90            return $this->oauth->getRequestToken($params);
91        } catch (Horde_Oauth_Exception $e) {
92            throw new Horde_Service_Twitter_Exception($e->getMessage());
93        }
94    }
95
96}
97