util/sasl/oauthbearer.lua

1
return function (stream, name)
2
	if name == "OAUTHBEARER" and stream.username then
3
		return function (stream)
4
			local auth = stream.bearer_token and ("Bearer "..stream.bearer_token) or "";
5
			local message, data = coroutine.yield("n,a="..stream.username.."@"..stream.host..",\001auth="..auth.."\001");
6
			if message == "success" then
7
				return true;
8
			elseif message == "challenge" then
9
				stream:event("oauth-failure", {
10
					json = data;
11
				});
12
				-- Note: No code after the yield should generally execute, as "failure"
13
				-- doesn't get passed through to us (it contains no data anyway)
14
				if coroutine.yield("\001") ~= "failure" then
15
					error("Unexpected SASL state: expected failure after challenge");
16
				end
17
				return false;
18
			end
19
		end, stream.bearer_token and 6 or 4; -- Prefer OAUTHBEARER if we have a token, otherwise prefer password if we have one
20
	end
21
end