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 java.io.InputStream;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Properties;
22  
23  import org.apache.log4j.Logger;
24  
25  public class AuthenticationMapper {
26  	Logger LOG = Logger.getLogger( AuthenticationMapper.class );
27  
28  	private String loginURL;
29  	private String logoutURL;
30  	
31  	private List<String> urlPatterns;
32  	
33  	public AuthenticationMapper( final String configFilePath )
34  	{
35  		urlPatterns = new ArrayList<String>();
36  		try {
37  			readProperties( configFilePath );
38  		} catch (Exception e) {
39  			LOG.error( "Unable to load configuration file: "+configFilePath+"\n"+e.getLocalizedMessage() );
40  		}
41  	}
42  	
43  	public boolean requiresAuthentication( final String url )
44  	{
45  		boolean requiresAuthN = false;
46  		for( String pattern : getUrlPatterns() )
47  		{
48  			if( url.startsWith(pattern) )
49  			{
50  				requiresAuthN = true;
51  			}
52  		}
53  		return requiresAuthN;
54  	}
55  
56  	public String getLoginURL() {
57  		return loginURL;
58  	}
59  
60  	public void setLoginURL(String loginURL) {
61  		this.loginURL = loginURL;
62  	}
63  
64  	public String getLogoutURL() {
65  		return logoutURL;
66  	}
67  
68  	public void setLogoutURL(String logoutURL) {
69  		this.logoutURL = logoutURL;
70  	}
71  
72  	public List<String> getUrlPatterns() {
73  		return urlPatterns;
74  	}
75  
76  	public void setUrlPatterns(List<String> urlPatterns) {
77  		this.urlPatterns = urlPatterns;
78  	}
79  	
80  	public void readProperties(final String configFilePath) throws Exception {
81  		Properties properties = new Properties();
82  		InputStream is = this.getClass().getResourceAsStream( configFilePath );
83  		properties.loadFromXML(is);
84  		
85  		for( Object s : properties.keySet() )
86  		{
87  			LOG.debug( "Loading property "+(String)s+" = "+properties.getProperty( (String)s ) );
88  			if( ((String)s).startsWith( AuthenticationConstants.AUTH_PATH_PREFIX ) )
89  			{
90  				this.urlPatterns.add( properties.getProperty( (String)s ) );
91  			}
92  			else if( "loginURL".equalsIgnoreCase( (String)s ) )
93  			{
94  				this.setLoginURL( properties.getProperty((String) s) );
95  			}
96  			else if( "logoutURL".equalsIgnoreCase( (String)s ) )
97  			{
98  				this.setLogoutURL( properties.getProperty((String) s) );
99  			}
100 		}
101 	}
102 }