Merge pull request #470 from thesabbir/patch-01
[project/luci.git] / modules / luci-mod-rpc / luasrc / controller / rpc.lua
index 359184c..759bb74 100644 (file)
@@ -1,17 +1,6 @@
---[[
-LuCI - Lua Configuration Interface
-
-Copyright 2008 Steven Barth <steven@midlink.org>
-Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-$Id$
-]]--
+-- Copyright 2008 Steven Barth <steven@midlink.org>
+-- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
+-- Licensed to the public under the Apache License 2.0.
 
 local require = require
 local pairs = pairs
@@ -25,7 +14,7 @@ function index()
        local function authenticator(validator, accs)
                local auth = luci.http.formvalue("auth", true)
                if auth then -- if authentication token was given
-                       local sdat = luci.sauth.read(auth)
+                       local sdat = (luci.util.ubus("session", "get", { ubus_rpc_session = auth }) or { }).values
                        if sdat then -- if given token is valid
                                if sdat.user and luci.util.contains(accs, sdat.user) then
                                        return sdat.user, auth
@@ -49,7 +38,6 @@ end
 
 function rpc_auth()
        local jsonrpc = require "luci.jsonrpc"
-       local sauth   = require "luci.sauth"
        local http    = require "luci.http"
        local sys     = require "luci.sys"
        local ltn12   = require "luci.ltn12"
@@ -61,18 +49,25 @@ function rpc_auth()
        server.challenge = function(user, pass)
                local sid, token, secret
 
+               local config = require "luci.config"
+
                if sys.user.checkpasswd(user, pass) then
-                       sid = sys.uniqueid(16)
-                       token = sys.uniqueid(16)
-                       secret = sys.uniqueid(16)
-
-                       http.header("Set-Cookie", "sysauth=" .. sid.."; path=/")
-                       sauth.reap()
-                       sauth.write(sid, {
-                               user=user,
-                               token=token,
-                               secret=secret
-                       })
+                       local sdat = util.ubus("session", "create", { timeout = config.sauth.sessiontime })
+                       if sdat then
+                               sid = sdat.ubus_rpc_session
+                               token = sys.uniqueid(16)
+                               secret = sys.uniqueid(16)
+
+                               http.header("Set-Cookie", "sysauth="..sid.."; path=/")
+                               util.ubus("session", "set", {
+                                       ubus_rpc_session = sid,
+                                       values = {
+                                               user = user,
+                                               token = token,
+                                               secret = secret
+                                       }
+                               })
+                       end
                end
 
                return sid and {sid=sid, token=token, secret=secret}