Prepare required data folder for integration tests
[prosody.git] / util / sasl.lua
blob508514053977de0bd59adb79bf52c13bbc7751e7
1 -- sasl.lua v0.4
2 -- Copyright (C) 2008-2010 Tobias Markmann
3 --
4 -- All rights reserved.
5 --
6 -- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 --
8 -- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 -- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 -- * Neither the name of Tobias Markmann nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
12 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 local pairs, ipairs = pairs, ipairs;
16 local t_insert = table.insert;
17 local type = type
18 local setmetatable = setmetatable;
19 local assert = assert;
20 local require = require;
22 local _ENV = nil;
23 -- luacheck: std none
25 --[[
26 Authentication Backend Prototypes:
28 state = false : disabled
29 state = true : enabled
30 state = nil : non-existant
32 Channel Binding:
34 To enable support of channel binding in some mechanisms you need to provide appropriate callbacks in a table
35 at profile.cb.
37 Example:
38 profile.cb["tls-unique"] = function(self)
39 return self.user
40 end
44 local method = {};
45 method.__index = method;
46 local registered_mechanisms = {};
47 local backend_mechanism = {};
48 local mechanism_channelbindings = {};
50 -- register a new SASL mechanims
51 local function registerMechanism(name, backends, f, cb_backends)
52 assert(type(name) == "string", "Parameter name MUST be a string.");
53 assert(type(backends) == "string" or type(backends) == "table", "Parameter backends MUST be either a string or a table.");
54 assert(type(f) == "function", "Parameter f MUST be a function.");
55 if cb_backends then assert(type(cb_backends) == "table"); end
56 registered_mechanisms[name] = f
57 if cb_backends then
58 mechanism_channelbindings[name] = {};
59 for _, cb_name in ipairs(cb_backends) do
60 mechanism_channelbindings[name][cb_name] = true;
61 end
62 end
63 for _, backend_name in ipairs(backends) do
64 if backend_mechanism[backend_name] == nil then backend_mechanism[backend_name] = {}; end
65 t_insert(backend_mechanism[backend_name], name);
66 end
67 end
69 -- create a new SASL object which can be used to authenticate clients
70 local function new(realm, profile)
71 local mechanisms = profile.mechanisms;
72 if not mechanisms then
73 mechanisms = {};
74 for backend in pairs(profile) do
75 if backend_mechanism[backend] then
76 for _, mechanism in ipairs(backend_mechanism[backend]) do
77 mechanisms[mechanism] = true;
78 end
79 end
80 end
81 profile.mechanisms = mechanisms;
82 end
83 return setmetatable({ profile = profile, realm = realm, mechs = mechanisms }, method);
84 end
86 -- add a channel binding handler
87 function method:add_cb_handler(name, f)
88 if type(self.profile.cb) ~= "table" then
89 self.profile.cb = {};
90 end
91 self.profile.cb[name] = f;
92 return self;
93 end
95 -- get a fresh clone with the same realm and profile
96 function method:clean_clone()
97 return new(self.realm, self.profile)
98 end
100 -- get a list of possible SASL mechanims to use
101 function method:mechanisms()
102 local current_mechs = {};
103 for mech, _ in pairs(self.mechs) do
104 if mechanism_channelbindings[mech] then
105 if self.profile.cb then
106 local ok = false;
107 for cb_name, _ in pairs(self.profile.cb) do
108 if mechanism_channelbindings[mech][cb_name] then
109 ok = true;
112 if ok == true then current_mechs[mech] = true; end
114 else
115 current_mechs[mech] = true;
118 return current_mechs;
121 -- select a mechanism to use
122 function method:select(mechanism)
123 if not self.selected and self.mechs[mechanism] then
124 self.selected = mechanism;
125 return true;
129 -- feed new messages to process into the library
130 function method:process(message)
131 --if message == "" or message == nil then return "failure", "malformed-request" end
132 return registered_mechanisms[self.selected](self, message);
135 -- load the mechanisms
136 require "util.sasl.plain" .init(registerMechanism);
137 require "util.sasl.digest-md5".init(registerMechanism);
138 require "util.sasl.anonymous" .init(registerMechanism);
139 require "util.sasl.scram" .init(registerMechanism);
140 require "util.sasl.external" .init(registerMechanism);
142 return {
143 registerMechanism = registerMechanism;
144 new = new;