Files
cloudron-box/webadmin/oauth2/index.html
T
Johannes Zellner f74f05b9af Make OAuth2 example work
The test page is inside the webadmin for now.
https://localhost/oauth2/
An account needs to be created first.
2014-04-13 20:46:58 -07:00

98 lines
2.7 KiB
HTML

<html>
<head>
<title> Accounts </title>
<link href="../css/index.css" rel="stylesheet" media="screen">
<style>
iframe {
width: 400px;
height: 600px;
}
</style>
<script src="javascripts/superagent.js"></script>
<script>
var frame, startBtn, tokenBtn, authCode, oauthResult, accessToken;
var isReady = false;
var server = 'https://localhost';
var clientId = 'cid-webadmin';
var clientSecret = 'unused';
function setAuthCode(code) {
if (!isReady) return;
console.log('---- setAuthCode()', code);
if (code) {
tokenBtn.style.display = 'block';
authCode = code;
oauthResult.innerHTML = 'Auth Code ' + authCode;
} else {
oauthResult.innerHTML = 'Access Denied';
}
}
function init() {
startBtn = document.getElementById('startButton');
tokenBtn = document.getElementById('tokenButton');
frame = document.getElementById('frame');
oauthResult = document.getElementById('oauthResult');
startBtn.onclick = function () {
frame.src = server + '/api/v1/oauth/dialog/authorize?response_type=code&client_id=' + clientId + '&redirect_uri=' + server + '/oauth2/oauth_callback.html';
frame.style.display = 'block';
};
// Rough example OAuth flow initiation for only client side bearer token (aka accessToken)
// startBtn.onclick = function () {
// frame.src = server + '/api/v1/oauth/dialog/authorize?response_type=token&client_id=' + clientId + '&redirect_uri=' + server + '/oauth2/oauth_callback.html';
// frame.style.display = 'block';
// };
tokenBtn.onclick = function () {
superagent.post(server + '/api/v1/oauth/token?response_type=token&client_id=' + clientId)
.send({
grant_type: 'authorization_code',
code: authCode,
redirect_uri: server + '/oauth2/oauth_callback.html',
client_id: clientId,
client_secret: clientSecret
})
.end(function (error, result) {
if (error) {
alert('Exchange failed, see console.');
console.error(error);
return;
}
tokenBtn.style.display = 'none';
accessToken = result.body.access_token
oauthResult.innerHTML = 'Access Token ' + accessToken;
console.log(result.body);
});
};
isReady = true;
}
</script>
</head>
<body onload="init()">
OAuth based login: <button id="startButton">Go for it</button>
<small>Development owner credentials: admin:test</small>
<div id="oauthResult"></div>
<button id="tokenButton" style="display: none"/>Exchange code for token</button>
<iframe id="frame" style="display: none" src="about:blank"></iframe>
</body>
</html>