| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.handlers; |
| 2 | |
| 3 | import de.ids_mannheim.korap.config.AuthCodeInfo; |
| 4 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame^] | 5 | import de.ids_mannheim.korap.interfaces.db.PersistenceClient; |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 6 | import de.ids_mannheim.korap.user.User; |
| 7 | import net.sf.ehcache.Cache; |
| 8 | import net.sf.ehcache.CacheManager; |
| 9 | import net.sf.ehcache.Element; |
| 10 | |
| 11 | /** |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 12 | * extends OAuthDb to allow temporary caching of tokens |
| 13 | * and authorizations (authorizations are not persisted in db) |
| 14 | * |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 15 | * @author hanl |
| 16 | * @date 04/05/2015 |
| 17 | */ |
| 18 | public 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 Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 34 | public void authorize(AuthCodeInfo code, User user) |
| 35 | throws KustvaktException { |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 36 | code.setUserId(user.getId()); |
| 37 | cache.put(new Element(code.getCode(), code)); |
| 38 | } |
| 39 | |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 40 | public boolean addToken(String code, String token, String refresh, int ttl) |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 41 | throws KustvaktException { |
| 42 | Element e = cache.get(code); |
| 43 | if (e != null) { |
| 44 | AuthCodeInfo info = (AuthCodeInfo) e.getObjectValue(); |
| 45 | cache.remove(code); |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 46 | return super.addToken(token, refresh, info.getUserId(), info.getClientId(), |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 47 | info.getScopes(), ttl); |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 52 | public void exchangeToken(String refresh) { |
| 53 | |
| 54 | } |
| 55 | |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 56 | } |