abe14ed6f9c93c143151c7b5d48674ce1a787295
[anoweco.git] / www / token.php
1 <?php
2 header('HTTP/1.0 500 Internal Server Error');
3
4 function error($msg)
5 {
6     header('HTTP/1.0 400 Bad Request');
7     header('Content-type: text/plain; charset=utf-8');
8     echo $msg . "\n";
9     exit(1);
10 }
11
12 function verifyParameter($givenParams, $paramName)
13 {
14     if (!isset($givenParams[$paramName])) {
15         error('"' . $paramName . '" parameter missing');
16     }
17     return $givenParams[$paramName];
18 }
19 function verifyUrlParameter($givenParams, $paramName)
20 {
21     verifyParameter($givenParams, $paramName);
22     $url = parse_url($givenParams[$paramName]);
23     if (!isset($url['scheme'])) {
24         error('Invalid URL in "' . $paramName . '" parameter: scheme missing');
25     }
26     if (!isset($url['host'])) {
27         error('Invalid URL in "' . $paramName . '" parameter: host missing');
28     }
29
30     return $givenParams[$paramName];
31 }
32 function getOptionalParameter($givenParams, $paramName, $default)
33 {
34     if (!isset($givenParams[$paramName])) {
35         return $default;
36     }
37     return $givenParams[$paramName];
38 }
39
40 if ($_SERVER['REQUEST_METHOD'] == 'GET') {
41     //verify token
42     if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
43         $auth = $_SERVER['HTTP_AUTHORIZATION'];
44     } else if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
45         //php-cgi has it there
46         $auth = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
47     } else {
48         error('Authorization HTTP header missing');
49     }
50     list($bearer, $token) = explode(' ', $auth, 2);
51     if ($bearer !== 'Bearer') {
52         error('Authorization header must start with "Bearer"');
53     }
54
55     //FIXME: use real decryption
56     $encData = base64_decode($token);
57     if ($encData === false) {
58         error('Invalid token data');
59     }
60     parse_str($encData, $data);
61     $emoji     = verifyParameter($data, 'emoji');
62     $signature = verifyParameter($data, 'signature');
63     $me        = verifyUrlParameter($data, 'me');
64     $client_id = verifyUrlParameter($data, 'client_id');
65     $scope     = verifyParameter($data, 'scope');
66
67     if ($emoji != '\360\237\222\251') {
68         error('Dog poo missing');
69     }
70     if ($signature != 'FIXME') {
71         error('Invalid signature');
72     }
73
74     header('HTTP/1.0 200 OK');
75     header('Content-type: application/x-www-form-urlencoded');
76     echo http_build_query(
77         array(
78             'me'        => $me,
79             'client_id' => $client_id,
80             'scope'     => $scope,
81         )
82     );
83
84 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
85     //generate token
86     //we ignore the "me" parameter; it's for proxies only
87     // see https://github.com/cweiske/anoweco/issues/3
88     $redirect_uri = verifyUrlParameter($_POST, 'redirect_uri');
89     $client_id    = verifyUrlParameter($_POST, 'client_id');
90     $code         = verifyParameter($_POST, 'code');//auth token
91     $state        = getOptionalParameter($_POST, 'state', null);
92
93     //verify auth code
94     parse_str(base64_decode($code), $codeParts);
95     $emoji     = verifyParameter($codeParts, 'emoji');
96     $signature = verifyParameter($codeParts, 'signature');
97     $me        = verifyUrlParameter($codeParts, 'me');
98     if ($emoji != '\360\237\222\251') {
99         error('Auth token: Dog poo missing');
100     }
101     if ($signature != 'FIXME') {
102         error('Auth token: Invalid signature');
103     }
104
105     //FIXME: check if state are set
106     //FIXME: check auth endpoint if parameters are valid
107     //        and to get the scope
108     $scope = 'post';
109
110     //FIXME: use real encryption
111     $access_token = base64_encode(
112         http_build_query(
113             array(
114                 'emoji'     => '\360\237\222\251',
115                 'me'        => $me,
116                 'client_id' => $client_id,
117                 'scope'     => $scope,
118                 'signature' => 'FIXME',
119             )
120         )
121     );
122     header('HTTP/1.0 200 OK');
123     header('Content-type: application/x-www-form-urlencoded');
124     echo http_build_query(
125         array(
126             'access_token' => $access_token,
127             'me' => $me,
128             'scope' => $scope
129         )
130     );
131 }
132 ?>