Skip to content

Commit

Permalink
add option to control if https should be secure (no restart required)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwielgolaski committed Jul 2, 2017
1 parent 8c2f379 commit eb5db59
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/main/java/jetbrains/buildServer/auth/oauth/OAuthClient.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
package jetbrains.buildServer.auth.oauth;

import com.intellij.openapi.util.text.StringUtil;
import okhttp3.*;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.log4j.Logger;
import org.json.simple.JSONValue;
import org.springframework.http.MediaType;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class OAuthClient {

private AuthenticationSchemeProperties properties;
private static final Logger log = Logger.getLogger(OAuthClient.class);
private final OkHttpClient httpClient;
private final Map<Boolean, OkHttpClient> httpClients = new HashMap<>();

public OAuthClient(AuthenticationSchemeProperties properties) {
this.properties = properties;
this.httpClient = HttpClientFactory.createClient(properties.isAllowInsecureHttps());
}

private OkHttpClient getHttpClient() {
return httpClients.computeIfAbsent(properties.isAllowInsecureHttps(), HttpClientFactory::createClient);
}

public String getRedirectUrl(String state) {
Expand Down Expand Up @@ -48,7 +57,7 @@ public String getAccessToken(String code) throws IOException {
.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE)
.post(formBody)
.build();
Response response = httpClient.newCall(request).execute();
Response response = getHttpClient().newCall(request).execute();
Map jsonResponse = (Map) JSONValue.parse(response.body().string());
return (String) jsonResponse.get("access_token");
}
Expand All @@ -58,7 +67,7 @@ public Map getUserData(String token) throws IOException {
.url(properties.getUserEndpoint())
.addHeader("Authorization","Bearer " + token)
.build();
String response = httpClient.newCall(request).execute().body().string();
String response = getHttpClient().newCall(request).execute().body().string();
log.debug("Fetched user data: " + response);
return (Map) JSONValue.parse(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<div>
<prop:checkboxProperty uncheckedValue="false" name="<%=ConfigKey.allowInsecureHttps.toString()%>"/>
<label width="100%" for="<%=ConfigKey.allowInsecureHttps%>">Insecure https</label><br/>
<span class="grayNote">Allow insecure https access like invalid certificate (restart required)</span>
<span class="grayNote">Allow insecure https access like invalid certificate</span>
</div>
<script type="text/javascript">
BS.TeamCityOAuth.init('#<%=ConfigKey.preset.toString()%>');
Expand Down

0 comments on commit eb5db59

Please sign in to comment.