6d417c695817bafcfa22275f5508c2cc56fd9817
[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     $me           = verifyUrlParameter($_POST, 'me');
87     $redirect_uri = verifyUrlParameter($_POST, 'redirect_uri');
88     $client_id    = verifyUrlParameter($_POST, 'client_id');
89     $code         = verifyParameter($_POST, 'code');//auth token
90     $state        = getOptionalParameter($_POST, 'state', null);
91
92     //verify auth code
93     parse_str(base64_decode($code), $codeParts);
94     $emoji     = verifyParameter($codeParts, 'emoji');
95     $signature = verifyParameter($codeParts, 'signature');
96     $codeMe    = verifyUrlParameter($codeParts, 'me');
97     if ($emoji != '\360\237\222\251') {
98         error('Auth token: Dog poo missing');
99     }
100     if ($signature != 'FIXME') {
101         error('Auth token: Invalid signature');
102     }
103     if ($me !== $codeMe) {
104         error('Auth token is not valid for the given "me"');
105     }
106
107     //FIXME: check if state are set
108     //FIXME: check auth endpoint if parameters are valid
109     //        and to get the scope
110     $scope = 'post';
111
112     //FIXME: use real encryption
113     $access_token = base64_encode(
114         http_build_query(
115             array(
116                 'emoji'     => '\360\237\222\251',
117                 'me'        => $me,
118                 'client_id' => $client_id,
119                 'scope'     => $scope,
120                 'signature' => 'FIXME',
121             )
122         )
123     );
124     header('HTTP/1.0 200 OK');
125     header('Content-type: application/x-www-form-urlencoded');
126     echo http_build_query(
127         array(
128             'access_token' => $access_token,
129             'me' => $me,
130             'scope' => $scope
131         )
132     );
133 }
134 ?>