Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
fixup! fixup! rh-che #541: Login to user project using oc CLI in work…
Browse files Browse the repository at this point in the history
…space containers
  • Loading branch information
Oleksandr Garagatyi committed Mar 5, 2018
1 parent b9f93f1 commit eb21c38
Showing 1 changed file with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.eclipse.che.api.core.ApiException;
import org.eclipse.che.api.core.rest.HttpJsonRequestFactory;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.commons.subject.Subject;

/**
Expand All @@ -37,14 +38,14 @@ public class OpenshiftUserTokenProvider {
private static final int CONCURRENT_USERS = 500;

private final String tokenEndpoint;
private final HttpJsonRequestFactory httpJsonRequestFactory;
private final OkHttpClient httpClient;
private final LoadingCache<String, String> tokenCache;

@Inject
public OpenshiftUserTokenProvider(@Named("che.fabric8.auth.endpoint") String authEndpoint,
HttpJsonRequestFactory httpJsonRequestFactory) {
OkHttpClient httpClient) {
this.tokenEndpoint = authEndpoint + "/api/token?for=openshift";
this.httpJsonRequestFactory = httpJsonRequestFactory;
this.httpClient = httpClient;
this.tokenCache =
CacheBuilder.newBuilder()
.maximumSize(CONCURRENT_USERS)
Expand Down Expand Up @@ -94,18 +95,30 @@ private String getUserDescription(Subject subject) {
return subject.getUserName() + "(" + subject.getUserId() + ")";
}

@Nullable
private String getOsToken(final String keycloakToken) throws RuntimeException {
Request request =
new Request.Builder().url(tokenEndpoint).get()
.header("Authorization", "Bearer " + keycloakToken).build();
try {
return httpJsonRequestFactory
.fromUrl(tokenEndpoint)
.setMethod("GET")
.setAuthorizationHeader("Bearer " + keycloakToken)
.request()
.asProperties()
.get("access_token");
} catch (ApiException | IOException e) {
throw new RuntimeException("Could not retrieve OSO token from of the user");
Response response = httpClient.newCall(request).execute();
// Ignore IDE warning:
// body is not null after call of execute() according to javadocs of method body()
String body = response.body().string();
response.body().close();

if (!response.isSuccessful()) {
throw new RuntimeException(
"Could not retrieve OSO token of the user. Response from auth service: " + body);
}

return new JsonParser()
.parse(body)
.getAsJsonObject()
.get("access_token")
.getAsString();
} catch (IOException e) {
throw new RuntimeException(
"Could not retrieve OSO token from of the user. Error: " + e.getMessage());
}
}
}

0 comments on commit eb21c38

Please sign in to comment.