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.jaas.spi;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.eclipse.jetty.util.security.Credential;
26  
27  /**
28   * UserInfo
29   *
30   * This is the information read from the external source
31   * about a user.
32   * 
33   * Can be cached.
34   */
35  public class UserInfo
36  {
37      
38      private String _userName;
39      private Credential _credential;
40      protected List<String> _roleNames = new ArrayList<>();
41      protected boolean _rolesLoaded = false;
42      
43      
44      /**
45       * @param userName
46       * @param credential
47       * @param roleNames
48       */
49      public UserInfo (String userName, Credential credential, List<String> roleNames)
50      {
51          _userName = userName;
52          _credential = credential;
53          if (roleNames != null)
54          {
55              synchronized (_roleNames)
56              {
57                  _roleNames.addAll(roleNames);
58                  _rolesLoaded = true;
59              }
60          }
61      }
62      
63      
64      /**
65       * @param userName
66       * @param credential
67       */
68      public UserInfo (String userName, Credential credential)
69      {
70          this (userName, credential, null);
71      }
72      
73      
74      
75      /**
76       * Should be overridden by subclasses to obtain
77       * role info
78       * 
79       * @return
80       * @throws Exception
81       */
82      public List<String> doFetchRoles ()
83      throws Exception
84      {
85          return Collections.emptyList();
86      }
87      
88      public void fetchRoles () throws Exception
89      {
90          synchronized (_roleNames)
91          {
92              if (!_rolesLoaded)
93              {
94                  _roleNames.addAll(doFetchRoles());
95                  _rolesLoaded = true;
96              }
97          }
98      }
99      
100     public String getUserName()
101     {
102         return this._userName;
103     }
104     
105     public List<String> getRoleNames ()
106     {   
107         return Collections.unmodifiableList(_roleNames);
108     }
109     
110     public boolean checkCredential (Object suppliedCredential)
111     {
112         return _credential.check(suppliedCredential);
113     }
114     
115     protected Credential getCredential ()
116     {
117         return _credential;
118     }
119     
120 }