telebit-relay.js/lib/extensions/admin/account.html

118 lines
3.9 KiB
HTML
Raw Normal View History

2018-07-07 09:45:33 +00:00
<html>
<head>
<title>Telebit Account</title>
</head>
<body>
2018-08-04 10:21:06 +00:00
<h1>Login</h1>
2018-07-07 09:45:33 +00:00
<form class="js-auth-form">
2018-08-04 10:21:06 +00:00
<input class="js-auth-subject" placeholder="email" type="email"/>
2018-07-07 09:45:33 +00:00
<button class="js-auth-submit" type="submit">Login</button>
</form>
<script src="assets/oauth3.org/oauth3.core.js"></script>
<script>
(function () {
'use strict';
var OAUTH3 = window.OAUTH3;
var oauth3 = OAUTH3.create({
host: window.location.host
, pathname: window.location.pathname.replace(/\/[^\/]*$/, '/')
});
var $ = function () { return document.querySelector.apply(document, arguments); }
2018-08-04 10:21:06 +00:00
function onChangeProvider(providerUri) {
// example https://oauth3.org
return oauth3.setIdentityProvider(providerUri);
}
2018-07-07 09:45:33 +00:00
2018-08-04 10:21:06 +00:00
// This opens up the login window for the specified provider
//
function onClickLogin(ev) {
2018-07-07 09:45:33 +00:00
ev.preventDefault();
ev.stopPropagation();
2018-08-04 10:21:06 +00:00
var email = $('.js-auth-subject').value;
2018-07-07 09:45:33 +00:00
// TODO check subject for provider viability
return oauth3.authenticate({
2018-08-04 10:21:06 +00:00
subject: email
, scope: 'email@oauth3.org'
2018-07-07 09:45:33 +00:00
}).then(function (session) {
2018-08-04 10:21:06 +00:00
console.info('Authentication was Successful:');
console.log(session);
// You can use the PPID (or preferably a hash of it) as the login for your app
// (it securely functions as both username and password which is known only by your app)
// If you use a hash of it as an ID, you can also use the PPID itself as a decryption key
//
console.info('Secure PPID (aka subject):', session.token.sub);
2018-07-07 09:45:33 +00:00
2018-08-04 10:21:06 +00:00
function listStuff() {
window.alert("TODO: show authorized devices, domains, and connectivity information");
}
2018-07-07 09:45:33 +00:00
2018-08-04 10:21:06 +00:00
return oauth3.request({
url: 'https://api.oauth3.org/api/issuer@oauth3.org/jwks/:sub/:kid.json'
2018-07-07 09:45:33 +00:00
.replace(/:sub/g, session.token.sub)
.replace(/:kid/g, session.token.iss)
2018-08-04 10:21:06 +00:00
, session: session
}).then(function (resp) {
2018-07-07 09:45:33 +00:00
console.info("Public Key:");
console.log(resp.data);
return oauth3.request({
url: 'https://api.oauth3.org/api/issuer@oauth3.org/acl/profile'
, session: session
}).then(function (resp) {
console.info("Inspect Token:");
console.log(resp.data);
return oauth3.request({
2018-08-04 10:21:06 +00:00
url: 'https://api.' + location.hostname + '/api/telebit.cloud/account'
2018-07-07 09:45:33 +00:00
, session: session
}).then(function (resp) {
console.info("Telebit Account:");
console.log(resp.data);
2018-08-04 10:21:06 +00:00
if (1 === resp.data.accounts.length) {
listStuff(resp);
} else if (0 === resp.data.accounts.length) {
return oauth3.request({
url: 'https://api.' + location.hostname + 'api/telebit.cloud/account'
, method: 'POST'
, session: session
, body: {
email: email
}
}).then(function (resp) {
listStuff(resp);
});
} if (resp.data.accounts.length > 2) {
window.alert("Multiple accounts.");
} else {
window.alert("Bad response.");
}
2018-07-07 09:45:33 +00:00
});
});
2018-08-04 10:21:06 +00:00
});
2018-07-07 09:45:33 +00:00
2018-08-04 10:21:06 +00:00
}, function (err) {
console.error('Authentication Failed:');
console.log(err);
});
}
2018-07-07 09:45:33 +00:00
$('body form.js-auth-form').addEventListener('submit', onClickLogin);
onChangeProvider('oauth3.org');
}());
</script>
</body>
</html>