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.openid.connect.sdk.Nonce;
import com.nimbusds.openid.connect.sdk.validators.IDTokenValidator;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
public class OidcData {
public final Nonce nonce;
public final CodeVerifier pkceVerifier;
public final String data;
@ -21,19 +18,13 @@ public class OidcData {
}
public boolean validateNonce(String nonce) {
return this.nonce.toString().equals(nonce);
return this.nonce.getValue().equals(nonce);
}
public String getState() {
String value = nonce + data;
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
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);
}
byte[] hash = DigestUtils.sha256(value);
return Base64.encodeBase64String(hash);
}
}