--- /dev/null
+package com.gitblit;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.InputStreamReader;\r
+import java.net.HttpURLConnection;\r
+import java.net.URL;\r
+\r
+import org.apache.wicket.util.io.IOUtils;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import com.gitblit.models.UserModel;\r
+import com.google.gson.Gson;\r
+\r
+/**\r
+ * Implementation of an Redmine user service.<br>\r
+ * you can login to gitblit with Redmine user id and api key.\r
+ */\r
+public class RedmineUserService extends GitblitUserService {\r
+\r
+ private final Logger logger = LoggerFactory.getLogger(RedmineUserService.class);\r
+\r
+ private IStoredSettings settings;\r
+\r
+ private String testingJson;\r
+\r
+ private class RedmineCurrent {\r
+ private class RedmineUser {\r
+ public String login;\r
+ public String firstname;\r
+ public String lastname;\r
+ public String mail;\r
+ }\r
+\r
+ public RedmineUser user;\r
+ }\r
+\r
+ public RedmineUserService() {\r
+ super();\r
+ }\r
+\r
+ @Override\r
+ public void setup(IStoredSettings settings) {\r
+ this.settings = settings;\r
+\r
+ String file = settings.getString(Keys.realm.redmine.backingUserService, "users.conf");\r
+ File realmFile = GitBlit.getFileOrFolder(file);\r
+\r
+ serviceImpl = createUserService(realmFile);\r
+ logger.info("Redmine User Service backed by " + serviceImpl.toString());\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsCredentialChanges() {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsDisplayNameChanges() {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsEmailAddressChanges() {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsTeamMembershipChanges() {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public boolean supportsCookies() {\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public UserModel authenticate(String username, char[] password) {\r
+ String urlText = this.settings.getString(Keys.realm.redmine.url, "");\r
+ if (!urlText.endsWith("/")) {\r
+ urlText.concat("/");\r
+ }\r
+ String apiKey = String.valueOf(password);\r
+\r
+ try {\r
+ String jsonString = getCurrentUserAsJson(urlText, apiKey);\r
+\r
+ RedmineCurrent current = new Gson().fromJson(jsonString, RedmineCurrent.class);\r
+ String login = current.user.login;\r
+\r
+ if (username.equalsIgnoreCase(login)) {\r
+ UserModel userModel = new UserModel(login);\r
+ userModel.displayName = current.user.firstname + " " + current.user.lastname;\r
+ userModel.emailAddress = current.user.mail;\r
+ userModel.canAdmin = true;\r
+ return userModel;\r
+ }\r
+\r
+ } catch (IOException e) {\r
+ logger.error("authenticate", e);\r
+ }\r
+ return null;\r
+ }\r
+\r
+ private String getCurrentUserAsJson(String url, String apiKey) throws IOException {\r
+ if (testingJson != null) { // for testing\r
+ return testingJson;\r
+ }\r
+\r
+ URL apiUrl = new URL(url + "users/current.json?key=" + apiKey);\r
+ HttpURLConnection http = (HttpURLConnection) apiUrl.openConnection();\r
+ http.setRequestMethod("GET");\r
+ http.connect();\r
+ InputStreamReader reader = new InputStreamReader(http.getInputStream());\r
+ return IOUtils.toString(reader);\r
+ }\r
+\r
+ /**\r
+ * set json response. do NOT invoke from production code.\r
+ * @param json json\r
+ */\r
+ public void setTestingCurrentUserAsJson(String json) {\r
+ this.testingJson = json;\r
+ }\r
+\r
+}\r
--- /dev/null
+package com.gitblit.tests;\r
+\r
+import static org.hamcrest.CoreMatchers.is;\r
+import static org.junit.Assert.assertNull;\r
+import static org.junit.Assert.assertThat;\r
+\r
+import java.util.HashMap;\r
+\r
+import org.junit.Test;\r
+\r
+import com.gitblit.RedmineUserService;\r
+import com.gitblit.models.UserModel;\r
+import com.gitblit.tests.mock.MemorySettings;\r
+\r
+public class RedmineUserServiceTest {\r
+\r
+ private static final String JSON = "{\"user\":{\"created_on\":\"2011-03-28T00:41:29Z\",\"lastname\":\"foo\","\r
+ + "\"last_login_on\":\"2012-09-06T23:59:26Z\",\"firstname\":\"baz\","\r
+ + "\"id\":4,\"login\":\"RedmineUserId\",\"mail\":\"baz@example.com\"}}";\r
+\r
+ @Test\r
+ public void testAuthenticate() throws Exception {\r
+ RedmineUserService redmineUserService = new RedmineUserService();\r
+ redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));\r
+ redmineUserService.setTestingCurrentUserAsJson(JSON);\r
+ UserModel userModel = redmineUserService.authenticate("RedmineUserId", "RedmineAPIKey".toCharArray());\r
+ assertThat(userModel.getName(), is("RedmineUserId"));\r
+ assertThat(userModel.getDisplayName(), is("baz foo"));\r
+ assertThat(userModel.emailAddress, is("baz@example.com"));\r
+ }\r
+\r
+ @Test\r
+ public void testAuthenticateWithWronId() throws Exception {\r
+ RedmineUserService redmineUserService = new RedmineUserService();\r
+ redmineUserService.setup(new MemorySettings(new HashMap<String, Object>()));\r
+ redmineUserService.setTestingCurrentUserAsJson(JSON);\r
+ UserModel userModel = redmineUserService.authenticate("WrongRedmineUserId", "RedmineAPIKey".toCharArray());\r
+ assertNull(userModel);\r
+ }\r
+\r
+}\r