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