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.

RedmineAuthProvider.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2012 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.IOException;
  18. import java.io.InputStreamReader;
  19. import java.net.HttpURLConnection;
  20. import org.apache.wicket.util.io.IOUtils;
  21. import com.gitblit.Constants;
  22. import com.gitblit.Constants.AccountType;
  23. import com.gitblit.Keys;
  24. import com.gitblit.auth.AuthenticationProvider.UsernamePasswordAuthenticationProvider;
  25. import com.gitblit.models.UserModel;
  26. import com.gitblit.utils.ArrayUtils;
  27. import com.gitblit.utils.ConnectionUtils;
  28. import com.gitblit.utils.StringUtils;
  29. import com.google.gson.Gson;
  30. /**
  31. * Implementation of Redmine authentication.<br>
  32. * you can login to gitblit with Redmine user id and api key.
  33. */
  34. public class RedmineAuthProvider extends UsernamePasswordAuthenticationProvider {
  35. private String testingJson;
  36. private class RedmineCurrent {
  37. private class RedmineUser {
  38. public String login;
  39. public String firstname;
  40. public String lastname;
  41. public String mail;
  42. }
  43. public RedmineUser user;
  44. }
  45. public RedmineAuthProvider() {
  46. super("redmine");
  47. }
  48. @Override
  49. public void setup() {
  50. }
  51. @Override
  52. public boolean supportsCredentialChanges() {
  53. return false;
  54. }
  55. @Override
  56. public boolean supportsDisplayNameChanges() {
  57. return false;
  58. }
  59. @Override
  60. public boolean supportsEmailAddressChanges() {
  61. return false;
  62. }
  63. @Override
  64. public boolean supportsTeamMembershipChanges() {
  65. return false;
  66. }
  67. @Override
  68. public AccountType getAccountType() {
  69. return AccountType.REDMINE;
  70. }
  71. @Override
  72. public UserModel authenticate(String username, char[] password) {
  73. String jsonString = null;
  74. try {
  75. // first attempt by username/password
  76. jsonString = getCurrentUserAsJson(username, password);
  77. } catch (Exception e1) {
  78. logger.warn("Failed to authenticate via username/password against Redmine");
  79. try {
  80. // second attempt is by apikey
  81. jsonString = getCurrentUserAsJson(null, password);
  82. username = null;
  83. } catch (Exception e2) {
  84. logger.error("Failed to authenticate via apikey against Redmine", e2);
  85. return null;
  86. }
  87. }
  88. if (StringUtils.isEmpty(jsonString)) {
  89. logger.error("Received empty authentication response from Redmine");
  90. return null;
  91. }
  92. RedmineCurrent current = null;
  93. try {
  94. current = new Gson().fromJson(jsonString, RedmineCurrent.class);
  95. } catch (Exception e) {
  96. logger.error("Failed to deserialize Redmine json response: " + jsonString, e);
  97. return null;
  98. }
  99. if (StringUtils.isEmpty(username)) {
  100. // if the username has been reset because of apikey authentication
  101. // then use the email address of the user. this is the original
  102. // behavior as contributed by github/mallowlabs
  103. username = current.user.mail;
  104. }
  105. UserModel user = userManager.getUserModel(username);
  106. if (user == null) // create user object for new authenticated user
  107. user = new UserModel(username.toLowerCase());
  108. // create a user cookie
  109. if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {
  110. user.cookie = StringUtils.getSHA1(user.username + new String(password));
  111. }
  112. // update user attributes from Redmine
  113. user.accountType = getAccountType();
  114. user.displayName = current.user.firstname + " " + current.user.lastname;
  115. user.emailAddress = current.user.mail;
  116. user.password = Constants.EXTERNAL_ACCOUNT;
  117. if (!StringUtils.isEmpty(current.user.login)) {
  118. // only admin users can get login name
  119. // evidently this is an undocumented behavior of Redmine
  120. user.canAdmin = true;
  121. }
  122. // TODO consider Redmine group mapping for team membership
  123. // http://www.redmine.org/projects/redmine/wiki/Rest_Users
  124. // push the changes to the backing user service
  125. updateUser(user);
  126. return user;
  127. }
  128. private String getCurrentUserAsJson(String username, char [] password) throws IOException {
  129. if (testingJson != null) { // for testing
  130. return testingJson;
  131. }
  132. String url = this.settings.getString(Keys.realm.redmine.url, "");
  133. if (!url.endsWith("/")) {
  134. url = url.concat("/");
  135. }
  136. HttpURLConnection http;
  137. if (username == null) {
  138. // apikey authentication
  139. String apiKey = String.valueOf(password);
  140. String apiUrl = url + "users/current.json?key=" + apiKey;
  141. http = (HttpURLConnection) ConnectionUtils.openConnection(apiUrl, null, null);
  142. } else {
  143. // username/password BASIC authentication
  144. String apiUrl = url + "users/current.json";
  145. http = (HttpURLConnection) ConnectionUtils.openConnection(apiUrl, username, password);
  146. }
  147. http.setRequestMethod("GET");
  148. http.connect();
  149. InputStreamReader reader = new InputStreamReader(http.getInputStream());
  150. return IOUtils.toString(reader);
  151. }
  152. /**
  153. * set json response. do NOT invoke from production code.
  154. * @param json json
  155. */
  156. public void setTestingCurrentUserAsJson(String json) {
  157. this.testingJson = json;
  158. }
  159. }