-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlocalhost-server.js
171 lines (163 loc) · 5.68 KB
/
localhost-server.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var fs = require('fs'),
url = require('url'),
crypto = require('crypto'),
RemotestorageServer = require('remotestorage-server');
exports.createInstance = function(kv, config) {
var tokenStore = {
get: function(username, token, cb) { return kv.get(username+':token:'+token, cb); },
set: function(username, token, obj, cb) { return kv.set(username+':token:'+token, obj, cb); }
};
var dataStore = {
get: function(username, key, cb) { return kv.get(username+':data:'+key, cb); },
set: function(username, key, buf, cb) { return kv.set(username+':data:'+key, buf, cb); }
};
var rootScope,
specVersion = 'draft-dejong-remotestorage-04',
remotestorageServer = new RemotestorageServer(specVersion, tokenStore, dataStore);
if (specVersion === 'draft-dejong-remotestorage-00' || specVersion === 'draft-dejong-remotestorage-01') {
rootScope = 'root';
} else {//02, 03, etc.
rootScope = '*';
}
function log() {
console.log.apply(console, arguments);
}
function createToken(userName, scopes, cb) {
crypto.randomBytes(48, function(ex, buf) {
var token = buf.toString('hex');
var scopePaths = remotestorageServer.makeScopePaths(scopes);
log('createToken ',userName,scopes);
log('adding ',scopePaths,' for',token);
tokenStore.set(userName, token, scopePaths, function(err) {
cb(token);
});
});
}
function writeHead(res, status, origin, timestamp, contentType, contentLength) {
console.log('writeHead', status, origin, timestamp, contentType, contentLength);
var headers = {
'Access-Control-Allow-Origin': (origin?origin:'*'),
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Origin',
'Access-Control-Allow-Methods': 'GET, PUT, DELETE',
'Expires': '0'
};
if(typeof(timestamp) != 'undefined') {
headers['etag']= '"'+timestamp.toString()+'"';
}
if(contentType) {
headers['content-type']= contentType;
}
if(contentLength) {
headers['content-length']= contentLength;
}
res.writeHead(status, headers);
}
function writeRaw(res, contentType, content, origin, timestamp) {
writeHead(res, 200, origin, timestamp, contentType, content.length);
res.write(content);
res.end();
}
function writeJson(res, obj, origin, timestamp) {
writeRaw(res, 'application/json', JSON.stringify(obj), origin, timestamp);
}
function writeHtml(res, html) {
res.writeHead(200, {
'content-type': 'text/html'
});
res.write('<!DOCTYPE html lang="en"><head><title>'+config.host+'</title><meta charset="utf-8"></head><body>'+html+'</body></html>');
res.end();
}
function give404(res, origin) {
log('404');
writeHead(res, 404, origin);
res.end();
}
function computerSaysNo(res, origin, status, timestamp) {
log('COMPUTER_SAYS_NO - '+status);
writeHead(res, status, origin, timestamp);
res.end();
}
function toHtml(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
function portal(req, res) {
var urlObj = url.parse(req.url, true);
res.writeHead(200, {
'content-type': 'text/html'
});
res.write('<!DOCTYPE html lang="en"><head><title>'+config.host+'</title><meta charset="utf-8"></head><body><ul>');
var outstanding = 0;
for(var i in config.apps) {
outstanding++;
(function(i) {
createToken(config.defaultUserName, [rootScope+':rw'], function(token) {
res.write('<li><a href="'+i+'#remotestorage=me@'+config.host+':'+config.mainPort
+'&access_token='+token+'">'+config.apps[i]+'</a></li>');
outstanding--;
if(outstanding==0) {
res.write('</ul></body></html>');
res.end();
}
});
})(i);
}
}
function webfinger(req, res) {
var urlObj = url.parse(req.url, true);
log('WEBFINGER');
if(urlObj.query['resource']) {
userAddress = urlObj.query['resource'].substring('acct:'.length);
userName = userAddress.split('@')[0];
}
writeJson(res, {
links:[
remotestorageServer.getWebfingerLink(
'http',
config.host,
config.storagePort,
userName,
'http://'+config.host+':'+config.mainPort+'/auth/'+userName,
config.host)
]
});
}
function oauth(req, res) {
var urlObj = url.parse(req.url, true);
var scopes = decodeURIComponent(urlObj.query['scope']).split(' '),
clientId = decodeURIComponent(urlObj.query['client_id']),
redirectUri = decodeURIComponent(urlObj.query['redirect_uri']),
state = (urlObj.query['state'] ? decodeURIComponent(urlObj.query['state']) : undefined),
clientIdToMatch,
userName;
var userName = urlObj.pathname.substring('/auth/'.length);
createToken(userName, scopes, function(token) {
var linkURL = toHtml(redirectUri)
+ '#access_token='+toHtml(token)
+ (state === undefined ? '' : '&state='+toHtml(state));
if (Array.isArray(scopes)
&& scopes.length === 2
&& scopes[0] === 'apps:rw'
&& scopes[1] === 'www:rw'
&& redirectUri === 'http://localhost:8002/') {
writeHtml(res, '<script>location = "'+linkURL+'";</script>');
} else {
writeHtml(res, '<a href="' + linkURL + '">Allow</a>');
}
});
}
return {
portal: portal,
webfinger: webfinger,
oauth: oauth,
storage: function(req, res) {
return remotestorageServer.storage(req, res);
},
backdoorSet: function(username, path, body, contentType, cb) {
return remotestorageServer.backdoorSet(username, path, body, contentType, cb);
}
};
};