Added oparent to sdc main
[sdc.git] / openecomp-be / backend / openecomp-sdc-security-util / src / main / java / org / openecomp / sdc / securityutil / AuthenticationCookie.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.securityutil;
22
23 import java.util.Set;
24
25
26 public class AuthenticationCookie  {
27
28     private String userID;
29     private Set<String> roles;
30     private long maxSessionTime;
31     private long currentSessionTime;
32
33     public AuthenticationCookie(){ }
34
35     public AuthenticationCookie(AuthenticationCookie authenticationCookie){
36         this.userID = authenticationCookie.userID;
37         this.roles = authenticationCookie.roles;
38         this.maxSessionTime = authenticationCookie.maxSessionTime;
39         this.currentSessionTime = authenticationCookie.currentSessionTime;
40     }
41
42     /**
43      * Create new cookie with max_session_time and current_session_time started with same value
44      * @param userId
45      */
46     public AuthenticationCookie(String userId) {
47         this.userID =userId;
48         long currentTimeMilliSec = System.currentTimeMillis();
49         this.maxSessionTime = currentTimeMilliSec;
50         this.currentSessionTime = currentTimeMilliSec;
51     }
52
53     public String getUserID() {
54         return userID;
55     }
56
57     public void setUserID(String userID) {
58         this.userID = userID;
59     }
60
61     public Set getRoles() {
62         return roles;
63     }
64
65     public void setRoles(Set<String> roles) {
66         this.roles = roles;
67     }
68
69     public long getMaxSessionTime() {
70         return maxSessionTime;
71     }
72
73     public void setMaxSessionTime(long maxSessionTime) {
74         this.maxSessionTime = maxSessionTime;
75     }
76
77
78     public long getCurrentSessionTime() {
79         return currentSessionTime;
80     }
81
82     public void setCurrentSessionTime(long currentSessionTime) {
83         this.currentSessionTime = currentSessionTime;
84     }
85
86     @Override
87     public boolean equals(Object o) {
88         if (this == o) return true;
89         if (!(o instanceof AuthenticationCookie)) return false;
90
91         AuthenticationCookie that = (AuthenticationCookie) o;
92
93         if (getMaxSessionTime() != that.getMaxSessionTime()) return false;
94         if (getCurrentSessionTime() != that.getCurrentSessionTime()) return false;
95         if (getUserID() != null ? !getUserID().equals(that.getUserID()) : that.getUserID() != null) return false;
96         return getRoles() != null ? getRoles().containsAll(that.getRoles()) : that.getRoles() == null;
97     }
98
99     @Override
100     public int hashCode() {
101         int result = getUserID() != null ? getUserID().hashCode() : 0;
102         result = 31 * result + (getRoles() != null ? getRoles().hashCode() : 0);
103         result = 31 * result + (int) (getMaxSessionTime() ^ (getMaxSessionTime() >>> 32));
104         result = 31 * result + (int) (getCurrentSessionTime() ^ (getCurrentSessionTime() >>> 32));
105         return result;
106     }
107
108     @Override
109     public String toString() {
110         return "AuthenticationCookie{" +
111                 "userID='" + userID + '\'' +
112                 ", roles=" + roles +
113                 ", maxSessionTime=" + maxSessionTime +
114                 ", currentSessionTime=" + currentSessionTime +
115                 '}';
116     }
117 }