* Added native basic authentication support
[project/luci.git] / libs / sgi-webuci / luasrc / sgi / webuci.lua
1 --[[
2 LuCI - SGI-Module for Haserl
3
4 Description:
5 Server Gateway Interface for Haserl
6
7 FileId:
8 $Id: webuci.lua 2027 2008-05-07 21:16:35Z Cyrus $
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
16
17 http://www.apache.org/licenses/LICENSE-2.0
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 ]]--
26 module("luci.sgi.webuci", package.seeall)
27
28 local status_set = false
29
30 -- Initialize the environment
31 function initenv(env, vars)
32 luci.http.env = env
33 luci.http.vars = vars
34 end
35
36 -- Enforces user authentification
37 function luci.http.basic_auth(verify_callback, realm)
38 local user = luci.http.env.auth_user
39 local pass = luci.http.env.auth_password
40 realm = realm or ""
41
42 if not user or not verify_callback(user, pass) then
43 luci.http.status("401", "Unauthorized")
44 luci.http.header("WWW-Authenticate", string.format('Basic realm="%s"', realm))
45 return false
46 else
47 return true
48 end
49 end
50
51 -- Returns the main dispatcher URL
52 function luci.http.dispatcher()
53 return luci.http.env.SCRIPT_NAME or ""
54 end
55
56 -- Returns the upload dispatcher URL
57 function luci.http.dispatcher_upload()
58 -- To be implemented
59 end
60
61 -- Returns a table of all COOKIE, GET and POST Parameters
62 function luci.http.formvalues()
63 return luci.http.vars
64 end
65
66 -- Gets form value from key
67 function luci.http.formvalue(key, default)
68 return luci.http.formvalues()[key] or default
69 end
70
71 -- Gets a table of values with a certain prefix
72 function luci.http.formvaluetable(prefix)
73 local vals = {}
74 prefix = prefix and prefix .. "." or "."
75
76 for k, v in pairs(luci.http.formvalues()) do
77 if k:find(prefix, 1, true) == 1 then
78 vals[k:sub(#prefix + 1)] = v
79 end
80 end
81
82 return vals
83 end
84
85 -- Sends a custom HTTP-Header
86 function luci.http.header(key, value)
87 print(key .. ": " .. value)
88 end
89
90 -- Set Content-Type
91 function luci.http.prepare_content(type)
92 if not status_set then
93 luci.http.status(200, "OK")
94 end
95
96 print("Content-Type: "..type.."\n")
97 end
98
99 -- Asks the browser to redirect to "url"
100 function luci.http.redirect(url)
101 luci.http.status(302, "Found")
102 luci.http.header("Location", url)
103 print()
104 end
105
106 -- Returns the path of an uploaded file
107 -- WARNING! File uploads can be easily spoofed! Do additional sanity checks!
108 function luci.http.upload(name)
109 -- To be implemented
110 end
111
112 -- Sets HTTP-Status-Header
113 function luci.http.status(code, message)
114 print(luci.http.env.SERVER_PROTOCOL .. " " .. tostring(code) .. " " .. message)
115 status_set = true
116 end