gonito/static/test-gonito-as-backend.html

100 lines
3.0 KiB
HTML
Raw Normal View History

2020-12-09 21:55:31 +01:00
<html>
<head>
<script src="/static/js/keycloak.js"></script>
<script>
var keycloak;
function initKeycloak() {
keycloak = new Keycloak({
url: 'http://127.0.0.1:8080/auth',
realm: 'master',
clientId: 'myapp',
"enable-cors": true
})
keycloak.init({
onLoad: 'login-required'
}).then(function(authenticated) {
// alert(authenticated ? 'authenticated' : 'not authenticated');
}).catch(function() {
alert('failed to initialize');
});
}
2020-12-10 23:24:10 +01:00
var loadData = function (target) {
2020-12-09 21:55:31 +01:00
2020-12-10 23:24:10 +01:00
var url = 'http://127.0.0.1:3000/api/' + target;
2020-12-09 21:55:31 +01:00
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.setRequestHeader('Accept', 'application/json');
req.setRequestHeader('Authorization', 'Bearer ' + keycloak.token);
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status == 200) {
alert(req.response);
} else if (req.status == 403) {
alert('Forbidden');
}
}
}
req.send();
};
function testCors() {
2020-12-09 21:55:31 +01:00
var createCORSRequest = function(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Most browsers.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// IE8 & IE9
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
xhr.setRequestHeader('Authorization', 'Bearer ' +
keycloak.token);
//xhr.setRequestHeader('Xyz', 'Blabla');
xhr.setRequestHeader('Accept', 'application/json');
return xhr;
};
var url = 'http://127.0.0.1:3000/api/list-challenges';
var method = 'GET';
var xhr = createCORSRequest(method, url);
xhr.onload = function() {
alert("OK");
};
xhr.onerror = function() {
alert("NOT OK")
};
xhr.send();
}
2020-12-09 21:55:31 +01:00
</script>
</head>
<body onload="initKeycloak()">
<h1>This is a simple web page to test Gonito as a backend with authorization by JWT tokens.</h1>
2020-12-10 23:24:10 +01:00
<p><button onclick="loadData('add-user')">Add user</button></p>
<p><button onclick="loadData('user-info')">Check user</button></p>
<p><button onclick="loadData('challenge-my-submissions/retroc2')">Other test</button></p>
<p><button onclick="loadData('list-challenges')">Yet another
test</button></p>
<p><button onclick="testCors()">CORS</button></p>
2020-12-09 21:55:31 +01:00
</body>
</html>