change the driver redirect timeout to 5 minute ; change the auth plugin white list...
[msb/apigateway.git] / msb-core / openresty-ext / src / assembly / resources / openresty / nginx / luaext / plugins / auth.lua
1 --[[
2
3     Copyright 2016 2015-2016 OEPN-O. and others. All rights reserved.
4
5     Licensed under the Apache License, Version 2.0 (the "License");
6     you may not use this file except in compliance with the License.
7     You may obtain a copy of the License at
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11     Unless required by applicable law or agreed to in writing, software
12     distributed under the License is distributed on an "AS IS" BASIS,
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14     See the License for the specific language governing permissions and
15     limitations under the License.
16
17 ]]
18 local _M = {}
19 _M._VERSION = '1.0.0'
20 local auth_url = '/openoapi/auth/v1';
21 local auth_token_url = auth_url..'/tokens';
22 local auth_token_key = "X-Auth-Token";
23 local redirect_url = "/openoui/common/login.html"
24
25 local white_list= {
26   auth_token_url,
27   redirect_url,
28   '/portal-package/common/'
29 };
30
31 local function verify_value(value)
32   if (nil == value or 0 == #value)
33   then
34     return false;
35   else
36     return true;
37   end
38 end
39
40 --[[checks str2 starts with str1]]--
41 local function starts_with(str1, str2)
42   return string.sub(str2, 1, string.len(str1)) == str1;
43 end
44
45 --  Check and ignore the request if it is from auth module.--
46 local function is_white_list(url)
47   for i, value in ipairs(white_list)
48   do
49     if (starts_with(value, url))
50       then
51         return true;
52       end
53   end
54   return false;
55 end
56
57 local function set_header(tokens)
58   for key,value in pairs(tokens)
59   do
60     ngx.log (ngx.ERR, "Headers: ", key, value);
61     ngx.req.set_header(key, value);
62   end
63
64 end
65 --[[ validates the token with auth ]]--
66 local function validate_token(tokens)
67   -- auth expects the token in header.
68   set_header(tokens);
69   -- call auth token check url to validate.
70   local res = ngx.location.capture(auth_token_url, { method = ngx.HTTP_HEAD});
71   ngx.log (ngx.ERR, "Auth Result:", res.status);
72   if (nil == res)
73   then
74     return false;
75   end
76   return (ngx.HTTP_OK == res.status);
77 end
78
79 --[[ get auth token from cookies ]]--
80 local function get_cookies()
81   local cookie_name = "cookie_"..auth_token_key;
82   local auth_token = ngx.var[cookie_name];
83   local tokens = {};
84   -- verify whether its empty or null.
85   if (verify_value(auth_token))
86   then
87   ngx.log(ngx.ERR, "token : ", auth_token );
88     tokens[auth_token_key] = auth_token;
89   end
90   return tokens;
91 end
92
93 local function get_service_url()
94   -- get host.
95   local host = ngx.var.host;
96   --get port
97   local port = ":"..ngx.var.server_port;
98   local proto = "";
99   --get protocol
100   if (ngx.var.https == "on")
101   then
102     proto = "https://";
103   else
104     proto = "http://";
105   end
106   --get url
107   local uri = ngx.var.uri;
108   --form complete service url.
109   --local complete_url = proto..host..port..url
110   local complete_url = uri;
111   local service = "?service="
112   --add arguments if any.
113   if ngx.var.args ~= nil
114   then
115     complete_url = complete_url.."?"..ngx.var.args;
116   end
117   ngx.log(ngx.ERR, "service url : ", complete_url);
118   return service..ngx.escape_uri(complete_url);
119 end
120
121 local function redirect(url)
122   local service = get_service_url();
123   ngx.log(ngx.ERR, "redirect: ", url..service);
124   ngx.redirect(url..service);
125 end
126
127 function _M.access()
128
129     ngx.log(ngx.ERR, "==============start check token===============: ");
130     local url = ngx.var.uri;
131     ngx.log(ngx.ERR, "Url : ", url);
132
133     -- ignore token validation if auth request.
134     if (is_white_list(url))
135     then
136       return;
137     end
138
139
140
141     -- get auth token from cookies.
142     local auth_tokens = get_cookies();
143
144     -- check if auth token is empty,
145     -- redirect it to login page in that case.
146     if (nil == next(auth_tokens))
147     then
148       ngx.log(ngx.ERR, "Token Invalidate, redirect to ", redirect_url);
149       redirect(redirect_url);
150       return;
151     end
152
153     -- validate the token with auth module.
154     -- continue if success, else redirect to login page.
155     if(validate_token(auth_tokens))
156     then
157       ngx.log(ngx.ERR, "Token Validate.");
158       return;
159     else
160       redirect(redirect_url);
161     end
162         ngx.log(ngx.INFO, "running auth plugin")
163     end
164
165 return _M