Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
136 lines (112 sloc) 4.65 KB
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
</head>
<body>
<h1>WAMP Challenge-Response Authentication</h1>
<p>Open JavaScript console to watch output.</p>
<script>AUTOBAHN_DEBUG = false;</script>
<script src="/shared/autobahn/autobahn.min.js"></script>
<script>
console.log("Ok, AutobahnJS loaded", autobahn.version);
if (false) {
// authenticate using authid "joe"
var user = "joe";
var key = "secret2";
} else {
// authenticate using authid "peter", and using a salted password
var user = "peter";
var key = autobahn.auth_cra.derive_key("secret1", "salt123", 100, 16);
}
// this callback is fired during WAMP-CRA authentication
//
function onchallenge (session, method, extra) {
console.log("onchallenge", method, extra);
if (method === "wampcra") {
console.log("authenticating via '" + method + "' and challenge '" + extra.challenge + "'");
return autobahn.auth_cra.sign(key, extra.challenge);
} else {
throw "don't know how to authenticate using '" + method + "'";
}
}
var connection = new autobahn.Connection({
url: 'ws://127.0.0.1:8080/ws',
realm: 'realm1',
// the following attributes must be set of WAMP-CRA authentication
//
authmethods: ["wampcra"],
authid: user,
onchallenge: onchallenge
});
connection.onopen = function (session, details) {
console.log("connected session with ID " + session.id);
console.log("authenticated using method '" + details.authmethod + "' and provider '" + details.authprovider + "'");
console.log("authenticated with authid '" + details.authid + "' and authrole '" + details.authrole + "'");
// call a procedure we are allowed to call (so this should succeed)
//
session.call('com.example.add2', [2, 3]).then(
function (res) {
console.log("call result", res);
},
function (error) {
console.log("call error", error);
}
);
// (try to) register a procedure where we are not allowed to (so this should fail)
//
session.register('com.example.mul2', function (args, kwargs) {})
.then(
function () {
console.log("huh, function registered!");
},
function (err) {
console.log("registration failed - this is expected", err);
}
);
// publish to some topics we are allowed to publish to.
//
var allowed_topics = [
'com.example.topic1',
'com.foobar.topic1'
];
for (var i = 0; i < allowed_topics.length; ++i) {
(function (j) {
session.publish(allowed_topics[j], ['hello'], null, {acknowledge: true})
.then(
function (pub) {
console.log("event published to topic", allowed_topics[j]);
},
function (err) {
console.log("publication to topic " + allowed_topics[j] + " failed", err);
}
);
})(i);
}
// (try to) publish to some topics we are not allowed to publish to (so this should fail)
//
var disallowed_topics = [
'com.example.topic2',
'com.foobar.topic2'
];
for (var i = 0; i < disallowed_topics.length; ++i) {
(function (j) {
session.publish(disallowed_topics[j], ['hello'], null, {acknowledge: true})
.then(
function (pub) {
console.log("event published to topic", disallowed_topics[j]);
},
function (err) {
console.log("publication to topic " + disallowed_topics[j] + " failed - this is expected", err);
}
);
})(i);
}
};
connection.onclose = function (reason, details) {
console.log("disconnected", reason, details.reason, details);
}
connection.open();
</script>
</body>
</html>