-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bugfix: handle secret keys correctly (#1)
Current key providers were expecting key files to be stored in the Plain Text format. Following the Key Generation Guide, we've updated the Key Initialization to make use of binary (.DER) format for Private Key and (X.509 Certificate) template for Public Key Signed-off-by: Kshitij Patil <[email protected]>
- Loading branch information
Showing
9 changed files
with
51 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 15 additions & 25 deletions
40
...boot-app/src/main/java/com/kshitijpatil/tazabazar/security/jwt/JwtPrivateKeyProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,38 @@ | ||
package com.kshitijpatil.tazabazar.security.jwt; | ||
|
||
import com.kshitijpatil.tazabazar.utils.Base64Util; | ||
import com.kshitijpatil.tazabazar.utils.ReadKeyMixin; | ||
import com.kshitijpatil.tazabazar.configuration.JwtConfig; | ||
import com.kshitijpatil.tazabazar.utils.ResourceUtil; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.io.IOException; | ||
import java.security.KeyFactory; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.spec.EncodedKeySpec; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtPrivateKeyProvider implements ReadKeyMixin { | ||
@Getter | ||
public class JwtPrivateKeyProvider { | ||
private final ResourceUtil resourceUtil; | ||
private final Base64Util base64Util; | ||
|
||
@Getter | ||
private final JwtConfig jwtConfig; | ||
private PrivateKey privateKey; | ||
|
||
@PostConstruct | ||
public void init() { | ||
privateKey = readKey( | ||
"classpath:keys/tzb_key.pkcs8.private", | ||
"PRIVATE", | ||
this::privateKeySpec, | ||
this::privateKeyGenerator | ||
); | ||
} | ||
|
||
private EncodedKeySpec privateKeySpec(String data) { | ||
return new PKCS8EncodedKeySpec(base64Util.decode(data)); | ||
} | ||
|
||
private PrivateKey privateKeyGenerator(KeyFactory kf, EncodedKeySpec spec) { | ||
try { | ||
return kf.generatePrivate(spec); | ||
} catch (InvalidKeySpecException e) { | ||
throw new JwtInitializationException(e); | ||
var keyBytes = resourceUtil.readAllBytes(jwtConfig.getPrivateKeyFilepath()); | ||
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); | ||
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | ||
privateKey = keyFactory.generatePrivate(spec); | ||
} catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException ex) { | ||
throw new JwtInitializationException(ex); | ||
} | ||
} | ||
|
||
public PrivateKey get() { | ||
return privateKey; | ||
} | ||
} |
43 changes: 17 additions & 26 deletions
43
...gboot-app/src/main/java/com/kshitijpatil/tazabazar/security/jwt/JwtPublicKeyProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,39 @@ | ||
package com.kshitijpatil.tazabazar.security.jwt; | ||
|
||
import com.kshitijpatil.tazabazar.utils.Base64Util; | ||
import com.kshitijpatil.tazabazar.utils.ReadKeyMixin; | ||
import com.kshitijpatil.tazabazar.configuration.JwtConfig; | ||
import com.kshitijpatil.tazabazar.utils.ResourceUtil; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.security.KeyFactory; | ||
import java.io.IOException; | ||
import java.security.PublicKey; | ||
import java.security.spec.EncodedKeySpec; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.X509EncodedKeySpec; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.CertificateFactory; | ||
import java.security.cert.X509Certificate; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtPublicKeyProvider implements ReadKeyMixin { | ||
public class JwtPublicKeyProvider { | ||
@Getter | ||
private final ResourceUtil resourceUtil; | ||
private final Base64Util base64Util; | ||
|
||
@Getter | ||
private final JwtConfig jwtConfig; | ||
private PublicKey publicKey; | ||
|
||
@PostConstruct | ||
public void init() { | ||
publicKey = readKey( | ||
"classpath:keys/tzb_key.x509.public", | ||
"PUBLIC", | ||
this::publicKeySpec, | ||
this::publicKeyGenerator | ||
); | ||
} | ||
|
||
private EncodedKeySpec publicKeySpec(String data) { | ||
return new X509EncodedKeySpec(base64Util.decode(data)); | ||
} | ||
|
||
private PublicKey publicKeyGenerator(KeyFactory kf, EncodedKeySpec spec) { | ||
try { | ||
return kf.generatePublic(spec); | ||
} catch (InvalidKeySpecException e) { | ||
throw new JwtInitializationException(e); | ||
CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); | ||
var keyInputStream = resourceUtil.getInputStream(jwtConfig.getPublicKeyFilepath()); | ||
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(keyInputStream); | ||
publicKey = cert.getPublicKey(); | ||
} catch (CertificateException | IOException ex) { | ||
throw new JwtInitializationException(ex); | ||
} | ||
} | ||
|
||
public PublicKey get() { | ||
return publicKey; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters