summaryrefslogtreecommitdiffstats
path: root/security-util-lib/src/main/java/org/onap/sdc/security/filters/SampleFilter.java
blob: 853c40d92e0abfecafd28fb40ea6a80259c8bac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.security.filters;

import org.onap.sdc.security.AuthenticationCookie;
import org.onap.sdc.security.ISessionValidationFilterConfiguration;
import org.onap.sdc.security.RedirectException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SampleFilter extends SessionValidationFilter {

    static final String FAILED_ON_USER_AUTH = "failedOnUserAuth";
    static final String FAILED_ON_ROLE = "failedOnRole";

    private static class Configuration implements ISessionValidationFilterConfiguration {

        private String securityKey = "AGLDdG4D04BKm2IxIWEr8o==";
        private long maxSessionTimeOut = 24*60*60*1000;
        private long sessionIdleTimeOut = 60*60*1000;
        private String redirectURL = "http://portal.api.simpledemo.onap.org:8989/ECOMPPORTAL/login.htm";
        private List<String> excludedUrls = new ArrayList<>(Arrays.asList("/config","/configmgr","/rest","/kibanaProxy","/healthcheck","/upload.*"));
        private String cookieName = "kuku";
        private final String cookieDomain = "";
        private final String cookiePath = "/";
        private boolean isCookieHttpOnly = true;

        private static final Configuration instance = new Configuration();


        private Configuration() {
        }

        public static Configuration getInstance(){
             return instance;
        }

        // --- set method only for tests START ---
        private void setSecurityKey(String securityKey) {
            this.securityKey = securityKey;
        }

        private void setMaxSessionTimeOut(long maxSessionTimeOut) {
            this.maxSessionTimeOut = maxSessionTimeOut;
        }

        private void setCookieName(String cookieName) {
            this.cookieName = cookieName;
        }

        private void setRedirectURL(String redirectURL) {
            this.redirectURL = redirectURL;
        }

        private void setExcludedUrls(List<String> excludedUrls) {
            this.excludedUrls = excludedUrls;
        }
        // --- set method only for tests START ---

        @Override
        public String getSecurityKey() {
            return securityKey;
        }

        @Override
        public long getMaxSessionTimeOut() {
            return maxSessionTimeOut;
        }

        @Override
        public long getSessionIdleTimeOut() {
            return sessionIdleTimeOut;
        }

        @Override
        public String getCookieName() {
            return cookieName;
        }

        @Override
        public String getCookieDomain() {
            return cookieDomain;
        }

        @Override
        public String getCookiePath() {
            return cookiePath;
        }

        @Override
        public boolean isCookieHttpOnly() {
            return isCookieHttpOnly;
        }

        @Override
        public String getRedirectURL() {
            return redirectURL;
        }

        @Override
        public List<String> getExcludedUrls() {
            return excludedUrls;
        }

    }

    // --- set methods only for tests START ---
    public void setSecurityKey(String securityKey) {
        Configuration.getInstance().setSecurityKey(securityKey);
    }

    public void setMaxSessionTimeOut(long maxSessionTimeOut) {
        Configuration.getInstance().setMaxSessionTimeOut(maxSessionTimeOut);
    }

    public void setCookieName(String cookieName) {
        Configuration.getInstance().setCookieName(cookieName);
    }

    public void setRedirectURL(String redirectURL) {
        Configuration.getInstance().setRedirectURL(redirectURL);
    }

    public void setExcludedUrls(List<String> excludedUrls) {
        Configuration.getInstance().setExcludedUrls(excludedUrls);
    }
    // --- set methods only for tests END ---

    @Override
    public ISessionValidationFilterConfiguration getFilterConfiguration() {
        return Configuration.getInstance();
    }

    @Override
    protected Cookie addRoleToCookie(Cookie updatedCookie) {
        return updatedCookie;
    }

    @Override
    protected void authorizeUserOnSessionExpiration(AuthenticationCookie authenticationCookie, Cookie[] cookies) throws RedirectException {
        if (FAILED_ON_USER_AUTH.equals(authenticationCookie.getUserID())) {
            throw new RedirectException();
        }
    }

    @Override
    protected void handleRedirectException(HttpServletResponse httpResponse) throws IOException {
        //do nothing in this implementation
    }

}