-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGithubPluginConfigExtension.java
More file actions
201 lines (164 loc) · 9.32 KB
/
Copy pathGithubPluginConfigExtension.java
File metadata and controls
201 lines (164 loc) · 9.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.oxygenxml.examples.github;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ro.sync.ecss.extensions.api.webapp.plugin.PluginConfigExtension;
/**
* Plugin extension used to handle the configuration of this plugin.
* Setting the client_id and client_secret.
*
* @author gabriel_titerlea
*/
public class GithubPluginConfigExtension extends PluginConfigExtension {
/**
* The name of the client id options property
*/
private final String CLIENT_ID = "github.client_id";
/**
* The name of the client secret options property
*/
private final String CLIENT_SECRET = "github.client_secret";
/**
* The name of the apiUrl option property.
*/
private final String API_URL = "github.api_url";
/**
* The default value for the apiUrl.
*/
private final String defaultApiUrl = null;
@Override
public void init() throws ServletException {
// Calling super init to enable the automatic saving of options on disk.
super.init();
Map<String, String> defaultOptions = new HashMap<String, String>();
// Set the initial values from GithubOauthServlet as default values.
defaultOptions.put(CLIENT_ID, GitHubOauthServlet.clientId);
defaultOptions.put(CLIENT_SECRET, GitHubOauthServlet.clientSecret);
// By default this plugin will connect to github.com.
defaultOptions.put(API_URL, defaultApiUrl);
// Setting the default options, otherwise the doDelete method won't do anything.
setDefaultOptions(defaultOptions);
String clientId = getOption(CLIENT_ID, GitHubOauthServlet.clientId);
String clientSecret = getOption(CLIENT_SECRET, GitHubOauthServlet.clientSecret);
String apiUrl = getOption(API_URL, defaultApiUrl);
GitHubOauthServlet.clientId = clientId;
GitHubOauthServlet.clientSecret = clientSecret;
GitHubOauthServlet.apiUrl = apiUrl;
try {
saveOptions();
} catch (IOException e) {}
}
@Override
public String getPath() {
return "github-config";
}
@Override
public void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doPut(req, resp);
// Removing all access tokens to force all users to relogin
GitHubPlugin.accessTokens.invalidateAll();
}
/**
* Overriding to make sure the GithubOauthServlet properties are updated as well.
*/
@Override
public void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Calling super doDelete to set the options and save them on disk
super.doDelete(req, resp);
GitHubOauthServlet.clientId = getDefaultOptions().get(CLIENT_ID);
GitHubOauthServlet.clientSecret = getDefaultOptions().get(CLIENT_SECRET);
GitHubOauthServlet.apiUrl = getDefaultOptions().get(API_URL);
// The client_id and client_secret have changed so we need all the users to re-login.
GitHubPlugin.accessTokens.invalidateAll();
}
/**
* Overriding to make sure that the apiUrl, client_id and client_secret are updated in the GithubOauthServlet as well
*/
@Override
protected void setOption(String key, String value) {
if ("".equals(value)) {
value = null;
}
if (CLIENT_ID.equals(key)) {
GitHubOauthServlet.clientId = value;
}
if (CLIENT_SECRET.equals(key)) {
GitHubOauthServlet.clientSecret = value;
}
if (API_URL.equals(key)) {
if (value!= null && value.endsWith("/")) {
value = value.substring(0, value.length() - 1);
}
if ("https://api.github.com".equals(value)) {
value = null;
}
GitHubOauthServlet.apiUrl = value;
}
super.setOption(key, value);
}
/**
* @see ro.sync.ecss.extensions.api.webapp.plugin.PluginConfigExtension#getOptionsForm.
* @return @return The options form representing an html form with associated javascript/css which handles the saving of the new options.
*/
@Override
public String getOptionsForm() {
String clientId = GitHubOauthServlet.clientId;
if (clientId == null) {
clientId = "";
}
String clientSecret = GitHubOauthServlet.clientSecret;
if (clientSecret == null) {
clientSecret = "";
}
String apiUrl = GitHubOauthServlet.apiUrl;
if (apiUrl == null) {
apiUrl = "";
}
return "<div style='word-wrap: break-word;font-family: robotolight, Arial, Helvetica, sans-serif;font-size:0.9em;font-weight: lighter;'>"
+ "<label title='Check this only if you are using a Github Enterprise deployment'><input id='use-github-enterprise' type='checkbox'" + (apiUrl.isEmpty() ? "" : " class='using-enterprise' checked") + "/>Use GitHub Enterprise</label>"
+ "<form style='font-weight:bold;line-height:1.5em;margin-top:6px;'>"
+ "<div id='using-enterprise-msg' style='" + (apiUrl.isEmpty() ? "display:none;" : "display:block;") + "font-weight:normal;color:#333;background-color: lightyellow;border: 1px solid #dadab4;padding:0 5px 0 5px'>"
+ "<p style='margin:5px 0'>The GitHub Plugin uses OAuth 2.0 to gain access to your files.</p>"
+ "<p style='margin:5px 0'>To obtain the configuration parameters listed below go to your GitHub Enterprise deployment in settings > applications > developer applications (http://enterprise-deployment.com/settings/applications/new) and register a new application.</p>"
+ "<p style='margin:5px 0'><b>Note:</b> The <span style='font-family:monospace'>Authorization callback URL</span> must be set to:<br/><span style='font-family:monospace'><span class='web-author-context'>web-author-context</span>/plugins-dispatcher/github-oauth/callback</span>"
+ "</div>"
+ "<div id='no-enterprise-msg' style='" + (apiUrl.isEmpty() ? "display:block;" : "display:none;") + "font-weight:normal;color:#333;background-color: lightyellow;border: 1px solid #dadab4;padding:0 5px 0 5px'>"
+ "<p style='margin:5px 0'>The GitHub Plugin uses OAuth 2.0 to gain access to your files.</p>"
+ "<p style='margin:5px 0'>To obtain the configuration parameters listed below go to your GitHub developer applications page <a target='_blank' href='https://github.com/settings/applications/new'>(https://github.com/settings/applications/new)</a> and register a new application.</p>"
+ "<p style='margin:5px 0'><b>Note:</b> The <span style='font-family:monospace'>Authorization callback URL</span> must be set to:<br/><span style='font-family:monospace'><span class='web-author-context'>web-author-context</span>/plugins-dispatcher/github-oauth/callback</span>"
+ "</div>"
+ "<label title='Your GitHub Enterprise deployment URL' id='gh-api-url' style='margin-top:6px;margin-bottom:6px;display:block;overflow:hidden;" + (apiUrl.isEmpty() ? "display:none;'" : "' class='using-enterprise'") + ">"
+ "API URL: <span style='font-weight:normal;font-size:0.85em'>(http://enterprise-deployment.com)</span>"
+ "<input placeholder='Your GitHub Enterprise deployment URL' name='" + API_URL + "' type='text' style='color:#606060;background-color:#FAFAFA;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;width:100%;border-radius:4px;border:1px solid #C8C1C1;padding:6px 4px' value='" + apiUrl +"'/>"
+ "</label>"
+ "<label style='margin-top:6px;display:block;overflow:hidden'>"
+ "Client ID: "
+ "<input placeholder='Client ID' name='" + CLIENT_ID + "' type='text' style='color:#606060;background-color:#FAFAFA;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;width:100%;border-radius:4px;border:1px solid #C8C1C1;padding:6px 4px' value='" + clientId + "'/>"
+ "</label>"
+ "<label style='margin-top:6px;display:block;overflow:hidden'>"
+ "Client Secret:"
+ "<input placeholder='Client Secret' name='" + CLIENT_SECRET + "' type='text' style='color:#606060;background-color:#FAFAFA;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;width:100%;border-radius:4px;border:1px solid #C8C1C1;padding:6px 4px' value='" + clientSecret +"'/>"
+ "</label>"
+ "</form>"
+ "</div>"
// Adding some javascript to show/hide some elements depending on the value of the #use-github-enterprise checkbox
+ "<script src='../plugin-resources/github-static/github-config.js'></script>";
}
/**
* @see ro.sync.ecss.extensions.api.webapp.plugin.PluginConfigExtension#getOptionsJson.
* @return A JSON representation of the current options.
*/
@Override
public String getOptionsJson() {
return "{" +
"\"" + API_URL + "\":\"" + (GitHubOauthServlet.apiUrl != null ? GitHubOauthServlet.apiUrl : "https://api.github.com") + "\"," +
"\"" + CLIENT_ID + "\":\"" + (GitHubOauthServlet.clientId != null ? GitHubOauthServlet.clientId : "") + "\"," +
"\"" + CLIENT_SECRET + "\":\"" + (GitHubOauthServlet.clientSecret != null ? GitHubOauthServlet.clientSecret : "") + "\""
+ "}";
}
}