generated from pagopa/pagopa-functions-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added logic to create token from userId
- Loading branch information
pierpaolo.didato@emeal.nttdata.com
authored and
pierpaolo.didato@emeal.nttdata.com
committed
Jul 12, 2024
1 parent
91b566b
commit d02caca
Showing
19 changed files
with
1,767 additions
and
1,297 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
11 changes: 10 additions & 1 deletion
11
...va/it/pagopa/selfcare/onboarding/client/auth/AuthenticationPropagationHeadersFactory.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
11 changes: 11 additions & 0 deletions
11
.../onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/config/TokenConfig.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package it.pagopa.selfcare.onboarding.config; | ||
|
||
import io.smallrye.config.ConfigMapping; | ||
|
||
@ConfigMapping(prefix = "onboarding-functions.jwt.token") | ||
public interface TokenConfig { | ||
String signingKey(); | ||
String kid(); | ||
String issuer(); | ||
String duration(); | ||
} |
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
6 changes: 6 additions & 0 deletions
6
...nboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/TokenService.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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package it.pagopa.selfcare.onboarding.service; | ||
|
||
public interface TokenService { | ||
|
||
String createJwt(String userId); | ||
} |
88 changes: 88 additions & 0 deletions
88
...ng-functions/src/main/java/it/pagopa/selfcare/onboarding/service/TokenServiceDefault.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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package it.pagopa.selfcare.onboarding.service; | ||
|
||
import io.jsonwebtoken.Header; | ||
import io.jsonwebtoken.JwsHeader; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import it.pagopa.selfcare.onboarding.config.TokenConfig; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.bouncycastle.asn1.pkcs.RSAPrivateKey; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.security.KeyFactory; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.KeySpec; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
import java.security.spec.RSAPrivateCrtKeySpec; | ||
import java.time.Duration; | ||
import java.util.Base64; | ||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
@ApplicationScoped | ||
public class TokenServiceDefault implements TokenService { | ||
|
||
@Inject | ||
TokenConfig tokenConfig; | ||
|
||
private static final String PRIVATE_KEY_HEADER_TEMPLATE = "-----BEGIN %s-----"; | ||
private static final String PRIVATE_KEY_FOOTER_TEMPLATE = "-----END %s-----"; | ||
private final Logger logger = LoggerFactory.getLogger(TokenServiceDefault.class.getName()); | ||
|
||
@Override | ||
public String createJwt(String userId) { | ||
PrivateKey privateKey; | ||
try { | ||
privateKey = getPrivateKey(tokenConfig.signingKey()); | ||
} catch (Exception e) { | ||
logger.error("Impossible to get private key. Error: {}", e.getMessage(), e); | ||
return null; | ||
} | ||
return Jwts.builder() | ||
.setId(UUID.randomUUID().toString()) | ||
.setIssuedAt(new Date()) | ||
.setIssuer(tokenConfig.issuer()) | ||
.setExpiration(Date.from(new Date().toInstant().plus(Duration.parse(tokenConfig.duration())))) | ||
.claim("uid", userId) | ||
.signWith(SignatureAlgorithm.RS256, privateKey) | ||
.setHeaderParam(JwsHeader.KEY_ID, tokenConfig.kid()) | ||
.setHeaderParam(Header.TYPE, Header.JWT_TYPE) | ||
.compact(); | ||
} | ||
|
||
private PrivateKey getPrivateKey(String signingKey) throws NoSuchAlgorithmException, InvalidKeySpecException { | ||
boolean isRsa = signingKey.contains("RSA"); | ||
String privateKeyEnvelopName = (isRsa ? "RSA " : "") + "PRIVATE KEY"; | ||
String privateKeyPEM = signingKey | ||
.replace("\r", "") | ||
.replace("\n", "") | ||
.replace(String.format(PRIVATE_KEY_HEADER_TEMPLATE, privateKeyEnvelopName), "") | ||
.replace(String.format(PRIVATE_KEY_FOOTER_TEMPLATE, privateKeyEnvelopName), ""); | ||
|
||
byte[] encoded = Base64.getDecoder().decode(privateKeyPEM); | ||
|
||
KeySpec keySpec; | ||
if (isRsa) { | ||
RSAPrivateKey rsaPrivateKey = RSAPrivateKey.getInstance(encoded); | ||
keySpec = new RSAPrivateCrtKeySpec( | ||
rsaPrivateKey.getModulus(), | ||
rsaPrivateKey.getPublicExponent(), | ||
rsaPrivateKey.getPrivateExponent(), | ||
rsaPrivateKey.getPrime1(), | ||
rsaPrivateKey.getPrime2(), | ||
rsaPrivateKey.getExponent1(), | ||
rsaPrivateKey.getExponent2(), | ||
rsaPrivateKey.getCoefficient()); | ||
|
||
} else { | ||
keySpec = new PKCS8EncodedKeySpec(encoded); | ||
} | ||
|
||
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | ||
return keyFactory.generatePrivate(keySpec); | ||
} | ||
} |
Oops, something went wrong.