xref: /OpenGrok/plugins/src/main/java/opengrok/auth/plugin/entity/User.java (revision cf8d876b669a88f2578319b4a452942876e58dcb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
22  */
23 package opengrok.auth.plugin.entity;
24 
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.Map;
28 
29 public class User {
30 
31     private String id;
32     private String username;
33     private Date cookieTimestamp;
34     private boolean timeouted;
35     private final Map<String, Object> attrs = new HashMap<>();
36 
User(String username)37     public User(String username) {
38         this.username = username;
39     }
40 
41     /**
42      * Construct User object.
43      * @param username username
44      * @param id user ID
45      */
User(String username, String id)46     public User(String username, String id) {
47         this.username = username;
48         this.id = id;
49     }
50 
51     /**
52      * Construct User object.
53      * @param username username
54      * @param id user ID
55      * @param cookieTimestamp cookie time stamp
56      * @param timeouted is the user timed out
57      */
User(String username, String id, Date cookieTimestamp, boolean timeouted)58     public User(String username, String id, Date cookieTimestamp, boolean timeouted) {
59         this(username, id);
60         this.cookieTimestamp = cookieTimestamp;
61         this.timeouted = timeouted;
62     }
63 
getId()64     public String getId() {
65         return id;
66     }
67 
setId(String id)68     public void setId(String id) {
69         this.id = id;
70     }
71 
getUsername()72     public String getUsername() {
73         return username;
74     }
75 
setUsername(String username)76     public void setUsername(String username) {
77         this.username = username;
78     }
79 
getCookieTimestamp()80     public Date getCookieTimestamp() {
81         return cookieTimestamp;
82     }
83 
setCookieTimestamp(Date cookieTimestamp)84     public void setCookieTimestamp(Date cookieTimestamp) {
85         this.cookieTimestamp = cookieTimestamp;
86     }
87 
getTimeouted()88     public boolean getTimeouted() {
89         return timeouted;
90     }
91 
setTimeouted(boolean timeouted)92     public void setTimeouted(boolean timeouted) {
93         this.timeouted = timeouted;
94     }
95 
isTimeouted()96     public boolean isTimeouted() {
97         return timeouted;
98     }
99 
100     /**
101      * Implemented for the forced authentication as described in
102      * <a href="https://docs.oracle.com/cd/B28196_01/idmanage.1014/b15997/mod_osso.htm#i1006381">mod_osso documentation</a>.
103      *
104      * @param forcedAuthDate the date of the forced authentication trigger
105      * @param newLoginDate the date of the new login
106      * @return true if login date was before forced auth date or cookie timestamp
107      */
isForcedTimeouted(Date forcedAuthDate, Date newLoginDate)108     public boolean isForcedTimeouted(Date forcedAuthDate, Date newLoginDate) {
109         if (cookieTimestamp == null || forcedAuthDate == null || newLoginDate == null) {
110             return true;
111         }
112 
113         return newLoginDate.before(forcedAuthDate) || newLoginDate.before(cookieTimestamp);
114     }
115 
116     /**
117      * Get custom user property.
118      *
119      * @param key the key
120      * @return the the value associated with the key
121      */
getAttribute(String key)122     public Object getAttribute(String key) {
123         return attrs.get(key);
124     }
125 
126     /**
127      * Set custom user property.
128      *
129      * @param key the key
130      * @param value the value
131      * @return the value previously associated with the key
132      */
setAttribute(String key, Object value)133     public Object setAttribute(String key, Object value) {
134         return attrs.put(key, value);
135     }
136 
137     /**
138      * Remote custom user property.
139      *
140      * @param key the key
141      * @return the value previously associated with the key
142      */
removeAttribute(String key)143     public Object removeAttribute(String key) {
144         return attrs.remove(key);
145     }
146 
147     @Override
toString()148     public String toString() {
149         return "User{" + "id=" + id + ", username=" + username + ", cookieTimestamp=" + cookieTimestamp +
150                 ", timeouted=" + timeouted + ", attrs=" + attrs + '}';
151     }
152 }
153