Mercurial > prosody-modules
annotate mod_post_msg/mod_post_msg.lua @ 1165:b8762c9fb270
mod_auth_pam: Initial commit of simple PAM authentication module
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 15 Aug 2013 18:38:02 +0200 |
parents | a6c8f252e5fa |
children | e556219cb43d |
rev | line source |
---|---|
661
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
1 module:depends"http" |
216
ac5289d5ac8c
mod_post_msg: A plugin that recives a single message by HTTP POST and relays it.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
ac5289d5ac8c
mod_post_msg: A plugin that recives a single message by HTTP POST and relays it.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local jid_split = require "util.jid".split; |
ac5289d5ac8c
mod_post_msg: A plugin that recives a single message by HTTP POST and relays it.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local jid_prep = require "util.jid".prep; |
ac5289d5ac8c
mod_post_msg: A plugin that recives a single message by HTTP POST and relays it.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local msg = require "util.stanza".message; |
ac5289d5ac8c
mod_post_msg: A plugin that recives a single message by HTTP POST and relays it.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local test_password = require "core.usermanager".test_password; |
ac5289d5ac8c
mod_post_msg: A plugin that recives a single message by HTTP POST and relays it.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local b64_decode = require "util.encodings".base64.decode; |
661
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
8 local formdecode = require "net.http".formdecode; |
216
ac5289d5ac8c
mod_post_msg: A plugin that recives a single message by HTTP POST and relays it.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
661
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
10 local function require_valid_user(f) |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
11 return function(event, path) |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
12 local request = event.request; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
13 local response = event.response; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
14 local headers = request.headers; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
15 if not headers.authorization then |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
16 response.headers.www_authenticate = ("Basic realm=%q"):format(module.host.."/"..module.name); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
17 return 401 |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
18 end |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
19 local from_jid, password = b64_decode(headers.authorization:match"[^ ]*$"):match"([^:]*):(.*)"; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
20 from_jid = jid_prep(from_jid); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
21 if from_jid and password then |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
22 local user, host = jid_split(from_jid); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
23 local ok, err = test_password(user, host, password); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
24 if ok and user and host then |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
25 module:log("debug", "Authed as %s", from_jid); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
26 return f(event, path, from_jid); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
27 elseif err then |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
28 module:log("debug", "User failed authentication: %s", err); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
29 end |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
30 end |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
31 return 401 |
321
661f64627fed
mod_post_msg: Add compatibility with usermanager in 0.7
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
32 end |
661f64627fed
mod_post_msg: Add compatibility with usermanager in 0.7
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
33 end |
661f64627fed
mod_post_msg: Add compatibility with usermanager in 0.7
Kim Alvefur <zash@zash.se>
parents:
320
diff
changeset
|
34 |
661
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
35 local function handle_post(event, path, authed_user) |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
36 local request = event.request; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
37 local response = event.response; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
38 local headers = request.headers; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
39 |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
40 local body_type = headers.content_type; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
41 local to = jid_prep(path); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
42 local message; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
43 if body_type == "text/plain" then |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
44 if to and request.body then |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
45 message = msg({ to = to, from = authed_user, type = "chat"},request.body); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
46 end |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
47 elseif body_type == "application/x-www-form-urlencoded" then |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
48 local post_body = formdecode(request.body); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
49 message = msg({ to = post_body.to or to, from = authed_user, |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
50 type = post_body.type or "chat"}, post_body.body); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
51 else |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
52 return 415; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
53 end |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
54 if message and message.attr.to then |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
55 module:log("debug", "Sending %s", tostring(message)); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
56 module:send(message); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
57 return 201; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
58 end |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
59 return 422; |
216
ac5289d5ac8c
mod_post_msg: A plugin that recives a single message by HTTP POST and relays it.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 end |
ac5289d5ac8c
mod_post_msg: A plugin that recives a single message by HTTP POST and relays it.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 |
661
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
62 module:provides("http", { |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
63 default_path = "/msg"; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
64 route = { |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
65 ["POST /*"] = require_valid_user(handle_post); |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
66 OPTIONS = function(e) |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
67 local headers = e.response.headers; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
68 headers.allow = "POST"; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
69 headers.accept = "application/x-www-form-urlencoded, text/plain"; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
70 return 200; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
71 end; |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
72 } |
a6c8f252e5fa
mod_post_msg: Update to the new HTTP API
Kim Alvefur <zash@zash.se>
parents:
321
diff
changeset
|
73 }); |