James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
commit | author | age
04a985 1 /*
JM 2  * Copyright 2013 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.auth;
17
18 import java.io.File;
f6cbed 19 import java.math.BigInteger;
04a985 20
JM 21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.gitblit.Constants.AccountType;
6e3481 25 import com.gitblit.Constants.Role;
04a985 26 import com.gitblit.IStoredSettings;
JM 27 import com.gitblit.manager.IRuntimeManager;
28 import com.gitblit.manager.IUserManager;
29 import com.gitblit.models.TeamModel;
30 import com.gitblit.models.UserModel;
c1b0e4 31 import com.gitblit.utils.ArrayUtils;
45ed92 32 import com.gitblit.utils.DeepCopier;
c1b0e4 33 import com.gitblit.utils.StringUtils;
04a985 34
JM 35 public abstract class AuthenticationProvider {
36
37     public static NullProvider NULL_PROVIDER = new NullProvider();
38
39     protected final Logger logger = LoggerFactory.getLogger(getClass());
40
41     protected final String serviceName;
42
43     protected File baseFolder;
44
45     protected IStoredSettings settings;
46
47     protected IRuntimeManager runtimeManager;
48
49     protected IUserManager userManager;
50
51     protected AuthenticationProvider(String serviceName) {
52         this.serviceName = serviceName;
53     }
54
55     /**
56      * Returns the file object for the specified configuration key.
57      *
58      * @return the file
59      */
60     public File getFileOrFolder(String key, String defaultFileOrFolder) {
61         return runtimeManager.getFileOrFolder(key, defaultFileOrFolder);
62     }
63
64     public final void setup(IRuntimeManager runtimeManager, IUserManager userManager) {
65         this.baseFolder = runtimeManager.getBaseFolder();
66         this.settings = runtimeManager.getSettings();
67         this.runtimeManager = runtimeManager;
68         this.userManager = userManager;
69         setup();
70     }
71
72     public String getServiceName() {
73         return serviceName;
74     }
75
c1b0e4 76     protected void setCookie(UserModel user, char [] password) {
JM 77         // create a user cookie
78         if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {
79             user.cookie = StringUtils.getSHA1(user.username + new String(password));
80         }
81     }
82
04a985 83     protected void updateUser(UserModel userModel) {
f6cbed 84         final UserModel userLocalDB = userManager.getUserModel(userModel.getName());
04a985 85
45ed92 86         // Establish the checksum of the current version of the user
JM 87         final BigInteger userCurrentCheck = DeepCopier.checksum(userModel);
04a985 88
45ed92 89         // Establish the checksum of the stored version of the user
JM 90         final BigInteger userLocalDBcheck = DeepCopier.checksum(userLocalDB);
91
92         // Compare the checksums
93         if (!userCurrentCheck.equals(userLocalDBcheck)) {
94             // If mismatch, save the new instance.
95             userManager.updateUserModel(userModel);
f6cbed 96         }
04a985 97     }
JM 98
99     protected void updateTeam(TeamModel teamModel) {
f6cbed 100         final TeamModel teamLocalDB = userManager.getTeamModel(teamModel.name);
04a985 101
45ed92 102         // Establish the checksum of the current version of the team
JM 103         final BigInteger teamCurrentCheck = DeepCopier.checksum(teamModel);
04a985 104
45ed92 105         // Establish the checksum of the stored version of the team
JM 106         final BigInteger teamLocalDBcheck = DeepCopier.checksum(teamLocalDB);
107
108         // Compare the checksums
109         if (!teamCurrentCheck.equals(teamLocalDBcheck)) {
110             // If mismatch, save the new instance.
111             userManager.updateTeamModel(teamModel);
f6cbed 112         }
04a985 113     }
JM 114
115     public abstract void setup();
116
6659fa 117     public abstract void stop();
JM 118
04a985 119     public abstract UserModel authenticate(String username, char[] password);
JM 120
121     public abstract AccountType getAccountType();
122
123     /**
124      * Does the user service support changes to credentials?
125      *
126      * @return true or false
127      * @since 1.0.0
128      */
129     public abstract boolean supportsCredentialChanges();
130
131     /**
132      * Returns true if the user's display name can be changed.
133      *
134      * @param user
135      * @return true if the user service supports display name changes
136      */
137     public abstract boolean supportsDisplayNameChanges();
138
139     /**
140      * Returns true if the user's email address can be changed.
141      *
142      * @param user
143      * @return true if the user service supports email address changes
144      */
145     public abstract boolean supportsEmailAddressChanges();
146
147     /**
148      * Returns true if the user's team memberships can be changed.
149      *
150      * @param user
151      * @return true if the user service supports team membership changes
152      */
153     public abstract boolean supportsTeamMembershipChanges();
154
6e3481 155     /**
JM 156      * Returns true if the user's role can be changed.
157      *
158      * @param user
159      * @param role
160      * @return true if the user's role can be changed
161      */
162     public abstract boolean supportsRoleChanges(UserModel user, Role role);
163
164     /**
165      * Returns true if the team's role can be changed.
166      *
167      * @param user
168      * @param role
169      * @return true if the team's role can be changed
170      */
171     public abstract boolean supportsRoleChanges(TeamModel team, Role role);
172
04a985 173     @Override
JM 174     public String toString() {
175         return getServiceName() + " (" + getClass().getName() + ")";
176     }
177
178     public abstract static class UsernamePasswordAuthenticationProvider extends AuthenticationProvider {
179         protected UsernamePasswordAuthenticationProvider(String serviceName) {
180             super(serviceName);
181         }
6659fa 182
JM 183         @Override
184         public void stop() {
185
186         }
04a985 187     }
JM 188
189     public static class NullProvider extends AuthenticationProvider {
190
191         protected NullProvider() {
192             super("NULL");
193         }
194
195         @Override
196         public void setup() {
197
198         }
199
200         @Override
6659fa 201         public void stop() {
JM 202
203         }
204
205         @Override
04a985 206         public UserModel authenticate(String username, char[] password) {
JM 207             return null;
208         }
209
210         @Override
211         public AccountType getAccountType() {
212             return AccountType.LOCAL;
213         }
214
215         @Override
216         public boolean supportsCredentialChanges() {
d97ee9 217             return true;
04a985 218         }
JM 219
220         @Override
221         public boolean supportsDisplayNameChanges() {
d97ee9 222             return true;
04a985 223         }
JM 224
225         @Override
226         public boolean supportsEmailAddressChanges() {
d97ee9 227             return true;
04a985 228         }
JM 229
230         @Override
231         public boolean supportsTeamMembershipChanges() {
d97ee9 232             return true;
04a985 233         }
6e3481 234
JM 235         @Override
236         public boolean supportsRoleChanges(UserModel user, Role role) {
237             return true;
238         }
239
240         @Override
241         public boolean supportsRoleChanges(TeamModel team, Role role) {
242             return true;
243         }
244
04a985 245     }
JM 246 }