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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import com.gitblit.Constants.AccountType;
  21. import com.gitblit.IStoredSettings;
  22. import com.gitblit.manager.IRuntimeManager;
  23. import com.gitblit.manager.IUserManager;
  24. import com.gitblit.models.TeamModel;
  25. import com.gitblit.models.UserModel;
  26. public abstract class AuthenticationProvider {
  27. public static NullProvider NULL_PROVIDER = new NullProvider();
  28. protected final Logger logger = LoggerFactory.getLogger(getClass());
  29. protected final String serviceName;
  30. protected File baseFolder;
  31. protected IStoredSettings settings;
  32. protected IRuntimeManager runtimeManager;
  33. protected IUserManager userManager;
  34. protected AuthenticationProvider(String serviceName) {
  35. this.serviceName = serviceName;
  36. }
  37. /**
  38. * Returns the file object for the specified configuration key.
  39. *
  40. * @return the file
  41. */
  42. public File getFileOrFolder(String key, String defaultFileOrFolder) {
  43. return runtimeManager.getFileOrFolder(key, defaultFileOrFolder);
  44. }
  45. public final void setup(IRuntimeManager runtimeManager, IUserManager userManager) {
  46. this.baseFolder = runtimeManager.getBaseFolder();
  47. this.settings = runtimeManager.getSettings();
  48. this.runtimeManager = runtimeManager;
  49. this.userManager = userManager;
  50. setup();
  51. }
  52. public String getServiceName() {
  53. return serviceName;
  54. }
  55. protected void updateUser(UserModel userModel) {
  56. // TODO implement user model change detection
  57. // account for new user and revised user
  58. // username
  59. // displayname
  60. // email address
  61. // cookie
  62. userManager.updateUserModel(userModel);
  63. }
  64. protected void updateTeam(TeamModel teamModel) {
  65. // TODO implement team model change detection
  66. // account for new team and revised team
  67. // memberships
  68. userManager.updateTeamModel(teamModel);
  69. }
  70. public abstract void setup();
  71. public abstract UserModel authenticate(String username, char[] password);
  72. public abstract AccountType getAccountType();
  73. /**
  74. * Does the user service support changes to credentials?
  75. *
  76. * @return true or false
  77. * @since 1.0.0
  78. */
  79. public abstract boolean supportsCredentialChanges();
  80. /**
  81. * Returns true if the user's display name can be changed.
  82. *
  83. * @param user
  84. * @return true if the user service supports display name changes
  85. */
  86. public abstract boolean supportsDisplayNameChanges();
  87. /**
  88. * Returns true if the user's email address can be changed.
  89. *
  90. * @param user
  91. * @return true if the user service supports email address changes
  92. */
  93. public abstract boolean supportsEmailAddressChanges();
  94. /**
  95. * Returns true if the user's team memberships can be changed.
  96. *
  97. * @param user
  98. * @return true if the user service supports team membership changes
  99. */
  100. public abstract boolean supportsTeamMembershipChanges();
  101. @Override
  102. public String toString() {
  103. return getServiceName() + " (" + getClass().getName() + ")";
  104. }
  105. public abstract static class UsernamePasswordAuthenticationProvider extends AuthenticationProvider {
  106. protected UsernamePasswordAuthenticationProvider(String serviceName) {
  107. super(serviceName);
  108. }
  109. }
  110. public static class NullProvider extends AuthenticationProvider {
  111. protected NullProvider() {
  112. super("NULL");
  113. }
  114. @Override
  115. public void setup() {
  116. }
  117. @Override
  118. public UserModel authenticate(String username, char[] password) {
  119. return null;
  120. }
  121. @Override
  122. public AccountType getAccountType() {
  123. return AccountType.LOCAL;
  124. }
  125. @Override
  126. public boolean supportsCredentialChanges() {
  127. return false;
  128. }
  129. @Override
  130. public boolean supportsDisplayNameChanges() {
  131. return false;
  132. }
  133. @Override
  134. public boolean supportsEmailAddressChanges() {
  135. return false;
  136. }
  137. @Override
  138. public boolean supportsTeamMembershipChanges() {
  139. return false;
  140. }
  141. }
  142. }