-
Notifications
You must be signed in to change notification settings - Fork 378
/
Copy pathindex.js
168 lines (144 loc) · 4.19 KB
/
index.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { encode, decode } from "@xmpp/base64";
import SASLError from "@xmpp/sasl/lib/SASLError.js";
import xml from "@xmpp/xml";
import { procedure } from "@xmpp/events";
import { getAvailableMechanisms } from "@xmpp/sasl";
// https://xmpp.org/extensions/xep-0388.html
const NS = "urn:xmpp:sasl:2";
async function authenticate({
saslFactory,
entity,
mechanism,
credentials,
userAgent,
streamFeatures,
features,
}) {
const mech = saslFactory.create([mechanism]);
if (!mech) {
throw new Error(`SASL: Mechanism ${mechanism} not found.`);
}
const { domain } = entity.options;
const creds = {
username: null,
password: null,
server: domain,
host: domain,
realm: domain,
serviceType: "xmpp",
serviceName: domain,
...credentials,
};
await procedure(
entity,
xml("authenticate", { xmlns: NS, mechanism: mech.name }, [
mech.clientFirst &&
xml("initial-response", {}, encode(await mech.response(creds))),
userAgent,
...streamFeatures,
]),
async (element, done) => {
if (element.getNS() !== NS) return;
if (element.name === "challenge") {
await mech.challenge(decode(element.text()));
const resp = await mech.response(creds);
await entity.send(
xml(
"response",
{ xmlns: NS, mechanism: mech.name },
typeof resp === "string" ? encode(resp) : "",
),
);
return;
}
if (element.name === "failure") {
throw SASLError.fromElement(element);
}
if (element.name === "continue") {
throw new Error("SASL continue is not supported yet");
}
if (element.name === "success") {
const additionalData = element.getChild("additional-data")?.text();
if (additionalData && mech.final) {
await mech.final(decode(additionalData));
}
// https://xmpp.org/extensions/xep-0388.html#success
// this is a bare JID, unless resource binding or stream resumption has occurred, in which case it is a full JID.
const aid = element.getChildText("authorization-identifier");
if (aid) {
entity._jid(aid);
}
for (const child of element.getChildElements()) {
const feature = features.get(child.getNS());
feature?.[1]?.(child);
}
return done();
}
},
);
}
export default function sasl2({ streamFeatures, saslFactory }, onAuthenticate) {
const features = new Map();
let fast;
streamFeatures.use(
"authentication",
NS,
async ({ entity }, _next, element) => {
const mechanisms = getAvailableMechanisms(element, NS, saslFactory);
const streamFeatures = await getStreamFeatures({ element, features });
const fast_available = !!fast?.mechanism;
if (mechanisms.length === 0 && !fast_available) {
throw new SASLError("SASL: No compatible mechanism available.");
}
await onAuthenticate(
done,
mechanisms,
fast_available ? fast : null,
entity,
);
async function done(credentials, mechanism, userAgent) {
// Try fast
const success = await fast.auth({
authenticate,
entity,
userAgent,
streamFeatures,
features,
credentials,
});
if (success) return;
// fast.auth may mutate streamFeatures to request a token
// If fast authentication fails, continue and try without
await authenticate({
entity,
userAgent,
streamFeatures,
features,
saslFactory,
mechanism,
credentials,
});
}
},
);
return {
use(ns, req, res) {
features.set(ns, [req, res]);
},
setup({ fast: _fast }) {
fast = _fast;
},
};
}
async function getStreamFeatures({ element, features }) {
const promises = [];
const inline = element.getChild("inline");
if (!inline) return promises;
for (const element of inline.getChildElements()) {
const xmlns = element.getNS();
const feature = features.get(xmlns);
if (!feature) continue;
promises.push(feature[0](element));
}
return Promise.all(promises);
}