commit | author | age
|
98ba4e
|
1 |
package com.gitblit;
|
M |
2 |
|
|
3 |
import java.io.File;
|
|
4 |
import java.io.IOException;
|
|
5 |
import java.io.InputStreamReader;
|
|
6 |
import java.net.HttpURLConnection;
|
|
7 |
|
|
8 |
import org.apache.wicket.util.io.IOUtils;
|
|
9 |
import org.slf4j.Logger;
|
|
10 |
import org.slf4j.LoggerFactory;
|
|
11 |
|
|
12 |
import com.gitblit.models.UserModel;
|
e334e3
|
13 |
import com.gitblit.utils.ConnectionUtils;
|
7cb82b
|
14 |
import com.gitblit.utils.StringUtils;
|
98ba4e
|
15 |
import com.google.gson.Gson;
|
M |
16 |
|
|
17 |
/**
|
|
18 |
* Implementation of an Redmine user service.<br>
|
|
19 |
* you can login to gitblit with Redmine user id and api key.
|
|
20 |
*/
|
|
21 |
public class RedmineUserService extends GitblitUserService {
|
|
22 |
|
|
23 |
private final Logger logger = LoggerFactory.getLogger(RedmineUserService.class);
|
|
24 |
|
|
25 |
private IStoredSettings settings;
|
|
26 |
|
|
27 |
private String testingJson;
|
|
28 |
|
|
29 |
private class RedmineCurrent {
|
|
30 |
private class RedmineUser {
|
|
31 |
public String login;
|
|
32 |
public String firstname;
|
|
33 |
public String lastname;
|
|
34 |
public String mail;
|
|
35 |
}
|
|
36 |
|
|
37 |
public RedmineUser user;
|
|
38 |
}
|
|
39 |
|
|
40 |
public RedmineUserService() {
|
|
41 |
super();
|
|
42 |
}
|
|
43 |
|
|
44 |
@Override
|
|
45 |
public void setup(IStoredSettings settings) {
|
|
46 |
this.settings = settings;
|
|
47 |
|
|
48 |
String file = settings.getString(Keys.realm.redmine.backingUserService, "users.conf");
|
|
49 |
File realmFile = GitBlit.getFileOrFolder(file);
|
|
50 |
|
|
51 |
serviceImpl = createUserService(realmFile);
|
|
52 |
logger.info("Redmine User Service backed by " + serviceImpl.toString());
|
|
53 |
}
|
|
54 |
|
|
55 |
@Override
|
|
56 |
public boolean supportsCredentialChanges() {
|
|
57 |
return false;
|
|
58 |
}
|
|
59 |
|
|
60 |
@Override
|
|
61 |
public boolean supportsDisplayNameChanges() {
|
|
62 |
return false;
|
|
63 |
}
|
|
64 |
|
|
65 |
@Override
|
|
66 |
public boolean supportsEmailAddressChanges() {
|
|
67 |
return false;
|
|
68 |
}
|
|
69 |
|
|
70 |
@Override
|
|
71 |
public boolean supportsTeamMembershipChanges() {
|
|
72 |
return false;
|
|
73 |
}
|
|
74 |
|
|
75 |
@Override
|
|
76 |
public UserModel authenticate(String username, char[] password) {
|
|
77 |
String urlText = this.settings.getString(Keys.realm.redmine.url, "");
|
|
78 |
if (!urlText.endsWith("/")) {
|
|
79 |
urlText.concat("/");
|
|
80 |
}
|
|
81 |
String apiKey = String.valueOf(password);
|
|
82 |
|
|
83 |
try {
|
|
84 |
String jsonString = getCurrentUserAsJson(urlText, apiKey);
|
|
85 |
|
|
86 |
RedmineCurrent current = new Gson().fromJson(jsonString, RedmineCurrent.class);
|
|
87 |
String login = current.user.login;
|
|
88 |
|
1684e0
|
89 |
boolean canAdmin = true;
|
M |
90 |
// non admin user can not get login name
|
|
91 |
if (StringUtils.isEmpty(login)) {
|
|
92 |
canAdmin = false;
|
|
93 |
login = current.user.mail;
|
98ba4e
|
94 |
}
|
M |
95 |
|
1684e0
|
96 |
UserModel userModel = new UserModel(login);
|
M |
97 |
userModel.canAdmin = canAdmin;
|
|
98 |
userModel.displayName = current.user.firstname + " " + current.user.lastname;
|
|
99 |
userModel.emailAddress = current.user.mail;
|
|
100 |
userModel.cookie = StringUtils.getSHA1(userModel.username + new String(password));
|
|
101 |
|
|
102 |
return userModel;
|
98ba4e
|
103 |
} catch (IOException e) {
|
M |
104 |
logger.error("authenticate", e);
|
|
105 |
}
|
|
106 |
return null;
|
|
107 |
}
|
|
108 |
|
|
109 |
private String getCurrentUserAsJson(String url, String apiKey) throws IOException {
|
|
110 |
if (testingJson != null) { // for testing
|
|
111 |
return testingJson;
|
|
112 |
}
|
|
113 |
|
e334e3
|
114 |
String apiUrl = url + "users/current.json?key=" + apiKey;
|
M |
115 |
HttpURLConnection http = (HttpURLConnection) ConnectionUtils.openConnection(apiUrl, null, null);
|
98ba4e
|
116 |
http.setRequestMethod("GET");
|
M |
117 |
http.connect();
|
|
118 |
InputStreamReader reader = new InputStreamReader(http.getInputStream());
|
|
119 |
return IOUtils.toString(reader);
|
|
120 |
}
|
|
121 |
|
|
122 |
/**
|
|
123 |
* set json response. do NOT invoke from production code.
|
|
124 |
* @param json json
|
|
125 |
*/
|
|
126 |
public void setTestingCurrentUserAsJson(String json) {
|
|
127 |
this.testingJson = json;
|
|
128 |
}
|
|
129 |
|
|
130 |
}
|