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.LoginAuthenticator;
17  import org.eclipse.jetty.server.Authentication;
18  import org.eclipse.jetty.server.UserIdentity;
19  import org.eclipse.jetty.server.UserIdentity.Scope;
20  
21  
22  /**
23   * @version $Rev: 4793 $ $Date: 2009-03-19 00:00:01 +0100 (Thu, 19 Mar 2009) $
24   */
25  public class UserAuthentication implements Authentication.User
26  {
27      private final Authenticator _authenticator;
28      private final UserIdentity _userIdentity;
29  
30      public UserAuthentication(Authenticator authenticator, UserIdentity userIdentity)
31      {
32          _authenticator = authenticator;
33          _userIdentity=userIdentity;
34      }
35  
36      public String getAuthMethod()
37      {
38          return _authenticator.getAuthMethod();
39      }
40  
41      public UserIdentity getUserIdentity()
42      {
43          return _userIdentity;
44      }
45  
46      public boolean isUserInRole(Scope scope, String role)
47      {
48          return _userIdentity.isUserInRole(role, scope);
49      }
50      
51      public void logout() 
52      {    
53          final Authenticator authenticator = _authenticator;
54          if (authenticator instanceof LoginAuthenticator)
55          {
56              IdentityService id_service=((LoginAuthenticator)authenticator).getLoginService().getIdentityService();
57              if (id_service!=null)
58                  id_service.disassociate(null); // TODO provide the previous value
59          }
60      }
61      
62      @Override
63      public String toString()
64      {
65          return "{Auth,"+getAuthMethod()+","+_userIdentity+"}";
66      }
67  }