4d0d7ca126f163947413399d1027a43bfc3f0340
[prosody.git] / plugins / mod_legacyauth.lua
1 -- Prosody IM
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 secure_auth_only = module:get_option("require_encryption");
15
16 local sessionmanager = require "core.sessionmanager";
17 local usermanager = require "core.usermanager";
18 local nodeprep = require "util.encodings".stringprep.nodeprep;
19 local resourceprep = require "util.encodings".stringprep.resourceprep;
20
21 module:add_feature("jabber:iq:auth");
22 module:add_event_hook("stream-features", function (session, features)
23         if secure_auth_only and not session.secure then
24                 -- Sorry, not offering to insecure streams!
25                 return;
26         elseif not session.username then
27                 features:tag("auth", {xmlns='http://jabber.org/features/iq-auth'}):up();
28         end
29 end);
30
31 module:add_iq_handler("c2s_unauthed", "jabber:iq:auth", 
32                 function (session, stanza)
33                         if secure_auth_only and not session.secure then
34                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "Encryption (SSL or TLS) is required to connect to this server"));
35                                 return true;
36                         end
37                         
38                         local username = stanza.tags[1]:child_with_name("username");
39                         local password = stanza.tags[1]:child_with_name("password");
40                         local resource = stanza.tags[1]:child_with_name("resource");
41                         if not (username and password and resource) then
42                                 local reply = st.reply(stanza);
43                                 session.send(reply:query("jabber:iq:auth")
44                                         :tag("username"):up()
45                                         :tag("password"):up()
46                                         :tag("resource"):up());
47                         else
48                                 username, password, resource = t_concat(username), t_concat(password), t_concat(resource);
49                                 username = nodeprep(username);
50                                 resource = resourceprep(resource)
51                                 local reply = st.reply(stanza);
52                                 if usermanager.validate_credentials(session.host, username, password) then
53                                         -- Authentication successful!
54                                         local success, err = sessionmanager.make_authenticated(session, username);
55                                         if success then
56                                                 local err_type, err_msg;
57                                                 success, err_type, err, err_msg = sessionmanager.bind_resource(session, resource);
58                                                 if not success then
59                                                         session.send(st.error_reply(stanza, err_type, err, err_msg));
60                                                         session.username, session.type = nil, "c2s_unauthed"; -- FIXME should this be placed in sessionmanager?
61                                                         return true;
62                                                 elseif resource ~= session.resource then -- server changed resource, not supported by legacy auth
63                                                         session.send(st.error_reply(stanza, "cancel", "conflict", "The requested resource could not be assigned to this session."));
64                                                         session:close(); -- FIXME undo resource bind and auth instead of closing the session?
65                                                         return true;
66                                                 end
67                                         end
68                                         session.send(st.reply(stanza));
69                                 else
70                                         session.send(st.error_reply(stanza, "auth", "not-authorized"));
71                                 end
72                         end
73                         return true;
74                 end);