| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.gsi.gssapi.auth; |
| 16 |
|
| 17 |
import org.gridforum.jgss.ExtendedGSSManager; |
| 18 |
|
| 19 |
import org.ietf.jgss.GSSManager; |
| 20 |
import org.ietf.jgss.GSSContext; |
| 21 |
import org.ietf.jgss.GSSName; |
| 22 |
import org.ietf.jgss.GSSException; |
| 23 |
import org.ietf.jgss.GSSCredential; |
| 24 |
|
| 25 |
import org.apache.commons.logging.Log; |
| 26 |
import org.apache.commons.logging.LogFactory; |
| 27 |
|
| 28 |
/** |
| 29 |
* Implements a simple identity authorization mechanism. |
| 30 |
* The given identify is compared with the peer's identity. |
| 31 |
*/ |
| 32 |
public class IdentityAuthorization |
| 33 |
extends GSSAuthorization { |
| 34 |
|
| 35 |
private static Log logger = |
| 36 |
LogFactory.getLog(IdentityAuthorization.class.getName()); |
| 37 |
|
| 38 |
protected String _identity; |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor used by superclasses. |
| 42 |
*/ |
| 43 |
protected IdentityAuthorization() { |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Creates a new instance of this class with given |
| 48 |
* expected identity. |
| 49 |
* |
| 50 |
* @param identity the expected identity. Must not be null. |
| 51 |
*/ |
| 52 |
public IdentityAuthorization(String identity) { |
| 53 |
setIdentity(identity); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Sets the expected identity for the authorization |
| 58 |
* check. |
| 59 |
* |
| 60 |
* @param identity the expected identity. Must not be null. |
| 61 |
*/ |
| 62 |
public void setIdentity(String identity) { |
| 63 |
if (identity == null) { |
| 64 |
throw new IllegalArgumentException("Identity cannot be null"); |
| 65 |
} |
| 66 |
_identity = identity; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns the expected identity. |
| 71 |
* |
| 72 |
* @return the expected identity. |
| 73 |
*/ |
| 74 |
public String getIdentity() { |
| 75 |
return _identity; |
| 76 |
} |
| 77 |
|
| 78 |
public GSSName getExpectedName(GSSCredential cred, String host) |
| 79 |
throws GSSException { |
| 80 |
GSSManager manager = ExtendedGSSManager.getInstance(); |
| 81 |
return manager.createName(_identity, null); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Performs identity authorization. The given identity is compared |
| 86 |
* with the peer's identity. |
| 87 |
* |
| 88 |
* @param context the security context |
| 89 |
* @param host host address of the peer. |
| 90 |
* @exception AuthorizationException if the peer's |
| 91 |
* identity does not match the expected identity. |
| 92 |
*/ |
| 93 |
public void authorize(GSSContext context, String host) |
| 94 |
throws AuthorizationException { |
| 95 |
logger.debug("Authorization: IDENTITY"); |
| 96 |
|
| 97 |
try { |
| 98 |
GSSName expected = getExpectedName(null, host); |
| 99 |
|
| 100 |
GSSName target = null; |
| 101 |
if (context.isInitiator()) { |
| 102 |
target = context.getTargName(); |
| 103 |
} else { |
| 104 |
target = context.getSrcName(); |
| 105 |
} |
| 106 |
|
| 107 |
if (!expected.equals(target)) { |
| 108 |
generateAuthorizationException(expected, target); |
| 109 |
} |
| 110 |
} catch (GSSException e) { |
| 111 |
throw new AuthorizationException("Authorization failure", e); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public boolean equals(Object o) { |
| 116 |
if (o == null) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
if (this == o) { |
| 120 |
return true; |
| 121 |
} |
| 122 |
if (o instanceof IdentityAuthorization) { |
| 123 |
IdentityAuthorization other = (IdentityAuthorization)o; |
| 124 |
if (this._identity == null) { |
| 125 |
return (other._identity == null); |
| 126 |
} else { |
| 127 |
return this._identity.equals(other._identity); |
| 128 |
} |
| 129 |
} |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
public int hashCode() { |
| 134 |
return (this._identity == null) ? 0 : this._identity.hashCode(); |
| 135 |
} |
| 136 |
|
| 137 |
} |