You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthenticationProvider.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  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. import java.io.File;
  18. import java.math.BigInteger;
  19. import javax.servlet.http.HttpServletRequest;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import com.gitblit.Constants.AccountType;
  23. import com.gitblit.Constants.Role;
  24. import com.gitblit.Constants.AuthenticationType;
  25. import com.gitblit.IStoredSettings;
  26. import com.gitblit.Keys;
  27. import com.gitblit.manager.IRuntimeManager;
  28. import com.gitblit.manager.IUserManager;
  29. import com.gitblit.models.TeamModel;
  30. import com.gitblit.models.UserModel;
  31. import com.gitblit.utils.ArrayUtils;
  32. import com.gitblit.utils.DeepCopier;
  33. import com.gitblit.utils.StringUtils;
  34. public abstract class AuthenticationProvider {
  35. public static NullProvider NULL_PROVIDER = new NullProvider();
  36. protected final Logger logger = LoggerFactory.getLogger(getClass());
  37. protected final String serviceName;
  38. protected File baseFolder;
  39. protected IStoredSettings settings;
  40. protected IRuntimeManager runtimeManager;
  41. protected IUserManager userManager;
  42. protected AuthenticationProvider(String serviceName) {
  43. this.serviceName = serviceName;
  44. }
  45. /**
  46. * Returns the file object for the specified configuration key.
  47. *
  48. * @return the file
  49. */
  50. public File getFileOrFolder(String key, String defaultFileOrFolder) {
  51. return runtimeManager.getFileOrFolder(key, defaultFileOrFolder);
  52. }
  53. public final void setup(IRuntimeManager runtimeManager, IUserManager userManager) {
  54. this.baseFolder = runtimeManager.getBaseFolder();
  55. this.settings = runtimeManager.getSettings();
  56. this.runtimeManager = runtimeManager;
  57. this.userManager = userManager;
  58. setup();
  59. }
  60. public String getServiceName() {
  61. return serviceName;
  62. }
  63. public abstract AuthenticationType getAuthenticationType();
  64. protected void setCookie(UserModel user) {
  65. // create a user cookie
  66. if (StringUtils.isEmpty(user.cookie)) {
  67. user.cookie = user.createCookie();
  68. }
  69. }
  70. protected void updateUser(UserModel userModel) {
  71. final UserModel userLocalDB = userManager.getUserModel(userModel.getName());
  72. String loginedUserDefaultTeam = settings.getString(Keys.realm.loginedUserDefaultTeam,null);
  73. if(!StringUtils.isEmpty(loginedUserDefaultTeam)){
  74. TeamModel defaultTeam = userManager.getTeamModel(loginedUserDefaultTeam);
  75. if( defaultTeam != null ) {
  76. userModel.teams.add(defaultTeam);
  77. }
  78. }
  79. // Establish the checksum of the current version of the user
  80. final BigInteger userCurrentCheck = DeepCopier.checksum(userModel);
  81. // Establish the checksum of the stored version of the user
  82. final BigInteger userLocalDBcheck = DeepCopier.checksum(userLocalDB);
  83. // Compare the checksums
  84. if (!userCurrentCheck.equals(userLocalDBcheck)) {
  85. // If mismatch, save the new instance.
  86. userManager.updateUserModel(userModel);
  87. }
  88. }
  89. protected void updateTeam(TeamModel teamModel) {
  90. final TeamModel teamLocalDB = userManager.getTeamModel(teamModel.name);
  91. // Establish the checksum of the current version of the team
  92. final BigInteger teamCurrentCheck = DeepCopier.checksum(teamModel);
  93. // Establish the checksum of the stored version of the team
  94. final BigInteger teamLocalDBcheck = DeepCopier.checksum(teamLocalDB);
  95. // Compare the checksums
  96. if (!teamCurrentCheck.equals(teamLocalDBcheck)) {
  97. // If mismatch, save the new instance.
  98. userManager.updateTeamModel(teamModel);
  99. }
  100. }
  101. public abstract void setup();
  102. public abstract void stop();
  103. /**
  104. * Used to handle requests for requests for pages requiring authentication.
  105. * This allows authentication to occur based on the contents of the request
  106. * itself.
  107. *
  108. * @param httpRequest
  109. * @return
  110. */
  111. public abstract UserModel authenticate(HttpServletRequest httpRequest);
  112. /**
  113. * Used to authentication user/password credentials, both for login form
  114. * and HTTP Basic authentication processing.
  115. *
  116. * @param username
  117. * @param password
  118. * @return
  119. */
  120. public abstract UserModel authenticate(String username, char[] password);
  121. public abstract AccountType getAccountType();
  122. /**
  123. * Returns true if the users's credentials can be changed.
  124. *
  125. * @return true if the authentication provider supports credential changes
  126. * @since 1.0.0
  127. */
  128. public abstract boolean supportsCredentialChanges();
  129. /**
  130. * Returns true if the user's display name can be changed.
  131. *
  132. * @param user
  133. * @return true if the authentication provider supports display name changes
  134. */
  135. public abstract boolean supportsDisplayNameChanges();
  136. /**
  137. * Returns true if the user's email address can be changed.
  138. *
  139. * @param user
  140. * @return true if the authentication provider supports email address changes
  141. */
  142. public abstract boolean supportsEmailAddressChanges();
  143. /**
  144. * Returns true if the user's team memberships can be changed.
  145. *
  146. * @param user
  147. * @return true if the authentication provider supports team membership changes
  148. */
  149. public abstract boolean supportsTeamMembershipChanges();
  150. /**
  151. * Returns true if the user's role can be changed.
  152. *
  153. * @param user
  154. * @param role
  155. * @return true if the user's role can be changed
  156. */
  157. public abstract boolean supportsRoleChanges(UserModel user, Role role);
  158. /**
  159. * Returns true if the team's role can be changed.
  160. *
  161. * @param user
  162. * @param role
  163. * @return true if the team's role can be changed
  164. */
  165. public abstract boolean supportsRoleChanges(TeamModel team, Role role);
  166. @Override
  167. public String toString() {
  168. return getServiceName() + " (" + getClass().getName() + ")";
  169. }
  170. public abstract static class UsernamePasswordAuthenticationProvider extends AuthenticationProvider {
  171. protected UsernamePasswordAuthenticationProvider(String serviceName) {
  172. super(serviceName);
  173. }
  174. @Override
  175. public UserModel authenticate(HttpServletRequest httpRequest) {
  176. return null;
  177. }
  178. @Override
  179. public AuthenticationType getAuthenticationType() {
  180. return AuthenticationType.CREDENTIALS;
  181. }
  182. @Override
  183. public void stop() {
  184. }
  185. }
  186. public static class NullProvider extends AuthenticationProvider {
  187. protected NullProvider() {
  188. super("NULL");
  189. }
  190. @Override
  191. public void setup() {
  192. }
  193. @Override
  194. public void stop() {
  195. }
  196. @Override
  197. public UserModel authenticate(HttpServletRequest httpRequest) {
  198. return null;
  199. }
  200. @Override
  201. public UserModel authenticate(String username, char[] password) {
  202. return null;
  203. }
  204. @Override
  205. public AccountType getAccountType() {
  206. return AccountType.LOCAL;
  207. }
  208. @Override
  209. public AuthenticationType getAuthenticationType() {
  210. return null;
  211. }
  212. @Override
  213. public boolean supportsCredentialChanges() {
  214. return true;
  215. }
  216. @Override
  217. public boolean supportsDisplayNameChanges() {
  218. return true;
  219. }
  220. @Override
  221. public boolean supportsEmailAddressChanges() {
  222. return true;
  223. }
  224. @Override
  225. public boolean supportsTeamMembershipChanges() {
  226. return true;
  227. }
  228. @Override
  229. public boolean supportsRoleChanges(UserModel user, Role role) {
  230. return true;
  231. }
  232. @Override
  233. public boolean supportsRoleChanges(TeamModel team, Role role) {
  234. return true;
  235. }
  236. }
  237. }