-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
183 lines (153 loc) · 5.91 KB
/
index.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
172
173
174
175
176
177
178
179
180
181
182
183
const publicLinks = {};
const params = new URLSearchParams(window.location.search);
const config = {
debug: params.get('debug') || false,
origin: params.get('origin'),
variation: params.get('locationPicker') ? 'location' : 'resource',
publicLink: params.get('publicLink') || false,
publicLinkDuration: parseInt(params.get('publicLinkDuration'), 10),
publicLinkDescription: params.get('publicLinkDescription'),
server: null,
authority: null,
clientId: null,
accessToken: null,
};
const filepickerContainer = document.querySelector('#filepicker-container');
const filepickerElem = document.createElement('file-picker');
filepickerElem.id = 'file-picker';
filepickerElem.setAttribute('variation', config.variation);
filepickerElem.setAttribute('is-select-btn-displayed', false);
filepickerContainer.appendChild(filepickerElem);
const wildcardToRegex = s => {
return new RegExp('^' + s.split(/\*+/).map(regexEscape).join('.*') + '$');
};
const regexEscape = s => {
return s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
};
const parseParams = async () => {
if (!config.origin) {
throw new Error('You must specify a origin query parameter');
}
const allowedOrigins = await getAllowedOrigins();
for (allowedOrigin of allowedOrigins) {
if (config.origin.match(wildcardToRegex(allowedOrigin))) {
return;
}
}
throw new Error(`Invalid origin ${config.origin}`);
};
const getConfig = async () => {
if (config.debug) console.info('fetching configuration');
const configRes = await fetch('file-picker-config.json');
const configData = await configRes.json();
config.server = configData.server;
config.authority = configData.openIdConnect.authority;
config.clientId = configData.openIdConnect.client_id;
};
const getAllowedOrigins = async () => {
const allowedOriginsRes = await fetch('allowed-origins.json');
const allowedOrigins = await allowedOriginsRes.json();
return allowedOrigins;
};
const checkAccessToken = async (accessToken, expiresAt) => {
const accessCheckUrl = `${config.server}/remote.php/webdav/?access_token=${accessToken}`;
try {
await fetch(accessCheckUrl, { method: 'HEAD', headers: { Authorization: `Bearer ${accessToken}` } });
return true;
} catch (e) {
if (expiresAt < Date.now() / 1000) {
if (config.debug) console.info('access token is expired, auth will take care of it');
return false;
}
// Right now cernbox causes a cors error when not authenticated, so there is no way to check status.
if (config.debug) console.info('access token does not seem to work, cleaning up');
sendSelection();
sessionStorage.clear();
location.reload();
return false;
}
};
const getAccessToken = async () => {
const authKey = JSON.parse(sessionStorage.getItem(`oc_oAuthuser:${config.authority}:${config.clientId}`));
if (!authKey) {
config.accessToken = null;
return;
}
const valid = await checkAccessToken(authKey.access_token, authKey.expires_at);
config.accessToken = valid ? authKey.access_token : null;
};
const handleUpdateBasic = paths => {
return paths.map(path => `${config.server}/remote.php/webdav${path}?access_token=${config.accessToken}`);
};
const generatePublicLink = async () => {
const publicLinkRequestUrl = `${config.server}/ocs/v1.php/apps/files_sharing/api/v1/shares`;
const data = new FormData();
data.append('shareType', 3);
data.append('path', path);
data.append('permissions', 1);
data.append('internal', true);
if (config.publicLinkDuration) {
const expireDate = new Date();
expireDate.setDate(expireDate.getDate() + config.publicLinkDuration);
data.append('expireDate', expireDate.toISOString());
}
if (config.publicLinkDescription) {
data.append('description', config.publicLinkDescription);
}
const publicLinkRes = await fetch(publicLinkRequestUrl, {
method: 'POST',
body: data,
headers: { Authorization: `Bearer ${config.accessToken}` },
});
const publicLinkText = await publicLinkRes.text();
const parser = new DOMParser();
const publicLinkDocument = parser.parseFromString(publicLinkText, 'text/xml');
const token = publicLinkDocument.querySelector('token').textContent;
const fileName = path.split('/').pop();
return `${config.server}/remote.php/dav/public-files/${token}/${encodeURIComponent(fileName)}`;
};
const handleUpdatePublicLink = async paths => {
newPaths = paths.filter(path => !publicLinks[path]);
for (path of newPaths) {
publicLinks[path] = await generatePublicLink(path);
}
if (config.debug) console.info('public link cache', publicLinks);
return paths.map(path => publicLinks[path]);
};
const sendSelection = (payload = { files: [], ready: false }) => {
if (config.debug) console.info('sending message to parent', payload, config.origin);
window.parent.postMessage(payload, config.origin);
};
async function handleUpdateSelection(event) {
// Send clear selected files and not ready while the filepicker works.
sendSelection();
// Get the newest access token.
if (!config.clientId) {
await getConfig();
}
await getAccessToken();
const paths = event.detail[0].map(r => r.path);
const files = config.publicLink ? await handleUpdatePublicLink(paths) : handleUpdateBasic(paths);
sendSelection({ files, ready: true });
}
async function handleUpdateLocation(event) {
const paths = event.detail[0].map(r => r.path);
if (!paths.length) {
sendSelection();
return;
}
const username = sessionStorage.getItem('sub');
await getAccessToken();
sendSelection({ files: paths, username, accessToken: config.accessToken, ready: true });
}
(async () => {
await parseParams();
await getConfig();
await getAccessToken();
const filePickerElem = document.getElementById('file-picker');
if (config.variation === 'resource') {
filePickerElem.addEventListener('update', handleUpdateSelection);
} else if (config.variation === 'location') {
filePickerElem.addEventListener('update', handleUpdateLocation);
}
})();