diff --git a/src/com/dotcms/plugin/saml/v3/cache/SamlCache.java b/src/com/dotcms/plugin/saml/v3/cache/SamlCache.java index 7f0c98d..30208e6 100644 --- a/src/com/dotcms/plugin/saml/v3/cache/SamlCache.java +++ b/src/com/dotcms/plugin/saml/v3/cache/SamlCache.java @@ -22,6 +22,7 @@ public abstract class SamlCache implements Cachable protected static final String INDEX = "index"; protected static final String DISABLED_SITES = "disabled-sites"; protected static final String COUNT = "count"; + protected static final String FLAG = "flag"; protected static final String DEFAULT_IDP_CONFIG_GROUP = "Default-Ipd-Config"; protected static final String IDP_CONFIG_GROUP = "Ipd-Config"; @@ -30,6 +31,7 @@ public abstract class SamlCache implements Cachable protected static final String DISABLED_SITES_GROUP = "Disabled-Sites"; protected static final String IDP_INDEX_GROUP = "Idp-Index"; protected static final String DISABLED_SITES_INDEX_GROUP = "Disabled-Sites-Index"; + protected static final String DISK_HAS_BEEN_READ_GROUP = "Disk-Has-Been-Read"; /** * Adds a {@link IdpConfig} to the caching structure. Null objects or with empty @@ -224,4 +226,8 @@ public String getPrimaryGroup() * @param idpConfigId - The ID of the {@link IdpConfig} object to cache. */ protected abstract void removeSitesIdpConfigId( Map sites, String idpConfigId ); + + public abstract void setIdpConfigRead(Boolean value); + + public abstract boolean hasDiskBeenRead(); } diff --git a/src/com/dotcms/plugin/saml/v3/cache/SamlCacheImpl.java b/src/com/dotcms/plugin/saml/v3/cache/SamlCacheImpl.java index 237ad62..d2bb84c 100644 --- a/src/com/dotcms/plugin/saml/v3/cache/SamlCacheImpl.java +++ b/src/com/dotcms/plugin/saml/v3/cache/SamlCacheImpl.java @@ -31,8 +31,7 @@ * @version 4.3.2 * @since 03-27-2018 */ -public class SamlCacheImpl extends SamlCache -{ +public class SamlCacheImpl extends SamlCache { protected DotCacheAdministrator cache = null; private final String assetsPath; @@ -42,609 +41,578 @@ public class SamlCacheImpl extends SamlCache * Default constructor. Instantiates the {@link DotCacheAdministrator} * object used to store all the configuration information. */ - public SamlCacheImpl() - { + public SamlCacheImpl() { cache = CacheLocator.getCacheAdministrator(); - this.assetsPath = Config.getStringProperty( "ASSET_REAL_PATH", FileUtil.getRealPath( Config.getStringProperty( "ASSET_PATH", "/assets" ) ) ); + this.assetsPath = Config.getStringProperty("ASSET_REAL_PATH", + FileUtil.getRealPath(Config.getStringProperty("ASSET_PATH", "/assets"))); this.idpFilePath = assetsPath + File.separator + "saml" + File.separator + "config.json"; - //Logger.info( this, "this.assetsPath = " + this.assetsPath ); - //Logger.info( this, "this.idpFilePath = " + this.idpFilePath ); + // Logger.info( this, "this.assetsPath = " + this.assetsPath ); + // Logger.info( this, "this.idpFilePath = " + this.idpFilePath ); } @Override - public void addDefaultIdpConfig( IdpConfig idpConfig ) throws DotCacheException - { + public void addDefaultIdpConfig(IdpConfig idpConfig) throws DotCacheException { String tag = "addDefaultIdpConfig( IdpConfig ) "; - idpConfig = checkNotNull( idpConfig, tag + "idpConfig is required." ); + idpConfig = checkNotNull(idpConfig, tag + "idpConfig is required."); - if ( Strings.isNullOrEmpty( idpConfig.getId() ) ) - { - throw new IllegalArgumentException( tag + "idpConfig must have an id." ); + if (Strings.isNullOrEmpty(idpConfig.getId())) { + throw new IllegalArgumentException(tag + "idpConfig must have an id."); } - //Logger.info( this, "Adding default idpConfig to cache id = " + idpConfig.getId() ); + // Logger.info( this, "Adding default idpConfig to cache id = " + + // idpConfig.getId() ); - this.cache.put( DEFAULT, idpConfig.getId(), DEFAULT_IDP_CONFIG_GROUP ); - this.addIdpConfig( idpConfig ); + this.cache.put(DEFAULT, idpConfig.getId(), DEFAULT_IDP_CONFIG_GROUP); + this.addIdpConfig(idpConfig); } @Override - protected void addDisabledSiteId( String site ) - { + protected void addDisabledSiteId(String site) { String tag = "addDisabledSiteId( String ) "; - site = checkNotNull( site.trim(), tag + "site is required." ).trim(); + site = checkNotNull(site.trim(), tag + "site is required.").trim(); - //Logger.info( this, "Adding disabled site to cache = " + site.trim() ); + // Logger.info( this, "Adding disabled site to cache = " + site.trim() + // ); - this.cache.put( site, site, DISABLED_SITES_INDEX_GROUP ); + this.cache.put(site, site, DISABLED_SITES_INDEX_GROUP); } @Override - public void addDisabledSitesMap( Map sites ) - { + public void addDisabledSitesMap(Map sites) { String tag = "addDisabledSitesMap( Map ) "; - try - { - sites = checkNotNull( sites, tag + "sites is required." ); + try { + sites = checkNotNull(sites, tag + "sites is required."); - //Logger.info( this, "Flushing DISABLED_SITES_GROUP cache." ); - //Logger.info( this, "Flushing DISABLED_SITES_INDEX_GROUP cache." ); + // Logger.info( this, "Flushing DISABLED_SITES_GROUP cache." ); + // Logger.info( this, "Flushing DISABLED_SITES_INDEX_GROUP cache." + // ); - cache.flushGroup( DISABLED_SITES_GROUP ); - cache.flushGroup( DISABLED_SITES_INDEX_GROUP ); + cache.flushGroup(DISABLED_SITES_GROUP); + cache.flushGroup(DISABLED_SITES_INDEX_GROUP); - this.cache.put( DISABLED_SITES, sites, DISABLED_SITES_GROUP ); + this.cache.put(DISABLED_SITES, sites, DISABLED_SITES_GROUP); - sites.forEach( ( identifier, hostname )->{ - this.addDisabledSiteId( identifier.trim() ); - this.addDisabledSiteId( hostname.trim() ); + sites.forEach((identifier, hostname) -> { + this.addDisabledSiteId(identifier.trim()); + this.addDisabledSiteId(hostname.trim()); }); - } - catch ( Exception exception ) - { - Logger.error( this, tag + "Error adding disabled sites to cache.", exception ); + } catch (Exception exception) { + Logger.error(this, tag + "Error adding disabled sites to cache.", exception); } } @Override - public void addIdpConfig( IdpConfig idpConfig ) throws DotCacheException - { + public void addIdpConfig(IdpConfig idpConfig) throws DotCacheException { String tag = "addIdpConfig( IdpConfig ) "; - idpConfig = checkNotNull( idpConfig, tag + "idpConfig is required." ); + idpConfig = checkNotNull(idpConfig, tag + "idpConfig is required."); - if ( Strings.isNullOrEmpty( idpConfig.getId() ) ) - { - throw new IllegalArgumentException( tag + "idpConfig must have an id and cannot be default." ); + if (Strings.isNullOrEmpty(idpConfig.getId())) { + throw new IllegalArgumentException(tag + "idpConfig must have an id and cannot be default."); } String idpConfigId = idpConfig.getId(); - //Logger.info( this, "Adding idpConfig to cache id = " + idpConfig.getId() ); + // Logger.info( this, "Adding idpConfig to cache id = " + + // idpConfig.getId() ); - this.cache.put( idpConfigId, idpConfig, IDP_CONFIG_GROUP ); - incrementIdpCount(); - this.addIdpConfigIdToIndex( idpConfigId ); + this.cache.put(idpConfigId, idpConfig, IDP_CONFIG_GROUP); + setIdpCount(); + this.addIdpConfigIdToIndex(idpConfigId); Map sites = idpConfig.getSites(); - if ( sites != null && sites.size() > 0 ) - { - this.addSitesIdpConfigId( sites, idpConfigId ); + if (sites != null && sites.size() > 0) { + this.addSitesIdpConfigId(sites, idpConfigId); } } - @SuppressWarnings( "unchecked" ) + @SuppressWarnings("unchecked") @Override - protected void addIdpConfigIdToIndex( String idpConfigId ) - { + protected void addIdpConfigIdToIndex(String idpConfigId) { String tag = "addIdpConfigToIndex( String ) "; List idpIndex = null; - idpConfigId = checkNotNull( idpConfigId, tag + "idpConfigId is required." ).trim(); + idpConfigId = checkNotNull(idpConfigId, tag + "idpConfigId is required.").trim(); - try - { - idpIndex = (List) this.cache.get( INDEX, IDP_INDEX_GROUP ); - } - catch ( DotCacheException dotCacheException ) - { - //Logger.info( this, tag + "idpIndex not found in [" + IDP_INDEX_GROUP + "] cache group: [" + INDEX + "]. Creating new entry." ); + try { + idpIndex = (List) this.cache.get(INDEX, IDP_INDEX_GROUP); + } catch (DotCacheException dotCacheException) { + // Logger.info( this, tag + "idpIndex not found in [" + + // IDP_INDEX_GROUP + "] cache group: [" + INDEX + "]. Creating new + // entry." ); } - if ( idpIndex == null ) - { + if (idpIndex == null) { idpIndex = new ArrayList(); } - idpIndex.remove( idpConfigId ); - idpIndex.add( idpConfigId ); + idpIndex.remove(idpConfigId); + idpIndex.add(idpConfigId); - //Logger.info( this, "Adding idpConfig id to index cache id = " + idpConfigId ); + // Logger.info( this, "Adding idpConfig id to index cache id = " + + // idpConfigId ); - this.cache.put( INDEX, idpIndex, IDP_INDEX_GROUP ); + this.cache.put(INDEX, idpIndex, IDP_INDEX_GROUP); } @Override - public void addIdpConfigs( List idpConfigs ) - { + public void addIdpConfigs(List idpConfigs) { String tag = "addIdpConfigs( List ) "; - //Logger.info( this, "Clearing idpConfig cache." ); + // Logger.info( this, "Clearing idpConfig cache." ); this.clearCache(); - idpConfigs.forEach( idpConfig ->{ - try - { - this.addIdpConfig( idpConfig ); + idpConfigs.forEach(idpConfig -> { + try { + this.addIdpConfig(idpConfig); Map sites = idpConfig.getSites(); - if ( sites != null && sites.size() > 0 ) - { - this.addSitesIdpConfigId( sites, idpConfig.getId() ); + if (sites != null && sites.size() > 0) { + this.addSitesIdpConfigId(sites, idpConfig.getId()); } - } - catch ( Exception exception ) - { - Logger.error( this, tag + "Error adding idpConfig to cache.", exception ); + } catch (Exception exception) { + Logger.error(this, tag + "Error adding idpConfig to cache.", exception); } }); } @Override - public void addSiteIdpConfig( String site, IdpConfig idpConfig ) throws DotCacheException - { + public void addSiteIdpConfig(String site, IdpConfig idpConfig) throws DotCacheException { String tag = "addSiteIdpConfig( String, IdpConfig ) "; - site = checkNotNull( site, tag + "site is required." ).trim(); - idpConfig = checkNotNull( idpConfig, tag + "idpConfig is required." ); + site = checkNotNull(site, tag + "site is required.").trim(); + idpConfig = checkNotNull(idpConfig, tag + "idpConfig is required."); - if ( Strings.isNullOrEmpty( idpConfig.getId() ) ) - { - throw new IllegalArgumentException( tag + "IdpConfig must have an id." ); + if (Strings.isNullOrEmpty(idpConfig.getId())) { + throw new IllegalArgumentException(tag + "IdpConfig must have an id."); } - //Logger.info( this, "Adding site to idpConfig id cache site = " + site + " idpConfig id = " + idpConfig.getId() ); + // Logger.info( this, "Adding site to idpConfig id cache site = " + site + // + " idpConfig id = " + idpConfig.getId() ); - this.cache.put( site, idpConfig.getId(), SITES_TO_IDP_GROUP ); - this.addIdpConfig( idpConfig ); + this.cache.put(site, idpConfig.getId(), SITES_TO_IDP_GROUP); + this.addIdpConfig(idpConfig); } @Override - protected void addSiteIdpConfigId( String site, String idpConfigId ) - { + protected void addSiteIdpConfigId(String site, String idpConfigId) { String tag = "addSiteIdpConfigId( String, String ) "; - site = checkNotNull( site, tag + "site is required." ).trim(); - idpConfigId = checkNotNull( idpConfigId, tag + "idpConfigId is required." ).trim(); + site = checkNotNull(site, tag + "site is required.").trim(); + idpConfigId = checkNotNull(idpConfigId, tag + "idpConfigId is required.").trim(); - //Logger.info( this, "Adding site to idpConfig id cache site = " + site + " idpConfig id = " + idpConfigId ); + // Logger.info( this, "Adding site to idpConfig id cache site = " + site + // + " idpConfig id = " + idpConfigId ); - this.cache.put( site, idpConfigId, SITES_TO_IDP_GROUP ); + this.cache.put(site, idpConfigId, SITES_TO_IDP_GROUP); } @Override - protected void addSitesIdpConfigId( Map sites, String idpConfigId ) - { + protected void addSitesIdpConfigId(Map sites, String idpConfigId) { String tag = "addSitesIdpConfigId( Map, String ) "; // It's ok for sites to be null, // but we need to check before processing - try - { - sites = checkNotNull( sites, tag + "sites is required." ); + try { + sites = checkNotNull(sites, tag + "sites is required."); - sites.forEach( ( identifier, hostname )->{ - this.addSiteIdpConfigId( identifier.trim(), idpConfigId.trim() ); - this.addSiteIdpConfigId( hostname.trim(), idpConfigId.trim() ); + sites.forEach((identifier, hostname) -> { + this.addSiteIdpConfigId(identifier.trim(), idpConfigId.trim()); + this.addSiteIdpConfigId(hostname.trim(), idpConfigId.trim()); }); - } - catch ( Exception exception ) - { - Logger.error( this, tag + "Error adding sites to cache.", exception ); + } catch (Exception exception) { + Logger.error(this, tag + "Error adding sites to cache.", exception); } } @Override - public void clearCache() - { - //Logger.info( this, "Flushing Saml cache." ); + public void clearCache() { + // Logger.info( this, "Flushing Saml cache." ); - for ( String cacheGroup : getGroups() ) - { - cache.flushGroup( cacheGroup ); + for (String cacheGroup : getGroups()) { + cache.flushGroup(cacheGroup); } } @Override - public IdpConfig getDefaultIdpConfig() - { + public IdpConfig getDefaultIdpConfig() { String tag = "getDefaultIdpConfig() "; IdpConfig idpConfig = null; - try - { - String idpConfigId = (String) this.cache.get( DEFAULT, DEFAULT_IDP_CONFIG_GROUP ); - idpConfig = this.getIdpConfig( idpConfigId ); + try { + String idpConfigId = (String) this.cache.get(DEFAULT, DEFAULT_IDP_CONFIG_GROUP); + idpConfig = this.getIdpConfig(idpConfigId); - //Logger.info( this, "Getting default idpConfig from cache id = " + idpConfigId ); - } - catch ( DotCacheException dotCacheException ) - { - //Logger.info( this, tag + "SamlCache entry not found in [" + DEFAULT_IDP_CONFIG_GROUP + "] cache group." ); + // Logger.info( this, "Getting default idpConfig from cache id = " + + // idpConfigId ); + } catch (DotCacheException dotCacheException) { + // Logger.info( this, tag + "SamlCache entry not found in [" + + // DEFAULT_IDP_CONFIG_GROUP + "] cache group." ); } return idpConfig; } @Override - public String getDefaultIdpConfigId() - { + public String getDefaultIdpConfigId() { String tag = "getDefaultIdpConfigId() "; String idpConfigId = null; - try - { - idpConfigId = (String) this.cache.get( DEFAULT, DEFAULT_IDP_CONFIG_GROUP ); + try { + idpConfigId = (String) this.cache.get(DEFAULT, DEFAULT_IDP_CONFIG_GROUP); - //Logger.info( this, "Getting default idpConfig from cache id = " + idpConfigId ); - } - catch ( DotCacheException dotCacheException ) - { - //Logger.info( this, tag + "SamlCache entry not found in [" + DEFAULT_IDP_CONFIG_GROUP + "] cache group." ); + // Logger.info( this, "Getting default idpConfig from cache id = " + + // idpConfigId ); + } catch (DotCacheException dotCacheException) { + // Logger.info( this, tag + "SamlCache entry not found in [" + + // DEFAULT_IDP_CONFIG_GROUP + "] cache group." ); } return idpConfigId; } - @SuppressWarnings( "unchecked" ) + @SuppressWarnings("unchecked") @Override - public Map getDisabledSitesMap() - { + public Map getDisabledSitesMap() { String tag = "getDisabledSites() "; Map disabledSitesMap = new HashMap(); - try - { - disabledSitesMap = (Map) this.cache.get( DISABLED_SITES, DISABLED_SITES_INDEX_GROUP ); + try { + disabledSitesMap = (Map) this.cache.get(DISABLED_SITES, DISABLED_SITES_INDEX_GROUP); - //Logger.info( this, "Getting disabled sites map from cache disabledSitesMap = " + disabledSitesMap ); - } - catch ( DotCacheException dotCacheException ) - { - //Logger.info( this, tag + "SamlCache entry not found in [" + DISABLED_SITES_INDEX_GROUP + "] cache group." ); + // Logger.info( this, "Getting disabled sites map from cache + // disabledSitesMap = " + disabledSitesMap ); + } catch (DotCacheException dotCacheException) { + // Logger.info( this, tag + "SamlCache entry not found in [" + + // DISABLED_SITES_INDEX_GROUP + "] cache group." ); } return disabledSitesMap; } @Override - public IdpConfig getIdpConfig( String idpConfigId ) - { + public IdpConfig getIdpConfig(String idpConfigId) { String tag = "getIdpConfig( String ) "; IdpConfig idpConfig = null; - idpConfigId = checkNotNull( idpConfigId, tag + "idpConfigId is required." ).trim(); + idpConfigId = checkNotNull(idpConfigId, tag + "idpConfigId is required.").trim(); - try - { - idpConfig = (IdpConfig) this.cache.get( idpConfigId, IDP_CONFIG_GROUP ); + try { + idpConfig = (IdpConfig) this.cache.get(idpConfigId, IDP_CONFIG_GROUP); - //Logger.info( this, "Getting idpConfig from cache id = " + idpConfigId ); - } - catch ( DotCacheException dotCacheException ) - { - //Logger.info( this, tag + "SamlCache entry not found in [" + IDP_CONFIG_GROUP + "] cache group: " + idpConfigId ); + // Logger.info( this, "Getting idpConfig from cache id = " + + // idpConfigId ); + } catch (DotCacheException dotCacheException) { + // Logger.info( this, tag + "SamlCache entry not found in [" + + // IDP_CONFIG_GROUP + "] cache group: " + idpConfigId ); } return idpConfig; } - @SuppressWarnings( "unchecked" ) + @SuppressWarnings("unchecked") @Override - public List getIdpConfigs() - { + public List getIdpConfigs() { String tag = "getIdpConfigs() "; List idpConfigs = new ArrayList(); - try - { - List idpConfigIds = (List) this.cache.get( INDEX, IDP_INDEX_GROUP ); + try { + List idpConfigIds = (List) this.cache.get(INDEX, IDP_INDEX_GROUP); - if ( idpConfigIds != null && !idpConfigIds.isEmpty() ) - { - idpConfigIds.forEach( idpConfigId -> { + if (idpConfigIds != null && !idpConfigIds.isEmpty()) { + idpConfigIds.forEach(idpConfigId -> { - IdpConfig idpConfig = this.getIdpConfig( idpConfigId ); + IdpConfig idpConfig = this.getIdpConfig(idpConfigId); - if ( idpConfig != null ) - { - idpConfigs.add( idpConfig ); + if (idpConfig != null) { + idpConfigs.add(idpConfig); } }); } - //Logger.info( this, "Getting idpConfigs from cache idpConfigIds.size() = " + idpConfigIds.size() ); - //Logger.info( this, "Getting idpConfigs from cache idpConfigs.size() = " + idpConfigs.size() ); + // Logger.info( this, "Getting idpConfigs from cache + // idpConfigIds.size() = " + idpConfigIds.size() ); + // Logger.info( this, "Getting idpConfigs from cache + // idpConfigs.size() = " + idpConfigs.size() ); Integer idpCount = getIdpCount(); - //Logger.info( this, "Checking if idpCount == idpConfigs.size()." ); - //Logger.info( this, idpCount + " == " + idpConfigs.size() + " ?" ); + // Logger.info( this, "Checking if idpCount == idpConfigs.size()." + // ); + // Logger.info( this, idpCount + " == " + idpConfigs.size() + " ?" + // ); - if ( idpCount != idpConfigs.size() ) - { + if (idpCount != idpConfigs.size()) { // Return an empty list. // This tells the IdpConfigHelper class to // try the file system and invalidate the cache. return new ArrayList(); + } else { + // Logger.info( this, "Counts match." ); } - else - { - //Logger.info( this, "Counts match." ); - } - } - catch ( DotCacheException dotCacheException ) - { - //Logger.info( this, tag + "SamlCache read error." ); + } catch (DotCacheException dotCacheException) { + // Logger.info( this, tag + "SamlCache read error." ); } return idpConfigs; } @Override - public IdpConfig getSiteIdpConfig( String site ) - { + public IdpConfig getSiteIdpConfig(String site) { String tag = "getSiteIdpConfig( String ) "; IdpConfig idpConfig = null; - site = checkNotNull( site, tag + "site is required." ).trim(); + site = checkNotNull(site, tag + "site is required.").trim(); - try - { - String idpConfigId = (String) this.cache.get( site, SITES_TO_IDP_GROUP ); - idpConfig = this.getIdpConfig( idpConfigId ); + try { + String idpConfigId = (String) this.cache.get(site, SITES_TO_IDP_GROUP); + idpConfig = this.getIdpConfig(idpConfigId); - //Logger.info( this, "Getting site idpConfig from cache site = " + site + " idpConfig id = " + idpConfigId ); - } - catch ( DotCacheException dotCacheException ) - { - //Logger.info( this, tag + "SamlCache entry not found in [" + SITES_TO_IDP_GROUP + "] cache group: " + site ); + // Logger.info( this, "Getting site idpConfig from cache site = " + + // site + " idpConfig id = " + idpConfigId ); + } catch (DotCacheException dotCacheException) { + // Logger.info( this, tag + "SamlCache entry not found in [" + + // SITES_TO_IDP_GROUP + "] cache group: " + site ); } return idpConfig; } - @SuppressWarnings( "unchecked" ) + @SuppressWarnings("unchecked") @Override - public List getSites() - { + public List getSites() { String tag = "getSites() "; List sites = new ArrayList(); - try - { - List idpConfigIds = (List) this.cache.get( INDEX, IDP_INDEX_GROUP ); + try { + List idpConfigIds = (List) this.cache.get(INDEX, IDP_INDEX_GROUP); - if ( idpConfigIds != null && !idpConfigIds.isEmpty() ) - { - idpConfigIds.forEach( idpConfigId -> { + if (idpConfigIds != null && !idpConfigIds.isEmpty()) { + idpConfigIds.forEach(idpConfigId -> { - IdpConfig idpConfig = this.getIdpConfig( idpConfigId ); + IdpConfig idpConfig = this.getIdpConfig(idpConfigId); - if ( idpConfig != null ) - { + if (idpConfig != null) { Map configSiteMap = idpConfig.getSites(); Collection configSites = configSiteMap.values(); - sites.addAll( configSites ); + sites.addAll(configSites); } }); } - //Logger.info( this, "Getting all sites from all idpConfigs from cache idpConfigIds.size() = " + idpConfigIds.size() + " sites.size() = " + sites.size() ); + // Logger.info( this, "Getting all sites from all idpConfigs from + // cache idpConfigIds.size() = " + idpConfigIds.size() + " + // sites.size() = " + sites.size() ); - } - catch ( DotCacheException dotCacheException ) - { - //Logger.info( this, tag + "SamlCache read error." ); + } catch (DotCacheException dotCacheException) { + // Logger.info( this, tag + "SamlCache read error." ); } return sites; } - private void incrementIdpCount() throws DotCacheException - { - Integer count = getIdpCount(); - count++; - this.cache.put( COUNT, "" + count, IDP_CONFIG_COUNT_GROUP ); - } + private void setIdpCount() throws DotCacheException { + Integer count = new Integer(0); + List idpConfigIds = (List) this.cache.get(INDEX, IDP_INDEX_GROUP); - private void decrementIdpCount() throws DotCacheException - { - Integer count = getIdpCount(); - count--; - this.cache.put( COUNT, "" + count, IDP_CONFIG_COUNT_GROUP ); + if (idpConfigIds != null && !idpConfigIds.isEmpty()) { + count = idpConfigIds.size(); + } + + this.cache.put(COUNT, "" + count, IDP_CONFIG_COUNT_GROUP); } - private Integer getIdpCount() throws DotCacheException - { - Integer count = 0; +// private void decrementIdpCount() throws DotCacheException { +// Integer count = getIdpCount(); +// count--; +// this.cache.put(COUNT, "" + count, IDP_CONFIG_COUNT_GROUP); +// } - try - { - count = Integer.parseInt( (String) this.cache.get( COUNT, IDP_CONFIG_COUNT_GROUP ) ); - } - catch ( Exception exception ) - { - //Logger.info( this, "IdpConfig count not set in cache. Setting to 0." ); + private Integer getIdpCount() throws DotCacheException { + Integer count = 0; + + try { + count = Integer.parseInt((String) this.cache.get(COUNT, IDP_CONFIG_COUNT_GROUP)); + } catch (Exception exception) { + // Logger.info( this, "IdpConfig count not set in cache. Setting to + // 0." ); - cache.flushGroup( IDP_CONFIG_COUNT_GROUP ); - this.cache.put( COUNT, "" + 0, IDP_CONFIG_COUNT_GROUP ); + cache.flushGroup(IDP_CONFIG_COUNT_GROUP); + this.cache.put(COUNT, "" + 0, IDP_CONFIG_COUNT_GROUP); } - return ( count != null ? count : 0 ); + return (count != null ? count : 0); } @Override - public void refresh() - { + public void refresh() { String tag = "refresh() "; - //Logger.info( this, "Start: Clearing cache and loading all configs." ); + // Logger.info( this, "Start: Clearing cache and loading all configs." + // ); this.clearCache(); - try - { - this.cache.put( COUNT, "" + 0, IDP_CONFIG_COUNT_GROUP ); + try { + this.cache.put(COUNT, "" + 0, IDP_CONFIG_COUNT_GROUP); // Read file system - List idpConfigs = IdpConfigWriterReader.readIdpConfigs( new File( idpFilePath ) ); + List idpConfigs = IdpConfigWriterReader.readIdpConfigs(new File(idpFilePath)); // Update cache - this.addIdpConfigs( idpConfigs ); + this.addIdpConfigs(idpConfigs); - //Logger.info( this, "End: Clearing cache and loading all configs." ); - } - catch ( IOException | JSONException exception ) - { - Logger.error( this, tag + "Error refreshing cache from file system.", exception ); + // Logger.info( this, "End: Clearing cache and loading all configs." + // ); + } catch (IOException | JSONException exception) { + Logger.error(this, tag + "Error refreshing cache from file system.", exception); } } @Override - public void removeDefaultIdpConfig() throws DotCacheException - { + public void removeDefaultIdpConfig() throws DotCacheException { IdpConfig idpConfig = this.getDefaultIdpConfig(); Map sites = idpConfig.getSites(); - if ( sites != null && sites.size() > 0 ) - { - this.removeSitesIdpConfigId( sites, idpConfig.getId() ); + if (sites != null && sites.size() > 0) { + this.removeSitesIdpConfigId(sites, idpConfig.getId()); } - this.removeIdpConfig( idpConfig.getId() ); - this.cache.remove( DEFAULT, IDP_CONFIG_GROUP ); + this.removeIdpConfig(idpConfig.getId()); + this.cache.remove(DEFAULT, IDP_CONFIG_GROUP); - //Logger.info( this, "Removing default idpConfig from cache id = " + idpConfig.getId() ); + // Logger.info( this, "Removing default idpConfig from cache id = " + + // idpConfig.getId() ); } @Override - public void removeIdpConfig( IdpConfig idpConfig ) throws DotCacheException - { + public void removeIdpConfig(IdpConfig idpConfig) throws DotCacheException { String tag = "removeIdpConfig( IdpConfig ) "; - idpConfig = checkNotNull( idpConfig, tag + "idpConfig is required." ); + idpConfig = checkNotNull(idpConfig, tag + "idpConfig is required."); - if ( Strings.isNullOrEmpty( idpConfig.getId() ) ) - { - throw new IllegalArgumentException( tag + "idpConfig must have an id." ); + if (Strings.isNullOrEmpty(idpConfig.getId())) { + throw new IllegalArgumentException(tag + "idpConfig must have an id."); } Map sites = idpConfig.getSites(); - if ( sites != null && sites.size() > 0 ) - { - this.removeSitesIdpConfigId( sites, idpConfig.getId() ); + if (sites != null && sites.size() > 0) { + this.removeSitesIdpConfigId(sites, idpConfig.getId()); } - this.removeIdpConfig( idpConfig.getId() ); + this.removeIdpConfig(idpConfig.getId()); } @Override - protected void removeIdpConfig( String idpConfigId ) throws DotCacheException - { + protected void removeIdpConfig(String idpConfigId) throws DotCacheException { String tag = "removeIdpConfig( String ) "; - idpConfigId = checkNotNull( idpConfigId, tag + "idpConfigId is required." ).trim(); + idpConfigId = checkNotNull(idpConfigId, tag + "idpConfigId is required.").trim(); - this.cache.remove( idpConfigId, IDP_CONFIG_GROUP ); - decrementIdpCount(); - this.removeIdpConfigIdFromIndex( idpConfigId ); + this.cache.remove(idpConfigId, IDP_CONFIG_GROUP); + setIdpCount(); + this.removeIdpConfigIdFromIndex(idpConfigId); - //Logger.info( this, "Removing idpConfig from cache id = " + idpConfigId ); + // Logger.info( this, "Removing idpConfig from cache id = " + + // idpConfigId ); } - @SuppressWarnings( "unchecked" ) + @SuppressWarnings("unchecked") @Override - protected void removeIdpConfigIdFromIndex( String idpConfigId ) - { + protected void removeIdpConfigIdFromIndex(String idpConfigId) { String tag = "removeIdpConfigFromIndex( String ) "; List idpIndex = null; - idpConfigId = checkNotNull( idpConfigId, tag + "idpConfigId is required." ).trim(); + idpConfigId = checkNotNull(idpConfigId, tag + "idpConfigId is required.").trim(); - try - { - idpIndex = (List) this.cache.get( INDEX, IDP_INDEX_GROUP ); - } - catch ( DotCacheException dotCacheException ) - { - //Logger.info( this, tag + "idpIndex not found in [" + IDP_INDEX_GROUP + "] cache group: [" + INDEX + "]. Creating new entry." ); + try { + idpIndex = (List) this.cache.get(INDEX, IDP_INDEX_GROUP); + } catch (DotCacheException dotCacheException) { + // Logger.info( this, tag + "idpIndex not found in [" + + // IDP_INDEX_GROUP + "] cache group: [" + INDEX + "]. Creating new + // entry." ); } - if ( idpIndex == null ) - { + if (idpIndex == null) { idpIndex = new ArrayList(); } - idpIndex.remove( idpConfigId ); + idpIndex.remove(idpConfigId); - this.cache.put( INDEX, idpIndex, IDP_INDEX_GROUP ); + this.cache.put(INDEX, idpIndex, IDP_INDEX_GROUP); - //Logger.info( this, "Removing idpConfig from cache index id = " + idpConfigId ); + // Logger.info( this, "Removing idpConfig from cache index id = " + + // idpConfigId ); } @Override - protected void removeSiteIdpConfigId( String site ) - { + protected void removeSiteIdpConfigId(String site) { String tag = "removeSiteIdpConfigId( String ) "; - site = checkNotNull( site, tag + "site is required." ).trim(); + site = checkNotNull(site, tag + "site is required.").trim(); - this.cache.remove( site, SITES_TO_IDP_GROUP ); + this.cache.remove(site, SITES_TO_IDP_GROUP); - //Logger.info( this, "Removing site from cache site = " + site ); + // Logger.info( this, "Removing site from cache site = " + site ); } @Override - protected void removeSitesIdpConfigId( Map sites, String idpConfigId ) - { + protected void removeSitesIdpConfigId(Map sites, String idpConfigId) { String tag = "removeSitesIdpConfigId( Map, String ) "; // It's ok for sites to be null, // but we need to check before processing - try - { - sites = checkNotNull( sites, tag + "sites is required." ); + try { + sites = checkNotNull(sites, tag + "sites is required."); - sites.forEach( ( identifier, hostname )->{ - this.removeSiteIdpConfigId( identifier.trim() ); - this.removeSiteIdpConfigId( hostname.trim() ); + sites.forEach((identifier, hostname) -> { + this.removeSiteIdpConfigId(identifier.trim()); + this.removeSiteIdpConfigId(hostname.trim()); }); + } catch (Exception exception) { + Logger.info(this, tag + "Error removing sites from cache."); } - catch ( Exception exception ) - { - Logger.info( this, tag + "Error removing sites from cache." ); + + } + + @Override + public void setIdpConfigRead(Boolean value) { + + this.cache.put(FLAG, value.toString(), DISK_HAS_BEEN_READ_GROUP); + } + + @Override + public boolean hasDiskBeenRead() { + boolean isDiskRead = false; + + try { + isDiskRead = Boolean.parseBoolean((String) this.cache.get(FLAG, DISK_HAS_BEEN_READ_GROUP)); + } catch (Exception exception) { + + cache.flushGroup(DISK_HAS_BEEN_READ_GROUP); + this.cache.put(FLAG, "false", DISK_HAS_BEEN_READ_GROUP); } + return isDiskRead; } } diff --git a/src/com/dotcms/plugin/saml/v3/config/IdpConfigHelper.java b/src/com/dotcms/plugin/saml/v3/config/IdpConfigHelper.java index b44989a..61e6fca 100644 --- a/src/com/dotcms/plugin/saml/v3/config/IdpConfigHelper.java +++ b/src/com/dotcms/plugin/saml/v3/config/IdpConfigHelper.java @@ -1,14 +1,5 @@ package com.dotcms.plugin.saml.v3.config; -import com.dotcms.plugin.saml.v3.cache.SamlCache; - -import com.dotmarketing.business.CacheLocator; -import com.dotmarketing.exception.DotDataException; -import com.dotmarketing.util.Logger; -import com.dotmarketing.util.UUIDGenerator; -import com.dotmarketing.util.UtilMethods; -import com.dotmarketing.util.json.JSONException; - import java.io.File; import java.io.IOException; import java.io.Serializable; @@ -17,6 +8,14 @@ import java.util.List; import java.util.Map; +import com.dotcms.plugin.saml.v3.cache.SamlCache; +import com.dotcms.plugin.saml.v3.exception.DotSamlByPassException; +import com.dotmarketing.business.CacheLocator; +import com.dotmarketing.exception.DotDataException; +import com.dotmarketing.util.UUIDGenerator; +import com.dotmarketing.util.UtilMethods; +import com.dotmarketing.util.json.JSONException; + public class IdpConfigHelper extends IdpConfigFileHelper implements Serializable { private static class SingletonHolder @@ -87,14 +86,14 @@ public void deleteIdpConfig( IdpConfig idpConfig ) throws IOException, JSONExcep } } - public IdpConfig findIdpConfig( String id ) throws IOException, JSONException, DotDataException + public IdpConfig findIdpConfig( String id ) throws IOException, JSONException, DotDataException, DotSamlByPassException { if ( UtilMethods.isSet( id ) ) { // Try cache IdpConfig idpConfig = this.findIdpConfigCache( id ); - if ( idpConfig == null ) + if ( idpConfig == null) { // Try file system idpConfig = this.findIdpConfigFileSystem( id ); @@ -126,7 +125,7 @@ private IdpConfig findIdpConfigCache( String id ) throws IOException, JSONExcept return idpConfig; } - private IdpConfig findIdpConfigFileSystem( String id ) throws IOException, JSONException, DotDataException + private IdpConfig findIdpConfigFileSystem( String id ) throws IOException, JSONException, DotDataException, DotSamlByPassException { IdpConfig idpConfig = null; List idpConfigList = this.getIdpConfigsFileSystem(); @@ -157,7 +156,7 @@ private IdpConfig findIdpConfigFileSystem( String id ) throws IOException, JSONE return idpConfig; } - public IdpConfig findSiteIdpConfig( String site ) throws DotDataException, IOException, JSONException + public IdpConfig findSiteIdpConfig( String site ) throws DotDataException, IOException, JSONException, DotSamlByPassException { if ( UtilMethods.isSet( site ) ) { @@ -196,7 +195,7 @@ private IdpConfig findSiteIdpConfigCache( String site ) return idpConfig; } - private IdpConfig findSiteIdpConfigFileSystem( String site ) throws DotDataException, IOException, JSONException + private IdpConfig findSiteIdpConfigFileSystem( String site ) throws DotDataException, IOException, JSONException, DotSamlByPassException { IdpConfig idpConfig = null; List idpConfigList = this.getIdpConfigsFileSystem(); @@ -331,7 +330,7 @@ public Map getDisabledSiteIdsFileSystem() throws IOException, JS return disabledSitesMap; } - public List getIdpConfigs() throws IOException, JSONException + public List getIdpConfigs() throws IOException, JSONException, DotSamlByPassException { // Try cache List idpConfigs = this.getIdpConfigsCache(); @@ -361,8 +360,12 @@ private List getIdpConfigsCache() throws IOException, JSONException return idpConfigs; } - private List getIdpConfigsFileSystem() throws IOException, JSONException + private List getIdpConfigsFileSystem() throws IOException, JSONException, DotSamlByPassException { + if ( samlCache.hasDiskBeenRead() ) { + throw new DotSamlByPassException("Disk Has Been Read."); + } + List idpConfigs = IdpConfigWriterReader.readIdpConfigs( new File( IDP_FILE_PATH ) ); // Update cache @@ -375,7 +378,7 @@ private List getIdpConfigsFileSystem() throws IOException, JSONExcept { //Logger.info( this, "Error writing to SamlCache" ); } - + samlCache.setIdpConfigRead(Boolean.TRUE); return idpConfigs; } diff --git a/src/com/dotcms/plugin/saml/v3/config/IdpConfigWriterReader.java b/src/com/dotcms/plugin/saml/v3/config/IdpConfigWriterReader.java index 3e3c9c9..926c8fe 100644 --- a/src/com/dotcms/plugin/saml/v3/config/IdpConfigWriterReader.java +++ b/src/com/dotcms/plugin/saml/v3/config/IdpConfigWriterReader.java @@ -16,9 +16,9 @@ public class IdpConfigWriterReader { - public static final String IDP_CONFIGS = "samlConfigs"; - public static final String DEFAULT_SAML_CONFIG = "defaultSamlConfig"; - public static final String DISABLE_SAML_SITES = "disabledSamlSites"; + public static volatile String IDP_CONFIGS = "samlConfigs"; + public static volatile String DEFAULT_SAML_CONFIG = "defaultSamlConfig"; + public static volatile String DISABLE_SAML_SITES = "disabledSamlSites"; public static String readDefaultIdpConfigId( final File idpConfigFile ) throws IOException, JSONException { diff --git a/src/com/dotcms/plugin/saml/v3/exception/DotSamlByPassException.java b/src/com/dotcms/plugin/saml/v3/exception/DotSamlByPassException.java new file mode 100644 index 0000000..972207d --- /dev/null +++ b/src/com/dotcms/plugin/saml/v3/exception/DotSamlByPassException.java @@ -0,0 +1,26 @@ +package com.dotcms.plugin.saml.v3.exception; + +/** + * Exception to report no SAML configuration + * + * @author tstave + */ +public class DotSamlByPassException extends RuntimeException +{ + private static final long serialVersionUID = -3569526825729783600L; + + public DotSamlByPassException() + { + + } + + public DotSamlByPassException( String message ) + { + super( message ); + } + + public DotSamlByPassException( String message, Throwable cause ) + { + super( message, cause ); + } +} diff --git a/src/com/dotcms/plugin/saml/v3/filter/SamlAccessFilter.java b/src/com/dotcms/plugin/saml/v3/filter/SamlAccessFilter.java index a418ee5..7bb4081 100755 --- a/src/com/dotcms/plugin/saml/v3/filter/SamlAccessFilter.java +++ b/src/com/dotcms/plugin/saml/v3/filter/SamlAccessFilter.java @@ -14,6 +14,7 @@ import com.dotcms.cms.login.LoginServiceAPI; import com.dotcms.plugin.saml.v3.config.EndpointHelper; import com.dotcms.plugin.saml.v3.config.IdpConfig; +import com.dotcms.plugin.saml.v3.exception.DotSamlByPassException; import com.dotcms.plugin.saml.v3.exception.DotSamlException; import com.dotcms.plugin.saml.v3.key.DotSamlConstants; import com.dotcms.plugin.saml.v3.parameters.DotsamlPropertiesService; @@ -105,7 +106,7 @@ public void doFilter(final ServletRequest servletRequest, final ServletResponse // id) is in the request query string // for artifact resolution or SAMLResponse for post // resolution. - + final AutoLoginResult autoLoginResult = super.autoLogin(httpServletRequest, httpServletResponse, session, idpConfig); @@ -168,6 +169,9 @@ public void doFilter(final ServletRequest servletRequest, final ServletResponse + ". Not any SAML filtering for this request: " + httpServletRequest.getRequestURI()); } + } catch (DotSamlByPassException exception) { + Logger.debug(this, "No SAML Configuration Defined"); + } catch (JSONException | DotDataException exception) { Logger.info(this, "Error reading idpConfig for the site: " + httpServletRequest.getServerName()); } diff --git a/src/com/dotcms/plugin/saml/v3/rest/DotSamlRestService.java b/src/com/dotcms/plugin/saml/v3/rest/DotSamlRestService.java index c7b2c4f..c3cc32d 100644 --- a/src/com/dotcms/plugin/saml/v3/rest/DotSamlRestService.java +++ b/src/com/dotcms/plugin/saml/v3/rest/DotSamlRestService.java @@ -19,6 +19,7 @@ import com.dotcms.plugin.saml.v3.config.IdpConfig; import com.dotcms.plugin.saml.v3.config.IdpConfigHelper; +import com.dotcms.plugin.saml.v3.exception.DotSamlByPassException; import com.dotcms.plugin.saml.v3.exception.DotSamlException; import com.dotcms.plugin.saml.v3.filter.SamlFilter; import com.dotcms.plugin.saml.v3.parameters.DotsamlPropertiesService; @@ -132,6 +133,10 @@ public void login(@PathParam("idpConfigId") final String idpConfigId, Logger.error(this, dotSamlException.getMessage(), dotSamlException); + } catch (DotSamlByPassException exception) { + + Logger.error(this, exception.getMessage(), exception); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. @@ -168,6 +173,10 @@ public void logout(@PathParam("idpConfigId") final String idpConfigId, Logger.error(this, dotSamlException.getMessage(), dotSamlException); + } catch (DotSamlByPassException exception) { + + Logger.error(this, exception.getMessage(), exception); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. @@ -205,6 +214,10 @@ public void metadata(@PathParam("idpConfigId") final String idpConfigId, Logger.error(this, dotSamlException.getMessage(), dotSamlException); + } catch (DotSamlByPassException exception) { + + Logger.error(this, exception.getMessage(), exception); + } catch (DotDataException dotDataException) { Logger.error(this, "Idp not found (" + dotDataException.getMessage() + ")", dotDataException); diff --git a/src/com/dotcms/plugin/saml/v3/rest/api/v1/DotSamlResource.java b/src/com/dotcms/plugin/saml/v3/rest/api/v1/DotSamlResource.java index 6224b04..09c7711 100755 --- a/src/com/dotcms/plugin/saml/v3/rest/api/v1/DotSamlResource.java +++ b/src/com/dotcms/plugin/saml/v3/rest/api/v1/DotSamlResource.java @@ -1,12 +1,26 @@ package com.dotcms.plugin.saml.v3.rest.api.v1; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; +import java.io.StringReader; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.FileUtils; + import com.dotcms.plugin.saml.v3.config.IdpConfig; import com.dotcms.plugin.saml.v3.config.IdpConfigHelper; import com.dotcms.plugin.saml.v3.config.IdpConfigWriterReader; +import com.dotcms.plugin.saml.v3.exception.DotSamlByPassException; import com.dotcms.plugin.saml.v3.exception.DotSamlException; import com.dotcms.plugin.saml.v3.filter.SamlFilter; import com.dotcms.plugin.saml.v3.util.pagination.IdpConfigPaginator; - import com.dotcms.repackage.javax.ws.rs.Consumes; import com.dotcms.repackage.javax.ws.rs.DELETE; import com.dotcms.repackage.javax.ws.rs.DefaultValue; @@ -22,7 +36,6 @@ import com.dotcms.repackage.org.glassfish.jersey.media.multipart.FormDataContentDisposition; import com.dotcms.repackage.org.glassfish.jersey.media.multipart.FormDataParam; import com.dotcms.repackage.org.glassfish.jersey.server.JSONP; - import com.dotcms.rest.InitDataObject; import com.dotcms.rest.ResponseEntityView; import com.dotcms.rest.WebResource; @@ -30,520 +43,451 @@ import com.dotcms.rest.exception.mapper.ExceptionMapperUtil; import com.dotcms.util.CollectionsUtils; import com.dotcms.util.PaginationUtil; - import com.dotmarketing.exception.DotDataException; import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; import com.dotmarketing.util.json.JSONException; - import com.fasterxml.jackson.databind.ObjectMapper; - import com.liferay.portal.model.User; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.Serializable; -import java.io.StringReader; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.io.FileUtils; - -@Path( "/v1/dotsaml" ) -public class DotSamlResource implements Serializable -{ +@Path("/v1/dotsaml") +public class DotSamlResource implements Serializable { private static final long serialVersionUID = 8015545653539491684L; private final IdpConfigHelper idpConfigHelper; private final WebResource webResource; private final PaginationUtil paginationUtil; - public DotSamlResource() - { + public DotSamlResource() { this.idpConfigHelper = IdpConfigHelper.getInstance(); this.webResource = new WebResource(); - this.paginationUtil = new PaginationUtil( new IdpConfigPaginator() ); + this.paginationUtil = new PaginationUtil(new IdpConfigPaginator()); } - @SuppressWarnings( "unchecked" ) + @SuppressWarnings("unchecked") @POST - @Path( "/idp" ) + @Path("/idp") @JSONP @NoCache - @Consumes( MediaType.MULTIPART_FORM_DATA ) - @Produces( { MediaType.APPLICATION_JSON, "application/javascript" } ) + @Consumes(MediaType.MULTIPART_FORM_DATA) + @Produces({ MediaType.APPLICATION_JSON, "application/javascript" }) // Saves an idp config. - public final Response createIdpConfig( @Context final HttpServletRequest request, @FormDataParam( "id" ) String id, @FormDataParam( "idpName" ) String idpName, @FormDataParam( "enabled" ) boolean enabled, @FormDataParam( "sPIssuerURL" ) String sPIssuerURL, @FormDataParam( "sPEndpointHostname" ) String sPEndpointHostname, @FormDataParam( "privateKey" ) InputStream privateKeyStream, @FormDataParam( "privateKey" ) FormDataContentDisposition privateKeyFileDetail, @FormDataParam( "publicCert" ) InputStream publicCertStream, @FormDataParam( "publicCert" ) FormDataContentDisposition publicCertFileDetail, @FormDataParam( "idPMetadataFile" ) InputStream idPMetadataFileStream, @FormDataParam( "idPMetadataFile" ) FormDataContentDisposition idPMetadataFileDetail, @FormDataParam( "signatureValidationType" ) String signatureValidationType, @FormDataParam( "optionalProperties" ) String optionalProperties, @FormDataParam( "sites" ) String sites ) - { - this.webResource.init( null, true, request, true, null ); + public final Response createIdpConfig(@Context final HttpServletRequest request, @FormDataParam("id") String id, + @FormDataParam("idpName") String idpName, @FormDataParam("enabled") boolean enabled, + @FormDataParam("sPIssuerURL") String sPIssuerURL, + @FormDataParam("sPEndpointHostname") String sPEndpointHostname, + @FormDataParam("privateKey") InputStream privateKeyStream, + @FormDataParam("privateKey") FormDataContentDisposition privateKeyFileDetail, + @FormDataParam("publicCert") InputStream publicCertStream, + @FormDataParam("publicCert") FormDataContentDisposition publicCertFileDetail, + @FormDataParam("idPMetadataFile") InputStream idPMetadataFileStream, + @FormDataParam("idPMetadataFile") FormDataContentDisposition idPMetadataFileDetail, + @FormDataParam("signatureValidationType") String signatureValidationType, + @FormDataParam("optionalProperties") String optionalProperties, @FormDataParam("sites") String sites) { + this.webResource.init(null, true, request, true, null); Response response; - try - { + try { IdpConfig idpConfig; - if ( UtilMethods.isSet( id ) ) - { - idpConfig = idpConfigHelper.findIdpConfig( id ); - } - else - { + if (UtilMethods.isSet(id)) { + idpConfig = idpConfigHelper.findIdpConfig(id); + } else { idpConfig = new IdpConfig(); } - idpConfig.setIdpName( idpName ); - idpConfig.setEnabled( enabled ); - idpConfig.setSpIssuerURL( sPIssuerURL ); - idpConfig.setSpEndpointHostname( sPEndpointHostname ); + idpConfig.setIdpName(idpName); + idpConfig.setEnabled(enabled); + idpConfig.setSpIssuerURL(sPIssuerURL); + idpConfig.setSpEndpointHostname(sPEndpointHostname); - if ( UtilMethods.isSet( privateKeyFileDetail ) && UtilMethods.isSet( privateKeyFileDetail.getFileName() ) ) - { - File privateKey = File.createTempFile( "privateKey", "key" ); - FileUtils.copyInputStreamToFile( privateKeyStream, privateKey ); - idpConfig.setPrivateKey( privateKey ); + if (UtilMethods.isSet(privateKeyFileDetail) && UtilMethods.isSet(privateKeyFileDetail.getFileName())) { + File privateKey = File.createTempFile("privateKey", "key"); + FileUtils.copyInputStreamToFile(privateKeyStream, privateKey); + idpConfig.setPrivateKey(privateKey); } - if ( UtilMethods.isSet( publicCertFileDetail ) && UtilMethods.isSet( publicCertFileDetail.getFileName() ) ) - { - File publicCert = File.createTempFile( "publicCert", "crt" ); - FileUtils.copyInputStreamToFile( publicCertStream, publicCert ); - idpConfig.setPublicCert( publicCert ); + if (UtilMethods.isSet(publicCertFileDetail) && UtilMethods.isSet(publicCertFileDetail.getFileName())) { + File publicCert = File.createTempFile("publicCert", "crt"); + FileUtils.copyInputStreamToFile(publicCertStream, publicCert); + idpConfig.setPublicCert(publicCert); } - if ( UtilMethods.isSet( idPMetadataFileDetail ) && UtilMethods.isSet( idPMetadataFileDetail.getFileName() ) ) - { - File idPMetadataFile = File.createTempFile( "idPMetadataFile", "xml" ); - FileUtils.copyInputStreamToFile( idPMetadataFileStream, idPMetadataFile ); - idpConfig.setIdPMetadataFile( idPMetadataFile ); + if (UtilMethods.isSet(idPMetadataFileDetail) && UtilMethods.isSet(idPMetadataFileDetail.getFileName())) { + File idPMetadataFile = File.createTempFile("idPMetadataFile", "xml"); + FileUtils.copyInputStreamToFile(idPMetadataFileStream, idPMetadataFile); + idpConfig.setIdPMetadataFile(idPMetadataFile); } - idpConfig.setSignatureValidationType( signatureValidationType ); + idpConfig.setSignatureValidationType(signatureValidationType); - if ( UtilMethods.isSet( optionalProperties ) ) - { + if (UtilMethods.isSet(optionalProperties)) { final Properties parsedProperties = new Properties(); - parsedProperties.load( new StringReader( optionalProperties ) ); - idpConfig.setOptionalProperties( parsedProperties ); + parsedProperties.load(new StringReader(optionalProperties)); + idpConfig.setOptionalProperties(parsedProperties); } - HashMap sitesMap = new ObjectMapper().readValue( sites, HashMap.class ); - idpConfig.setSites( sitesMap ); + HashMap sitesMap = new ObjectMapper().readValue(sites, HashMap.class); + idpConfig.setSites(sitesMap); - idpConfig = idpConfigHelper.saveIdpConfig( idpConfig ); + idpConfig = idpConfigHelper.saveIdpConfig(idpConfig); - response = Response.ok( new ResponseEntityView( idpConfig ) ).build(); - } - catch ( IOException ioException ) - { - Logger.error( this, "Idp is not valid (" + ioException.getMessage() + ")", ioException ); - response = ExceptionMapperUtil.createResponse( null, "Idp is not valid (" + ioException.getMessage() + ")" ); - } - catch ( JSONException jsonException ) - { - Logger.error( this, "Error handling json (" + jsonException.getMessage() + ")", jsonException ); - response = ExceptionMapperUtil.createResponse( null, "Error handling json (" + jsonException.getMessage() + ")" ); - } - catch ( Exception exception ) - { + response = Response.ok(new ResponseEntityView(idpConfig)).build(); + } catch (IOException ioException) { + Logger.error(this, "Idp is not valid (" + ioException.getMessage() + ")", ioException); + response = ExceptionMapperUtil.createResponse(null, "Idp is not valid (" + ioException.getMessage() + ")"); + } catch (JSONException jsonException) { + Logger.error(this, "Error handling json (" + jsonException.getMessage() + ")", jsonException); + response = ExceptionMapperUtil.createResponse(null, + "Error handling json (" + jsonException.getMessage() + ")"); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. - Logger.error( this, "Error getting posting idp", exception ); - response = ExceptionMapperUtil.createResponse( exception, Response.Status.INTERNAL_SERVER_ERROR ); + Logger.error(this, "Error getting posting idp", exception); + response = ExceptionMapperUtil.createResponse(exception, Response.Status.INTERNAL_SERVER_ERROR); } return response; } @DELETE - @Path( "/idp/{id}" ) + @Path("/idp/{id}") @JSONP @NoCache - @Produces( { MediaType.APPLICATION_JSON, "application/javascript" } ) + @Produces({ MediaType.APPLICATION_JSON, "application/javascript" }) // Deletes an idp config. - public Response deleteIdpConfig( @PathParam( "id" ) final String id, @Context final HttpServletRequest request ) - { - this.webResource.init( null, true, request, true, null ); + public Response deleteIdpConfig(@PathParam("id") final String id, @Context final HttpServletRequest request) { + this.webResource.init(null, true, request, true, null); Response response; - try - { + try { IdpConfig idpConfig = new IdpConfig(); - idpConfig.setId( id ); - - idpConfigHelper.deleteIdpConfig( idpConfig ); - - response = Response.ok( new ResponseEntityView( CollectionsUtils.map( "deleted", id ) ) ).build(); - } - catch ( IOException ioException ) - { - Logger.error( this, "Idp is not valid (" + ioException.getMessage() + ")", ioException ); - response = ExceptionMapperUtil.createResponse( null, "Idp is not valid (" + ioException.getMessage() + ")" ); - } - catch ( JSONException jsonException ) - { - Logger.error( this, "Error handling json (" + jsonException.getMessage() + ")", jsonException ); - response = ExceptionMapperUtil.createResponse( null, "Error handling json (" + jsonException.getMessage() + ")" ); - } - catch ( Exception exception ) - { + idpConfig.setId(id); + + idpConfigHelper.deleteIdpConfig(idpConfig); + + response = Response.ok(new ResponseEntityView(CollectionsUtils.map("deleted", id))).build(); + } catch (IOException ioException) { + Logger.error(this, "Idp is not valid (" + ioException.getMessage() + ")", ioException); + response = ExceptionMapperUtil.createResponse(null, "Idp is not valid (" + ioException.getMessage() + ")"); + } catch (JSONException jsonException) { + Logger.error(this, "Error handling json (" + jsonException.getMessage() + ")", jsonException); + response = ExceptionMapperUtil.createResponse(null, + "Error handling json (" + jsonException.getMessage() + ")"); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. - Logger.error( this, "Error deleting idps", exception ); - response = ExceptionMapperUtil.createResponse( exception, Response.Status.INTERNAL_SERVER_ERROR ); + Logger.error(this, "Error deleting idps", exception); + response = ExceptionMapperUtil.createResponse(exception, Response.Status.INTERNAL_SERVER_ERROR); } return response; } @GET - @Path( "/default" ) + @Path("/default") @JSONP @NoCache - @Produces( { MediaType.APPLICATION_JSON, "application/javascript" } ) + @Produces({ MediaType.APPLICATION_JSON, "application/javascript" }) // Gets default idp config id - public Response getDefault( @Context final HttpServletRequest request ) - { - this.webResource.init( null, true, request, true, null ); + public Response getDefault(@Context final HttpServletRequest request) { + this.webResource.init(null, true, request, true, null); Response response; - try - { + try { final String defaultIdpConfigId = idpConfigHelper.getDefaultIdpConfigId(); - response = Response.ok( new ResponseEntityView( CollectionsUtils.map( IdpConfigWriterReader.DEFAULT_SAML_CONFIG, defaultIdpConfigId ) ) ).build(); - } - catch ( IOException ioException ) - { - Logger.error( this, "Error reading file with Idps (" + ioException.getMessage() + ")", ioException ); - response = ExceptionMapperUtil.createResponse( null, "Idp is not valid (" + ioException.getMessage() + ")" ); - } - catch ( JSONException jsonException ) - { - Logger.error( this, "Error handling json with Idps (" + jsonException.getMessage() + ")", jsonException ); - response = ExceptionMapperUtil.createResponse( null, "Error handling json (" + jsonException.getMessage() + ")" ); - } - catch ( Exception exception ) - { + response = Response + .ok(new ResponseEntityView( + CollectionsUtils.map(IdpConfigWriterReader.DEFAULT_SAML_CONFIG, defaultIdpConfigId))) + .build(); + } catch (IOException ioException) { + Logger.error(this, "Error reading file with Idps (" + ioException.getMessage() + ")", ioException); + response = ExceptionMapperUtil.createResponse(null, "Idp is not valid (" + ioException.getMessage() + ")"); + } catch (JSONException jsonException) { + Logger.error(this, "Error handling json with Idps (" + jsonException.getMessage() + ")", jsonException); + response = ExceptionMapperUtil.createResponse(null, + "Error handling json (" + jsonException.getMessage() + ")"); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. - Logger.error( this, "Error getting default idp", exception ); - response = ExceptionMapperUtil.createResponse( exception, Response.Status.INTERNAL_SERVER_ERROR ); + Logger.error(this, "Error getting default idp", exception); + response = ExceptionMapperUtil.createResponse(exception, Response.Status.INTERNAL_SERVER_ERROR); } return response; } @GET - @Path( "/disabledsites" ) + @Path("/disabledsites") @JSONP @NoCache - @Produces( { MediaType.APPLICATION_JSON, "application/javascript" } ) + @Produces({ MediaType.APPLICATION_JSON, "application/javascript" }) // Gets disabled sites map - public Response getDisabledSites( @Context final HttpServletRequest request ) - { - this.webResource.init( null, true, request, true, null ); + public Response getDisabledSites(@Context final HttpServletRequest request) { + this.webResource.init(null, true, request, true, null); Response response; - try - { + try { final Map disabledSiteIds = idpConfigHelper.getDisabledSiteIds(); - response = Response.ok( new ResponseEntityView( CollectionsUtils.map( IdpConfigWriterReader.DISABLE_SAML_SITES, disabledSiteIds ) ) ).build(); - } - catch ( IOException ioException ) - { - Logger.error( this, "Error reading file with disabled sites (" + ioException.getMessage() + ")", ioException ); - response = ExceptionMapperUtil.createResponse( null, "disable site is not valid (" + ioException.getMessage() + ")" ); - } - catch ( JSONException jsonException ) - { - Logger.error( this, "Error handling json with Idps (" + jsonException.getMessage() + ")", jsonException ); - response = ExceptionMapperUtil.createResponse( null, "Error handling disabled site json (" + jsonException.getMessage() + ")" ); - } - catch ( Exception exception ) - { + response = Response.ok(new ResponseEntityView( + CollectionsUtils.map(IdpConfigWriterReader.DISABLE_SAML_SITES, disabledSiteIds))).build(); + } catch (IOException ioException) { + Logger.error(this, "Error reading file with disabled sites (" + ioException.getMessage() + ")", + ioException); + response = ExceptionMapperUtil.createResponse(null, + "disable site is not valid (" + ioException.getMessage() + ")"); + } catch (JSONException jsonException) { + Logger.error(this, "Error handling json with Idps (" + jsonException.getMessage() + ")", jsonException); + response = ExceptionMapperUtil.createResponse(null, + "Error handling disabled site json (" + jsonException.getMessage() + ")"); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. - Logger.error( this, "Error getting default idp", exception ); - response = ExceptionMapperUtil.createResponse( exception, Response.Status.INTERNAL_SERVER_ERROR ); + Logger.error(this, "Error getting default idp", exception); + response = ExceptionMapperUtil.createResponse(exception, Response.Status.INTERNAL_SERVER_ERROR); } return response; } @GET - @Path( "/idp/{id}" ) + @Path("/idp/{id}") @JSONP @NoCache - @Produces( { MediaType.APPLICATION_JSON, "application/javascript" } ) + @Produces({ MediaType.APPLICATION_JSON, "application/javascript" }) // Gets an idp configuration by id - public Response getIdp( @PathParam( "id" ) final String id, @Context final HttpServletRequest request ) - { + public Response getIdp(@PathParam("id") final String id, @Context final HttpServletRequest request) { Response response; - try - { - final IdpConfig idpConfig = idpConfigHelper.findIdpConfig( id ); - response = Response.ok( new ResponseEntityView( idpConfig ) ).build(); - } - catch ( DotDataException dotDataException ) - { - Logger.error( this, "Idp not found (" + dotDataException.getMessage() + ")", dotDataException ); - response = ExceptionMapperUtil.createResponse( null, "Idp not found (" + dotDataException.getMessage() + ")" ); - } - catch ( IOException ioException ) - { - Logger.error( this, "Idp is not valid (" + ioException.getMessage() + ")", ioException ); - response = ExceptionMapperUtil.createResponse( null, "Idp is not valid (" + ioException.getMessage() + ")" ); - } - catch ( JSONException jsonException ) - { - Logger.error( this, "Error handling json (" + jsonException.getMessage() + ")", jsonException ); - response = ExceptionMapperUtil.createResponse( null, "Error handling json (" + jsonException.getMessage() + ")" ); - } - catch ( Exception exception ) - { + try { + final IdpConfig idpConfig = idpConfigHelper.findIdpConfig(id); + response = Response.ok(new ResponseEntityView(idpConfig)).build(); + } catch (DotSamlByPassException dotDataException) { + Logger.error(this, "Idp not found in cache (" + dotDataException.getMessage() + ")", dotDataException); + response = ExceptionMapperUtil.createResponse(null, + "Idp not found in cache (" + dotDataException.getMessage() + ")"); + } catch (DotDataException dotDataException) { + Logger.error(this, "Idp not found (" + dotDataException.getMessage() + ")", dotDataException); + response = ExceptionMapperUtil.createResponse(null, + "Idp not found (" + dotDataException.getMessage() + ")"); + } catch (IOException ioException) { + Logger.error(this, "Idp is not valid (" + ioException.getMessage() + ")", ioException); + response = ExceptionMapperUtil.createResponse(null, "Idp is not valid (" + ioException.getMessage() + ")"); + } catch (JSONException jsonException) { + Logger.error(this, "Error handling json (" + jsonException.getMessage() + ")", jsonException); + response = ExceptionMapperUtil.createResponse(null, + "Error handling json (" + jsonException.getMessage() + ")"); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. - Logger.error( this, "Error getting posting idp", exception ); - response = ExceptionMapperUtil.createResponse( exception, Response.Status.INTERNAL_SERVER_ERROR ); + Logger.error(this, "Error getting posting idp", exception); + response = ExceptionMapperUtil.createResponse(exception, Response.Status.INTERNAL_SERVER_ERROR); } return response; } @GET - @Path( "/idps" ) + @Path("/idps") @JSONP @NoCache - @Produces( { MediaType.APPLICATION_JSON, "application/javascript" } ) + @Produces({ MediaType.APPLICATION_JSON, "application/javascript" }) // Gets paginated list of all idp configurations - public final Response getIdps( @Context final HttpServletRequest request, @QueryParam( PaginationUtil.FILTER ) final String filter, @QueryParam( PaginationUtil.PAGE ) final int page, @QueryParam( PaginationUtil.PER_PAGE ) final int perPage, @DefaultValue( "upper(name)" ) @QueryParam( PaginationUtil.ORDER_BY ) String orderbyParam, @DefaultValue( "ASC" ) @QueryParam( PaginationUtil.DIRECTION ) String direction ) - { - final InitDataObject initData = this.webResource.init( null, true, request, true, null ); + public final Response getIdps(@Context final HttpServletRequest request, + @QueryParam(PaginationUtil.FILTER) final String filter, @QueryParam(PaginationUtil.PAGE) final int page, + @QueryParam(PaginationUtil.PER_PAGE) final int perPage, + @DefaultValue("upper(name)") @QueryParam(PaginationUtil.ORDER_BY) String orderbyParam, + @DefaultValue("ASC") @QueryParam(PaginationUtil.DIRECTION) String direction) { + final InitDataObject initData = this.webResource.init(null, true, request, true, null); final User user = initData.getUser(); Response response; - try - { - response = this.paginationUtil.getPage( request, user, filter, page, perPage, orderbyParam, direction ); - } - catch ( Exception exception ) - { + try { + response = this.paginationUtil.getPage(request, user, filter, page, perPage, orderbyParam, direction); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. - Logger.error( this, "Error getting idps", exception ); - response = ExceptionMapperUtil.createResponse( exception, Response.Status.INTERNAL_SERVER_ERROR ); + Logger.error(this, "Error getting idps", exception); + response = ExceptionMapperUtil.createResponse(exception, Response.Status.INTERNAL_SERVER_ERROR); } return response; } @GET - @Path( "/login/{idpConfigId}" ) + @Path("/login/{idpConfigId}") @JSONP @NoCache - @Produces( { MediaType.APPLICATION_JSON, "application/javascript" } ) + @Produces({ MediaType.APPLICATION_JSON, "application/javascript" }) // Login configuration by id - public Response login( @PathParam( "idpConfigId" ) final String idpConfigId, @Context final HttpServletRequest httpServletRequest, @Context final HttpServletResponse httpServletResponse ) - { + public Response login(@PathParam("idpConfigId") final String idpConfigId, + @Context final HttpServletRequest httpServletRequest, + @Context final HttpServletResponse httpServletResponse) { Response response; - try - { - final IdpConfig idpConfig = idpConfigHelper.findIdpConfig( idpConfigId ); + try { + final IdpConfig idpConfig = idpConfigHelper.findIdpConfig(idpConfigId); - // If idpConfig is null, means this site does not need SAML processing - if ( idpConfig != null && idpConfig.isEnabled() ) - { - Logger.debug( this, "Processing saml login request for idpConfig id: " + idpConfigId ); + // If idpConfig is null, means this site does not need SAML + // processing + if (idpConfig != null && idpConfig.isEnabled()) { + Logger.debug(this, "Processing saml login request for idpConfig id: " + idpConfigId); SamlFilter samlFilter = new SamlFilter(); - samlFilter.doRequestLoginSecurityLog( httpServletRequest, idpConfig ); + samlFilter.doRequestLoginSecurityLog(httpServletRequest, idpConfig); - try - { + try { // This will redirect the user to the IdP Login Page. - samlFilter.samlAuthenticationService.authentication( httpServletRequest, httpServletResponse, idpConfig ); - } - catch ( DotSamlException | DotDataException exception ) - { - Logger.error( this, "Error on authentication: " + exception.getMessage(), exception ); - Logger.debug( this, "Error on authentication, setting 500 response status." ); - response = ExceptionMapperUtil.createResponse( exception, Response.Status.INTERNAL_SERVER_ERROR ); + samlFilter.samlAuthenticationService.authentication(httpServletRequest, httpServletResponse, + idpConfig); + } catch (DotSamlException | DotDataException exception) { + Logger.error(this, "Error on authentication: " + exception.getMessage(), exception); + Logger.debug(this, "Error on authentication, setting 500 response status."); + response = ExceptionMapperUtil.createResponse(exception, Response.Status.INTERNAL_SERVER_ERROR); } response = Response.ok().build(); + } else { + String message = "No idpConfig for idpConfigId: " + idpConfigId + ". At " + + httpServletRequest.getRequestURI(); + Logger.debug(this, message); + throw new DotSamlException(message); } - else - { - String message = "No idpConfig for idpConfigId: " + idpConfigId + ". At " + httpServletRequest.getRequestURI(); - Logger.debug( this, message ); - throw new DotSamlException( message ); - } - } - catch ( DotSamlException dotSamlException ) - { - Logger.error( this, dotSamlException.getMessage(), dotSamlException ); - response = ExceptionMapperUtil.createResponse( null, dotSamlException.getMessage() ); - } - catch ( DotDataException dotDataException ) - { - Logger.error( this, "Idp not found (" + dotDataException.getMessage() + ")", dotDataException ); - response = ExceptionMapperUtil.createResponse( null, "Idp not found (" + dotDataException.getMessage() + ")" ); - } - catch ( IOException ioException ) - { - Logger.error( this, "Idp is not valid (" + ioException.getMessage() + ")", ioException ); - response = ExceptionMapperUtil.createResponse( null, "Idp is not valid (" + ioException.getMessage() + ")" ); - } - catch ( JSONException jsonException ) - { - Logger.error( this, "Error handling json (" + jsonException.getMessage() + ")", jsonException ); - response = ExceptionMapperUtil.createResponse( null, "Error handling json (" + jsonException.getMessage() + ")" ); - } - catch ( Exception exception ) - { + } catch (DotSamlException dotSamlException) { + Logger.error(this, dotSamlException.getMessage(), dotSamlException); + response = ExceptionMapperUtil.createResponse(null, dotSamlException.getMessage()); + } catch (DotSamlByPassException dotDataException) { + Logger.error(this, "Idp not found in cache (" + dotDataException.getMessage() + ")", dotDataException); + response = ExceptionMapperUtil.createResponse(null, + "Idp not found in cache (" + dotDataException.getMessage() + ")"); + } catch (DotDataException dotDataException) { + Logger.error(this, "Idp not found (" + dotDataException.getMessage() + ")", dotDataException); + response = ExceptionMapperUtil.createResponse(null, + "Idp not found (" + dotDataException.getMessage() + ")"); + } catch (IOException ioException) { + Logger.error(this, "Idp is not valid (" + ioException.getMessage() + ")", ioException); + response = ExceptionMapperUtil.createResponse(null, "Idp is not valid (" + ioException.getMessage() + ")"); + } catch (JSONException jsonException) { + Logger.error(this, "Error handling json (" + jsonException.getMessage() + ")", jsonException); + response = ExceptionMapperUtil.createResponse(null, + "Error handling json (" + jsonException.getMessage() + ")"); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. - Logger.error( this, "Error getting posting idp", exception ); - response = ExceptionMapperUtil.createResponse( exception, Response.Status.INTERNAL_SERVER_ERROR ); + Logger.error(this, "Error getting posting idp", exception); + response = ExceptionMapperUtil.createResponse(exception, Response.Status.INTERNAL_SERVER_ERROR); } return response; } @GET - @Path( "/metadata/{idpConfigId}" ) + @Path("/metadata/{idpConfigId}") @JSONP @NoCache - @Produces( { MediaType.APPLICATION_JSON, "application/javascript" } ) + @Produces({ MediaType.APPLICATION_JSON, "application/javascript" }) // Gets metadata configuration by id - public void metadata( @PathParam( "idpConfigId" ) final String idpConfigId, @Context final HttpServletRequest httpServletRequest, @Context final HttpServletResponse httpServletResponse ) - { - try - { - final IdpConfig idpConfig = idpConfigHelper.findIdpConfig( idpConfigId ); - - // If idpConfig is null, means this site does not need SAML processing - if ( idpConfig != null ) - { - Logger.debug( this, "Processing saml login request for idpConfig id: " + idpConfigId ); + public void metadata(@PathParam("idpConfigId") final String idpConfigId, + @Context final HttpServletRequest httpServletRequest, + @Context final HttpServletResponse httpServletResponse) { + try { + final IdpConfig idpConfig = idpConfigHelper.findIdpConfig(idpConfigId); + + // If idpConfig is null, means this site does not need SAML + // processing + if (idpConfig != null) { + Logger.debug(this, "Processing saml login request for idpConfig id: " + idpConfigId); SamlFilter samlFilter = new SamlFilter(); - samlFilter.printMetaData( httpServletRequest, httpServletResponse, idpConfig ); + samlFilter.printMetaData(httpServletRequest, httpServletResponse, idpConfig); + } else { + String message = "No idpConfig for idpConfigId: " + idpConfigId + ". At " + + httpServletRequest.getRequestURI(); + Logger.debug(this, message); + throw new DotSamlException(message); } - else - { - String message = "No idpConfig for idpConfigId: " + idpConfigId + ". At " + httpServletRequest.getRequestURI(); - Logger.debug( this, message ); - throw new DotSamlException( message ); - } - } - catch ( DotSamlException dotSamlException ) - { - Logger.error( this, dotSamlException.getMessage(), dotSamlException ); - } - catch ( DotDataException dotDataException ) - { - Logger.error( this, "Idp not found (" + dotDataException.getMessage() + ")", dotDataException ); - } - catch ( IOException ioException ) - { - Logger.error( this, "Idp is not valid (" + ioException.getMessage() + ")", ioException ); - } - catch ( JSONException jsonException ) - { - Logger.error( this, "Error handling json (" + jsonException.getMessage() + ")", jsonException ); - } - catch ( Exception exception ) - { + } catch (DotSamlException dotSamlException) { + Logger.error(this, dotSamlException.getMessage(), dotSamlException); + } catch (DotSamlByPassException dotDataException) { + Logger.error(this, "Idp not found in cache (" + dotDataException.getMessage() + ")", dotDataException); + }catch (DotDataException dotDataException) { + Logger.error(this, "Idp not found (" + dotDataException.getMessage() + ")", dotDataException); + } catch (IOException ioException) { + Logger.error(this, "Idp is not valid (" + ioException.getMessage() + ")", ioException); + } catch (JSONException jsonException) { + Logger.error(this, "Error handling json (" + jsonException.getMessage() + ")", jsonException); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. - Logger.error( this, "Error getting posting idp", exception ); + Logger.error(this, "Error getting posting idp", exception); } } - @SuppressWarnings( "unchecked" ) + @SuppressWarnings("unchecked") @POST - @Path( "/disabledsites" ) + @Path("/disabledsites") @JSONP @NoCache - @Consumes( MediaType.MULTIPART_FORM_DATA ) - @Produces( { MediaType.APPLICATION_JSON, "application/javascript" } ) + @Consumes(MediaType.MULTIPART_FORM_DATA) + @Produces({ MediaType.APPLICATION_JSON, "application/javascript" }) // Save disabled sites map - public final Response saveDisabledSited( @Context final HttpServletRequest request, @FormDataParam( "disabledsites" ) String disabledSites ) - { - this.webResource.init( null, true, request, true, null ); + public final Response saveDisabledSited(@Context final HttpServletRequest request, + @FormDataParam("disabledsites") String disabledSites) { + this.webResource.init(null, true, request, true, null); Response response; - try - { - HashMap disabledSitesMap = new ObjectMapper().readValue( disabledSites, HashMap.class ); - idpConfigHelper.saveDisabledSiteIds( disabledSitesMap ); + try { + HashMap disabledSitesMap = new ObjectMapper().readValue(disabledSites, HashMap.class); + idpConfigHelper.saveDisabledSiteIds(disabledSitesMap); response = Response.ok().build(); - } - catch ( IOException ioException ) - { - Logger.error( this, "Error reading file with disabled sites (" + ioException.getMessage() + ")", ioException ); - response = ExceptionMapperUtil.createResponse( null, "disable site is not valid (" + ioException.getMessage() + ")" ); - } - catch ( JSONException jsonException ) - { - Logger.error( this, "Error handling json with Idps (" + jsonException.getMessage() + ")", jsonException ); - response = ExceptionMapperUtil.createResponse( null, "Error handling disabled site json (" + jsonException.getMessage() + ")" ); - } - catch ( Exception exception ) - { + } catch (IOException ioException) { + Logger.error(this, "Error reading file with disabled sites (" + ioException.getMessage() + ")", + ioException); + response = ExceptionMapperUtil.createResponse(null, + "disable site is not valid (" + ioException.getMessage() + ")"); + } catch (JSONException jsonException) { + Logger.error(this, "Error handling json with Idps (" + jsonException.getMessage() + ")", jsonException); + response = ExceptionMapperUtil.createResponse(null, + "Error handling disabled site json (" + jsonException.getMessage() + ")"); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. - Logger.error( this, "Error getting default idp", exception ); - response = ExceptionMapperUtil.createResponse( exception, Response.Status.INTERNAL_SERVER_ERROR ); + Logger.error(this, "Error getting default idp", exception); + response = ExceptionMapperUtil.createResponse(exception, Response.Status.INTERNAL_SERVER_ERROR); } return response; } @POST - @Path( "/default/{id}" ) + @Path("/default/{id}") @JSONP @NoCache - @Produces( { MediaType.APPLICATION_JSON, "application/javascript" } ) + @Produces({ MediaType.APPLICATION_JSON, "application/javascript" }) // Sets default idp configuration id - public Response setDefault( @PathParam( "id" ) final String id, @Context final HttpServletRequest request ) - { - this.webResource.init( null, true, request, true, null ); + public Response setDefault(@PathParam("id") final String id, @Context final HttpServletRequest request) { + this.webResource.init(null, true, request, true, null); Response response; - try - { - idpConfigHelper.setDefaultIdpConfig( id ); - - response = Response.ok( new ResponseEntityView( CollectionsUtils.map( "default", id ) ) ).build(); - } - catch ( DotDataException dotDataException ) - { - Logger.error( this, "Idp not found (" + dotDataException.getMessage() + ")", dotDataException ); - response = ExceptionMapperUtil.createResponse( null, "Idp not found (" + dotDataException.getMessage() + ")" ); - } - catch ( IOException ioException ) - { - Logger.error( this, "Idp is not valid (" + ioException.getMessage() + ")", ioException ); - response = ExceptionMapperUtil.createResponse( null, "Idp is not valid (" + ioException.getMessage() + ")" ); - } - catch ( JSONException jsonException ) - { - Logger.error( this, "Error handling json (" + jsonException.getMessage() + ")", jsonException ); - response = ExceptionMapperUtil.createResponse( null, "Error handling json (" + jsonException.getMessage() + ")" ); - } - catch ( Exception exception ) - { + try { + idpConfigHelper.setDefaultIdpConfig(id); + + response = Response.ok(new ResponseEntityView(CollectionsUtils.map("default", id))).build(); + } catch (DotDataException dotDataException) { + Logger.error(this, "Idp not found (" + dotDataException.getMessage() + ")", dotDataException); + response = ExceptionMapperUtil.createResponse(null, + "Idp not found (" + dotDataException.getMessage() + ")"); + } catch (IOException ioException) { + Logger.error(this, "Idp is not valid (" + ioException.getMessage() + ")", ioException); + response = ExceptionMapperUtil.createResponse(null, "Idp is not valid (" + ioException.getMessage() + ")"); + } catch (JSONException jsonException) { + Logger.error(this, "Error handling json (" + jsonException.getMessage() + ")", jsonException); + response = ExceptionMapperUtil.createResponse(null, + "Error handling json (" + jsonException.getMessage() + ")"); + } catch (Exception exception) { // this is an unknown error, so we report as a 500. - Logger.error( this, "Error getting setting idp", exception ); - response = ExceptionMapperUtil.createResponse( exception, Response.Status.INTERNAL_SERVER_ERROR ); + Logger.error(this, "Error getting setting idp", exception); + response = ExceptionMapperUtil.createResponse(exception, Response.Status.INTERNAL_SERVER_ERROR); } return response; diff --git a/src/com/dotcms/plugin/saml/v3/util/SiteIdpConfigResolver.java b/src/com/dotcms/plugin/saml/v3/util/SiteIdpConfigResolver.java index fd1d892..b67fdf0 100644 --- a/src/com/dotcms/plugin/saml/v3/util/SiteIdpConfigResolver.java +++ b/src/com/dotcms/plugin/saml/v3/util/SiteIdpConfigResolver.java @@ -34,6 +34,7 @@ public static SiteIdpConfigResolver getInstance() { return SiteIdpConfigResolver.SingletonHolder.INSTANCE; } + /** * Returns the Configuration associate to the current site, if it is not diff --git a/src/com/dotcms/plugin/saml/v3/util/pagination/IdpConfigPaginator.java b/src/com/dotcms/plugin/saml/v3/util/pagination/IdpConfigPaginator.java index 8465861..295194d 100755 --- a/src/com/dotcms/plugin/saml/v3/util/pagination/IdpConfigPaginator.java +++ b/src/com/dotcms/plugin/saml/v3/util/pagination/IdpConfigPaginator.java @@ -1,65 +1,62 @@ package com.dotcms.plugin.saml.v3.util.pagination; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; + import com.dotcms.plugin.saml.v3.config.IdpConfig; import com.dotcms.plugin.saml.v3.config.IdpConfigHelper; - +import com.dotcms.plugin.saml.v3.exception.DotSamlByPassException; import com.dotcms.util.pagination.OrderDirection; import com.dotcms.util.pagination.Paginator; - import com.dotmarketing.exception.DotRuntimeException; import com.dotmarketing.util.Logger; import com.dotmarketing.util.UtilMethods; import com.dotmarketing.util.json.JSONException; - import com.liferay.portal.model.User; -import java.io.IOException; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.Collectors; - -public class IdpConfigPaginator implements Paginator -{ - private final AtomicInteger lastTotalRecords = new AtomicInteger( 0 ); +public class IdpConfigPaginator implements Paginator { + private final AtomicInteger lastTotalRecords = new AtomicInteger(0); - public IdpConfigPaginator() - { + public IdpConfigPaginator() { } @Override - public long getTotalRecords( String s ) - { + public long getTotalRecords(String s) { return lastTotalRecords.get(); } @Override - public Collection getItems( final User user, final String filter, final int limit, final int offset, final String orderby, final OrderDirection direction, final Map extraParams ) - { - try - { + public Collection getItems(final User user, final String filter, final int limit, final int offset, + final String orderby, final OrderDirection direction, final Map extraParams) { + try { List idpConfigs = IdpConfigHelper.getInstance().getIdpConfigs(); - if ( UtilMethods.isSet( filter ) ) - { - idpConfigs = idpConfigs.stream() - .filter(x -> x.contains(filter)) - .collect( Collectors.toList() ); + if (UtilMethods.isSet(filter)) { + idpConfigs = idpConfigs.stream().filter(x -> x.contains(filter)).collect(Collectors.toList()); } - List paginatedAndFiltered = idpConfigs.stream().skip( offset ).limit( limit ).collect( Collectors.toList() ); + List paginatedAndFiltered = idpConfigs.stream().skip(offset).limit(limit) + .collect(Collectors.toList()); - lastTotalRecords.set( idpConfigs.size() ); + lastTotalRecords.set(idpConfigs.size()); return paginatedAndFiltered; - } - catch ( IOException | JSONException exception ) - { - Logger.error( IdpConfigPaginator.class, "Error getting paginated IdpConfigs", exception ); - throw new DotRuntimeException( exception ); + } catch (DotSamlByPassException exception) { + + Logger.error(this, exception.getMessage(), exception); + lastTotalRecords.set(0); + return new ArrayList(); + + } catch (IOException | JSONException exception) { + Logger.error(IdpConfigPaginator.class, "Error getting paginated IdpConfigs", exception); + throw new DotRuntimeException(exception); } } }