| margaretha | 064eb6f | 2018-07-10 18:33:01 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.oauth2.dao; |
| 2 | |
| margaretha | 6ad08b4 | 2018-08-22 18:33:54 +0200 | [diff] [blame^] | 3 | import java.time.ZoneId; |
| margaretha | 064eb6f | 2018-07-10 18:33:01 +0200 | [diff] [blame] | 4 | import java.time.ZonedDateTime; |
| margaretha | 7f5071f | 2018-08-14 15:58:51 +0200 | [diff] [blame] | 5 | import java.util.ArrayList; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| margaretha | 064eb6f | 2018-07-10 18:33:01 +0200 | [diff] [blame] | 8 | import java.util.Set; |
| 9 | |
| margaretha | 6ad08b4 | 2018-08-22 18:33:54 +0200 | [diff] [blame^] | 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | |
| 12 | import de.ids_mannheim.korap.config.Attributes; |
| 13 | import de.ids_mannheim.korap.config.FullConfiguration; |
| margaretha | 064eb6f | 2018-07-10 18:33:01 +0200 | [diff] [blame] | 14 | import de.ids_mannheim.korap.config.KustvaktCacheable; |
| 15 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 16 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 17 | import de.ids_mannheim.korap.oauth2.constant.OAuth2Error; |
| 18 | import de.ids_mannheim.korap.oauth2.entity.AccessScope; |
| 19 | import de.ids_mannheim.korap.oauth2.entity.Authorization; |
| 20 | import de.ids_mannheim.korap.oauth2.interfaces.AuthorizationDaoInterface; |
| 21 | import de.ids_mannheim.korap.utils.ParameterChecker; |
| margaretha | 7f5071f | 2018-08-14 15:58:51 +0200 | [diff] [blame] | 22 | import net.sf.ehcache.Element; |
| margaretha | 064eb6f | 2018-07-10 18:33:01 +0200 | [diff] [blame] | 23 | |
| 24 | public class AuthorizationCacheDao extends KustvaktCacheable |
| 25 | implements AuthorizationDaoInterface { |
| 26 | |
| margaretha | 6ad08b4 | 2018-08-22 18:33:54 +0200 | [diff] [blame^] | 27 | @Autowired |
| 28 | private FullConfiguration config; |
| 29 | |
| margaretha | 064eb6f | 2018-07-10 18:33:01 +0200 | [diff] [blame] | 30 | 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); |
| margaretha | 6ad08b4 | 2018-08-22 18:33:54 +0200 | [diff] [blame^] | 54 | |
| 55 | ZonedDateTime now = |
| 56 | ZonedDateTime.now(ZoneId.of(Attributes.DEFAULT_TIME_ZONE)); |
| 57 | authorization.setCreatedDate(now); |
| 58 | authorization.setExpiryDate( |
| 59 | now.plusSeconds(config.getAuthorizationCodeExpiry())); |
| margaretha | 064eb6f | 2018-07-10 18:33:01 +0200 | [diff] [blame] | 60 | |
| 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 | |
| margaretha | 7f5071f | 2018-08-14 15:58:51 +0200 | [diff] [blame] | 89 | @Override |
| 90 | public List<Authorization> retrieveAuthorizationsByClientId ( |
| 91 | String clientId) { |
| 92 | List<Authorization> authList = new ArrayList<>(); |
| margaretha | 6ad08b4 | 2018-08-22 18:33:54 +0200 | [diff] [blame^] | 93 | |
| margaretha | 7f5071f | 2018-08-14 15:58:51 +0200 | [diff] [blame] | 94 | Map<Object, Element> map = getAllCacheElements(); |
| margaretha | 6ad08b4 | 2018-08-22 18:33:54 +0200 | [diff] [blame^] | 95 | for (Object key : map.keySet()) { |
| 96 | Authorization auth = (Authorization) map.get(key).getObjectValue(); |
| 97 | if (auth.getClientId().equals(clientId)) { |
| margaretha | 7f5071f | 2018-08-14 15:58:51 +0200 | [diff] [blame] | 98 | authList.add(auth); |
| 99 | } |
| 100 | } |
| 101 | return authList; |
| 102 | } |
| 103 | |
| margaretha | 064eb6f | 2018-07-10 18:33:01 +0200 | [diff] [blame] | 104 | } |