forked from redhat-developer/app-services-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request redhat-developer#32 from bf2fc6cc711aee1a0c2a/java…
…_draft Java draft
- Loading branch information
Showing
16 changed files
with
367 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--- | ||
components: | ||
examples: | ||
400CreationExample: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
java/rhoas/src/main/java/com/openshift/cloud/beans/TokenExchanger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.openshift.cloud.beans; | ||
|
||
import io.vertx.core.json.JsonObject; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
import javax.inject.Singleton; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URLEncoder; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
|
||
/** | ||
* Utility bean to exchange offline tokens for access tokens | ||
*/ | ||
@Singleton | ||
public class TokenExchanger { | ||
|
||
@ConfigProperty(name = "auth.serverUrl", defaultValue = "https://sso.redhat.com/auth/realms/redhat-external") | ||
String authServerUrl; | ||
|
||
@ConfigProperty(name = "auth.clientId", defaultValue = "cloud-services") | ||
String clientId; | ||
|
||
@ConfigProperty(name = "auth.tokenPath", defaultValue = "protocol/openid-connect/token") | ||
String tokenPath; | ||
|
||
public String getToken(String secret) { | ||
try { | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(authServerUrl + "/" + tokenPath)) | ||
.header("content-type", "application/x-www-form-urlencoded") | ||
.timeout(Duration.ofMinutes(2)) | ||
.POST(ofFormData("grant_type","refresh_token", "client_id","cloud-services", "refresh_token", secret)) | ||
.build(); | ||
|
||
HttpClient client = HttpClient.newBuilder().build(); | ||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
if (response.statusCode() == 200 ) | ||
{ | ||
var tokens = response.body(); | ||
var json = new JsonObject(tokens); | ||
return json.getString("access_token"); | ||
} else { | ||
throw new RuntimeException(response.body()); | ||
} | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
|
||
} | ||
|
||
public static HttpRequest.BodyPublisher ofFormData(String... data) { | ||
|
||
var builder = new StringBuilder(); | ||
if (data.length % 2 == 1) { | ||
throw new IllegalArgumentException("Data must be key value pairs, but an number of data were given. "); | ||
} | ||
|
||
for (int index = 0; index < data.length; index+=2) { | ||
if (builder.length() > 0) { | ||
builder.append("&"); | ||
} | ||
builder.append(URLEncoder.encode(data[index], StandardCharsets.UTF_8)); | ||
builder.append("="); | ||
builder.append(URLEncoder.encode(data[index + 1], StandardCharsets.UTF_8)); | ||
} | ||
|
||
return HttpRequest.BodyPublishers.ofString(builder.toString()); | ||
} | ||
|
||
} |
Oops, something went wrong.