-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathremoteStorage-0.4.4.js
88 lines (87 loc) · 3.03 KB
/
remoteStorage-0.4.4.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
define(
['require', './lib-0.4.4/ajax', './lib-0.4.4/couch', './lib-0.4.4/dav', './lib-0.4.4/webfinger'],
function (require, ajax, couch, dav, webfinger) {
var onError = function (code, msg) {
console.log(msg);
},
getStorageInfo = function (userAddress, cb) {
webfinger.getAttributes(
userAddress, {
allowHttpWebfinger: true,
allowSingleOriginWebfinger: false,
allowFakefinger: true
},
function (err, data) {
cb(err, null);
},
function (attributes) {
cb(0, attributes);
var storageAddresses = {};
}
);
},
createOAuthAddress = function (storageInfo, categories, redirectUri) {
var terms = [
'redirect_uri='+encodeURIComponent(redirectUri),
'scope='+encodeURIComponent(categories.join(',')),
'response_type=token',
'client_id='+encodeURIComponent(redirectUri)
];
return storageInfo.auth + (storageInfo.auth.indexOf('?') === -1?'?':'&') + terms.join('&');
},
getDriver = function (api, cb) {
require([api === 'CouchDB'?'./lib/couch':'./lib/dav'], cb);
},
createClient = function (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);
});
}
};
},
receiveToken = function () {
var params, kv;
/**
this needs more attention.
**/
if(location.hash.length > 0) {
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);
}
kv = params[i].split('=');
if(kv.length >= 2) {
if(kv[0]==='access_token') {
///XXX: ok im guessing its a base64 string and you somehow adding an = sign to the end of it ok, why?
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
};
});