Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.security;
 
 
 
 
 public class SpnegoLoginService extends AbstractLifeCycle implements LoginService
 {
     private static final Logger LOG = Log.getLogger(SpnegoLoginService.class);
 
     protected IdentityService _identityService;// = new LdapIdentityService();
     protected String _name;
     private String _config;
     
     private String _targetName;
 
     public SpnegoLoginService()
     {
         
     }
     
     public SpnegoLoginServiceString name )
     {
         setName(name);
     }
     
     public SpnegoLoginServiceString nameString config )
     {
         setName(name);
         setConfig(config);
     }
     
     public String getName()
     {
         return ;
     }
 
     public void setName(String name)
     {
         if (isRunning())
         {
             throw new IllegalStateException("Running");
         }
         
          = name;
     }
     
     public String getConfig()
     {
         return ;
     }
     
     public void setConfigString config )
     {
         if (isRunning())
         {
             throw new IllegalStateException("Running");
         }
         
          = config;
     }
     
     
     
     @Override
     protected void doStart() throws Exception
     {
         Properties properties = new Properties();
        Resource resource = Resource.newResource();
        properties.load(resource.getInputStream());
        
         = properties.getProperty("targetName");
        
        .debug("Target Name {}");
        
        super.doStart();
    }

    
username will be null since the credentials will contain all the relevant info
    public UserIdentity login(String usernameObject credentials)
    {
        String encodedAuthToken = (String)credentials;
        
        byte[] authToken = B64Code.decode(encodedAuthToken);
        
        GSSManager manager = GSSManager.getInstance();
        try
        {
            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
            GSSName gssName = manager.createName(,null);
            GSSCredential serverCreds = manager.createCredential(gssName,.,krb5Oid,.);
            GSSContext gContext = manager.createContext(serverCreds);
            if (gContext == null)
            {
                .debug("SpnegoUserRealm: failed to establish GSSContext");
            }
            else
            {
                while (!gContext.isEstablished())
                {
                    authToken = gContext.acceptSecContext(authToken,0,authToken.length);
                }
                if (gContext.isEstablished())
                {
                    String clientName = gContext.getSrcName().toString();
                    String role = clientName.substring(clientName.indexOf('@') + 1);
                    
                    .debug("SpnegoUserRealm: established a security context");
                    .debug("Client Principal is: " + gContext.getSrcName());
                    .debug("Server Principal is: " + gContext.getTargName());
                    .debug("Client Default Role: " + role);
                    SpnegoUserPrincipal user = new SpnegoUserPrincipal(clientName,authToken);
                    Subject subject = new Subject();
                    subject.getPrincipals().add(user);
                    
                    return .newUserIdentity(subject,usernew String[]{role});
                }
            }
        }
        catch (GSSException gsse)
        {
            .warn(gsse);
        }
        return null;
    }
    public boolean validate(UserIdentity user)
    {
        return false;
    }
    {
        return ;
    }
    public void setIdentityService(IdentityService service)
    {
         = service;
    }
	public void logout(UserIdentity user) {
		// TODO Auto-generated method stub
	}
New to GrepCode? Check out our FAQ X