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   * Associates UserIdentities from with threads and UserIdentity.Contexts.
29   */
30  public interface IdentityService
31  {
32      final static String[] NO_ROLES = new String[]{};
33  
34      /* ------------------------------------------------------------ */
35      /**
36       * Associate a user identity with the current thread.
37       * This is called with as a thread enters the
38       * {@link SecurityHandler#handle(String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)}
39       * method and then again with a null argument as that call exits.
40       * @param user The current user or null for no user to associated.
41       * @return an object representing the previous associated state
42       */
43      Object associate(UserIdentity user);
44  
45      /* ------------------------------------------------------------ */
46      /**
47       * Disassociate the user identity from the current thread
48       * and restore previous identity.
49       * @param previous The opaque object returned from a call to {@link IdentityService#associate(UserIdentity)}
50       */
51      void disassociate(Object previous);
52  
53      /* ------------------------------------------------------------ */
54      /**
55       * Associate a runas Token with the current user and thread.
56       * @param user The UserIdentity
57       * @param token The runAsToken to associate.
58       * @return The previous runAsToken or null.
59       */
60      Object setRunAs(UserIdentity user, RunAsToken token);
61  
62      /* ------------------------------------------------------------ */
63      /**
64       * Disassociate the current runAsToken from the thread
65       * and reassociate the previous token.
66       * @param token RUNAS returned from previous associateRunAs call
67       */
68      void unsetRunAs(Object token);
69  
70      /* ------------------------------------------------------------ */
71      /**
72       * Create a new UserIdentity for use with this identity service.
73       * The UserIdentity should be immutable and able to be cached.
74       *
75       * @param subject Subject to include in UserIdentity
76       * @param userPrincipal Principal to include in UserIdentity.  This will be returned from getUserPrincipal calls
77       * @param roles set of roles to include in UserIdentity.
78       * @return A new immutable UserIdententity
79       */
80      UserIdentity newUserIdentity(Subject subject, Principal userPrincipal, String[] roles);
81  
82      /* ------------------------------------------------------------ */
83      /**
84       * Create a new RunAsToken from a runAsName (normally a role).
85       * @param runAsName Normally a role name
86       * @return A new immutable RunAsToken
87       */
88      RunAsToken newRunAsToken(String runAsName);
89  
90      /* ------------------------------------------------------------ */
91      UserIdentity getSystemUserIdentity();
92  }