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 |
|
4e3c15
|
12 |
import com.gitblit.Constants.AccountType;
|
98ba4e
|
13 |
import com.gitblit.models.UserModel;
|
4e3c15
|
14 |
import com.gitblit.utils.ArrayUtils;
|
e334e3
|
15 |
import com.gitblit.utils.ConnectionUtils;
|
7cb82b
|
16 |
import com.gitblit.utils.StringUtils;
|
98ba4e
|
17 |
import com.google.gson.Gson;
|
M |
18 |
|
|
19 |
/**
|
|
20 |
* Implementation of an Redmine user service.<br>
|
|
21 |
* you can login to gitblit with Redmine user id and api key.
|
|
22 |
*/
|
|
23 |
public class RedmineUserService extends GitblitUserService {
|
|
24 |
|
|
25 |
private final Logger logger = LoggerFactory.getLogger(RedmineUserService.class);
|
|
26 |
|
|
27 |
private IStoredSettings settings;
|
|
28 |
|
|
29 |
private String testingJson;
|
|
30 |
|
|
31 |
private class RedmineCurrent {
|
|
32 |
private class RedmineUser {
|
|
33 |
public String login;
|
|
34 |
public String firstname;
|
|
35 |
public String lastname;
|
|
36 |
public String mail;
|
|
37 |
}
|
|
38 |
|
|
39 |
public RedmineUser user;
|
|
40 |
}
|
|
41 |
|
|
42 |
public RedmineUserService() {
|
|
43 |
super();
|
|
44 |
}
|
|
45 |
|
|
46 |
@Override
|
|
47 |
public void setup(IStoredSettings settings) {
|
|
48 |
this.settings = settings;
|
|
49 |
|
93d506
|
50 |
String file = settings.getString(Keys.realm.redmine.backingUserService, "${baseFolder}/users.conf");
|
98ba4e
|
51 |
File realmFile = GitBlit.getFileOrFolder(file);
|
M |
52 |
|
|
53 |
serviceImpl = createUserService(realmFile);
|
|
54 |
logger.info("Redmine User Service backed by " + serviceImpl.toString());
|
|
55 |
}
|
|
56 |
|
|
57 |
@Override
|
|
58 |
public boolean supportsCredentialChanges() {
|
|
59 |
return false;
|
|
60 |
}
|
|
61 |
|
|
62 |
@Override
|
|
63 |
public boolean supportsDisplayNameChanges() {
|
|
64 |
return false;
|
|
65 |
}
|
|
66 |
|
|
67 |
@Override
|
|
68 |
public boolean supportsEmailAddressChanges() {
|
|
69 |
return false;
|
|
70 |
}
|
|
71 |
|
|
72 |
@Override
|
|
73 |
public boolean supportsTeamMembershipChanges() {
|
|
74 |
return false;
|
|
75 |
}
|
4e3c15
|
76 |
|
JM |
77 |
@Override
|
|
78 |
protected AccountType getAccountType() {
|
|
79 |
return AccountType.REDMINE;
|
|
80 |
}
|
98ba4e
|
81 |
|
M |
82 |
@Override
|
|
83 |
public UserModel authenticate(String username, char[] password) {
|
4e3c15
|
84 |
if (isLocalAccount(username)) {
|
JM |
85 |
// local account, bypass Redmine authentication
|
|
86 |
return super.authenticate(username, password);
|
|
87 |
}
|
|
88 |
|
b8fab6
|
89 |
String jsonString = null;
|
98ba4e
|
90 |
try {
|
b8fab6
|
91 |
// first attempt by username/password
|
JM |
92 |
jsonString = getCurrentUserAsJson(username, password);
|
|
93 |
} catch (Exception e1) {
|
|
94 |
logger.warn("Failed to authenticate via username/password against Redmine");
|
|
95 |
try {
|
|
96 |
// second attempt is by apikey
|
|
97 |
jsonString = getCurrentUserAsJson(null, password);
|
|
98 |
username = null;
|
|
99 |
} catch (Exception e2) {
|
|
100 |
logger.error("Failed to authenticate via apikey against Redmine", e2);
|
|
101 |
return null;
|
|
102 |
}
|
98ba4e
|
103 |
}
|
b8fab6
|
104 |
|
1f8262
|
105 |
if (StringUtils.isEmpty(jsonString)) {
|
JM |
106 |
logger.error("Received empty authentication response from Redmine");
|
|
107 |
return null;
|
|
108 |
}
|
|
109 |
|
b8fab6
|
110 |
RedmineCurrent current = null;
|
JM |
111 |
try {
|
|
112 |
current = new Gson().fromJson(jsonString, RedmineCurrent.class);
|
|
113 |
} catch (Exception e) {
|
|
114 |
logger.error("Failed to deserialize Redmine json response: " + jsonString, e);
|
|
115 |
return null;
|
|
116 |
}
|
|
117 |
|
|
118 |
if (StringUtils.isEmpty(username)) {
|
|
119 |
// if the username has been reset because of apikey authentication
|
|
120 |
// then use the email address of the user. this is the original
|
|
121 |
// behavior as contributed by github/mallowlabs
|
|
122 |
username = current.user.mail;
|
|
123 |
}
|
|
124 |
|
|
125 |
UserModel user = getUserModel(username);
|
|
126 |
if (user == null) // create user object for new authenticated user
|
|
127 |
user = new UserModel(username.toLowerCase());
|
|
128 |
|
|
129 |
// create a user cookie
|
|
130 |
if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {
|
|
131 |
user.cookie = StringUtils.getSHA1(user.username + new String(password));
|
|
132 |
}
|
|
133 |
|
|
134 |
// update user attributes from Redmine
|
|
135 |
user.accountType = getAccountType();
|
|
136 |
user.displayName = current.user.firstname + " " + current.user.lastname;
|
|
137 |
user.emailAddress = current.user.mail;
|
|
138 |
user.password = ExternalAccount;
|
|
139 |
if (!StringUtils.isEmpty(current.user.login)) {
|
|
140 |
// only admin users can get login name
|
|
141 |
// evidently this is an undocumented behavior of Redmine
|
|
142 |
user.canAdmin = true;
|
|
143 |
}
|
|
144 |
|
|
145 |
// TODO consider Redmine group mapping for team membership
|
|
146 |
// http://www.redmine.org/projects/redmine/wiki/Rest_Users
|
|
147 |
|
|
148 |
// push the changes to the backing user service
|
|
149 |
super.updateUserModel(user);
|
|
150 |
|
|
151 |
return user;
|
98ba4e
|
152 |
}
|
M |
153 |
|
b8fab6
|
154 |
private String getCurrentUserAsJson(String username, char [] password) throws IOException {
|
98ba4e
|
155 |
if (testingJson != null) { // for testing
|
M |
156 |
return testingJson;
|
|
157 |
}
|
|
158 |
|
b8fab6
|
159 |
String url = this.settings.getString(Keys.realm.redmine.url, "");
|
JM |
160 |
if (!url.endsWith("/")) {
|
70a0c7
|
161 |
url = url.concat("/");
|
b8fab6
|
162 |
}
|
JM |
163 |
HttpURLConnection http;
|
|
164 |
if (username == null) {
|
|
165 |
// apikey authentication
|
|
166 |
String apiKey = String.valueOf(password);
|
|
167 |
String apiUrl = url + "users/current.json?key=" + apiKey;
|
|
168 |
http = (HttpURLConnection) ConnectionUtils.openConnection(apiUrl, null, null);
|
|
169 |
} else {
|
|
170 |
// username/password BASIC authentication
|
|
171 |
String apiUrl = url + "users/current.json";
|
|
172 |
http = (HttpURLConnection) ConnectionUtils.openConnection(apiUrl, username, password);
|
|
173 |
}
|
98ba4e
|
174 |
http.setRequestMethod("GET");
|
M |
175 |
http.connect();
|
|
176 |
InputStreamReader reader = new InputStreamReader(http.getInputStream());
|
|
177 |
return IOUtils.toString(reader);
|
|
178 |
}
|
b8fab6
|
179 |
|
98ba4e
|
180 |
/**
|
M |
181 |
* set json response. do NOT invoke from production code.
|
|
182 |
* @param json json
|
|
183 |
*/
|
|
184 |
public void setTestingCurrentUserAsJson(String json) {
|
|
185 |
this.testingJson = json;
|
|
186 |
}
|
|
187 |
}
|