View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.security;
15  
16  import org.eclipse.jetty.security.authentication.DelegateAuthenticator;
17  import org.eclipse.jetty.security.authentication.LoginAuthenticator;
18  import org.eclipse.jetty.server.Authentication;
19  import org.eclipse.jetty.server.UserIdentity;
20  import org.eclipse.jetty.server.UserIdentity.Scope;
21  
22  
23  /**
24   * @version $Rev: 4793 $ $Date: 2009-03-19 00:00:01 +0100 (Thu, 19 Mar 2009) $
25   */
26  public class UserAuthentication implements Authentication.User
27  {
28      private final Authenticator _authenticator;
29      private final UserIdentity _userIdentity;
30  
31      public UserAuthentication(Authenticator authenticator, UserIdentity userIdentity)
32      {
33          _authenticator = authenticator;
34          _userIdentity=userIdentity;
35      }
36  
37      public String getAuthMethod()
38      {
39          return _authenticator.getAuthMethod();
40      }
41  
42      public UserIdentity getUserIdentity()
43      {
44          return _userIdentity;
45      }
46  
47      public boolean isUserInRole(Scope scope, String role)
48      {
49          return _userIdentity.isUserInRole(role, scope);
50      }
51      
52      public void logout() 
53      {    
54          Authenticator authenticator = _authenticator;
55          while (true)
56          {
57              if (authenticator instanceof LoginAuthenticator)
58              {
59                  ((LoginAuthenticator)authenticator).getLoginService().logout(this.getUserIdentity());
60                  break;
61              }
62              else if (authenticator instanceof DelegateAuthenticator)
63              {
64                  authenticator=((DelegateAuthenticator)authenticator).getDelegate();
65              }
66              else
67                  break;
68          }
69      }
70      
71      public String toString()
72      {
73          return "{Auth,"+getAuthMethod()+","+_userIdentity+"}";
74      }
75  }