RPC initial authentication API completed
[project/luci.git] / modules / rpc / luasrc / controller / rpc.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15 module("luci.controller.rpc", package.seeall)
16
17 function index()
18         local function authenticator(validator, accs)
19                 local args = luci.dispatcher.context.args
20                 if args and #args > 0 then
21                         local user = luci.sauth.read(args[1])
22                         if user and luci.util.contains(accs, user) then
23                                 return user
24                         end
25                 end
26                 luci.http.status(403, "Forbidden")
27         end
28         
29         uci = entry({"rpc", "uci"}, call("rpc_uci"))
30         uci.sysauth = "root"
31         uci.sysauth_authenticator = authenticator
32         uci.leaf = true
33         
34         uci = entry({"rpc", "auth"}, call("rpc_auth"))
35 end
36
37 function rpc_auth()
38         require "luci.jsonrpc"
39         require "luci.sauth"
40         
41         luci.http.setfilehandler()
42         
43         local loginstat
44         
45         local server = {}
46         server.login = function(user, pass)
47                 local sid
48                 
49                 if luci.sys.user.checkpasswd(user, pass) then
50                         sid = luci.sys.uniqueid(16)
51                         luci.http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
52                         luci.sauth.write(sid, user)
53                 end
54                 
55                 return sid
56         end
57         
58         luci.http.prepare_content("application/json")
59         luci.http.write(luci.jsonrpc.handle(server, luci.http.content()))
60         
61         return loginstat
62 end
63
64 function rpc_uci()
65         
66 end