| 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; |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 4 | import de.ids_mannheim.korap.config.ClientInfo; |
| 5 | import de.ids_mannheim.korap.config.KustvaktCacheable; |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 6 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 7 | import de.ids_mannheim.korap.interfaces.db.PersistenceClient; |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 8 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 9 | |
| 10 | /** |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 11 | * extends OAuthDb to allow temporary caching of tokens |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 12 | * and authorization codes. |
| 13 | * Authorization codes are not persisted in db, |
| 14 | * but stored in file of ehcache |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 15 | * |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 16 | * @author hanl |
| 17 | * @date 04/05/2015 |
| 18 | */ |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 19 | public class OAuth2Handler extends KustvaktCacheable { |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 20 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 21 | private OAuthDb oauthdb; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 22 | |
| 23 | public OAuth2Handler (PersistenceClient client) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 24 | super("auth_codes", "key:auth_codes"); |
| 25 | this.oauthdb = new OAuthDb(client); |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 26 | } |
| 27 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 28 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 29 | // fixme: caching should not be obligatory here. alternative to caching if not available? |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 30 | public AuthCodeInfo getAuthorization (String code) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 31 | Object value = this.getCacheValue(code); |
| 32 | if (value != null) |
| 33 | return (AuthCodeInfo) value; |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 34 | return null; |
| 35 | } |
| 36 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 37 | |
| Bodmo | 3d6bd35 | 2017-04-25 11:31:39 +0200 | [diff] [blame] | 38 | public void authorize (AuthCodeInfo info, User user) throws KustvaktException { |
| 39 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 40 | info.setUserId(user.getId()); |
| 41 | this.storeInCache(info.getCode(), info); |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 44 | |
| 45 | public boolean addToken (String code, String token, String refresh, int ttl) |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 46 | throws KustvaktException { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 47 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 52 | info.getClientId(), info.getScopes(), ttl); |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 57 | |
| 58 | public void exchangeToken (String refresh) { |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 59 | // todo: |
| 60 | } |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 61 | |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 62 | public OAuthDb getPersistenceHandler(){ |
| 63 | return this.oauthdb; |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 66 | } |