View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.security.Principal;
22  
23  import javax.security.auth.Subject;
24  
25  import org.eclipse.jetty.server.UserIdentity;
26  
27  
28  /* ------------------------------------------------------------ */
29  /**
30   * The default implementation of UserIdentity.
31   *
32   */
33  public class DefaultUserIdentity implements UserIdentity
34  {
35      private final Subject _subject;
36      private final Principal _userPrincipal;
37      private final String[] _roles;
38  
39      public DefaultUserIdentity(Subject subject, Principal userPrincipal, String[] roles)
40      {
41          _subject=subject;
42          _userPrincipal=userPrincipal;
43          _roles=roles;
44      }
45  
46      public Subject getSubject()
47      {
48          return _subject;
49      }
50  
51      public Principal getUserPrincipal()
52      {
53          return _userPrincipal;
54      }
55  
56      public boolean isUserInRole(String role, Scope scope)
57      {
58          if (scope!=null && scope.getRoleRefMap()!=null)
59              role=scope.getRoleRefMap().get(role);
60  
61          for (String r :_roles)
62              if (r.equals(role))
63                  return true;
64          return false;
65      }
66  
67      @Override
68      public String toString()
69      {
70          return DefaultUserIdentity.class.getSimpleName()+"('"+_userPrincipal+"')";
71      }
72  }