Rework authentication system
[project/luci.git] / libs / web / luasrc / sauth.lua
1 --[[
2
3 Session authentication
4 (c) 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 --- LuCI session library.
17 module("luci.sauth", package.seeall)
18 require("luci.util")
19 require("luci.sys")
20 require("luci.config")
21 local nixio = require "nixio", require "nixio.util"
22 local fs = require "nixio.fs"
23
24
25 luci.config.sauth = luci.config.sauth or {}
26 sessionpath = luci.config.sauth.sessionpath
27 sessiontime = tonumber(luci.config.sauth.sessiontime) or 15 * 60
28
29 --- Prepare session storage by creating the session directory.
30 function prepare()
31         fs.mkdir(sessionpath, 700)
32         if not sane() then
33                 error("Security Exception: Session path is not sane!")
34         end
35 end
36
37 function encode(t)
38         return luci.util.get_bytecode({
39                 user=t.user,
40                 token=t.token,
41                 secret=t.secret,
42                 atime=luci.sys.uptime()
43         })
44 end
45
46 function decode(blob)
47         local t = loadstring(blob)()
48         return {
49                 user = t.user,
50                 token = t.token,
51                 secret = t.secret,
52                 atime = t.atime
53         }
54 end
55
56 --- Read a session and return its content.
57 -- @param id    Session identifier
58 -- @return              Session data
59 local function _read(id)
60         local blob = fs.readfile(sessionpath .. "/" .. id)
61         return blob
62 end
63
64 --- Write session data to a session file.
65 -- @param id    Session identifier
66 -- @param data  Session data
67 local function _write(id, data)
68         local f = nixio.open(sessionpath .. "/" .. id, "w", 600)
69         f:writeall(data)
70         f:close()
71 end
72
73 function write(id, data)
74         if not sane() then
75                 prepare()
76         end
77
78         if not id or #id == 0 or not id:match("^%w+$") then
79                 error("Session ID is not sane!")
80         end
81
82         _write(id, data)
83 end
84
85 function read(id)
86         if not id or #id == 0 then
87                 return
88         end
89         if not id:match("^%w+$") then
90                 error("Session ID is not sane!")
91         end
92         if not sane(sessionpath .. "/" .. id) then
93                 return
94         end
95
96         local blob = _read(id)
97         if decode(blob).atime + sessiontime < luci.sys.uptime()then
98                 fs.unlink(sessionpath .. "/" .. id)
99                 return
100         end
101         -- refresh atime in session
102         refreshed = encode(decode(blob))
103         write(id, refreshed)
104         return blob
105 end
106
107
108 --- Check whether Session environment is sane.
109 -- @return Boolean status
110 function sane(file)
111         return luci.sys.process.info("uid")
112                         == fs.stat(file or sessionpath, "uid")
113                 and fs.stat(file or sessionpath, "modestr")
114                         == (file and "rw-------" or "rwx------")
115 end
116
117 --- Kills a session
118 -- @param id    Session identifier
119 function kill(id)
120         if not id:match("^%w+$") then
121                 error("Session ID is not sane!")
122         end
123         fs.unlink(sessionpath .. "/" .. id)
124 end