ClientKeyCacheService.java

  1. /*******************************************************************************
  2.  * Copyright 2017 The MIT Internet Trust Consortium
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *******************************************************************************/

  16. package org.mitre.jwt.signer.service.impl;

  17. import java.util.concurrent.ExecutionException;
  18. import java.util.concurrent.TimeUnit;

  19. import org.mitre.jose.keystore.JWKSetKeyStore;
  20. import org.mitre.jwt.encryption.service.JWTEncryptionAndDecryptionService;
  21. import org.mitre.jwt.encryption.service.impl.DefaultJWTEncryptionAndDecryptionService;
  22. import org.mitre.jwt.signer.service.JWTSigningAndValidationService;
  23. import org.mitre.oauth2.model.ClientDetailsEntity;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Service;

  28. import com.google.common.base.Strings;
  29. import com.google.common.cache.CacheBuilder;
  30. import com.google.common.cache.CacheLoader;
  31. import com.google.common.cache.LoadingCache;
  32. import com.google.common.util.concurrent.UncheckedExecutionException;
  33. import com.nimbusds.jose.JWSAlgorithm;
  34. import com.nimbusds.jose.jwk.JWKSet;

  35. /**
  36.  *
  37.  * Takes in a client and returns the appropriate validator or encrypter for
  38.  * that client's registered key types.
  39.  *
  40.  * @author jricher
  41.  *
  42.  */
  43. @Service
  44. public class ClientKeyCacheService {

  45.     private static Logger logger = LoggerFactory.getLogger(ClientKeyCacheService.class);

  46.     @Autowired
  47.     private JWKSetCacheService jwksUriCache = new JWKSetCacheService();

  48.     @Autowired
  49.     private SymmetricKeyJWTValidatorCacheService symmetricCache = new SymmetricKeyJWTValidatorCacheService();

  50.     // cache of validators for by-value JWKs
  51.     private LoadingCache<JWKSet, JWTSigningAndValidationService> jwksValidators;

  52.     // cache of encryptors for by-value JWKs
  53.     private LoadingCache<JWKSet, JWTEncryptionAndDecryptionService> jwksEncrypters;

  54.     public ClientKeyCacheService() {
  55.         this.jwksValidators = CacheBuilder.newBuilder()
  56.                 .expireAfterWrite(1, TimeUnit.HOURS) // expires 1 hour after fetch
  57.                 .maximumSize(100)
  58.                 .build(new JWKSetVerifierBuilder());
  59.         this.jwksEncrypters = CacheBuilder.newBuilder()
  60.                 .expireAfterWrite(1, TimeUnit.HOURS) // expires 1 hour after fetch
  61.                 .maximumSize(100)
  62.                 .build(new JWKSetEncryptorBuilder());
  63.     }


  64.     public JWTSigningAndValidationService getValidator(ClientDetailsEntity client, JWSAlgorithm alg) {

  65.         try {
  66.             if (alg.equals(JWSAlgorithm.RS256)
  67.                     || alg.equals(JWSAlgorithm.RS384)
  68.                     || alg.equals(JWSAlgorithm.RS512)
  69.                     || alg.equals(JWSAlgorithm.ES256)
  70.                     || alg.equals(JWSAlgorithm.ES384)
  71.                     || alg.equals(JWSAlgorithm.ES512)
  72.                     || alg.equals(JWSAlgorithm.PS256)
  73.                     || alg.equals(JWSAlgorithm.PS384)
  74.                     || alg.equals(JWSAlgorithm.PS512)) {

  75.                 // asymmetric key
  76.                 if (client.getJwks() != null) {
  77.                     return jwksValidators.get(client.getJwks());
  78.                 } else if (!Strings.isNullOrEmpty(client.getJwksUri())) {
  79.                     return jwksUriCache.getValidator(client.getJwksUri());
  80.                 } else {
  81.                     return null;
  82.                 }

  83.             } else if (alg.equals(JWSAlgorithm.HS256)
  84.                     || alg.equals(JWSAlgorithm.HS384)
  85.                     || alg.equals(JWSAlgorithm.HS512)) {

  86.                 // symmetric key

  87.                 return symmetricCache.getSymmetricValidtor(client);

  88.             } else {

  89.                 return null;
  90.             }
  91.         } catch (UncheckedExecutionException | ExecutionException e) {
  92.             logger.error("Problem loading client validator", e);
  93.             return null;
  94.         }

  95.     }

  96.     public JWTEncryptionAndDecryptionService getEncrypter(ClientDetailsEntity client) {

  97.         try {
  98.             if (client.getJwks() != null) {
  99.                 return jwksEncrypters.get(client.getJwks());
  100.             } else if (!Strings.isNullOrEmpty(client.getJwksUri())) {
  101.                 return jwksUriCache.getEncrypter(client.getJwksUri());
  102.             } else {
  103.                 return null;
  104.             }
  105.         } catch (UncheckedExecutionException | ExecutionException e) {
  106.             logger.error("Problem loading client encrypter", e);
  107.             return null;
  108.         }

  109.     }


  110.     private class JWKSetEncryptorBuilder extends CacheLoader<JWKSet, JWTEncryptionAndDecryptionService> {

  111.         @Override
  112.         public JWTEncryptionAndDecryptionService load(JWKSet key) throws Exception {
  113.             return new DefaultJWTEncryptionAndDecryptionService(new JWKSetKeyStore(key));
  114.         }

  115.     }

  116.     private class JWKSetVerifierBuilder extends CacheLoader<JWKSet, JWTSigningAndValidationService> {

  117.         @Override
  118.         public JWTSigningAndValidationService load(JWKSet key) throws Exception {
  119.             return new DefaultJWTSigningAndValidationService(new JWKSetKeyStore(key));
  120.         }

  121.     }


  122. }