-
Notifications
You must be signed in to change notification settings - Fork 152
/
index.html
141 lines (117 loc) · 4.96 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!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);
var use_unsalted_key = true;
if (use_unsalted_key) {
// authenticate using authid "client1"
var user = "client1";
var key = "secret123";
console.log("using unsalted (plain) secret key:", key);
} else {
// FIXME: fails with "WAMP-CRA client signature is invalid"
// authenticate using authid "client2", and using a salted password
var user = "client2";
var key = autobahn.auth_cra.derive_key("secret123", "salt123", 100, 16);
console.log("derived salted secret key:", key);
}
// 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://localhost:8080/ws',
realm: 'realm1',
// the following attributes must be set of WAMP-CRA authentication
//
// authmethods: ["cookie", "wampcra"],
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>