Getting PLAIN mechanism work with the new API.
[prosody.git] / util / sasl.lua
1 -- sasl.lua v0.4
2 -- Copyright (C) 2008-2009 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.
11 --
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.
13
14
15 local md5 = require "util.hashes".md5;
16 local log = require "util.logger".init("sasl");
17 local tostring = tostring;
18 local st = require "util.stanza";
19 local generate_uuid = require "util.uuid".generate;
20 local pairs, ipairs = pairs, ipairs;
21 local t_insert, t_concat = table.insert, table.concat;
22 local to_byte, to_char = string.byte, string.char;
23 local to_unicode = require "util.encodings".idna.to_unicode;
24 local s_match = string.match;
25 local gmatch = string.gmatch
26 local string = string
27 local math = require "math"
28 local type = type
29 local error = error
30 local print = print
31 local setmetatable = setmetatable;
32 local assert = assert;
33 local dofile = dofile;
34 local require = require;
35
36 require "util.iterators"
37 local keys = keys
38
39 local array = require "util.array"
40 module "sasl"
41
42 --[[
43 Authentication Backend Prototypes:
44
45 plain:
46         function(username, realm)
47                 return password, state;
48         end
49
50 plain-test:
51         function(username, realm, password)
52                 return true or false, state;
53         end
54
55 digest-md5:
56         function(username, realm, encoding)
57                 return digesthash, state;
58         end
59
60 digest-md5-test:
61         function(username, realm, encoding, digesthash)
62                 return true or false, state;
63         end
64 ]]
65
66 local method = {};
67 method.__index = method;
68 local mechanisms = {};
69 local backend_mechanism = {};
70
71 -- register a new SASL mechanims
72 local function registerMechanism(name, backends, f)
73         assert(type(name) == "string", "Parameter name MUST be a string.");
74         assert(type(backends) == "string" or type(backends) == "table", "Parameter backends MUST be either a string or a table.");
75         assert(type(f) == "function", "Parameter f MUST be a function.");
76         mechanisms[name] = f
77         for _, backend_name in ipairs(backends) do
78                 if backend_mechanism[backend_name] == nil then backend_mechanism[backend_name] = {}; end
79                 t_insert(backend_mechanism[backend_name], name);
80         end
81 end
82
83 -- create a new SASL object which can be used to authenticate clients
84 function new(realm, profile)
85         sasl_i = {profile = profile};
86         sasl_i.realm = realm;
87         return setmetatable(sasl_i, method);
88 end
89
90 -- get a list of possible SASL mechanims to use
91 function method:mechanisms()
92         local mechanisms = {}
93         for backend, f in pairs(self.profile) do
94                 print(backend)
95                 if backend_mechanism[backend] then
96                         for _, mechanism in ipairs(backend_mechanism[backend]) do
97                                 mechanisms[mechanism] = true;
98                         end
99                 end
100         end
101         self["possible_mechanisms"] = mechanisms;
102         return array.collect(keys(mechanisms));
103 end
104
105 -- select a mechanism to use
106 function method:select(mechanism)
107         if self.mech_i then
108                 return false;
109         end
110         
111         self.mech_i = mechanisms[mechanism]
112         if self.mech_i == nil then 
113                 return false;
114         end
115         return true;
116 end
117
118 -- feed new messages to process into the library
119 function method:process(message)
120         if message == "" or message == nil then return "failure", "malformed-request" end
121         return self.mech_i(self, message);
122 end
123
124 -- load the mechanisms
125 m = require "util.sasl.plain"
126 m.init(registerMechanism)
127 --dofile "util/sasl/digest-md5.lua"
128 --dofile "util/sasl/scram.lua"
129
130 return _M;