8000 [DRAFT] Set and check nonce during authentication sequence. by creechy · Pull Request #840 · unitycatalog/unitycatalog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[DRAFT] Set and check nonce during authentication sequence. #840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ lazy val client = (project in file("target/clients/java"))
"com.google.code.findbugs" % "jsr305" % "3.0.2",
"jakarta.annotation" % "jakarta.annotation-api" % "3.0.0" % Provided,

"com.auth0" % "java-jwt" % "4.4.0",

// Test dependencies
"org.junit.jupiter" % "junit-jupiter" % "5.10.3" % Test,
"net.aichler" % "jupiter-interface" % JupiterKeys.jupiterVersion.value % Test,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -102,18 +104,24 @@ public String authenticate() throws IOException {

byte[] stateBytes = new byte[16];
new SecureRandom().nextBytes(stateBytes);
String state = Hex.encodeHexString(stateBytes);

byte[] nonceBytes = new byte[16];
new SecureRandom().nextBytes(nonceBytes);
String nonce = Hex.encodeHexString(nonceBytes);

String authUrl =
String.format(
"%s?%s=%s& 8000 %s=%s&response_type=%s&scope=%s&state=%s",
"%s?%s=%s&%s=%s&response_type=%s&scope=%s&state=%s&nonce=%s",
authorizationUrl,
Fields.CLIENT_ID,
URLEncoder.encode(clientId, StandardCharsets.UTF_8),
Fields.REDIRECT_URL,
URLEncoder.encode(redirectUrl, StandardCharsets.UTF_8),
Fields.CODE,
URLEncoder.encode("openid profile email", StandardCharsets.UTF_8),
Hex.encodeHexString(stateBytes));
state,
nonce);

System.out.println("Attempting to open the authorization page in your default browser.");
System.out.println("If the browser does not open, you can manually open the following URL:");
Expand Down Expand Up @@ -184,7 +192,18 @@ public String authenticate() throws IOException {
Map<String, String> tokenResponseParams =
mapper.readValue(tokenResponse, new TypeReference<>() {});
urlConnection.disconnect();
return tokenResponseParams.get("id_token");

String idToken = tokenResponseParams.get("id_token");
if (idToken == null) {
throw new IOException("No id_token in response.");
}

DecodedJWT decodedJWT = JWT.decode(idToken);
if (!nonce.equals(decodedJWT.getClaim("nonce").asString())) {
throw new IOException("Nonce does not match.");
}

return idToken;
} else {
String errorResponse;
try (InputStream inputStream = urlConnection.getErrorStream()) {
Expand Down
Loading
0