1: <?php
2: /**
3: * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
4: *
5: * @author Chuck Hagenbuch <chuck@horde.org>
6: * @license http://www.horde.org/licenses/bsd BSD
7: * @category Horde
8: * @package Oauth
9: */
10:
11: /**
12: * OAuth consumer class
13: *
14: * @author Chuck Hagenbuch <chuck@horde.org>
15: * @license http://www.horde.org/licenses/bsd BSD
16: * @category Horde
17: * @package Oauth
18: */
19: class Horde_Oauth_Consumer
20: {
21: protected $_config;
22:
23: /**
24: * Const'r for consumer.
25: *
26: * @param array $config Configuration values:
27: * <pre>
28: * 'key' - Consumer key
29: * 'secret' - Consumer secret
30: * 'requestTokenUrl' - The request token URL
31: * 'authorizeTokenUrl' - The authorize URL
32: * 'accessTokenUrl' = To obtain an access token
33: * 'signatureMethod - Horde_Oauth_SignatureMethod object
34: * </pre>
35: *
36: * @return Horde_Oauth_Consumer
37: */
38: public function __construct($config)
39: {
40: // Check for required config
41: if (!is_array($config) || empty($config['key']) || empty($config['secret']) ||
42: empty($config['requestTokenUrl']) || empty($config['authorizeTokenUrl']) ||
43: empty($config['signatureMethod'])) {
44:
45: throw new InvalidArgumentException('Missing a required parameter in Horde_Oauth_Consumer::__construct');
46: }
47: $this->_config = $config;
48: }
49:
50: public function __get($name)
51: {
52: return isset($this->_config[$name]) ? $this->_config[$name] : null;
53: }
54:
55: /**
56: * Obtain an unprivileged request token
57: *
58: * @param array $params Parameter array
59: *
60: * @return Horde_Oauth_Token The oauth request token
61: */
62: public function getRequestToken($params = array())
63: {
64: $params['oauth_consumer_key'] = $this->key;
65: $params['oauth_callback'] = $this->callbackUrl;
66:
67: $request = new Horde_Oauth_Request($this->requestTokenUrl, $params);
68: $request->sign($this->signatureMethod, $this);
69:
70: $client = new Horde_Http_Client;
71:
72: try {
73: $response = $client->post(
74: $this->requestTokenUrl,
75: $request->buildHttpQuery()
76: );
77: } catch (Horde_Http_Exception $e) {
78: throw new Horde_Oauth_Exception($e->getMessage());
79: }
80:
81: return Horde_Oauth_Token::fromString($response->getBody());
82: }
83:
84: /**
85: * Get the user authorization url used to request user authorization
86: *
87: * @param Horde_Oauth_Token $token the oauth request token
88: *
89: * @return string The user authorization url string
90: */
91: public function getUserAuthorizationUrl($token)
92: {
93: return $this->authorizeTokenUrl . '?oauth_token=' . urlencode($token->key) . '&oauth_callback=' . urlencode($this->callbackUrl);
94: }
95:
96: /**
97: * Obtain an access token from a request token
98: *
99: * @param Horde_Oauth_Token $token Open auth token containing the oauth_token
100: * returned from provider after authorization
101: * and the token secret returned with the
102: * original request token.
103: * @param array $params Any additional parameters for this request
104: *
105: * @return unknown_type
106: */
107: public function getAccessToken($token, $params = array())
108: {
109: $params['oauth_consumer_key'] = $this->key;
110: $params['oauth_token'] = $token->key;
111:
112: $request = new Horde_Oauth_Request($this->accessTokenUrl, $params);
113: $request->sign($this->signatureMethod, $this, $token);
114:
115: $client = new Horde_Http_Client;
116: try {
117: $response = $client->post(
118: $this->accessTokenUrl,
119: $request->buildHttpQuery()
120: );
121: } catch (Horde_Http_Exception $e) {
122: throw new Horde_Oauth_Exception($e->getMessage());
123: }
124:
125: return Horde_Oauth_Token::fromString($response->getBody());
126: }
127: }
128: