blob: 1d571aea5d995e78748545bedfddb6372b4ba7e5 [file] [log] [blame]
Michael Hanl72c7b832015-09-03 08:42:15 +02001package de.ids_mannheim.korap.handlers;
2
3import de.ids_mannheim.korap.config.AuthCodeInfo;
4import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanlf21773f2015-10-16 23:02:31 +02005import de.ids_mannheim.korap.interfaces.db.PersistenceClient;
Michael Hanl72c7b832015-09-03 08:42:15 +02006import de.ids_mannheim.korap.user.User;
7import net.sf.ehcache.Cache;
8import net.sf.ehcache.CacheManager;
9import net.sf.ehcache.Element;
10
11/**
Michael Hanl482f30d2015-09-25 12:39:46 +020012 * extends OAuthDb to allow temporary caching of tokens
13 * and authorizations (authorizations are not persisted in db)
14 *
Michael Hanl72c7b832015-09-03 08:42:15 +020015 * @author hanl
16 * @date 04/05/2015
17 */
18public class OAuth2Handler extends OAuthDb {
19
20 private Cache cache;
21
22 public OAuth2Handler(PersistenceClient client) {
23 super(client);
24 this.cache = CacheManager.getInstance().getCache("auth_codes");
25 }
26
27 public AuthCodeInfo getAuthorization(String code) {
28 Element e = this.cache.get(code);
29 if (e != null)
30 return (AuthCodeInfo) e.getObjectValue();
31 return null;
32 }
33
Michael Hanl482f30d2015-09-25 12:39:46 +020034 public void authorize(AuthCodeInfo code, User user)
35 throws KustvaktException {
Michael Hanl72c7b832015-09-03 08:42:15 +020036 code.setUserId(user.getId());
37 cache.put(new Element(code.getCode(), code));
38 }
39
Michael Hanl482f30d2015-09-25 12:39:46 +020040 public boolean addToken(String code, String token, String refresh, int ttl)
Michael Hanl72c7b832015-09-03 08:42:15 +020041 throws KustvaktException {
42 Element e = cache.get(code);
43 if (e != null) {
44 AuthCodeInfo info = (AuthCodeInfo) e.getObjectValue();
45 cache.remove(code);
Michael Hanl482f30d2015-09-25 12:39:46 +020046 return super.addToken(token, refresh, info.getUserId(), info.getClientId(),
Michael Hanl72c7b832015-09-03 08:42:15 +020047 info.getScopes(), ttl);
48 }
49 return false;
50 }
51
Michael Hanl482f30d2015-09-25 12:39:46 +020052 public void exchangeToken(String refresh) {
53
54 }
55
Michael Hanl72c7b832015-09-03 08:42:15 +020056}