-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
123 lines (111 loc) · 4.67 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WebAppPassword Demo</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap.min.css" integrity="sha384-VCmXjywReHh4PwowAiWNagnWcLhlEJLA5buUprzK8rxFgeH0kww/aWY76TfkUoSX" crossorigin="anonymous">
<style>
textarea { width: 100%; height: 50px; }
textarea#output2 { height: 200px; }
.hidden { display: none; }
.container-fluid { margin: 10px 0; }
</style>
</head>
<body>
<header>
</header>
<main>
<div class="container-fluid">
<h1>WebAppPassword Demo</h1>
<div class="form-group">
<label for="app-url">App url</label>
<div class="input-group mb-3">
<input type="text" class="form-control" id="app-url" aria-describedby="basic-addon3" name="app-url" value="http://localhost:8081/index.php/apps/webapppassword">
</div>
</div>
<div class="form-group">
<label for="webdav-url">WebDAV base url</label>
<div class="input-group mb-3">
<input type="text" class="form-control" id="webdav-url" aria-describedby="basic-addon3" name="webdav-url" value="http://localhost:8081/remote.php/dav/files">
</div>
</div>
<button onclick="openFilePicker()" class="btn btn-primary">Open WebDAV file picker</button>
<button onclick="login()" class="btn btn-secondary">Basic window.open()</button>
</div>
<div class="container-fluid hidden" id="output-area">
<h2>Output</h2>
<div class="form-group">
<label for="output">Output from WebAppPassword</label>
<div class="input-group mb-3">
<textarea id="output" readonly></textarea>
</div>
</div>
<div class="form-group">
<label for="output2">Example WebDAV fetch</label>
<div class="input-group mb-3">
<textarea id="output2" readonly></textarea>
</div>
</div>
</div>
<!-- see https://github.com/digital-blueprint/toolkit/tree/main/packages/file-handling#filesource -->
<dbp-file-source
id="file-source"
lang="en"
enabled-sources="nextcloud"></dbp-file-source>
</main>
<footer></footer>
</body>
<script>
let wnd = null;
const outputArea = document.querySelector("#output-area");
const output = document.querySelector("#output");
const output2 = document.querySelector("#output2");
const appUrl = document.querySelector("#app-url").value;
const webdavUrl = document.querySelector("#webdav-url").value;
const fileSource = document.querySelector("#file-source");
function login() {
outputArea.classList.remove("hidden");
output.value = "";
output2.value = "";
// open auth window of WebAppPassword application
wnd = window.open(appUrl + "?target-origin=" + encodeURIComponent(window.location.href), "Nextcloud Login",
"width=400,height=400,menubar=no,scrollbars=no,status=no,titlebar=no,toolbar=no");
}
function openFilePicker() {
outputArea.classList.add("hidden");
fileSource.setAttribute("nextcloud-auth-url", appUrl);
fileSource.setAttribute("nextcloud-web-dav-url", webdavUrl);
fileSource.setAttribute("dialog-open", "");
}
// receives the "window.opener.postMessage" from https://github.com/digital-blueprint/webapppassword/blob/main/js/script.js
window.addEventListener('message', (event) => {
const data = event.data;
if (data.type === "webapppassword") {
if (wnd !== null) {
wnd.close();
}
output.value = JSON.stringify(data);
// set auth header from data received by WebAppPassword application
let headers = new Headers();
headers.set('Authorization', 'Basic ' + btoa(data.loginName + ":" + data.token));
// do PROPFIND WebDAV request
fetch(webdavUrl + "/" + data.loginName, {
method: 'PROPFIND', // *GET, POST, PUT, DELETE, etc.
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
headers: headers
})
.then(result => {
if (!result.ok) throw result;
return result.text();
})
.then((xml) => {
output2.value = xml;
}).catch(error => {
output2.value = error.message;
console.error(error);
});
}
});
</script>
<script type="module" src="dist/dbp-file-source.js"></script>
</html>