Mercurial > prosody-modules
annotate mod_auth_http_async/mod_auth_http_async.lua @ 2629:a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
author | JC Brand <jc@opkode.com> |
---|---|
date | Tue, 21 Mar 2017 09:14:03 +0000 |
parents | b2a198665946 |
children | 96eb1c4f9ff7 |
rev | line source |
---|---|
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- Prosody IM |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2013 Matthew Wild |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2013 Waqas Hussain |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- Copyright (C) 2014 Kim Alvefur |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 -- This project is MIT/X11 licensed. Please see the |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 -- COPYING file in the source package for more information. |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 -- |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local new_sasl = require "util.sasl".new; |
2629
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
11 local http = require "socket.http"; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
12 local https = require "ssl.https"; |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local base64 = require "util.encodings".base64.encode; |
2159
5e8dec076afc
mod_auth_http_async: Fall back to non-async calling of http_auth_url
JC Brand <jcbrand@minddistrict.com>
parents:
1939
diff
changeset
|
14 local have_async, async = pcall(require, "util.async"); |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local log = module._log; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local host = module.host; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local api_base = module:get_option_string("http_auth_url", ""):gsub("$host", host); |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 if api_base == "" then error("http_auth_url required") end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
2629
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
22 local provider = {}; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
23 |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
24 -- globals required by socket.http |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
25 if rawget(_G, "PROXY") == nil then |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
26 rawset(_G, "PROXY", false) |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
27 end |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
28 if rawget(_G, "base_parsed") == nil then |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
29 rawset(_G, "base_parsed", false) |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
30 end |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
31 |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
32 local function async_http_auth(url, username, password) |
2159
5e8dec076afc
mod_auth_http_async: Fall back to non-async calling of http_auth_url
JC Brand <jcbrand@minddistrict.com>
parents:
1939
diff
changeset
|
33 local wait, done = async.waiter(); |
1927
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
34 local content, code, request, response; |
2629
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
35 local ex = { |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
36 headers = { Authorization = "Basic "..base64(username..":"..password); }; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
37 } |
1930
95bbf3c4aa27
mod_auth_http_async: Don't set global
Kim Alvefur <zash@zash.se>
parents:
1927
diff
changeset
|
38 local function cb(content_, code_, request_, response_) |
1927
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
39 content, code, request, response = content_, code_, request_, response_; |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
40 done(); |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
41 end |
1931
bd5412eb0a6d
mod_auth_http_async: Actually do the HTTP request
Kim Alvefur <zash@zash.se>
parents:
1930
diff
changeset
|
42 http.request(url, ex, cb); |
1927
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
43 wait(); |
2629
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
44 if code >= 200 and code <= 299 then |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
45 module:log("debug", "HTTP auth provider confirmed valid password"); |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
46 return true; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
47 else |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
48 module:log("debug", "HTTP auth provider returned status code %d", code); |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
49 end |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
50 return nil, "Auth failed. Invalid username or password."; |
1927
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
51 end |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
52 |
2629
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
53 local function sync_http_auth(url) |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
54 local request; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
55 if string.sub(url, 1, string.len('https')) == 'https' then |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
56 request = https.request; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
57 else |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
58 request = http.request; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
59 end |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
60 local _, code, headers, status = request{ |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
61 url = url, |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
62 headers = { ACCEPT = "application/json, text/plain, */*"; } |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
63 }; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
64 if type(code) == "number" and code >= 200 and code <= 299 then |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
65 module:log("debug", "HTTP auth provider confirmed valid password"); |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
66 return true; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
67 else |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
68 module:log("debug", "HTTP auth provider returned status code: "..code); |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
69 end |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
70 return nil, "Auth failed. Invalid username or password."; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
71 end |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 function provider.test_password(username, password) |
2629
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
74 local url = api_base:gsub("$user", username):gsub("$password", password); |
2442
b2a198665946
mod_auth_http_async: Log URL when testing password
JC Brand <jc@opkode.com>
parents:
2159
diff
changeset
|
75 log("debug", "Testing password for user %s at host %s with URL %s", username, host, url); |
2159
5e8dec076afc
mod_auth_http_async: Fall back to non-async calling of http_auth_url
JC Brand <jcbrand@minddistrict.com>
parents:
1939
diff
changeset
|
76 if (have_async) then |
2629
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
77 return async_http_auth(url, username, password); |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 else |
2629
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
79 return sync_http_auth(url); |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 end |
2629
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
81 end |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
82 |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
83 function provider.users() |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
84 return function() |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
85 return nil; |
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
86 end |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 function provider.set_password(username, password) |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 return nil, "Changing passwords not supported"; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 function provider.user_exists(username) |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 return true; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 function provider.create_user(username, password) |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 return nil, "User creation not supported"; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 function provider.delete_user(username) |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 return nil , "User deletion not supported"; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 function provider.get_sasl_handler() |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 return new_sasl(host, { |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 plain_test = function(sasl, username, password, realm) |
1939
54f9e8663139
mod_auth_http_async: Correctly pass password to provider.test_password (thanks mother)
Kim Alvefur <zash@zash.se>
parents:
1938
diff
changeset
|
108 return provider.test_password(username, password), true; |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 }); |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 end |
2629
a11568bfaf4c
mod_auth_http_async: For sync calls, use LuaSockets' HTTP lib
JC Brand <jc@opkode.com>
parents:
2442
diff
changeset
|
112 |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 module:provides("auth", provider); |