fix docblocks
[grauphel.git] / lib / oauth.php
1 <?php
2 /**
3  * Part of grauphel
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Grauphel
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/grauphel.htm
13  */
14 namespace OCA\Grauphel\Lib;
15
16 /**
17  * Storage base class that implements note updating
18  *
19  * @category  Tools
20  * @package   Grauphel
21  * @author    Christian Weiske <cweiske@cweiske.de>
22  * @copyright 2014 Christian Weiske
23  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24  * @version   Release: @package_version@
25  * @link      http://cweiske.de/grauphel.htm
26  */
27 class OAuth
28 {
29     /**
30      * Token data store
31      *
32      * @var Token_Storage
33      */
34     protected $tokens;
35
36     public function setDeps(Dependencies $deps)
37     {
38         $this->tokens = $deps->tokens;
39     }
40
41     /**
42      * Register callbacks for the oauth dance.
43      */
44     public function registerHandler(\OAuthProvider $provider)
45     {
46         $provider->consumerHandler(array($this, 'lookupConsumer'));
47         $provider->timestampNonceHandler(array($this, 'timestampNonceChecker'));
48         return $this;
49     }
50
51     public function registerVerificationTokenHandler(\OAuthProvider $provider)
52     {
53         $provider->tokenHandler(array($this, 'verifyTokenHandler'));
54         return $this;
55     }
56
57     public function registerAccessTokenHandler(\OAuthProvider $provider)
58     {
59         $provider->tokenHandler(array($this, 'accessTokenHandler'));
60         return $this;
61     }
62
63     public function validateToken($tokenKey)
64     {
65         return (bool) preg_match('#^[a-z0-9]+$#', $tokenKey);
66     }
67
68     public function lookupConsumer(\OAuthProvider $provider)
69     {
70         //tomboy assumes secret==key=="anyone"
71         $provider->consumer_secret = $provider->consumer_key;//'anyone';
72         $provider->addRequiredParameter('oauth_callback');
73
74         return OAUTH_OK;
75     }
76
77     public function timestampNonceChecker(\OAuthProvider $provider)
78     {
79         //var_dump($provider->nonce, $provider->timestamp);
80         //OAUTH_BAD_NONCE
81         //OAUTH_BAD_TIMESTAMP
82         return OAUTH_OK;
83     }
84
85     public function verifyTokenHandler(\OAuthProvider $provider)
86     {
87         $token = $this->tokens->load('verify', $provider->token);
88         if ($provider->verifier == '') {
89             return OAUTH_VERIFIER_INVALID;
90         }
91         if ($provider->verifier != $token->verifier) {
92             return OAUTH_VERIFIER_INVALID;
93         }
94
95         $provider->token_secret = $token->secret;
96         return OAUTH_OK;
97     }
98
99     public function accessTokenHandler(\OAuthProvider $provider)
100     {
101         $token = $this->tokens->load('access', $provider->token);
102         $provider->token_secret = $token->secret;
103         return OAUTH_OK;
104     }
105
106     public function verifyOAuthUser($username, $url)
107     {
108         try {
109             $provider = OAuth::getProvider();
110             $this->registerHandler($provider);
111             $this->registerAccessTokenHandler($provider);
112             //do not use "user" in signature
113             $provider->setParam('user', null);
114
115             $provider->checkOAuthRequest($url);
116
117             $token = $this->tokens->load('access', $provider->token);
118             if ($token->user != $username) {
119                 errorOut('Invalid user');
120             }
121         } catch (\OAuthException $e) {
122             $this->error($e);
123         }
124     }
125
126     public function error(\OAuthException $e)
127     {
128         header('HTTP/1.0 400 Bad Request');
129         //header('Content-type: application/x-www-form-urlencoded');
130         echo \OAuthProvider::reportProblem($e);
131         //var_dump($e);
132         exit(1);
133     }
134
135     /**
136      * Get a new oauth provider instance.
137      * Used to work around the fastcgi bug in oauthprovider.
138      * 
139      * @return \OAuthProvider
140      */
141     public static function getProvider()
142     {
143         //$_SERVER['REDIRECT_HTTP_AUTHORIZATION'] = $_SERVER['HTTP_AUTHORIZATION'];
144         //unset($_SERVER['HTTP_AUTHORIZATION']);
145
146         $params = array();
147         if (!isset($_SERVER['HTTP_AUTHORIZATION'])
148             && isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])
149         ) {
150             //FastCgi puts the headers in REDIRECT_HTTP_AUTHORIZATION,
151             // but the oauth extension does not read that.
152             // we have to parse the parameters manually
153             $regex = "/(oauth_[a-z_-]*)=(?:\"([^\"]*)\"|([^,]*))/";
154             preg_match_all(
155                 $regex, $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches
156             );
157
158             foreach ($matches[1] as $key => $paramName) {
159                 $params[$paramName] = urldecode($matches[2][$key]);
160             }
161         }
162
163         return new \OAuthProvider($params);
164     }
165 }
166 ?>