3 Copyright 2016 2015-2016 OEPN-O. and others. All rights reserved.
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
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"
31 local function verify_value(value)
32 if (nil == value or 0 == #value)
40 --[[checks str2 starts with str1]]--
41 local function starts_with(str1, str2)
42 return string.sub(str2, 1, string.len(str1)) == str1;
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)
49 if (starts_with(value, url))
57 local function set_header(tokens)
58 for key,value in pairs(tokens)
60 ngx.log (ngx.ERR, "Headers: ", key, value);
61 ngx.req.set_header(key, value);
65 --[[ validates the token with auth ]]--
66 local function validate_token(tokens)
67 -- auth expects the token in header.
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);
76 return (ngx.HTTP_OK == res.status);
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];
84 -- verify whether its empty or null.
85 if (verify_value(auth_token))
87 ngx.log(ngx.ERR, "token : ", auth_token );
88 tokens[auth_token_key] = auth_token;
93 local function get_service_url()
95 local host = ngx.var.host;
97 local port = ":"..ngx.var.server_port;
100 if (ngx.var.https == "on")
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
115 complete_url = complete_url.."?"..ngx.var.args;
117 ngx.log(ngx.ERR, "service url : ", complete_url);
118 return service..ngx.escape_uri(complete_url);
121 local function redirect(url)
122 local service = get_service_url();
123 ngx.log(ngx.ERR, "redirect: ", url..service);
124 ngx.redirect(url..service);
129 ngx.log(ngx.ERR, "==============start check token===============: ");
130 local url = ngx.var.uri;
131 ngx.log(ngx.ERR, "Url : ", url);
133 -- ignore token validation if auth request.
134 if (is_white_list(url))
141 -- get auth token from cookies.
142 local auth_tokens = get_cookies();
144 -- check if auth token is empty,
145 -- redirect it to login page in that case.
146 if (nil == next(auth_tokens))
148 ngx.log(ngx.ERR, "Token Invalidate, redirect to ", redirect_url);
149 redirect(redirect_url);
153 -- validate the token with auth module.
154 -- continue if success, else redirect to login page.
155 if(validate_token(auth_tokens))
157 ngx.log(ngx.ERR, "Token Validate.");
160 redirect(redirect_url);
162 ngx.log(ngx.INFO, "running auth plugin")