View Javadoc
1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.security.authn.util;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  import java.io.InputStream;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Properties;
25  
26  public class AuthenticationMapper {
27  	Logger LOG = LoggerFactory.getLogger( AuthenticationMapper.class );
28  
29  	private String loginURL;
30  	private String logoutURL;
31  	
32  	private List<String> urlPatterns;
33  	
34  	public AuthenticationMapper( final String configFilePath )
35  	{
36  		urlPatterns = new ArrayList<String>();
37  		try {
38  			readProperties( configFilePath );
39  		} catch (Exception e) {
40  			LOG.error( "Unable to load configuration file: "+configFilePath+"\n"+e.getLocalizedMessage() );
41  		}
42  	}
43  	
44  	public boolean requiresAuthentication( final String url )
45  	{
46  		boolean requiresAuthN = false;
47  		for( String pattern : getUrlPatterns() )
48  		{
49  			if( url.startsWith(pattern) )
50  			{
51  				requiresAuthN = true;
52  			}
53  		}
54  		return requiresAuthN;
55  	}
56  
57  	public String getLoginURL() {
58  		return loginURL;
59  	}
60  
61  	public void setLoginURL(String loginURL) {
62  		this.loginURL = loginURL;
63  	}
64  
65  	public String getLogoutURL() {
66  		return logoutURL;
67  	}
68  
69  	public void setLogoutURL(String logoutURL) {
70  		this.logoutURL = logoutURL;
71  	}
72  
73  	public List<String> getUrlPatterns() {
74  		return urlPatterns;
75  	}
76  
77  	public void setUrlPatterns(List<String> urlPatterns) {
78  		this.urlPatterns = urlPatterns;
79  	}
80  	
81  	public void readProperties(final String configFilePath) throws Exception {
82  		Properties properties = new Properties();
83  		InputStream is = this.getClass().getResourceAsStream( configFilePath );
84  		properties.loadFromXML(is);
85  		
86  		for( Object s : properties.keySet() )
87  		{
88  			LOG.debug( "Loading property "+(String)s+" = "+properties.getProperty( (String)s ) );
89  			if( ((String)s).startsWith( AuthenticationConstants.AUTH_PATH_PREFIX ) )
90  			{
91  				this.urlPatterns.add( properties.getProperty( (String)s ) );
92  			}
93  			else if( "loginURL".equalsIgnoreCase( (String)s ) )
94  			{
95  				this.setLoginURL( properties.getProperty((String) s) );
96  			}
97  			else if( "logoutURL".equalsIgnoreCase( (String)s ) )
98  			{
99  				this.setLogoutURL( properties.getProperty((String) s) );
100 			}
101 		}
102 	}
103 }