View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
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  
17  package org.gwe.utils.security;
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.gwe.drivers.HandleCreationException;
26  import org.gwe.drivers.HandleOperationException;
27  import org.gwe.drivers.ResourceHandle;
28  
29  /**
30   * A realm is a collection of resources that have the same authentication information.
31   *
32   * @author Marco Ruiz
33   * @author Neil Jones
34   * @since Jun 18, 2007
35   */
36  public class Realm implements Serializable {
37  
38  	private static Log log = LogFactory.getLog(Realm.class);
39  
40  	private static List<String> semicolonSplitted(String target) {
41  		String[] entries = (target != null) ? target.split(";") : new String[]{};
42  		List<String> results = new ArrayList<String>(entries.length);
43  		for (String entry : entries) results.add(entry.toLowerCase());
44  		return results;
45      }
46  
47  	private String schemes;
48  	private String domain;
49  	private String testHost;
50  	private AccountInfo account;
51  	private RealmTestResult testResult;
52  
53  	public Realm() { super(); }
54  
55      public Realm(String schemes, String domain, String testHost) {
56  		this.schemes = schemes;
57  		this.domain = domain;
58  		this.testHost = testHost;
59      }
60      
61  	public void setAccount(AccountInfo acct) {
62  		this.account = acct;
63      }
64  
65      public AccountInfo getAccount() {
66      	return account;
67      }
68  
69  	public String getDomain() {
70      	return domain;
71      }
72  
73  	public String getTestHost() {
74      	return testHost;
75      }
76  	
77  	public RealmTestResult getTestResult() {
78      	return testResult;
79      }
80  
81      public boolean implies(String otherScheme, String otherDomain) {
82      	if (!acceptsScheme(otherScheme)) return false;
83      	if (otherDomain == null) otherDomain = "";
84          String[] hostParts = otherDomain.split("\\.");
85          for(int ii = 0; ii < hostParts.length; ++ii) {
86              String hostStr = (ii == 0) ? "" : "*";
87              for(int jj = ii; jj < hostParts.length; ++jj)
88                  hostStr = (hostStr.length() != 0) ? hostStr + "." + hostParts[jj] : hostParts[jj];
89          
90              if (domain.equals(hostStr)) return true;
91          }
92          return false;
93      }
94  
95  	public boolean acceptsScheme(ProtocolScheme scheme) {
96  	    return acceptsScheme(scheme.toString());
97      }
98      
99  	public boolean acceptsScheme(String scheme) {
100 	    return semicolonSplitted(schemes).contains(scheme.toLowerCase());
101     }
102     
103 	public <HANDLE_TYPE extends ResourceHandle> ResourceLink<HANDLE_TYPE> createResourceLink(ThinURI uri) {
104 		return new ResourceLink<HANDLE_TYPE>(uri, account);
105     }
106 	
107 	public void test() {
108 		testResult = determineTestResult();
109 		log.info("Test result of realm ["  + this + "]: " + testResult.getMessage());
110 	}
111 	
112 	private RealmTestResult determineTestResult() {
113 	    // Missing authentication
114 		if (getAccount() == null) 
115 	    	return RealmTestResult.MISSING_ACCOUNT; 
116 
117 		if (getAccount().missingPasskey()) 
118 	    	return RealmTestResult.MISSING_PASSKEY; 
119 
120 	    if (getTestHost() == null || "".equals(getTestHost())) 
121 	    	return RealmTestResult.MISSING_TEST_HOST;  
122     	
123 		// No host to test against
124 		if (!acceptsScheme(ProtocolScheme.SSH))
125 	    	return RealmTestResult.NO_SSH_REALM;  
126 			
127         try {
128         	createTestHostLink().createHandle().close();
129         } catch (HandleCreationException e) {
130         	// Authentication failed with test host - could work with other hosts though
131     	    return RealmTestResult.ERROR;
132         } catch (HandleOperationException e) {
133         	// Closing handle failed with test host - ignore, connecting is the only thing we are testing
134         }
135 		// Authentication worked at least with test host
136     	return RealmTestResult.OK;
137     }
138 
139 	public ResourceLink<ResourceHandle> createTestHostLink() {
140 	    String uriStr = ProtocolScheme.SSH.toURIStr(getTestHost());
141 	    ThinURI uri = ThinURI.createBlind(uriStr);
142 	    return createResourceLink(uri);
143     }
144 	
145     public String toString() {
146 		boolean started = false;
147     	String result = "";
148     	for (String ele : semicolonSplitted(schemes)) {
149 			if (started) result += ";";
150     		result += ele;
151     		started = true;
152     	}
153     	return result + ThinURI.SCHEME_SEPARATOR + domain;
154     }
155 }
156