blob: 28e848b3332e698c5a1498497e9d256019cd101e [file] [log] [blame]
Michael Hanl72c7b832015-09-03 08:42:15 +02001package de.ids_mannheim.korap.handlers;
2
3import de.ids_mannheim.korap.config.AuthCodeInfo;
Michael Hanlc0ed00f2016-06-23 14:33:10 +02004import de.ids_mannheim.korap.config.ClientInfo;
5import de.ids_mannheim.korap.config.KustvaktCacheable;
Michael Hanl72c7b832015-09-03 08:42:15 +02006import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanlf21773f2015-10-16 23:02:31 +02007import de.ids_mannheim.korap.interfaces.db.PersistenceClient;
Michael Hanl72c7b832015-09-03 08:42:15 +02008import de.ids_mannheim.korap.user.User;
Michael Hanl72c7b832015-09-03 08:42:15 +02009
10/**
Michael Hanl482f30d2015-09-25 12:39:46 +020011 * extends OAuthDb to allow temporary caching of tokens
Michael Hanlc0ed00f2016-06-23 14:33:10 +020012 * and authorization codes.
13 * Authorization codes are not persisted in db,
14 * but stored in file of ehcache
Michael Hanl8abaf9e2016-05-23 16:46:35 +020015 *
Michael Hanl72c7b832015-09-03 08:42:15 +020016 * @author hanl
17 * @date 04/05/2015
18 */
Michael Hanlc0ed00f2016-06-23 14:33:10 +020019public class OAuth2Handler extends KustvaktCacheable {
Michael Hanl72c7b832015-09-03 08:42:15 +020020
Michael Hanlc0ed00f2016-06-23 14:33:10 +020021 private OAuthDb oauthdb;
Michael Hanl8abaf9e2016-05-23 16:46:35 +020022
23 public OAuth2Handler (PersistenceClient client) {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020024 super("auth_codes", "key:auth_codes");
25 this.oauthdb = new OAuthDb(client);
Michael Hanl72c7b832015-09-03 08:42:15 +020026 }
27
Michael Hanl8abaf9e2016-05-23 16:46:35 +020028
Michael Hanlc0ed00f2016-06-23 14:33:10 +020029 // fixme: caching should not be obligatory here. alternative to caching if not available?
Michael Hanl8abaf9e2016-05-23 16:46:35 +020030 public AuthCodeInfo getAuthorization (String code) {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020031 Object value = this.getCacheValue(code);
32 if (value != null)
33 return (AuthCodeInfo) value;
Michael Hanl72c7b832015-09-03 08:42:15 +020034 return null;
35 }
36
Michael Hanl8abaf9e2016-05-23 16:46:35 +020037
Bodmo3d6bd352017-04-25 11:31:39 +020038 public void authorize (AuthCodeInfo info, User user) throws KustvaktException {
39
Michael Hanlc0ed00f2016-06-23 14:33:10 +020040 info.setUserId(user.getId());
41 this.storeInCache(info.getCode(), info);
Michael Hanl72c7b832015-09-03 08:42:15 +020042 }
43
Michael Hanl8abaf9e2016-05-23 16:46:35 +020044
45 public boolean addToken (String code, String token, String refresh, int ttl)
Michael Hanl72c7b832015-09-03 08:42:15 +020046 throws KustvaktException {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020047 Object o = this.getCacheValue(code);
48 if (o != null) {
49 AuthCodeInfo info = (AuthCodeInfo) o;
50 this.removeCacheEntry(code);
51 return oauthdb.addToken(token, refresh, info.getUserId(),
Michael Hanl8abaf9e2016-05-23 16:46:35 +020052 info.getClientId(), info.getScopes(), ttl);
Michael Hanl72c7b832015-09-03 08:42:15 +020053 }
54 return false;
55 }
56
Michael Hanl8abaf9e2016-05-23 16:46:35 +020057
58 public void exchangeToken (String refresh) {
Michael Hanlc0ed00f2016-06-23 14:33:10 +020059 // todo:
60 }
Michael Hanl482f30d2015-09-25 12:39:46 +020061
Michael Hanlc0ed00f2016-06-23 14:33:10 +020062 public OAuthDb getPersistenceHandler(){
63 return this.oauthdb;
Michael Hanl482f30d2015-09-25 12:39:46 +020064 }
65
Michael Hanl72c7b832015-09-03 08:42:15 +020066}