6a7fa81332960785cb0f4a5ef7e43b0b5728fd4e
[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
51     $parts = explode(' ', $auth, 2);
52     if (count($parts) != 2) {
53         error('Authorization header must container "Bearer" and the token');
54     }
55
56     list($bearer, $token) = $parts;
57     if ($bearer !== 'Bearer') {
58         error('Authorization header must start with "Bearer"');
59     }
60
61     //FIXME: use real decryption
62     $encData = base64_decode($token);
63     if ($encData === false) {
64         error('Invalid token data');
65     }
66     parse_str($encData, $data);
67     $emoji     = verifyParameter($data, 'emoji');
68     $signature = verifyParameter($data, 'signature');
69     $me        = verifyUrlParameter($data, 'me');
70     $client_id = verifyUrlParameter($data, 'client_id');
71     $scope     = verifyParameter($data, 'scope');
72
73     if ($emoji != '\360\237\222\251') {
74         error('Dog poo missing');
75     }
76     if ($signature != 'FIXME') {
77         error('Invalid signature');
78     }
79
80     header('HTTP/1.0 200 OK');
81     header('Content-type: application/x-www-form-urlencoded');
82     echo http_build_query(
83         array(
84             'me'        => $me,
85             'client_id' => $client_id,
86             'scope'     => $scope,
87         )
88     );
89
90 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
91     //generate token
92     //we ignore the "me" parameter; it's for proxies only
93     // see https://github.com/cweiske/anoweco/issues/3
94     $redirect_uri = verifyUrlParameter($_POST, 'redirect_uri');
95     $client_id    = verifyUrlParameter($_POST, 'client_id');
96     $code         = verifyParameter($_POST, 'code');//auth token
97     $state        = getOptionalParameter($_POST, 'state', null);
98
99     //verify auth code
100     parse_str(base64_decode($code), $codeParts);
101     $emoji     = verifyParameter($codeParts, 'emoji');
102     $signature = verifyParameter($codeParts, 'signature');
103     $me        = verifyUrlParameter($codeParts, 'me');
104     if ($emoji != '\360\237\222\251') {
105         error('Auth token: Dog poo missing');
106     }
107     if ($signature != 'FIXME') {
108         error('Auth token: Invalid signature');
109     }
110
111     //FIXME: check if state are set
112     //FIXME: check auth endpoint if parameters are valid
113     //        and to get the scope
114     $scope = 'post';
115
116     //FIXME: use real encryption
117     $access_token = base64_encode(
118         http_build_query(
119             array(
120                 'emoji'     => '\360\237\222\251',
121                 'me'        => $me,
122                 'client_id' => $client_id,
123                 'scope'     => $scope,
124                 'signature' => 'FIXME',
125             )
126         )
127     );
128     header('HTTP/1.0 200 OK');
129     header('Content-type: application/x-www-form-urlencoded');
130     echo http_build_query(
131         array(
132             'access_token' => $access_token,
133             'me' => $me,
134             'scope' => $scope
135         )
136     );
137 }
138 ?>