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