Comparison

util/sasl.lua @ 2178:28d841403a21 sasl

Adjust SASL PLAIN mechanism to the new API.
author Tobias Markmann <tm@ayena.de>
date Thu, 27 Aug 2009 21:29:36 +0200
parent 2177:8505e1da5408
child 2179:c985536d5452
comparison
equal deleted inserted replaced
2177:8505e1da5408 2178:28d841403a21
99 return array.collect(keys(mechanisms)); 99 return array.collect(keys(mechanisms));
100 end 100 end
101 101
102 -- select a mechanism to use 102 -- select a mechanism to use
103 function method:select(mechanism) 103 function method:select(mechanism)
104 104 self.mech_i = mechanisms[mechanism]
105 if self.mech_i == nil then return false; end
106 return true;
105 end 107 end
106 108
107 -- feed new messages to process into the library 109 -- feed new messages to process into the library
108 function method:process(message) 110 function method:process(message)
109 111 if message == "" or message == nil then return "failure", "malformed-request" end
112 return self.mech_i(self, message);
110 end 113 end
111 114
112 --========================= 115 --=========================
113 --SASL PLAIN 116 --SASL PLAIN
114 local function sasl_mechanism_plain(realm, credentials_handler) 117 local function sasl_mechanism_plain(self, message)
115 local object = { mechanism = "PLAIN", realm = realm, credentials_handler = credentials_handler} 118 local response = message
116 function object.feed(self, message) 119 local authorization = s_match(response, "([^&%z]+)")
117 if message == "" or message == nil then return "failure", "malformed-request" end 120 local authentication = s_match(response, "%z([^&%z]+)%z")
118 local response = message 121 local password = s_match(response, "%z[^&%z]+%z([^&%z]+)")
119 local authorization = s_match(response, "([^&%z]+)")
120 local authentication = s_match(response, "%z([^&%z]+)%z")
121 local password = s_match(response, "%z[^&%z]+%z([^&%z]+)")
122 122
123 if authentication == nil or password == nil then return "failure", "malformed-request" end 123 if authentication == nil or password == nil then return "failure", "malformed-request" end
124 self.username = authentication
125 local auth_success = self.credentials_handler("PLAIN", self.username, self.realm, password)
126 124
127 if auth_success then 125 local correct, state = false, false, false;
128 return "success" 126 if self.profile.plain then
129 elseif auth_success == nil then 127 local correct_password, state = self.profile.plain(authentication, self.realm);
130 return "failure", "account-disabled" 128 if correct_password == password then correct = true; else correct = false; end
131 else 129 else if self.profile.plain_test then
132 return "failure", "not-authorized" 130 correct, state = self.profile.plain_test(authentication, self.realm, password);
133 end
134 end 131 end
135 return object 132
133 self.username = authentication
134 if not state then
135 return "failure", "account-disabled";
136 end
137
138 if correct then
139 return "success";
140 else
141 return "failure", "not-authorized";
142 end
136 end 143 end
137 registerMechanism("PLAIN", {"plain", "plain_test"}, sasl_mechanism_plain); 144 registerMechanism("PLAIN", {"plain", "plain_test"}, sasl_mechanism_plain);
138 145
139 return _M; 146 return _M;