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.

WindowsAuthProvider.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.util.Set;
  18. import java.util.TreeSet;
  19. import waffle.windows.auth.IWindowsAccount;
  20. import waffle.windows.auth.IWindowsAuthProvider;
  21. import waffle.windows.auth.IWindowsComputer;
  22. import waffle.windows.auth.IWindowsIdentity;
  23. import waffle.windows.auth.impl.WindowsAuthProviderImpl;
  24. import com.gitblit.Constants;
  25. import com.gitblit.Constants.AccountType;
  26. import com.gitblit.Keys;
  27. import com.gitblit.auth.AuthenticationProvider.UsernamePasswordAuthenticationProvider;
  28. import com.gitblit.models.UserModel;
  29. import com.gitblit.utils.ArrayUtils;
  30. import com.gitblit.utils.StringUtils;
  31. import com.sun.jna.platform.win32.Win32Exception;
  32. /**
  33. * Implementation of a Windows authentication provider.
  34. *
  35. * @author James Moger
  36. */
  37. public class WindowsAuthProvider extends UsernamePasswordAuthenticationProvider {
  38. private IWindowsAuthProvider waffle;
  39. public WindowsAuthProvider() {
  40. super("windows");
  41. }
  42. @Override
  43. public void setup() {
  44. waffle = new WindowsAuthProviderImpl();
  45. IWindowsComputer computer = waffle.getCurrentComputer();
  46. logger.info("Windows Authentication Provider");
  47. logger.info(" name = " + computer.getComputerName());
  48. logger.info(" status = " + describeJoinStatus(computer.getJoinStatus()));
  49. logger.info(" memberOf = " + computer.getMemberOf());
  50. //logger.info(" groups = " + Arrays.asList(computer.getGroups()));
  51. }
  52. protected String describeJoinStatus(String value) {
  53. if ("NetSetupUnknownStatus".equals(value)) {
  54. return "unknown";
  55. } else if ("NetSetupUnjoined".equals(value)) {
  56. return "not joined";
  57. } else if ("NetSetupWorkgroupName".equals(value)) {
  58. return "joined to a workgroup";
  59. } else if ("NetSetupDomainName".equals(value)) {
  60. return "joined to a domain";
  61. }
  62. return value;
  63. }
  64. @Override
  65. public boolean supportsCredentialChanges() {
  66. return false;
  67. }
  68. @Override
  69. public boolean supportsDisplayNameChanges() {
  70. return false;
  71. }
  72. @Override
  73. public boolean supportsEmailAddressChanges() {
  74. return true;
  75. }
  76. @Override
  77. public boolean supportsTeamMembershipChanges() {
  78. return true;
  79. }
  80. @Override
  81. public AccountType getAccountType() {
  82. return AccountType.WINDOWS;
  83. }
  84. @Override
  85. public UserModel authenticate(String username, char[] password) {
  86. String defaultDomain = settings.getString(Keys.realm.windows.defaultDomain, null);
  87. if (StringUtils.isEmpty(defaultDomain)) {
  88. // ensure that default domain is null
  89. defaultDomain = null;
  90. }
  91. if (defaultDomain != null) {
  92. // sanitize username
  93. if (username.startsWith(defaultDomain + "\\")) {
  94. // strip default domain from domain\ username
  95. username = username.substring(defaultDomain.length() + 1);
  96. } else if (username.endsWith("@" + defaultDomain)) {
  97. // strip default domain from username@domain
  98. username = username.substring(0, username.lastIndexOf('@'));
  99. }
  100. }
  101. IWindowsIdentity identity = null;
  102. try {
  103. if (username.indexOf('@') > -1 || username.indexOf('\\') > -1) {
  104. // manually specified domain
  105. identity = waffle.logonUser(username, new String(password));
  106. } else {
  107. // no domain specified, use default domain
  108. identity = waffle.logonDomainUser(username, defaultDomain, new String(password));
  109. }
  110. } catch (Win32Exception e) {
  111. logger.error(e.getMessage());
  112. return null;
  113. }
  114. if (identity.isGuest() && !settings.getBoolean(Keys.realm.windows.allowGuests, false)) {
  115. logger.warn("Guest account access is disabled");
  116. identity.dispose();
  117. return null;
  118. }
  119. UserModel user = userManager.getUserModel(username);
  120. if (user == null) // create user object for new authenticated user
  121. user = new UserModel(username.toLowerCase());
  122. // create a user cookie
  123. if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {
  124. user.cookie = StringUtils.getSHA1(user.username + new String(password));
  125. }
  126. // update user attributes from Windows identity
  127. user.accountType = getAccountType();
  128. String fqn = identity.getFqn();
  129. if (fqn.indexOf('\\') > -1) {
  130. user.displayName = fqn.substring(fqn.lastIndexOf('\\') + 1);
  131. } else {
  132. user.displayName = fqn;
  133. }
  134. user.password = Constants.EXTERNAL_ACCOUNT;
  135. Set<String> groupNames = new TreeSet<String>();
  136. for (IWindowsAccount group : identity.getGroups()) {
  137. groupNames.add(group.getFqn());
  138. }
  139. if (groupNames.contains("BUILTIN\\Administrators")) {
  140. // local administrator
  141. user.canAdmin = true;
  142. }
  143. // TODO consider mapping Windows groups to teams
  144. // push the changes to the backing user service
  145. updateUser(user);
  146. // cleanup resources
  147. identity.dispose();
  148. return user;
  149. }
  150. }