f1ffef349ea8a2b5cc3b27ea9576a33fab88f969
[prosody.git] / plugins / mod_legacyauth.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11 local st = require "util.stanza";
12 local t_concat = table.concat;
13
14 local config = require "core.configmanager";
15 local secure_auth_only = config.get(module:get_host(), "core", "require_encryption");
16
17 local sessionmanager = require "core.sessionmanager";
18 local usermanager = require "core.usermanager";
19
20 module:add_feature("jabber:iq:auth");
21 module:add_event_hook("stream-features", function (session, features)
22         if not session.username then features:tag("auth", {xmlns='http://jabber.org/features/iq-auth'}):up(); end
23 end);
24
25 module:add_iq_handler("c2s_unauthed", "jabber:iq:auth", 
26                 function (session, stanza)
27                         if secure_auth_only and not session.secure then
28                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "Encryption (SSL or TLS) is required to connect to this server"));
29                                 return true;
30                         end
31                         
32                         local username = stanza.tags[1]:child_with_name("username");
33                         local password = stanza.tags[1]:child_with_name("password");
34                         local resource = stanza.tags[1]:child_with_name("resource");
35                         if not (username and password and resource) then
36                                 local reply = st.reply(stanza);
37                                 session.send(reply:query("jabber:iq:auth")
38                                         :tag("username"):up()
39                                         :tag("password"):up()
40                                         :tag("resource"):up());
41                                 return true;                    
42                         else
43                                 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
44                                 local reply = st.reply(stanza);
45                                 require "core.usermanager"
46                                 if usermanager.validate_credentials(session.host, username, password) then
47                                         -- Authentication successful!
48                                         local success, err = sessionmanager.make_authenticated(session, username);
49                                         if success then
50                                                 local err_type, err_msg;
51                                                 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
52                                                 if not success then
53                                                         session.send(st.error_reply(stanza, err_type, err, err_msg));
54                                                         return true;
55                                                 end
56                                         end
57                                         session.send(st.reply(stanza));
58                                         return true;
59                                 else
60                                         local reply = st.reply(stanza);
61                                         reply.attr.type = "error";
62                                         reply:tag("error", { code = "401", type = "auth" })
63                                                 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
64                                         session.send(reply);
65                                         return true;
66                                 end
67                         end
68                         
69                 end);