Mercurial > prosody-modules
annotate mod_auth_wordpress/mod_auth_wordpress.lua @ 6299:5cf5ee23b361
mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs.
diff --git a/mod_voipms/README.md b/mod_voipms/README.md
new file mode 100644
--- /dev/null
+++ b/mod_voipms/README.md
@@ -0,0 +1,51 @@
+---
+labels:
+- 'Stage-Alpha'
+- 'Type-Web'
+summary: Send and receive SMS/MMS via VoIP.ms APIs.
+rockspec:
+ build:
+ modules:
+ mod_voipms: mod_voipms.lua
+...
+
+Introduction
+============
+
+This is a Prosody module to map JIDs to DIDs on VoIP.ms and support sending/receiving SMS/MMS.
+
+Configuration
+=============
+
+| option | type | default |
+|-----------------------|--------|---------|
+| voipms\_api\_username | string | nil |
+| voipms\_api\_password | string | nil |
+| voipms\_query\_key | string | nil |
+| voipms\_jid\_map | table | nil |
+```
+VirtualHost "sms.example.com"
+modules_enabled = {
+ "voipms";
+}
+voipms_api_username = john@example.com -- E-mail registered with VoIP.ms
+voipms_api_password = abcd1234 -- API password configured in VoIP.ms
+voipms_query_key = some_query_key -- query param 'key' part of your URL callback
+voipms_jid_map = {
+ ["your_jid@your_domain.com"] = "+1234567890"
+}
+```
+
+HTTP
+====
+
+The module is served on Prosody's default HTTP ports at the path /voipms. More details on configuring HTTP modules in Prosody can be found in the HTTP documentation.
+
+VoIP.ms Webhook URL
+===================
+
+This module receives the VoIP.ms Webhook URL (POST) at the /voipms endpoint. It uses the sendSMS/sendMMS GET methods against the VoIP.ms APIs. This is an example webhook to use in VoIP.ms:
+
+```
+https://sms.example.com/voipms?key=some_query_key
+```
diff --git a/mod_voipms/mod_voipms.lua b/mod_voipms/mod_voipms.lua
new file mode 100644
--- /dev/null
+++ b/mod_voipms/mod_voipms.lua
@@ -0,0 +1,166 @@
+local http = require "net.http"
+local json = require "util.json"
+local st = require "util.stanza"
+
+local api_username = module:get_option_string("voipms_api_username")
+local api_password = module:get_option_string("voipms_api_password")
+local query_key = module:get_option("voipms_query_key")
+local jid_map = module:get_option("voipms_jid_map") or {}
+local rest_endpoint = "https://voip.ms/api/v1/rest.php"
+
+if not api_username or not api_password or not query_key then
+ module:log("error", "Missing required config values (voipms_api_username, voipms_api_password, voipms_query_key)")
+ return
+end
+
+module:depends("http")
+
+local function normalize_number(num)
+ if not num then return nil end
+ if num:sub(1, 1) ~= "+" then
+ return "+1" .. num
+ end
+ return num
+end
+
+local function extract_query_key(event)
+ return (event.request.url.query or ""):match("key=([^&]+)")
+end
+
+module:provides("http", {
+ route = {
+ ["POST"] = function(event)
+ local req = event.request
+ local body = req.body or ""
+
+ if extract_query_key(event) ~= query_key then
+ module:log("warn", "Unauthorized webhook: missing or invalid key")
+ return { status_code = 403 }
+ end
+
+ local json_payload, err = json.decode(body)
+ if not json_payload then
+ module:log("warn", "Invalid JSON: %s", err or "unknown error")
+ return { status_code = 400 }
+ end
+
+ local payload = json_payload.data and json_payload.data.payload
+ if not payload then
+ module:log("warn", "Missing payload in JSON")
+ return { status_code = 400 }
+ end
+
+ local from = payload.from and payload.from.phone_number
+ local to_list = payload.to or {}
+ local to = #to_list > 0 and to_list[1].phone_number or nil
+
+ if not from or not to then
+ module:log("warn", "Missing phone numbers (from: %s, to: %s)", tostring(from), tostring(to))
+ return { status_code = 400 }
+ end
+
+ local normalized_to = normalize_number(to)
+ local target_jid = nil
+
+ for jid, did in pairs(jid_map) do
+ if normalize_number(did) == normalized_to then
+ target_jid = jid
+ break
+ end
+ end
+
+ if not target_jid then
+ module:log("warn", "No JID mapping for DID %s", normalized_to)
+ return { status_code = 404 }
+ end
+
+ local normalized_from = normalize_number(from)
+ local message_text = payload.text or ""
+
+ local message = st.message({
+ from = normalized_from .. "@" .. module.host,
+ to = target_jid,
+ type = "chat"
+ }):tag("body"):text(message_text):up()
+
+ if payload.media and #payload.media > 0 then
+ for _, media_item in ipairs(payload.media) do
+ if media_item.url then
+ message_text = message_text .. "\n" .. media_item.url
+ message:tag("x", { xmlns = "jabber:x:oob" })
+ :tag("url"):text(media_item.url):up():up()
+ end
+ end
+ end
+
+ module:send(message)
+ module:log("info", "Delivered SMS from %s to %s", normalized_from, target_jid)
+
+ return { status_code = 204 }
+ end
+ }
+})
+
+module:hook("message/bare", function(event)
+ local stanza = event.stanza
+ if stanza.attr.type ~= "chat" then return end
+
+ local from_jid = stanza.attr.from
+ local to_jid = stanza.attr.to
+ local body = stanza:get_child_text("body")
+ if not body or body == "" then return end
+
+ local from_number = jid_map[from_jid]
+ if not from_number then
+ module:log("warn", "No DID mapping for JID %s", from_jid)
+ return
+ end
+
+ local to_number = to_jid:match("^([^@]+)")
+ if not to_number then
+ module:log("warn", "Malformed JID in message.to: %s", to_jid)
+ return
+ end
+
+ to_number = normalize_number(to_number)
+
+ local media_urls = {}
+ for line in body:gmatch("[^\r\n]+") do
+ if line:match("^https?://") then
+ table.insert(media_urls, line)
+ end
+ end
+
+ local method = (#media_urls > 0) and "sendMMS" or "sendSMS"
+ local query = {
+ api_username = api_username,
+ api_password = api_password,
+ method = method,
+ did = from_number,
+ dst = to_number,
+ message = body
+ }
+
+ if method == "sendMMS" then
+ for i, url in ipairs(media_urls) do
+ query["media_url[" .. (i - 1) .. "]"] = url
+ end
+ end
+
+ local query_str = http.formencode(query)
+
+ http.request(rest_endpoint .. "?" .. query_str, {
+ method = "GET";
+ }, function(response_body, code)
+ if code == 200 then
+ local resp, err = json.decode(response_body)
+ if not resp or resp.status ~= "success" then
+ module:log("error", "Failed to send %s: %s", method, err or (resp and resp.status) or "unknown")
+ else
+ module:log("info", "Sent %s from %s to %s", method, from_number, to_number)
+ end
+ else
+ module:log("error", "HTTP error sending %s: code %s", method, tostring(code))
+ end
+ end)
+end)
| author | Chaz <menel@snikket.de> |
|---|---|
| date | Mon, 21 Jul 2025 23:57:37 +0200 |
| parents | 7dbde05b48a9 |
| children |
| rev | line source |
|---|---|
|
423
524dda2ecb6a
mod_auth_wordpress: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
421
diff
changeset
|
1 -- Wordpress authentication backend for Prosody |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 -- |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2011 Waqas Hussain |
|
428
a46c2326eed7
mod_auth_wordpress: Update Copyright
Kim Alvefur <zash@zash.se>
parents:
427
diff
changeset
|
4 -- Copyright (C) 2011 Kim Alvefur |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 -- |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 local log = require "util.logger".init("auth_sql"); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 local new_sasl = require "util.sasl".new; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 local nodeprep = require "util.encodings".stringprep.nodeprep; |
|
419
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
10 local saslprep = require "util.encodings".stringprep.saslprep; |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local DBI = require "DBI" |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 local md5 = require "util.hashes".md5; |
|
377
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
13 local uuid_gen = require "util.uuid".generate; |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 local connection; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 local params = module:get_option("sql"); |
|
426
6f2e37d0a1b0
mod_auth_wordpress: Allow table prefix to be configured
Kim Alvefur <zash@zash.se>
parents:
425
diff
changeset
|
17 local table_prefix = module:get_option_string("wordpress_table_prefix", "wp_"); |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 local resolve_relative_path = require "core.configmanager".resolve_relative_path; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 local function test_connection() |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 if not connection then return nil; end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 if connection:ping() then |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 return true; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 else |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 module:log("debug", "Database connection closed"); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 connection = nil; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 local function connect() |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 if not test_connection() then |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 prosody.unlock_globals(); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 local dbh, err = DBI.Connect( |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 params.driver, params.database, |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 params.username, params.password, |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 params.host, params.port |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 ); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 prosody.lock_globals(); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 if not dbh then |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 module:log("debug", "Database connection failed: %s", tostring(err)); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 return nil, err; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 module:log("debug", "Successfully connected to database"); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 dbh:autocommit(true); -- don't run in transaction |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 connection = dbh; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 return connection; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 do -- process options to get a db connection |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 params = params or { driver = "SQLite3" }; |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
814
diff
changeset
|
52 |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 if params.driver == "SQLite3" then |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 end |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
814
diff
changeset
|
56 |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified"); |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
814
diff
changeset
|
58 |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 assert(connect()); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 local function getsql(sql, ...) |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 if params.driver == "PostgreSQL" then |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 sql = sql:gsub("`", "\""); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 if not test_connection() then connect(); end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 -- do prepared statement stuff |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 local stmt, err = connection:prepare(sql); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 if not stmt and not test_connection() then error("connection failed"); end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 -- run query |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 local ok, err = stmt:execute(...); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 if not ok and not test_connection() then error("connection failed"); end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 if not ok then return nil, err; end |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
814
diff
changeset
|
75 |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 return stmt; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 end |
|
377
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
78 local function setsql(sql, ...) |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
79 local stmt, err = getsql(sql, ...); |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
80 if not stmt then return stmt, err; end |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
81 return stmt:affected(); |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
82 end |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 local function get_password(username) |
|
426
6f2e37d0a1b0
mod_auth_wordpress: Allow table prefix to be configured
Kim Alvefur <zash@zash.se>
parents:
425
diff
changeset
|
85 local stmt, err = getsql("SELECT `user_pass` FROM `"..table_prefix.."users` WHERE `user_login`=?", username); |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 if stmt then |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 for row in stmt:rows(true) do |
|
427
35b3ea156156
mod_auth_wordpress: Fix wrong column name
Kim Alvefur <zash@zash.se>
parents:
426
diff
changeset
|
88 return row.user_pass; |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 |
|
421
816d8e3e83a3
mod_auth_phpbb3: A little refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
420
diff
changeset
|
93 |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 local itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
|
377
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
95 local function hashEncode64(input, count) |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 local output = ""; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 local i, value = 0, 0; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 while true do |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 value = input:byte(i+1) |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 i = i+1; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 local idx = value % 0x40 + 1; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 output = output .. itoa64:sub(idx, idx); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 if i < count then |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 value = value + input:byte(i+1) * 256; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 local _ = value % (2^6); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 local idx = ((value - _) / (2^6)) % 0x40 + 1 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 output = output .. itoa64:sub(idx, idx); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 if i >= count then break; end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 i = i+1; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 if i < count then |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 value = value + input:byte(i+1) * 256 * 256; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 local _ = value % (2^12); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 local idx = ((value - _) / (2^12)) % 0x40 + 1 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 output = output .. itoa64:sub(idx, idx); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 if i >= count then break; end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 i = i+1; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 local _ = value % (2^18); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 local idx = ((value - _) / (2^18)) % 0x40 + 1 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 output = output .. itoa64:sub(idx, idx); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 if not(i < count) then break; end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 return output; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 end |
|
377
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
133 local function hashCryptPrivate(password, genSalt) |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 local output = "*"; |
|
423
524dda2ecb6a
mod_auth_wordpress: Initial commit.
Kim Alvefur <zash@zash.se>
parents:
421
diff
changeset
|
135 if not genSalt:match("^%$P%$") then return output; end |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 local count_log2 = itoa64:find(genSalt:sub(4,4)) - 1; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 if count_log2 < 7 or count_log2 > 30 then return output; end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 local count = 2 ^ count_log2; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 local salt = genSalt:sub(5, 12); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 if #salt ~= 8 then return output; end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 local hash = md5(salt..password); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 while true do |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 hash = md5(hash..password); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 if not(count > 1) then break; end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 count = count-1; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 output = genSalt:sub(1, 12); |
|
377
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
154 output = output .. hashEncode64(hash, 16); |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 return output; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 end |
|
377
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
158 local function hashGensaltPrivate(input) |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
159 local iteration_count_log2 = 6; |
|
425
34eb9df9e37f
mod_auth_wordpress: Fix hash identifier.
Kim Alvefur <zash@zash.se>
parents:
424
diff
changeset
|
160 local output = "$P$"; |
|
377
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
161 local idx = math.min(iteration_count_log2 + 5, 30) + 1; |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
162 output = output .. itoa64:sub(idx, idx); |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
163 output = output .. hashEncode64(input, 6); |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
164 return output; |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
165 end |
|
424
22935ef37284
mod_auth_wordpress: Rename functions to match module name
Kim Alvefur <zash@zash.se>
parents:
423
diff
changeset
|
166 local function wordpressCheckHash(password, hash) |
|
22935ef37284
mod_auth_wordpress: Rename functions to match module name
Kim Alvefur <zash@zash.se>
parents:
423
diff
changeset
|
167 if #hash == 32 then return hash == md5(password, true); end |
|
377
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
168 return #hash == 34 and hashCryptPrivate(password, hash) == hash; |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 end |
|
424
22935ef37284
mod_auth_wordpress: Rename functions to match module name
Kim Alvefur <zash@zash.se>
parents:
423
diff
changeset
|
170 local function wordpressCreateHash(password) |
|
377
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
171 local random = uuid_gen():sub(-6); |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
172 local salt = hashGensaltPrivate(random); |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
173 local hash = hashCryptPrivate(password, salt); |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
174 if #hash == 34 then return hash; end |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
175 return md5(password, true); |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
176 end |
|
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
177 |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
178 |
|
814
881ec9919144
mod_auth_*: Use module:provides(), and don't explicitly specify provider.name.
Waqas Hussain <waqas20@gmail.com>
parents:
428
diff
changeset
|
179 provider = {}; |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 function provider.test_password(username, password) |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 local hash = get_password(username); |
|
424
22935ef37284
mod_auth_wordpress: Rename functions to match module name
Kim Alvefur <zash@zash.se>
parents:
423
diff
changeset
|
183 return hash and wordpressCheckHash(password, hash); |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 function provider.user_exists(username) |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 module:log("debug", "test user %s existence", username); |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 return get_password(username) and true; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 function provider.get_password(username) |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 return nil, "Getting password is not supported."; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 function provider.set_password(username, password) |
|
424
22935ef37284
mod_auth_wordpress: Rename functions to match module name
Kim Alvefur <zash@zash.se>
parents:
423
diff
changeset
|
194 local hash = wordpressCreateHash(password); |
|
426
6f2e37d0a1b0
mod_auth_wordpress: Allow table prefix to be configured
Kim Alvefur <zash@zash.se>
parents:
425
diff
changeset
|
195 local stmt, err = setsql("UPDATE `"..table_prefix.."users` SET `user_pass`=? WHERE `user_login`=?", hash, username); |
|
377
145fa870321c
mod_auth_phpbb3: Implement password change.
Waqas Hussain <waqas20@gmail.com>
parents:
376
diff
changeset
|
196 return stmt and true, err; |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 function provider.create_user(username, password) |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 return nil, "Account creation/modification not supported."; |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 |
|
419
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
202 local escapes = { |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
203 [" "] = "\\20"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
204 ['"'] = "\\22"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
205 ["&"] = "\\26"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
206 ["'"] = "\\27"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
207 ["/"] = "\\2f"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
208 [":"] = "\\3a"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
209 ["<"] = "\\3c"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
210 [">"] = "\\3e"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
211 ["@"] = "\\40"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
212 ["\\"] = "\\5c"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
213 }; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
214 local unescapes = {}; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
215 for k,v in pairs(escapes) do unescapes[v] = k; end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
216 local function jid_escape(s) return s and (s:gsub(".", escapes)); end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
217 local function jid_unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
218 |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
219 function provider.get_sasl_handler() |
|
419
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
220 local sasl = {}; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
221 function sasl:clean_clone() return provider.get_sasl_handler(); end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
222 function sasl:mechanisms() return { PLAIN = true; }; end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
223 function sasl:select(mechanism) |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
224 if not self.selected and mechanism == "PLAIN" then |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
225 self.selected = mechanism; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
226 return true; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
227 end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
228 end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
229 function sasl:process(message) |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
230 if not message then return "failure", "malformed-request"; end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
231 local authorization, authentication, password = message:match("^([^%z]*)%z([^%z]+)%z([^%z]+)"); |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
232 if not authorization then return "failure", "malformed-request"; end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
233 authentication = saslprep(authentication); |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
234 password = saslprep(password); |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
235 if (not password) or (password == "") or (not authentication) or (authentication == "") then |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
236 return "failure", "malformed-request", "Invalid username or password."; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
237 end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
238 local function test(authentication) |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
239 local prepped = nodeprep(authentication); |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
240 local normalized = jid_unescape(prepped); |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
241 return normalized and provider.test_password(normalized, password) and prepped; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
242 end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
243 local username = test(authentication) or test(jid_escape(authentication)); |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
244 if username then |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
245 self.username = username; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
246 return "success"; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
247 end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
248 return "failure", "not-authorized", "Unable to authorize you with the authentication credentials you've sent."; |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
249 end |
|
2a2b70e1a998
mod_auth_phpbb3: Apply stringprep, and try automatic JID escaping to derive username.
Waqas Hussain <waqas20@gmail.com>
parents:
377
diff
changeset
|
250 return sasl; |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
251 end |
|
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
252 |
|
814
881ec9919144
mod_auth_*: Use module:provides(), and don't explicitly specify provider.name.
Waqas Hussain <waqas20@gmail.com>
parents:
428
diff
changeset
|
253 module:provides("auth", provider); |
|
373
81c7b36e6cdd
mod_auth_phpbb3: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
254 |
