blob: ee2b13079f2c4ac1da3af700d80b5e1dbf85fafd [file] [log] [blame]
margaretha064eb6f2018-07-10 18:33:01 +02001package de.ids_mannheim.korap.oauth2.dao;
2
margaretha6ad08b42018-08-22 18:33:54 +02003import java.time.ZoneId;
margaretha064eb6f2018-07-10 18:33:01 +02004import java.time.ZonedDateTime;
margaretha7f5071f2018-08-14 15:58:51 +02005import java.util.ArrayList;
6import java.util.List;
7import java.util.Map;
margaretha064eb6f2018-07-10 18:33:01 +02008import java.util.Set;
9
margaretha6ad08b42018-08-22 18:33:54 +020010import org.springframework.beans.factory.annotation.Autowired;
11
12import de.ids_mannheim.korap.config.Attributes;
13import de.ids_mannheim.korap.config.FullConfiguration;
margaretha064eb6f2018-07-10 18:33:01 +020014import de.ids_mannheim.korap.config.KustvaktCacheable;
15import de.ids_mannheim.korap.exceptions.KustvaktException;
16import de.ids_mannheim.korap.exceptions.StatusCodes;
17import de.ids_mannheim.korap.oauth2.constant.OAuth2Error;
18import de.ids_mannheim.korap.oauth2.entity.AccessScope;
19import de.ids_mannheim.korap.oauth2.entity.Authorization;
20import de.ids_mannheim.korap.oauth2.interfaces.AuthorizationDaoInterface;
21import de.ids_mannheim.korap.utils.ParameterChecker;
margaretha7f5071f2018-08-14 15:58:51 +020022import net.sf.ehcache.Element;
margaretha064eb6f2018-07-10 18:33:01 +020023
24public class AuthorizationCacheDao extends KustvaktCacheable
25 implements AuthorizationDaoInterface {
26
margaretha6ad08b42018-08-22 18:33:54 +020027 @Autowired
28 private FullConfiguration config;
29
margaretha064eb6f2018-07-10 18:33:01 +020030 public AuthorizationCacheDao () {
31 super("authorization", "key:authorization");
32 }
33
34 @Override
35 public Authorization storeAuthorizationCode (String clientId, String userId,
36 String code, Set<AccessScope> scopes, String redirectURI,
37 ZonedDateTime authenticationTime, String nonce)
38 throws KustvaktException {
39 ParameterChecker.checkStringValue(clientId, "client_id");
40 ParameterChecker.checkStringValue(userId, "userId");
41 ParameterChecker.checkStringValue(code, "authorization code");
42 ParameterChecker.checkCollection(scopes, "scopes");
43 ParameterChecker.checkObjectValue(authenticationTime,
44 "user authentication time");
45
46 Authorization authorization = new Authorization();
47 authorization.setCode(code);
48 authorization.setClientId(clientId);
49 authorization.setUserId(userId);
50 authorization.setScopes(scopes);
51 authorization.setRedirectURI(redirectURI);
52 authorization.setUserAuthenticationTime(authenticationTime);
53 authorization.setNonce(nonce);
margaretha6ad08b42018-08-22 18:33:54 +020054
55 ZonedDateTime now =
56 ZonedDateTime.now(ZoneId.of(Attributes.DEFAULT_TIME_ZONE));
57 authorization.setCreatedDate(now);
58 authorization.setExpiryDate(
59 now.plusSeconds(config.getAuthorizationCodeExpiry()));
margaretha064eb6f2018-07-10 18:33:01 +020060
61 this.storeInCache(code, authorization);
62 return authorization;
63 }
64
65 @Override
66 public Authorization retrieveAuthorizationCode (String code)
67 throws KustvaktException {
68
69 Object auth = this.getCacheValue(code);
70 if (auth != null) {
71 return (Authorization) auth;
72 }
73 else {
74 throw new KustvaktException(StatusCodes.INVALID_AUTHORIZATION,
75 "Authorization is invalid.", OAuth2Error.INVALID_REQUEST);
76 }
77 }
78
79 @Override
80 public Authorization updateAuthorization (Authorization authorization)
81 throws KustvaktException {
82
83 this.storeInCache(authorization.getCode(), authorization);
84 Authorization auth =
85 (Authorization) this.getCacheValue(authorization.getCode());
86 return auth;
87 }
88
margaretha7f5071f2018-08-14 15:58:51 +020089 @Override
90 public List<Authorization> retrieveAuthorizationsByClientId (
91 String clientId) {
92 List<Authorization> authList = new ArrayList<>();
margaretha6ad08b42018-08-22 18:33:54 +020093
margaretha7f5071f2018-08-14 15:58:51 +020094 Map<Object, Element> map = getAllCacheElements();
margaretha6ad08b42018-08-22 18:33:54 +020095 for (Object key : map.keySet()) {
96 Authorization auth = (Authorization) map.get(key).getObjectValue();
97 if (auth.getClientId().equals(clientId)) {
margaretha7f5071f2018-08-14 15:58:51 +020098 authList.add(auth);
99 }
100 }
101 return authList;
102 }
103
margaretha064eb6f2018-07-10 18:33:01 +0200104}