Am 26.10.2010 17:49, schrieb Ignacio Burgueño:
>
> You could try with HTTP_AUTHORIZATION.

Great, thanks! That's working.
Startx, it seems that env.REMOTE_USER was empty here.

I made a small "module" which just parses the HTTP_AUTHORIZATION and
returns a table. It's not perfect code because i am a lua beginner but
it can be used like that:
  
require('wsapi.response);                                                       
                                                                                
                                             

require('wsapi.authorization');                                                 
                                                                                
                                                   


local function serve(res)
    res:write("Serving...");
end

function run(wsapi_env)
    local res = wsapi.response.new();
    local auth =
wsapi.authorization.new(wsapi_env);                                             
                                                                                
                                                               

   
    if auth:check("user","pass")
then                                                                            
                                                                                
                                

--        log.info("Auth ok,
serving");                                                                      
                                                                                
                                     

       
serve(res)                                                                      
                                                                                
                                                           

   
else                                                                            
                                                                                
                                                            

        if auth.user ~= nil
then                                                                            
                                                                                
                                 

--            log.info("Auth passed. User: " .. auth.user .. "=>" ..
(auth.passwd or
""));                                                                           
                                                       

       
end                                                                             
                                                                                
                                                        

       
auth:request("RealmName",res);                                                  
                                                                                
                                                         

   
end                                                                             
                                                                                
                                                            

                                                                                
                                                                                
                                                                

                                                                                
                                                                                
                                                                

    return
res:finish();                                                                   
                                                                                
                                                  

end                              

Maybe this is helpful for other WSAPI users...


Bye
Björn

module("wsapi.authorization", package.seeall)

                                                                                
                                                                                
                                                   
local function split(str,sep)
    local sep, fields = sep or ":", {}
    local pattern = string.format("([^%s]+)", sep)
    string.gsub(str,pattern, function(c) fields[#fields+1] = c end)
    return fields
end                

local function parseAuth(wsapi_env)
    local ret = { type = nil, user = nil, passwd = nil }
    
    if wsapi_env["HTTP_AUTHORIZATION"] ~= "" then
        local authT = split(wsapi_env["HTTP_AUTHORIZATION"]," ");
        if authT[2] ~= nil then
            local mime = require('mime');
            ret.type = authT[1];
            if authT[2] ~= nil then
                authT = split(mime.unb64(authT[2]),":");
                ret.user = authT[1];
                ret.passwd = authT[2];
            end
        end
    end
    
    return ret;
end

local function requestAuth(self,realm, response)
    if response then
        response["WWW-Authenticate"] = 'Basic realm="' .. (realm or "") .. "'";
        response.status = 401
        return true;
    end
    return false;
end

local function checkAuth(self,user,passwd)
    if self.user == user and self.passwd == passwd then
        return true;
    else
        return false;
    end
end

function new(wsapi_env)
    local auth = parseAuth(wsapi_env);
    auth.HTTP_AUTHORIZATION = wsapi_env["HTTP_AUTHORIZATION"] or "";
    auth.request = requestAuth;
    auth.check = checkAuth;

  return auth
end

_______________________________________________
Kepler-Project mailing list
Kepler-Project@lists.luaforge.net
http://lists.luaforge.net/cgi-bin/mailman/listinfo/kepler-project
http://www.keplerproject.org/

Reply via email to