]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/security/HashLoginService.java
updating jetty to jetty-9.2.16.v2016040
[gigi.git] / lib / jetty / org / eclipse / jetty / security / HashLoginService.java
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.io.IOException;
22
23 import org.eclipse.jetty.security.PropertyUserStore.UserListener;
24 import org.eclipse.jetty.server.UserIdentity;
25 import org.eclipse.jetty.util.Scanner;
26 import org.eclipse.jetty.util.log.Log;
27 import org.eclipse.jetty.util.log.Logger;
28 import org.eclipse.jetty.util.resource.Resource;
29 import org.eclipse.jetty.util.security.Credential;
30
31 /* ------------------------------------------------------------ */
32 /**
33  * Properties User Realm.
34  * 
35  * An implementation of UserRealm that stores users and roles in-memory in HashMaps.
36  * <P>
37  * Typically these maps are populated by calling the load() method or passing a properties resource to the constructor. The format of the properties file is:
38  * 
39  * <PRE>
40  *  username: password [,rolename ...]
41  * </PRE>
42  * 
43  * Passwords may be clear text, obfuscated or checksummed. The class com.eclipse.Util.Password should be used to generate obfuscated passwords or password
44  * checksums.
45  * 
46  * If DIGEST Authentication is used, the password must be in a recoverable format, either plain text or OBF:.
47  */
48 public class HashLoginService extends MappedLoginService implements UserListener
49 {
50     private static final Logger LOG = Log.getLogger(HashLoginService.class);
51
52     private PropertyUserStore _propertyUserStore;
53     private String _config;
54     private Resource _configResource;
55     private int _refreshInterval = 0;// default is not to reload
56
57     /* ------------------------------------------------------------ */
58     public HashLoginService()
59     {
60     }
61
62     /* ------------------------------------------------------------ */
63     public HashLoginService(String name)
64     {
65         setName(name);
66     }
67
68     /* ------------------------------------------------------------ */
69     public HashLoginService(String name, String config)
70     {
71         setName(name);
72         setConfig(config);
73     }
74
75     /* ------------------------------------------------------------ */
76     public String getConfig()
77     {
78         return _config;
79     }
80
81     /* ------------------------------------------------------------ */
82     public void getConfig(String config)
83     {
84         _config = config;
85     }
86
87     /* ------------------------------------------------------------ */
88     public Resource getConfigResource()
89     {
90         return _configResource;
91     }
92
93     /* ------------------------------------------------------------ */
94     /**
95      * Load realm users from properties file. The property file maps usernames to password specs followed by an optional comma separated list of role names.
96      * 
97      * @param config
98      *            Filename or url of user properties file.
99      */
100     public void setConfig(String config)
101     {
102         _config = config;
103     }
104
105     /* ------------------------------------------------------------ */
106     public void setRefreshInterval(int msec)
107     {
108         _refreshInterval = msec;
109     }
110
111     /* ------------------------------------------------------------ */
112     public int getRefreshInterval()
113     {
114         return _refreshInterval;
115     }
116
117     /* ------------------------------------------------------------ */
118     @Override
119     protected UserIdentity loadUser(String username)
120     {
121         return null;
122     }
123
124     /* ------------------------------------------------------------ */
125     @Override
126     public void loadUsers() throws IOException
127     {
128         // TODO: Consider refactoring MappedLoginService to not have to override with unused methods
129     }
130
131     /* ------------------------------------------------------------ */
132     /**
133      * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStart()
134      */
135     @Override
136     protected void doStart() throws Exception
137     {
138         super.doStart();
139         
140         if (_propertyUserStore == null)
141         {
142             if(LOG.isDebugEnabled())
143                 LOG.debug("doStart: Starting new PropertyUserStore. PropertiesFile: " + _config + " refreshInterval: " + _refreshInterval);
144             
145             _propertyUserStore = new PropertyUserStore();
146             _propertyUserStore.setRefreshInterval(_refreshInterval);
147             _propertyUserStore.setConfig(_config);
148             _propertyUserStore.registerUserListener(this);
149             _propertyUserStore.start();
150         }
151     }
152
153     /* ------------------------------------------------------------ */
154     /**
155      * @see org.eclipse.jetty.util.component.AbstractLifeCycle#doStop()
156      */
157     @Override
158     protected void doStop() throws Exception
159     {
160         super.doStop();
161         if (_propertyUserStore != null)
162             _propertyUserStore.stop();
163         _propertyUserStore = null;
164     }
165     
166     /* ------------------------------------------------------------ */
167     @Override
168     public void update(String userName, Credential credential, String[] roleArray)
169     {
170         if (LOG.isDebugEnabled())
171             LOG.debug("update: " + userName + " Roles: " + roleArray.length);
172         putUser(userName,credential,roleArray);
173     }
174
175     /* ------------------------------------------------------------ */
176     @Override
177     public void remove(String userName)
178     {
179         if (LOG.isDebugEnabled())
180             LOG.debug("remove: " + userName);
181         removeUser(userName);
182     }
183 }