blob: 10b60ee2eba25cf7925f3d66b4e74f241adcb561 [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;
margaretha2afb97d2017-12-07 19:18:44 +01005import java.util.HashMap;
6import java.util.Map;
7
Michael Hanlca740d72015-06-16 10:04:58 +02008import com.fasterxml.jackson.databind.JsonNode;
margaretha2afb97d2017-12-07 19:18:44 +01009
Michael Hanl00b64e02016-05-24 20:24:27 +020010import de.ids_mannheim.korap.config.Attributes;
margaretha0e8f4e72018-04-05 14:11:52 +020011import de.ids_mannheim.korap.constant.TokenType;
margaretha894a7d72017-11-08 19:24:20 +010012import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha0e8f4e72018-04-05 14:11:52 +020013import de.ids_mannheim.korap.user.User;
Michael Hanlca740d72015-06-16 10:04:58 +020014import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020015import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanl19390652016-01-16 11:01:24 +010016import lombok.AccessLevel;
Michael Hanlca740d72015-06-16 10:04:58 +020017import lombok.Data;
Michael Hanlf1e85e72016-01-21 16:55:45 +010018import lombok.Setter;
Michael Hanlca740d72015-06-16 10:04:58 +020019
20/**
margaretha35e1ca22023-11-16 22:00:01 +010021 * EM:
margarethaa2ce63d2018-06-28 10:11:43 +020022 * - change datatype of tokenType from string to enum
23 * - added authenticationTime
24 *
Michael Hanlca740d72015-06-16 10:04:58 +020025 * @author hanl
26 * @date 27/01/2014
27 */
28@Data
Michael Hanlf1e85e72016-01-21 16:55:45 +010029public class TokenContext implements java.security.Principal, Serializable {
Michael Hanlca740d72015-06-16 10:04:58 +020030
margarethaa2ce63d2018-06-28 10:11:43 +020031 private ZonedDateTime authenticationTime;
Michael Hanlca740d72015-06-16 10:04:58 +020032 /**
33 * session relevant data. Are never persisted into a database
34 */
35 private String username;
Michael Hanlc0ed00f2016-06-23 14:33:10 +020036 private long expirationTime;
Michael Hanlca740d72015-06-16 10:04:58 +020037 // either "session_token " / "api_token
margaretha2afb97d2017-12-07 19:18:44 +010038 private TokenType tokenType;
Michael Hanlca740d72015-06-16 10:04:58 +020039 private String token;
Michael Hanlca740d72015-06-16 10:04:58 +020040 private boolean secureRequired;
41
margaretha35e1ca22023-11-16 22:00:01 +010042 // @Getter(AccessLevel.PRIVATE)
Michael Hanlf1e85e72016-01-21 16:55:45 +010043 @Setter(AccessLevel.PRIVATE)
Michael Hanl5fac8ab2016-01-29 16:33:04 +010044 private Map<String, Object> parameters;
Michael Hanlca740d72015-06-16 10:04:58 +020045 private String hostAddress;
46 private String userAgent;
47
Michael Hanl8abaf9e2016-05-23 16:46:35 +020048 public TokenContext () {
Michael Hanlca740d72015-06-16 10:04:58 +020049 this.parameters = new HashMap<>();
50 this.setUsername("");
51 this.setToken("");
52 this.setSecureRequired(false);
Michael Hanlc0ed00f2016-06-23 14:33:10 +020053 this.setExpirationTime(-1);
Michael Hanlca740d72015-06-16 10:04:58 +020054 }
55
Michael Hanl8abaf9e2016-05-23 16:46:35 +020056 private Map statusMap () {
Michael Hanlca740d72015-06-16 10:04:58 +020057 Map m = new HashMap();
58 if (username != null && !username.isEmpty())
59 m.put(Attributes.USERNAME, username);
60 m.put(Attributes.TOKEN_EXPIRATION,
Michael Hanl2c3b0b12016-07-01 18:30:12 +020061 TimeUtils.format(this.expirationTime));
Michael Hanlca740d72015-06-16 10:04:58 +020062 m.put(Attributes.TOKEN, this.token);
margaretha2afb97d2017-12-07 19:18:44 +010063 m.put(Attributes.TOKEN_TYPE, this.tokenType);
Michael Hanlca740d72015-06-16 10:04:58 +020064 return m;
65 }
66
Michael Hanl8abaf9e2016-05-23 16:46:35 +020067 public Map<String, Object> params () {
Michael Hanl19390652016-01-16 11:01:24 +010068 return new HashMap<>(parameters);
69 }
margaretha35e1ca22023-11-16 22:00:01 +010070
Michael Hanl8abaf9e2016-05-23 16:46:35 +020071 public boolean match (TokenContext other) {
Michael Hanlca740d72015-06-16 10:04:58 +020072 if (other.getToken().equals(this.token))
73 if (this.getHostAddress().equals(this.hostAddress))
margarethaa2ce63d2018-06-28 10:11:43 +020074 // user agent should be irrelvant -- what about os
75 // system version?
76 // if (other.getUserAgent().equals(this.userAgent))
Michael Hanlca740d72015-06-16 10:04:58 +020077 return true;
78 return false;
79 }
80
Michael Hanl8abaf9e2016-05-23 16:46:35 +020081 public void addContextParameter (String key, String value) {
Michael Hanlca740d72015-06-16 10:04:58 +020082 this.parameters.put(key, value);
83 }
84
Michael Hanl8abaf9e2016-05-23 16:46:35 +020085 public void addParams (Map<String, Object> map) {
Michael Hanlf1e85e72016-01-21 16:55:45 +010086 for (Map.Entry<String, Object> e : map.entrySet())
87 this.parameters.put(e.getKey(), String.valueOf(e.getValue()));
88 }
89
Michael Hanl8abaf9e2016-05-23 16:46:35 +020090 public void removeContextParameter (String key) {
Michael Hanlca740d72015-06-16 10:04:58 +020091 this.parameters.remove(key);
92 }
93
Michael Hanl8abaf9e2016-05-23 16:46:35 +020094 public void setExpirationTime (long date) {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020095 this.expirationTime = date;
Michael Hanlca740d72015-06-16 10:04:58 +020096 }
97
margarethaa2ce63d2018-06-28 10:11:43 +020098 // todo: complete
margaretha894a7d72017-11-08 19:24:20 +010099 public static TokenContext fromJSON (String s) throws KustvaktException {
Michael Hanlca740d72015-06-16 10:04:58 +0200100 JsonNode node = JsonUtils.readTree(s);
Michael Hanl482f30d2015-09-25 12:39:46 +0200101 TokenContext c = new TokenContext();
102 if (node != null) {
103 c.setUsername(node.path(Attributes.USERNAME).asText());
104 c.setToken(node.path(Attributes.TOKEN).asText());
105 }
Michael Hanlca740d72015-06-16 10:04:58 +0200106 return c;
107 }
108
margaretha894a7d72017-11-08 19:24:20 +0100109 public static TokenContext fromOAuth2 (String s) throws KustvaktException {
Michael Hanlca740d72015-06-16 10:04:58 +0200110 JsonNode node = JsonUtils.readTree(s);
111 TokenContext c = new TokenContext();
Michael Hanl482f30d2015-09-25 12:39:46 +0200112 if (node != null) {
113 c.setToken(node.path("token").asText());
margarethaa2ce63d2018-06-28 10:11:43 +0200114 c.setTokenType(TokenType.valueOf(node.path("token_type").asText()));
Michael Hanl482f30d2015-09-25 12:39:46 +0200115 c.setExpirationTime(node.path("expires_in").asLong());
margarethaa2ce63d2018-06-28 10:11:43 +0200116 c.addContextParameter("refresh_token",
117 node.path("refresh_token").asText());
Michael Hanl482f30d2015-09-25 12:39:46 +0200118
119 }
Michael Hanlca740d72015-06-16 10:04:58 +0200120 return c;
121 }
122
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200123 public boolean isValid () {
124 return (this.username != null && !this.username.isEmpty())
125 && (this.token != null && !this.token.isEmpty())
margaretha2afb97d2017-12-07 19:18:44 +0100126 && (this.tokenType != null);
Michael Hanl7368aa42016-02-05 18:15:47 +0100127 }
128
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200129 public String getToken () {
Michael Hanlca740d72015-06-16 10:04:58 +0200130 return token;
131 }
132
margarethaa2ce63d2018-06-28 10:11:43 +0200133 public String toJson () throws KustvaktException {
Michael Hanlca740d72015-06-16 10:04:58 +0200134 return JsonUtils.toJSON(this.statusMap());
135 }
136
margarethaa2ce63d2018-06-28 10:11:43 +0200137 public boolean isDemo () {
Michael Hanl99cb9632016-06-29 16:24:40 +0200138 return User.UserFactory.isDemo(this.username);
139 }
140
Michael Hanlca740d72015-06-16 10:04:58 +0200141 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200142 public String getName () {
Michael Hanlca740d72015-06-16 10:04:58 +0200143 return this.getUsername();
144 }
145
margarethaa2ce63d2018-06-28 10:11:43 +0200146 public ZonedDateTime getAuthenticationTime () {
147 return authenticationTime;
148 }
149
margarethaa2ce63d2018-06-28 10:11:43 +0200150 public void setAuthenticationTime (ZonedDateTime authTime) {
151 this.authenticationTime = authTime;
152 }
153
Michael Hanlca740d72015-06-16 10:04:58 +0200154}