| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.security.auth; |
| 2 | |
| 3 | import com.nimbusds.jwt.SignedJWT; |
| 4 | import de.ids_mannheim.korap.config.JWTSigner; |
| 5 | import de.ids_mannheim.korap.config.KustvaktConfiguration; |
| 6 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 7 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 8 | import de.ids_mannheim.korap.handlers.OAuthDb; |
| 9 | import de.ids_mannheim.korap.interfaces.AuthenticationIface; |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 10 | import de.ids_mannheim.korap.interfaces.db.PersistenceClient; |
| Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 11 | import de.ids_mannheim.korap.config.Attributes; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 12 | import de.ids_mannheim.korap.user.TokenContext; |
| 13 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 14 | import de.ids_mannheim.korap.utils.NamingUtils; |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 15 | import de.ids_mannheim.korap.utils.StringUtils; |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 16 | import net.sf.ehcache.CacheManager; |
| 17 | import net.sf.ehcache.Element; |
| 18 | import org.springframework.cache.annotation.CacheEvict; |
| 19 | import org.springframework.cache.annotation.Cacheable; |
| 20 | |
| 21 | import java.text.ParseException; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | /** |
| 25 | * @author hanl |
| 26 | * @date 12/11/2014 |
| 27 | */ |
| 28 | public class OpenIDconnectAuthentication implements AuthenticationIface { |
| 29 | |
| 30 | private OAuthDb database; |
| 31 | private KustvaktConfiguration config; |
| 32 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 33 | |
| 34 | public OpenIDconnectAuthentication (KustvaktConfiguration config, |
| 35 | PersistenceClient client) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 36 | this.database = new OAuthDb(client); |
| 37 | this.config = config; |
| 38 | } |
| 39 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 40 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 41 | @Override |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 42 | public TokenContext getTokenContext(String authToken) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 43 | throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 44 | return this.database.getContext(authToken); |
| 45 | } |
| 46 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 47 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 48 | @Override |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 49 | public TokenContext createTokenContext(User user, Map<String, Object> attr) |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 50 | throws KustvaktException { |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 51 | String cl_secret = (String) attr.get(Attributes.CLIENT_SECRET); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 52 | if (cl_secret == null) |
| 53 | throw new KustvaktException(StatusCodes.REQUEST_INVALID); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 54 | attr.remove(cl_secret); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 55 | JWTSigner signer = new JWTSigner(cl_secret.getBytes(), |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 56 | config.getIssuer(), config.getTokenTTL()); |
| Michael Hanl | e25dea2 | 2015-09-24 19:37:56 +0200 | [diff] [blame] | 57 | TokenContext c = new TokenContext(); |
| 58 | c.setUsername(user.getUsername()); |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 59 | SignedJWT jwt = signer.createJWT(user, attr); |
| 60 | try { |
| 61 | c.setExpirationTime(jwt.getJWTClaimsSet().getExpirationTimeClaim()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 62 | } |
| 63 | catch (ParseException e) { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 64 | throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT); |
| 65 | } |
| 66 | c.setTokenType(Attributes.OPENID_AUTHENTICATION); |
| 67 | c.setToken(jwt.serialize()); |
| 68 | CacheManager.getInstance().getCache("id_tokens") |
| 69 | .put(new Element(c.getToken(), c)); |
| 70 | return c; |
| 71 | } |
| 72 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 73 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 74 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 75 | public void removeUserSession (String token) throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 76 | // emit token from cache only |
| 77 | } |
| 78 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 79 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 80 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 81 | public TokenContext refresh (TokenContext context) throws KustvaktException { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 82 | throw new UnsupportedOperationException("method not supported"); |
| 83 | } |
| 84 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 85 | |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 86 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 87 | public String getIdentifier () { |
| Michael Hanl | 87106d1 | 2015-09-14 18:13:51 +0200 | [diff] [blame] | 88 | return Attributes.OPENID_AUTHENTICATION; |
| 89 | } |
| 90 | } |