mod_ping: Convert from Windows line endings
[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 secure_auth_only and not session.secure then
23                 -- Sorry, not offering to insecure streams!
24                 return;
25         elseif not session.username then
26                 features:tag("auth", {xmlns='http://jabber.org/features/iq-auth'}):up();
27         end
28 end);
29
30 module:add_iq_handler("c2s_unauthed", "jabber:iq:auth", 
31                 function (session, stanza)
32                         if secure_auth_only and not session.secure then
33                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "Encryption (SSL or TLS) is required to connect to this server"));
34                                 return true;
35                         end
36                         
37                         local username = stanza.tags[1]:child_with_name("username");
38                         local password = stanza.tags[1]:child_with_name("password");
39                         local resource = stanza.tags[1]:child_with_name("resource");
40                         if not (username and password and resource) then
41                                 local reply = st.reply(stanza);
42                                 session.send(reply:query("jabber:iq:auth")
43                                         :tag("username"):up()
44                                         :tag("password"):up()
45                                         :tag("resource"):up());
46                                 return true;                    
47                         else
48                                 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
49                                 local reply = st.reply(stanza);
50                                 require "core.usermanager"
51                                 if usermanager.validate_credentials(session.host, username, password) then
52                                         -- Authentication successful!
53                                         local success, err = sessionmanager.make_authenticated(session, username);
54                                         if success then
55                                                 local err_type, err_msg;
56                                                 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
57                                                 if not success then
58                                                         session.send(st.error_reply(stanza, err_type, err, err_msg));
59                                                         return true;
60                                                 end
61                                         end
62                                         session.send(st.reply(stanza));
63                                         return true;
64                                 else
65                                         local reply = st.reply(stanza);
66                                         reply.attr.type = "error";
67                                         reply:tag("error", { code = "401", type = "auth" })
68                                                 :tag("not-authorized", { xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas" });
69                                         session.send(reply);
70                                         return true;
71                                 end
72                         end
73                         
74                 end);