-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathfast.js
166 lines (147 loc) · 3.7 KB
/
fast.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
import { EventEmitter } from "@xmpp/events";
import { getAvailableMechanisms } from "@xmpp/sasl";
import SASLError from "@xmpp/sasl/lib/SASLError.js";
import xml from "@xmpp/xml";
import SASLFactory from "saslmechanisms";
const NS = "urn:xmpp:fast:0";
export default function fast({ sasl2, entity }) {
const saslFactory = new SASLFactory();
let token;
const fast = new EventEmitter();
Object.assign(fast, {
mechanism: null,
mechanisms: [],
async saveToken(t) {
token = t;
},
async fetchToken() {
return token;
},
async deleteToken() {
token = null;
},
async save(token) {
try {
await this.saveToken(token);
} catch (err) {
entity.emit("error", err);
}
},
async fetch() {
try {
return this.fetchToken();
} catch (err) {
entity.emit("error", err);
}
},
async delete() {
try {
await this.deleteToken();
} catch (err) {
entity.emit("error", err);
}
},
saslFactory,
async auth({
authenticate,
entity,
userAgent,
credentials,
streamFeatures,
features,
}) {
// Unavailable
if (!fast.mechanism) {
return false;
}
const { token } = credentials;
if (!isTokenValid(token, fast.mechanisms)) {
return onInvalidToken();
}
try {
await authenticate({
saslFactory: fast.saslFactory,
mechanism: token.mechanism,
credentials: {
...credentials,
password: token.token,
},
streamFeatures: [
...streamFeatures,
xml("fast", {
xmlns: NS,
}),
],
entity,
userAgent,
features,
});
return true;
} catch (err) {
if (
err instanceof SASLError &&
["not-authorized", "credentials-expired"].includes(err.condition)
) {
return onInvalidToken();
}
entity.emit("error", err);
return false;
}
async function onInvalidToken() {
await fast.delete();
requestToken(streamFeatures);
return false;
}
},
});
function requestToken(streamFeatures) {
streamFeatures.push(
xml("request-token", {
xmlns: NS,
mechanism: fast.mechanism,
}),
);
}
function reset() {
fast.mechanism = null;
fast.mechanisms = [];
}
reset();
sasl2.use(
NS,
async (element) => {
if (!element.is("fast", NS)) return reset();
fast.available = true;
const mechanisms = getAvailableMechanisms(element, NS, saslFactory);
const mechanism = mechanisms[0];
if (!mechanism) return reset();
fast.mechanisms = mechanisms;
fast.mechanism = mechanism;
// The rest is handled by @xmpp/sasl2
},
async (element) => {
if (element.is("token", NS)) {
await fast.save({
// The token is bound by the mechanism
// > Servers MUST bind tokens to the mechanism selected by the client in its original request, and reject attempts to use them with other mechanisms.
mechanism: fast.mechanism,
token: element.attrs.token,
expiry: element.attrs.expiry,
});
}
},
);
return fast;
}
export function isTokenValid(token, mechanisms) {
if (!token) return false;
// Avoid an error round trip if the server does not support the token mechanism anymore
if (!mechanisms.includes(token.mechanism)) {
return false;
}
// Avoid an error round trip if the token is already expired
if (new Date(token.expiry) <= new Date()) {
return false;
}
return true;
}