| 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; |
| 16 |
|
| 17 |
import org.globus.gsi.gssapi.jaas.GlobusPrincipal; |
| 18 |
|
| 19 |
import java.util.Set; |
| 20 |
import java.util.Iterator; |
| 21 |
|
| 22 |
import javax.security.auth.Subject; |
| 23 |
|
| 24 |
|
| 25 |
import org.ietf.jgss.GSSCredential; |
| 26 |
import org.ietf.jgss.GSSName; |
| 27 |
import org.ietf.jgss.GSSException; |
| 28 |
|
| 29 |
/** |
| 30 |
* An utility class for handling JAAS Subject with GSSCredential. |
| 31 |
*/ |
| 32 |
public class JaasGssUtil { |
| 33 |
|
| 34 |
/** |
| 35 |
* Creates a new <code>Subject</code> object from specified |
| 36 |
* <code>GSSCredential</code>. The GSSCredential is added |
| 37 |
* to the private credential set of the Subject object. |
| 38 |
* Also, if the GSSCredential.getName() is of type <code> |
| 39 |
* org.globus.gsi.gssapi.GlobusGSSName</code> |
| 40 |
* a <code>org.globus.gsi.jaas.GlobusPrincipal</code> |
| 41 |
* is added to the principals set of the Subject object. |
| 42 |
*/ |
| 43 |
public static Subject createSubject(GSSCredential cred) |
| 44 |
throws GSSException { |
| 45 |
return createSubject(null, cred); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Creates a new <code>Subject</code> object from specified |
| 50 |
* <code>GSSCredential</code> and <code>GSSName</code>. |
| 51 |
* If the GSSCredential is specified it is added |
| 52 |
* to the private credential set of the Subject object. |
| 53 |
* Also, if the GSSCredential.getName() is of type <code> |
| 54 |
* org.globus.gsi.gssapi.GlobusGSSName</code> and the |
| 55 |
* GSSName parameter was not specified a |
| 56 |
* <code>org.globus.gsi.jaas.GlobusPrincipal</code> |
| 57 |
* is added to the principals set of the Subject object. |
| 58 |
* If the GSSName parameter was specified of type |
| 59 |
* <code>org.globus.gsi.gssapi.GlobusGSSName</code> a |
| 60 |
* <code>org.globus.gsi.jaas.GlobusPrincipal</code> |
| 61 |
* is added to the principals set of the Subject object. |
| 62 |
*/ |
| 63 |
public static Subject createSubject(GSSName name, GSSCredential cred) |
| 64 |
throws GSSException { |
| 65 |
if (cred == null && name == null) { |
| 66 |
return null; |
| 67 |
} |
| 68 |
Subject subject = new Subject(); |
| 69 |
if (cred != null) { |
| 70 |
subject.getPrivateCredentials().add(cred); |
| 71 |
if (name == null) { |
| 72 |
GlobusPrincipal nm = toGlobusPrincipal(cred.getName()); |
| 73 |
subject.getPrincipals().add(nm); |
| 74 |
} |
| 75 |
} |
| 76 |
if (name != null) { |
| 77 |
GlobusPrincipal nm = toGlobusPrincipal(name); |
| 78 |
subject.getPrincipals().add(nm); |
| 79 |
} |
| 80 |
|
| 81 |
return subject; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Converts the specified GSSName to GlobusPrincipal. |
| 86 |
* The GSSName is converted into the GlobusPrincipal |
| 87 |
* only if the GSSName is of type |
| 88 |
* <code>org.globus.gsi.gssapi.GlobusGSSName</code> |
| 89 |
* and the name is not anonymous. |
| 90 |
*/ |
| 91 |
public static GlobusPrincipal toGlobusPrincipal(GSSName name) { |
| 92 |
return (!name.isAnonymous() && |
| 93 |
(name instanceof GlobusGSSName)) ? |
| 94 |
new GlobusPrincipal(name.toString()) : |
| 95 |
null; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Retrieves the first <code>GSSCredential</code> from the |
| 100 |
* private credential set of the specified <code>Subject</code> |
| 101 |
* object. |
| 102 |
* |
| 103 |
* @return the <code>GSSCredential</code>. Might be null. |
| 104 |
*/ |
| 105 |
public static GSSCredential getCredential(Subject subject) { |
| 106 |
if (subject == null) { |
| 107 |
return null; |
| 108 |
} |
| 109 |
Set gssCreds = subject.getPrivateCredentials(GSSCredential.class); |
| 110 |
if (gssCreds == null) { |
| 111 |
return null; |
| 112 |
} |
| 113 |
Iterator iter = gssCreds.iterator(); |
| 114 |
return (iter.hasNext()) ? |
| 115 |
(GSSCredential)iter.next() : |
| 116 |
null; |
| 117 |
} |
| 118 |
|
| 119 |
} |