Skip to content

Commit ccfcbb6

Browse files
committed
Use verify size in line with requirements. Allow additional query parameter to be set with request.
1 parent 16b536e commit ccfcbb6

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

cwbi-auth-http-client/src/main/java/hec/army/usace/hec/cwbi/auth/http/client/AuthCodePkceTokenRequestBuilder.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,18 @@ OAuth2Token retrieveToken() throws IOException {
6363
HttpServer server = null;
6464
// https://datatracker.ietf.org/doc/html/rfc7636#section-4.1
6565
try {
66-
byte[] verifierBytes = new byte[128];
66+
// there is a character limit on the challenge, too much and it all just fails
67+
// this is more than is common (32) but reasonably below the cap.
68+
byte[] verifierBytes = new byte[40];
6769
SecureRandom.getInstanceStrong().nextBytes(verifierBytes);
6870
Base64.Encoder b64encoder = Base64.getUrlEncoder().withoutPadding();
6971
final String verifier = b64encoder.encodeToString(verifierBytes);
7072
final String originalState = UUID.randomUUID().toString();
7173

7274
MessageDigest md = MessageDigest.getInstance("SHA-256");
7375
final String challenge = b64encoder.encodeToString(md.digest(verifier.getBytes(StandardCharsets.US_ASCII)));
74-
server = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
76+
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
7577
int port = server.getAddress().getPort();
76-
String host = server.getAddress().getHostName();
7778

7879
final CompletableFuture<Result> future = new CompletableFuture<>();
7980

@@ -103,7 +104,7 @@ public void handle(HttpExchange exchange) throws IOException {
103104
}
104105

105106
});
106-
final String redirectUri = String.format("http://%s:%d", host, port);
107+
final String redirectUri = String.format("http://%s:%d", "127.0.0.1", port);
107108
final QueryParameters authParameters = QueryParameters.empty()
108109
.set("grant_type", "code")
109110
.set("client_id", getClientId())
@@ -113,6 +114,19 @@ public void handle(HttpExchange exchange) throws IOException {
113114
.set("code_challenge", challenge)
114115
.set("redirect_uri", redirectUri)
115116
.set("state", originalState);
117+
118+
/**
119+
* This absolutely needs to be handled in a more generic way. However, it's definitely required
120+
* for proper interaction in various environments. It should be injected as part of the setup
121+
* of the instance but that's another change to the interface that will trickly through 3 other
122+
* libraries. It's at least setup to be limited by client id and not used by everything using this flow.
123+
*/
124+
125+
final String idpHint = System.getProperty(String.format("cwms.openid.%s.idp.hint", this.getClientId()));
126+
if (idpHint != null) {
127+
authParameters.set("kc_idp_hint", idpHint);
128+
}
129+
116130
String urlStr= String.format("%s?%s", getAuthUrl().getApiRoot(), authParameters.encode());
117131
// start server to listen
118132
server.start();

0 commit comments

Comments
 (0)