View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.security;
20  
21  import java.util.Properties;
22  import javax.security.auth.Subject;
23  
24  import org.eclipse.jetty.server.UserIdentity;
25  import org.eclipse.jetty.util.B64Code;
26  import org.eclipse.jetty.util.component.AbstractLifeCycle;
27  import org.eclipse.jetty.util.log.Log;
28  import org.eclipse.jetty.util.log.Logger;
29  import org.eclipse.jetty.util.resource.Resource;
30  import org.ietf.jgss.GSSContext;
31  import org.ietf.jgss.GSSCredential;
32  import org.ietf.jgss.GSSException;
33  import org.ietf.jgss.GSSManager;
34  import org.ietf.jgss.GSSName;
35  import org.ietf.jgss.Oid;
36  
37  public class SpnegoLoginService extends AbstractLifeCycle implements LoginService
38  {
39      private static final Logger LOG = Log.getLogger(SpnegoLoginService.class);
40  
41      protected IdentityService _identityService;// = new LdapIdentityService();
42      protected String _name;
43      private String _config;
44  
45      private String _targetName;
46  
47      public SpnegoLoginService()
48      {
49  
50      }
51  
52      public SpnegoLoginService( String name )
53      {
54          setName(name);
55      }
56  
57      public SpnegoLoginService( String name, String config )
58      {
59          setName(name);
60          setConfig(config);
61      }
62  
63      @Override
64      public String getName()
65      {
66          return _name;
67      }
68  
69      public void setName(String name)
70      {
71          if (isRunning())
72          {
73              throw new IllegalStateException("Running");
74          }
75  
76          _name = name;
77      }
78  
79      public String getConfig()
80      {
81          return _config;
82      }
83  
84      public void setConfig( String config )
85      {
86          if (isRunning())
87          {
88              throw new IllegalStateException("Running");
89          }
90  
91          _config = config;
92      }
93  
94  
95  
96      @Override
97      protected void doStart() throws Exception
98      {
99          Properties properties = new Properties();
100         Resource resource = Resource.newResource(_config);
101         properties.load(resource.getInputStream());
102 
103         _targetName = properties.getProperty("targetName");
104 
105         LOG.debug("Target Name {}", _targetName);
106 
107         super.doStart();
108     }
109 
110     /**
111      * username will be null since the credentials will contain all the relevant info
112      */
113     @Override
114     public UserIdentity login(String username, Object credentials)
115     {
116         String encodedAuthToken = (String)credentials;
117 
118         byte[] authToken = B64Code.decode(encodedAuthToken);
119 
120         GSSManager manager = GSSManager.getInstance();
121         try
122         {
123             Oid krb5Oid = new Oid("1.3.6.1.5.5.2"); // http://java.sun.com/javase/6/docs/technotes/guides/security/jgss/jgss-features.html
124             GSSName gssName = manager.createName(_targetName,null);
125             GSSCredential serverCreds = manager.createCredential(gssName,GSSCredential.INDEFINITE_LIFETIME,krb5Oid,GSSCredential.ACCEPT_ONLY);
126             GSSContext gContext = manager.createContext(serverCreds);
127 
128             if (gContext == null)
129             {
130                 LOG.debug("SpnegoUserRealm: failed to establish GSSContext");
131             }
132             else
133             {
134                 while (!gContext.isEstablished())
135                 {
136                     authToken = gContext.acceptSecContext(authToken,0,authToken.length);
137                 }
138                 if (gContext.isEstablished())
139                 {
140                     String clientName = gContext.getSrcName().toString();
141                     String role = clientName.substring(clientName.indexOf('@') + 1);
142 
143                     LOG.debug("SpnegoUserRealm: established a security context");
144                     LOG.debug("Client Principal is: " + gContext.getSrcName());
145                     LOG.debug("Server Principal is: " + gContext.getTargName());
146                     LOG.debug("Client Default Role: " + role);
147 
148                     SpnegoUserPrincipal user = new SpnegoUserPrincipal(clientName,authToken);
149 
150                     Subject subject = new Subject();
151                     subject.getPrincipals().add(user);
152 
153                     return _identityService.newUserIdentity(subject,user, new String[]{role});
154                 }
155             }
156 
157         }
158         catch (GSSException gsse)
159         {
160             LOG.warn(gsse);
161         }
162 
163         return null;
164     }
165 
166     @Override
167     public boolean validate(UserIdentity user)
168     {
169         return false;
170     }
171 
172     @Override
173     public IdentityService getIdentityService()
174     {
175         return _identityService;
176     }
177 
178     @Override
179     public void setIdentityService(IdentityService service)
180     {
181         _identityService = service;
182     }
183 
184     @Override
185     public void logout(UserIdentity user) 
186     {
187         // TODO Auto-generated method stub
188     }
189 
190 }