JWKSetStore.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.security.keystore;

  21. import java.io.Serializable;

  22. import org.openspcoop2.security.SecurityException;
  23. import org.openspcoop2.utils.certificate.JWKSet;
  24. import org.openspcoop2.utils.certificate.byok.BYOKRequestParams;
  25. import org.openspcoop2.utils.resources.Charset;

  26. /**
  27.  * JWKSetStore
  28.  *
  29.  * @author Andrea Poli (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class JWKSetStore implements Serializable {

  34.     /**
  35.      *
  36.      */
  37.     private static final long serialVersionUID = 1L;
  38.    
  39.     private String jwkSetPath;
  40.     private String jwkSetContent;

  41.     private transient JWKSet jwkSet;
  42.    
  43.     @Override
  44.     public String toString() {
  45.         StringBuilder bf = new StringBuilder();
  46.         bf.append("JWKSetStore (").append(this.jwkSetPath).append(") ");
  47.         return bf.toString();
  48.     }
  49.    
  50.     public JWKSetStore(String path) throws SecurityException{
  51.         this(path, null);
  52.     }
  53.     public JWKSetStore(String path, BYOKRequestParams requestParams) throws SecurityException{

  54.         this.jwkSetPath = path;
  55.                        
  56.         byte [] archive = StoreUtils.readContent("FilePath", this.jwkSetPath);
  57.         try {
  58.             archive = readBytes(archive, requestParams);
  59.         }catch(Exception e){
  60.             throw new SecurityException(e.getMessage(),e);
  61.         }
  62.         try {
  63.             this.jwkSetContent = new String(archive, Charset.UTF_8.getValue());
  64.         }catch(Exception e){
  65.             throw new SecurityException(e.getMessage(),e);
  66.         }
  67.        
  68.     }
  69.    
  70.     public JWKSetStore(byte[] archive) throws SecurityException{
  71.         this(archive, null);
  72.     }
  73.     public JWKSetStore(byte[] archiveParam, BYOKRequestParams requestParams) throws SecurityException{

  74.         try{
  75.             if(archiveParam==null){
  76.                 throw new SecurityException("Store non indicato");
  77.             }
  78.            
  79.             byte [] archive = readBytes(archiveParam, requestParams);
  80.             this.jwkSetContent = new String(archive, Charset.UTF_8.getValue());
  81.            
  82.         }catch(Exception e){
  83.             throw new SecurityException(e.getMessage(),e);
  84.         }
  85.        
  86.     }

  87.     private byte[] readBytes(byte[] archive, BYOKRequestParams requestParams) throws SecurityException {
  88.         if(requestParams!=null) {
  89.             return StoreUtils.unwrapBYOK(archive, requestParams);
  90.         }
  91.         return archive;
  92.     }
  93.    
  94.     public String getJwkSetPath() {
  95.         return this.jwkSetPath;
  96.     }
  97.     public String getJwkSetContent() {
  98.         return this.jwkSetContent;
  99.     }
  100.    
  101.     public JWKSet getJwkSet() {
  102.         if(this.jwkSet==null) {
  103.             initialize();
  104.         }
  105.         return this.jwkSet;
  106.     }
  107.     private synchronized void initialize() {
  108.         if(this.jwkSet==null) {
  109.             this.jwkSet = new JWKSet(this.jwkSetContent);
  110.         }
  111.     }
  112.    
  113. }