By default, CoCart JWT uses HS256 (HMAC SHA-256) for token signing. You can switch to RS256 (RSA SHA-256) for enhanced security, especially in distributed systems.1. Generate RSA KeysFirst, generate a private/public key pair:
2. Configure KeysAdd these filters to a custom must-use plugin:
Copy
// Set the algorithm to RS256add_filter( 'cocart_jwt_auth_algorithm', function( $algorithm ) { return 'RS256';});// Set the private key for token signing.add_filter( 'cocart_jwt_auth_secret_private_key', function( $key ) { return file_get_contents( ABSPATH . 'path/to/private.key' );});// Set the public key for token validation.add_filter( 'cocart_jwt_auth_secret_public_key', function( $key ) { return file_get_contents( ABSPATH . 'path/to/public.key' );});
Store your keys securely and never commit them to version control. Consider using environment variables or WordPress constants in wp-config.php to store the key paths.
1. Key Storage ExampleA secure way to configure keys using constants:
Copy
// In wp-config.phpdefine( 'COCART_JWT_AUTH_PRIVATE_KEY_PATH', '/secure/path/private.key' );define( 'COCART_JWT_AUTH_PUBLIC_KEY_PATH', '/secure/path/public.key' );// In your codeadd_filter( 'cocart_jwt_auth_secret_private_key', function( $key ) { return file_get_contents( COCART_JWT_AUTH_PRIVATE_KEY_PATH );});add_filter( 'cocart_jwt_auth_secret_public_key', function( $key ) { return file_get_contents( COCART_JWT_AUTH_PUBLIC_KEY_PATH );});
4. Using Key Strings DirectlyAlternatively, you can use the RSA key strings directly in your code:
While using key strings directly in code is possible, it’s recommended to store them in secure environment variables or files for better security and key management.