msb protocol synch change
[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   '/openoui/common/css',
29   '/openoui/common/js',
30   '/openoui/common/thirdparty',
31   '/openoui/common/i18n',
32   '/openoui/common/image',
33   '/openoui/common/login.html',
34   '/openoui/common/json'
35 };
36
37 local function verify_value(value)
38   if (nil == value or 0 == #value)
39   then
40     return false;
41   else
42     return true;
43   end
44 end
45
46 --[[checks str2 starts with str1]]--
47 local function starts_with(str1, str2)
48   return string.sub(str2, 1, string.len(str1)) == str1;
49 end
50
51 --  Check and ignore the request if it is from auth module.--
52 local function is_white_list(url)
53   for i, value in ipairs(white_list)
54   do
55     if (starts_with(value, url))
56       then
57         return true;
58       end
59   end
60   return false;
61 end
62
63 local function set_header(tokens)
64   for key,value in pairs(tokens)
65   do
66     ngx.log (ngx.ERR, "Headers: ", key, value);
67     ngx.req.set_header(key, value);
68   end
69
70 end
71 --[[ validates the token with auth ]]--
72 local function validate_token(tokens)
73   -- auth expects the token in header.
74   set_header(tokens);
75   -- call auth token check url to validate.
76   local res = ngx.location.capture(auth_token_url, { method = ngx.HTTP_HEAD});
77   ngx.log (ngx.ERR, "Auth Result:", res.status);
78   if (nil == res)
79   then
80     return false;
81   end
82   return (ngx.HTTP_OK == res.status);
83 end
84
85 --[[ get auth token from cookies ]]--
86 local function get_cookies()
87   local cookie_name = "cookie_"..auth_token_key;
88   local auth_token = ngx.var[cookie_name];
89   local tokens = {};
90   -- verify whether its empty or null.
91   if (verify_value(auth_token))
92   then
93   ngx.log(ngx.ERR, "token : ", auth_token );
94     tokens[auth_token_key] = auth_token;
95   end
96   return tokens;
97 end
98
99 local function get_service_url()
100   -- get host.
101   local host = ngx.var.host;
102   --get port
103   local port = ":"..ngx.var.server_port;
104   local proto = "";
105   --get protocol
106   if (ngx.var.https == "on")
107   then
108     proto = "https://";
109   else
110     proto = "http://";
111   end
112   --get url
113   local uri = ngx.var.uri;
114   --form complete service url.
115   --local complete_url = proto..host..port..url
116   local complete_url = uri;
117   local service = "?service="
118   --add arguments if any.
119   if ngx.var.args ~= nil
120   then
121     complete_url = complete_url.."?"..ngx.var.args;
122   end
123   ngx.log(ngx.ERR, "service url : ", complete_url);
124   return service..ngx.escape_uri(complete_url);
125 end
126
127 local function redirect(url)
128   local service = get_service_url();
129   ngx.log(ngx.ERR, "redirect: ", url..service);
130   ngx.redirect(url..service);
131 end
132
133 function _M.access()
134
135     ngx.log(ngx.ERR, "==============start check token===============: ");
136     local url = ngx.var.uri;
137     ngx.log(ngx.ERR, "Url : ", url);
138
139     -- ignore token validation if auth request.
140     if (is_white_list(url))
141     then
142       return;
143     end
144
145
146
147     -- get auth token from cookies.
148     local auth_tokens = get_cookies();
149
150     -- check if auth token is empty,
151     -- redirect it to login page in that case.
152     if (nil == next(auth_tokens))
153     then
154       ngx.log(ngx.ERR, "Token Invalidate, redirect to ", redirect_url);
155       redirect(redirect_url);
156       return;
157     end
158
159     -- validate the token with auth module.
160     -- continue if success, else redirect to login page.
161     if(validate_token(auth_tokens))
162     then
163       ngx.log(ngx.ERR, "Token Validate.");
164       return;
165     else
166       redirect(redirect_url);
167     end
168         ngx.log(ngx.INFO, "running auth plugin")
169     end
170
171 return _M