Skip to content

Conversation

@nbaars
Copy link
Owner

@nbaars nbaars commented Aug 10, 2025

This PR removes the strange behavior of the commons classes surrounding PrivateKey and PublicKey. In some cases and byte[] could be passed and in some cases a instance of a java.security.Key was expected. The keys are now strictly typed. We also removed the Key superclass as it no longer plays a role.

The class SecretKey stays behind as we only work with the raw byte[] in all algorithms.

Due to the strict typing we no longer need to check for purpose explicitly as PasetoLocal and PasetoPublic as the typing forces a correct key to be used. The superclass which might introduce the wrong usage of the API no longer exists.

nbaars added 7 commits August 10, 2025 15:12
Break inheritance with Key superclass as the byte[] is all we need.

All the time the key is 32 of length and by using a type for SecretKey we don't need to check as the code does not allow to pass a SecretKey instance to a PrivateKey instance. Typing to the rescue.

This comment was marked as outdated.

@nbaars nbaars requested a review from Copilot August 10, 2025 15:28

This comment was marked as outdated.

@nbaars nbaars requested a review from Copilot August 10, 2025 15:38
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the key handling system by removing the dubious PrivateKey and PublicKey commons classes and the abstract Key superclass, replacing them with strict typing using Java's built-in key interfaces. The refactor eliminates ambiguity between byte array and key instance handling while maintaining the SecretKey class for raw byte array operations.

  • Replaces generic key wrappers with specific Java security interface types (EdECPrivateKey, EdECPublicKey, ECPrivateKey, ECPublicKey, RSAPrivateKey, RSAPublicKey)
  • Removes version and purpose validation checks that are now enforced through type safety
  • Simplifies key constructors and eliminates deprecated byte array constructors for newer versions

Reviewed Changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
version4/src/main/java/org/paseto4j/version4/PasetoPublic.java Updates to use EdECPrivateKey/EdECPublicKey interfaces directly, removes validation checks
version4/src/main/java/org/paseto4j/version4/PasetoLocal.java Removes version/purpose validation, updates key access methods
version4/src/main/java/org/paseto4j/version4/Paseto.java Updates method signatures to use typed key interfaces
version4/src/main/java/org/paseto4j/version4/CryptoFunctions.java Updates signature methods to accept EdECPrivateKey directly
version3/src/main/java/org/paseto4j/version3/PasetoPublic.java Updates to use ECPrivateKey/ECPublicKey interfaces, adds key conversion logic
version3/src/main/java/org/paseto4j/version3/PasetoLocal.java Removes validation checks, updates key access
version3/src/main/java/org/paseto4j/version3/Paseto.java Updates method signatures for EC key types
version3/src/main/java/org/paseto4j/version3/CryptoFunctions.java Updates crypto functions to work with EC key interfaces
version2/src/main/java/org/paseto4j/version2/PrivateKey.java Adds new version-specific PrivateKey record with validation
version2/src/main/java/org/paseto4j/version2/PublicKey.java Adds new version-specific PublicKey record with validation
version2/src/main/java/org/paseto4j/version2/PasetoPublic.java Updates to use new key records, removes validation
version2/src/main/java/org/paseto4j/version2/PasetoLocal.java Updates key access methods
version1/src/main/java/org/paseto4j/version1/PasetoPublic.java Updates to use RSA key interfaces directly
version1/src/main/java/org/paseto4j/version1/PasetoLocal.java Removes validation checks
version1/src/main/java/org/paseto4j/version1/Paseto.java Updates method signatures for RSA key types
version1/src/main/java/org/paseto4j/version1/CryptoFunctions.java Adds key conversion utilities, updates crypto functions
commons/src/main/java/org/paseto4j/commons/SecretKey.java Converts to record with built-in validation
commons/src/main/java/org/paseto4j/commons/TokenAlgorithm.java Converts to record

String expectedToken)
throws IOException, SignatureException {
Reader rdr = new StringReader(publicKeyPem);
Object parsed = new PEMParser(rdr).readObject();
Copy link

Copilot AI Aug 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The removed debug print statement System.out.println(parsed); suggests this may have been used for debugging. Consider adding proper logging if debugging information is needed in production.

Copilot uses AI. Check for mistakes.
@sonarqubecloud
Copy link

@nbaars nbaars merged commit 078bcce into main Aug 27, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants