View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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          //Servlet Spec 3.1, pg 125
59          if ("*".equals(role))
60              return false;
61          
62          String roleToTest = null;
63          if (scope!=null && scope.getRoleRefMap()!=null)
64              roleToTest=scope.getRoleRefMap().get(role);
65  
66          //Servlet Spec 3.1, pg 125
67          if (roleToTest == null)
68              roleToTest = role;
69         
70          for (String r :_roles)
71              if (r.equals(roleToTest))
72                  return true;
73          return false;
74      }
75  
76      @Override
77      public String toString()
78      {
79          return DefaultUserIdentity.class.getSimpleName()+"('"+_userPrincipal+"')";
80      }
81  }