show tags and note titles in the notebook
[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         if ($provider->token == '') {
102             //conboy sends empty token when not authed yet
103             return OAUTH_PARAMETER_ABSENT;
104         }
105
106         $token = $this->tokens->load('access', $provider->token);
107         $provider->token_secret = $token->secret;
108         return OAUTH_OK;
109     }
110
111     public function verifyOAuthUser($username, $url)
112     {
113         try {
114             $provider = OAuth::getProvider();
115             $this->registerHandler($provider);
116             $this->registerAccessTokenHandler($provider);
117             //do not use "user" in signature
118             $provider->setParam('user', null);
119
120             $provider->checkOAuthRequest($url);
121
122             $token = $this->tokens->load('access', $provider->token);
123             if ($token->user != $username) {
124                 errorOut('Invalid user');
125             }
126         } catch (\OAuthException $e) {
127             $this->error($e);
128         }
129     }
130
131     public function error(\OAuthException $e)
132     {
133         header('HTTP/1.0 400 Bad Request');
134         //header('Content-type: application/x-www-form-urlencoded');
135         echo \OAuthProvider::reportProblem($e);
136         //var_dump($e);
137         exit(1);
138     }
139
140     /**
141      * Get a new oauth provider instance.
142      * Used to work around the fastcgi bug in oauthprovider.
143      * 
144      * @return \OAuthProvider
145      */
146     public static function getProvider()
147     {
148         //$_SERVER['REDIRECT_HTTP_AUTHORIZATION'] = $_SERVER['HTTP_AUTHORIZATION'];
149         //unset($_SERVER['HTTP_AUTHORIZATION']);
150
151         $params = array();
152         if (!isset($_SERVER['HTTP_AUTHORIZATION'])
153             && isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])
154         ) {
155             //FastCgi puts the headers in REDIRECT_HTTP_AUTHORIZATION,
156             // but the oauth extension does not read that.
157             // we have to parse the parameters manually
158             $regex = "/(oauth_[a-z_-]*)=(?:\"([^\"]*)\"|([^,]*))/";
159             preg_match_all(
160                 $regex, $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches
161             );
162
163             foreach ($matches[1] as $key => $paramName) {
164                 $params[$paramName] = urldecode($matches[2][$key]);
165             }
166         }
167
168         return new \OAuthProvider($params);
169     }
170 }
171 ?>