Simplify oidc hash generation.

This commit is contained in:
Kavin 2024-11-18 00:23:24 +05:30
parent 074e4bc136
commit 580eb7fbbe
No known key found for this signature in database
GPG Key ID: 6E4598CA5C92C41F

View File

@ -2,14 +2,11 @@ package me.kavin.piped.utils.obj;
import com.nimbusds.oauth2.sdk.pkce.CodeVerifier; import com.nimbusds.oauth2.sdk.pkce.CodeVerifier;
import com.nimbusds.openid.connect.sdk.Nonce; import com.nimbusds.openid.connect.sdk.Nonce;
import com.nimbusds.openid.connect.sdk.validators.IDTokenValidator; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class OidcData { public class OidcData {
public final Nonce nonce; public final Nonce nonce;
public final CodeVerifier pkceVerifier; public final CodeVerifier pkceVerifier;
public final String data; public final String data;
@ -21,19 +18,13 @@ public class OidcData {
} }
public boolean validateNonce(String nonce) { public boolean validateNonce(String nonce) {
return this.nonce.toString().equals(nonce); return this.nonce.getValue().equals(nonce);
} }
public String getState() { public String getState() {
String value = nonce + data; String value = nonce + data;
try { byte[] hash = DigestUtils.sha256(value);
MessageDigest md = MessageDigest.getInstance("SHA-256"); return Base64.encodeBase64String(hash);
byte[] hash = md.digest(value.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(hash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-256 not supported", e);
}
} }
} }