mod_muc_webchat_url: Fix default url
[prosody-modules.git] / mod_sasl_oauthbearer / mod_sasl_oauthbearer.lua
blob8fdabc9a8c631d1677073a0a94a131a14260ed1b
1 local s_match = string.match;
2 local registerMechanism = require "util.sasl".registerMechanism;
3 local saslprep = require "util.encodings".stringprep.saslprep;
4 local nodeprep = require "util.encodings".stringprep.nodeprep;
5 local log = require "util.logger".init("sasl");
6 local _ENV = nil;
9 local function oauthbearer(self, message)
10 if not message then
11 return "failure", "malformed-request";
12 end
14 local authorization, password = s_match(message, "^n,a=([^,]*),\1auth=Bearer ([^\1]+)");
15 if not authorization then
16 return "failure", "malformed-request";
17 end
19 local authentication = s_match(authorization, "(.-)@.*");
21 -- SASLprep password and authentication
22 authentication = saslprep(authentication);
23 password = saslprep(password);
25 if (not password) or (password == "") or (not authentication) or (authentication == "") then
26 log("debug", "Username or password violates SASLprep.");
27 return "failure", "malformed-request", "Invalid username or password.";
28 end
30 local _nodeprep = self.profile.nodeprep;
31 if _nodeprep ~= false then
32 authentication = (_nodeprep or nodeprep)(authentication);
33 if not authentication or authentication == "" then
34 return "failure", "malformed-request", "Invalid username or password."
35 end
36 end
38 local correct, state = false, false;
39 correct, state = self.profile.oauthbearer(self, authentication, password, self.realm);
41 self.username = authentication
42 if state == false then
43 return "failure", "account-disabled";
44 elseif state == nil or not correct then
45 return "failure", "not-authorized", "Unable to authorize you with the authentication credentials you've sent.";
46 end
47 return "success";
48 end
50 registerMechanism("OAUTHBEARER", {"oauthbearer"}, oauthbearer);