blob: b31c64317d72b1d5a9f5f4505dca012aae4f7f78 [file] [log] [blame]
margaretha0e8f4e72018-04-05 14:11:52 +02001package de.ids_mannheim.korap.security.context;
Michael Hanlca740d72015-06-16 10:04:58 +02002
margaretha2afb97d2017-12-07 19:18:44 +01003import java.io.Serializable;
margarethaa2ce63d2018-06-28 10:11:43 +02004import java.time.ZonedDateTime;
5import java.util.Date;
margaretha2afb97d2017-12-07 19:18:44 +01006import java.util.HashMap;
7import java.util.Map;
8
Michael Hanlca740d72015-06-16 10:04:58 +02009import com.fasterxml.jackson.databind.JsonNode;
margaretha2afb97d2017-12-07 19:18:44 +010010
Michael Hanl00b64e02016-05-24 20:24:27 +020011import de.ids_mannheim.korap.config.Attributes;
margaretha0e8f4e72018-04-05 14:11:52 +020012import de.ids_mannheim.korap.constant.TokenType;
margaretha894a7d72017-11-08 19:24:20 +010013import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha0e8f4e72018-04-05 14:11:52 +020014import de.ids_mannheim.korap.user.User;
Michael Hanlca740d72015-06-16 10:04:58 +020015import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020016import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanl19390652016-01-16 11:01:24 +010017import lombok.AccessLevel;
Michael Hanlca740d72015-06-16 10:04:58 +020018import lombok.Data;
Michael Hanl19390652016-01-16 11:01:24 +010019import lombok.Getter;
Michael Hanlf1e85e72016-01-21 16:55:45 +010020import lombok.Setter;
Michael Hanlca740d72015-06-16 10:04:58 +020021
22/**
margarethaa2ce63d2018-06-28 10:11:43 +020023 * EM:
24 * - change datatype of tokenType from string to enum
25 * - added authenticationTime
26 *
Michael Hanlca740d72015-06-16 10:04:58 +020027 * @author hanl
28 * @date 27/01/2014
29 */
30@Data
Michael Hanlf1e85e72016-01-21 16:55:45 +010031public class TokenContext implements java.security.Principal, Serializable {
Michael Hanlca740d72015-06-16 10:04:58 +020032
margarethaa2ce63d2018-06-28 10:11:43 +020033 private ZonedDateTime authenticationTime;
Michael Hanlca740d72015-06-16 10:04:58 +020034 /**
35 * session relevant data. Are never persisted into a database
36 */
37 private String username;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020038 private long expirationTime;
Michael Hanlca740d72015-06-16 10:04:58 +020039 // either "session_token " / "api_token
margaretha2afb97d2017-12-07 19:18:44 +010040 private TokenType tokenType;
Michael Hanlca740d72015-06-16 10:04:58 +020041 private String token;
Michael Hanlca740d72015-06-16 10:04:58 +020042 private boolean secureRequired;
43
margaretha20f31232018-07-09 17:49:39 +020044// @Getter(AccessLevel.PRIVATE)
Michael Hanlf1e85e72016-01-21 16:55:45 +010045 @Setter(AccessLevel.PRIVATE)
Michael Hanl5fac8ab2016-01-29 16:33:04 +010046 private Map<String, Object> parameters;
Michael Hanlca740d72015-06-16 10:04:58 +020047 private String hostAddress;
48 private String userAgent;
49
Michael Hanl8abaf9e2016-05-23 16:46:35 +020050
51 public TokenContext () {
Michael Hanlca740d72015-06-16 10:04:58 +020052 this.parameters = new HashMap<>();
53 this.setUsername("");
54 this.setToken("");
55 this.setSecureRequired(false);
Michael Hanlc0ed00f2016-06-23 14:33:10 +020056 this.setExpirationTime(-1);
Michael Hanlca740d72015-06-16 10:04:58 +020057 }
58
Michael Hanl8abaf9e2016-05-23 16:46:35 +020059
60 private Map statusMap () {
Michael Hanlca740d72015-06-16 10:04:58 +020061 Map m = new HashMap();
62 if (username != null && !username.isEmpty())
63 m.put(Attributes.USERNAME, username);
64 m.put(Attributes.TOKEN_EXPIRATION,
Michael Hanl2c3b0b12016-07-01 18:30:12 +020065 TimeUtils.format(this.expirationTime));
Michael Hanlca740d72015-06-16 10:04:58 +020066 m.put(Attributes.TOKEN, this.token);
margaretha2afb97d2017-12-07 19:18:44 +010067 m.put(Attributes.TOKEN_TYPE, this.tokenType);
Michael Hanlca740d72015-06-16 10:04:58 +020068 return m;
69 }
70
Michael Hanl8abaf9e2016-05-23 16:46:35 +020071
72 public Map<String, Object> params () {
Michael Hanl19390652016-01-16 11:01:24 +010073 return new HashMap<>(parameters);
74 }
margaretha20f31232018-07-09 17:49:39 +020075
Michael Hanl8abaf9e2016-05-23 16:46:35 +020076 public boolean match (TokenContext other) {
Michael Hanlca740d72015-06-16 10:04:58 +020077 if (other.getToken().equals(this.token))
78 if (this.getHostAddress().equals(this.hostAddress))
margarethaa2ce63d2018-06-28 10:11:43 +020079 // user agent should be irrelvant -- what about os
80 // system version?
81 // if (other.getUserAgent().equals(this.userAgent))
Michael Hanlca740d72015-06-16 10:04:58 +020082 return true;
83 return false;
84 }
85
Michael Hanl8abaf9e2016-05-23 16:46:35 +020086
87 public void addContextParameter (String key, String value) {
Michael Hanlca740d72015-06-16 10:04:58 +020088 this.parameters.put(key, value);
89 }
90
Michael Hanl8abaf9e2016-05-23 16:46:35 +020091
92 public void addParams (Map<String, Object> map) {
Michael Hanlf1e85e72016-01-21 16:55:45 +010093 for (Map.Entry<String, Object> e : map.entrySet())
94 this.parameters.put(e.getKey(), String.valueOf(e.getValue()));
95 }
96
Michael Hanl8abaf9e2016-05-23 16:46:35 +020097
98 public void removeContextParameter (String key) {
Michael Hanlca740d72015-06-16 10:04:58 +020099 this.parameters.remove(key);
100 }
101
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200102
103 public void setExpirationTime (long date) {
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200104 this.expirationTime = date;
Michael Hanlca740d72015-06-16 10:04:58 +0200105 }
106
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200107
margarethaa2ce63d2018-06-28 10:11:43 +0200108 // todo: complete
margaretha894a7d72017-11-08 19:24:20 +0100109 public static TokenContext fromJSON (String s) throws KustvaktException {
Michael Hanlca740d72015-06-16 10:04:58 +0200110 JsonNode node = JsonUtils.readTree(s);
Michael Hanl482f30d2015-09-25 12:39:46 +0200111 TokenContext c = new TokenContext();
112 if (node != null) {
113 c.setUsername(node.path(Attributes.USERNAME).asText());
114 c.setToken(node.path(Attributes.TOKEN).asText());
115 }
Michael Hanlca740d72015-06-16 10:04:58 +0200116 return c;
117 }
118
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200119
margaretha894a7d72017-11-08 19:24:20 +0100120 public static TokenContext fromOAuth2 (String s) throws KustvaktException {
Michael Hanlca740d72015-06-16 10:04:58 +0200121 JsonNode node = JsonUtils.readTree(s);
122 TokenContext c = new TokenContext();
Michael Hanl482f30d2015-09-25 12:39:46 +0200123 if (node != null) {
124 c.setToken(node.path("token").asText());
margarethaa2ce63d2018-06-28 10:11:43 +0200125 c.setTokenType(TokenType.valueOf(node.path("token_type").asText()));
Michael Hanl482f30d2015-09-25 12:39:46 +0200126 c.setExpirationTime(node.path("expires_in").asLong());
margarethaa2ce63d2018-06-28 10:11:43 +0200127 c.addContextParameter("refresh_token",
128 node.path("refresh_token").asText());
Michael Hanl482f30d2015-09-25 12:39:46 +0200129
130 }
Michael Hanlca740d72015-06-16 10:04:58 +0200131 return c;
132 }
133
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200134
135 public boolean isValid () {
136 return (this.username != null && !this.username.isEmpty())
137 && (this.token != null && !this.token.isEmpty())
margaretha2afb97d2017-12-07 19:18:44 +0100138 && (this.tokenType != null);
Michael Hanl7368aa42016-02-05 18:15:47 +0100139 }
140
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200141
142 public String getToken () {
Michael Hanlca740d72015-06-16 10:04:58 +0200143 return token;
144 }
145
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200146
margarethaa2ce63d2018-06-28 10:11:43 +0200147 public String toJson () throws KustvaktException {
Michael Hanlca740d72015-06-16 10:04:58 +0200148 return JsonUtils.toJSON(this.statusMap());
149 }
150
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200151
margarethaa2ce63d2018-06-28 10:11:43 +0200152 public boolean isDemo () {
Michael Hanl99cb9632016-06-29 16:24:40 +0200153 return User.UserFactory.isDemo(this.username);
154 }
155
156
157
Michael Hanlca740d72015-06-16 10:04:58 +0200158 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200159 public String getName () {
Michael Hanlca740d72015-06-16 10:04:58 +0200160 return this.getUsername();
161 }
162
margarethaa2ce63d2018-06-28 10:11:43 +0200163
164 public ZonedDateTime getAuthenticationTime () {
165 return authenticationTime;
166 }
167
168
169 public void setAuthenticationTime (ZonedDateTime authTime) {
170 this.authenticationTime = authTime;
171 }
172
Michael Hanlca740d72015-06-16 10:04:58 +0200173}