Software /
code /
prosody-modules
Annotate
mod_auth_http_async/mod_auth_http_async.lua @ 1931:bd5412eb0a6d
mod_auth_http_async: Actually do the HTTP request
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 24 Oct 2015 13:19:25 +0200 |
parent | 1930:95bbf3c4aa27 |
child | 1932:3729245498ae |
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; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local base64 = require "util.encodings".base64.encode; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local waiter =require "util.async".waiter; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local http = require "net.http"; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local log = module._log; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local host = module.host; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 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
|
19 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
|
20 |
1927
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
21 local function async_http_request(url, ex); |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
22 local wait, done = waiter(); |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
23 |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
24 local 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
|
25 local ret; |
1930
95bbf3c4aa27
mod_auth_http_async: Don't set global
Kim Alvefur <zash@zash.se>
parents:
1927
diff
changeset
|
26 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
|
27 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
|
28 done(); |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
29 end |
1931
bd5412eb0a6d
mod_auth_http_async: Actually do the HTTP request
Kim Alvefur <zash@zash.se>
parents:
1930
diff
changeset
|
30 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
|
31 wait(); |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
32 return 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
|
33 end |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
34 |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 local provider = {}; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 function provider.test_password(username, password) |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 log("debug", "test password for user %s at host %s", username, host); |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
1927
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
41 local _, code = async_http_request(api_base:gsub("$user", username), { |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
42 headers = { Authorization = "Basic "..base64(username..":"..password); }; |
439711709d29
mod_auth_http_async: Wrap up async http request in a function
Kim Alvefur <zash@zash.se>
parents:
1749
diff
changeset
|
43 }); |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 if code >= 200 and code <= 299 then |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 return true; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 else |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 module:log("debug", "HTTP auth provider returned status code %d", code); |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 return nil, "Auth failed. Invalid username or password."; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 function provider.set_password(username, password) |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 return nil, "Changing passwords not supported"; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 function provider.user_exists(username) |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 return true; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 function provider.create_user(username, password) |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 return nil, "User creation not supported"; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 function provider.delete_user(username) |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 return nil , "User deletion not supported"; |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 function provider.get_sasl_handler() |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 return new_sasl(host, { |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 plain_test = function(sasl, username, password, realm) |
1749
39a0a35f02bc
mod_auth_http_async: Don't go throug usermanager to call a function from the same module
Kim Alvefur <zash@zash.se>
parents:
1421
diff
changeset
|
72 return provider.test_password(username, realm, password), true; |
1421
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 }); |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 module:provides("auth", provider); |
295c30e44ba8
mod_auth_http_async: Async HTTP auth module
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 |