-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
93 lines (63 loc) · 2.41 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Mardy\Hmac\Hmac;
use Mardy\Hmac\Config\Config as HmacConfig;
use Mardy\Hmac\Storage\NonPersistent as HmacStorage;
$klein = new \Klein\Klein();
$klein->respond(function($request, $response, $service, $app) {
$app->config = require_once __DIR__ . '/config.php';
$app->register('db', function() use ($app) {
$cfg = $app->config['mysql'];
$db = new PDO('mysql:host='.$cfg['hostname'].';dbname='.$cfg['database'].';charset=UTF-8', $cfg['username'], $cfg['password']);
return $db;
});
$app->register('parseKey', function($auth) {
$exploded = explode(':', $auth);
if(count($exploded) != 2) {
return false;
}
return array('key' => $exploded[0], 'secret' => $exploded[1]);
});
});
$klein->with('/v2', function() use ($klein) {
/**
* File System
*
* Requires authentication
*/
$klein->with('/fs', function() use ($klein) {
$klein->respond(function(\Klein\Request $request, \Klein\Response $response, $service, $app) {
$headers = $request->headers();
// Pre-Auth
$service->auth_key = $headers['auth-key'];
$service->auth_secret = $headers['auth-secret'];
$authorization = $app->parseKey($headers['Authorization']);
if(!$authorization) {
return $response->json("Nope");
}
// Get user & key
$app->register('hmac', function() use($request) {
$hmac = new Hmac(new HmacConfig, new HmacStorage, new Mardy\Hmac\Headers\Values);
$hmac->getConfig()->setAlgorithm("sha512");
return $hmac;
});
$app->hmac->getStorage()
->setHmac($service->auth_key)
->setTimestamp($headers['auth-timestamp'])
->setUri($request->uri());
// Test authentication
if(!$app->hmac->check()) {
return $response->json(array('errors' => array($app->hmac->getError())));
}
});
/**
* List our filesystem
*/
$klein->respond('GET', '/?', function($request, $response, $service, $app) {
return $response->json(array('stuff'));
});
$klein->respond('POST', '/?', function($request, $response, $service) {
});
});
});
$klein->dispatch();