-
Notifications
You must be signed in to change notification settings - Fork 28
/
remoteStorage-0.4.2.js
85 lines (85 loc) · 2.57 KB
/
remoteStorage-0.4.2.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
define([
'require',
'http://unhosted.org/lib/ajax-0.4.2.js',
'http://unhosted.org/lib/couch-0.4.2.js',
'http://unhosted.org/lib/dav-0.4.2.js',
'http://unhosted.org/lib/webfinger-0.4.2.js'
], function(require, ajax, couch, dav, webfinger) {
function onError(code, msg) {
alert(msg);
}
function getStorageInfo(userAddress, cb) {
webfinger.getAttributes(userAddress, {
allowHttpWebfinger: true,
allowSingleOriginWebfinger: false,
allowFakefinger: true
}, function(err, data) {
cb(err, null);
}, function(attributes) {
cb(0, attributes);
var storageAddresses = {};
});
}
function createOAuthAddress(storageInfo, categories, redirectUri) {
return storageInfo.auth
+'?redirect_uri='+encodeURIComponent(redirectUri)
+'&scope='+encodeURIComponent(categories.join(','))
+'&response_type=token'
+'&client_id='+encodeURIComponent(redirectUri);
}
function getDriver(api, cb) {
if(api == 'CouchDB') {
require(['http://unhosted.org/lib/couch-0.4.2.js'], cb);
} else {//'simple', 'WebDAV'
require(['http://unhosted.org/lib/dav-0.4.2.js'], cb);
}
}
function createClient(storageInfo, category, token) {
var storageAddress = webfinger.resolveTemplate(storageInfo.template, category)
return {
get: function(key, cb) {
getDriver(storageInfo.api, function(d) {
d.get(storageAddress, token, key, cb);
});
},
put: function(key, value, cb) {
getDriver(storageInfo.api, function(d) {
d.put(storageAddress, token, key, value, cb);
});
},
delete: function(key, cb) {
getDriver(storageInfo.api, function(d) {
d.delete(storageAddress, token, key, cb);
});
}
};
}
function receiveToken() {
if(location.hash.length == 0) {
return null;
}
var params = location.hash.split('&');
for(var i = 0; i < params.length; i++){
if(params[i].length && params[i][0] =='#') {
params[i] = params[i].substring(1);
}
var kv = params[i].split('=');
if(kv.length >= 2) {
if(kv[0]=='access_token') {
var token = unescape(kv[1]);//unescaping is needed in chrome, otherwise you get %3D%3D at the end instead of ==
for(var i = 2; i < kv.length; i++) {
token += '='+kv[i];
}
return token;
}
}
}
return null;
}
return {
getStorageInfo: getStorageInfo,
createOAuthAddress: createOAuthAddress,
createClient: createClient,
receiveToken: receiveToken
};
});