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