-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
167 additions
and
2 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: 11 additions & 0 deletions
11
shared/src/main/java/org/pac4j/lagom/jwt/JwtGenerationException.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 org.pac4j.lagom.jwt; | ||
|
||
/** | ||
* @author Irina Lebedeva. | ||
*/ | ||
public class JwtGenerationException extends RuntimeException { | ||
|
||
public JwtGenerationException(String message) { | ||
super(message); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
shared/src/main/java/org/pac4j/lagom/jwt/JwtHeaderHandlerHelper.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,89 @@ | ||
package org.pac4j.lagom.jwt; | ||
|
||
import com.lightbend.lagom.javadsl.api.transport.RequestHeader; | ||
import com.nimbusds.jose.JOSEException; | ||
import com.nimbusds.jwt.JWT; | ||
import com.nimbusds.jwt.JWTClaimsSet; | ||
import com.nimbusds.jwt.PlainJWT; | ||
import com.typesafe.config.Config; | ||
import org.pac4j.jwt.config.encryption.EncryptionConfiguration; | ||
import org.pac4j.jwt.config.signature.SignatureConfiguration; | ||
|
||
import javax.inject.Inject; | ||
import java.text.ParseException; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import static org.apache.commons.lang3.StringUtils.join; | ||
import static org.pac4j.core.context.HttpConstants.AUTHORIZATION_HEADER; | ||
import static org.pac4j.core.context.HttpConstants.BEARER_HEADER_PREFIX; | ||
import static org.pac4j.lagom.jwt.JwtAuthenticatorHelper.parseEncryption; | ||
import static org.pac4j.lagom.jwt.JwtAuthenticatorHelper.parseSignature; | ||
|
||
/** | ||
* @author Irina Lebedeva. | ||
*/ | ||
@SuppressWarnings("PMD.TooManyStaticImports") | ||
public class JwtHeaderHandlerHelper { | ||
|
||
private Config config; | ||
|
||
@Inject | ||
public JwtHeaderHandlerHelper(Config config) { | ||
this.config = config; | ||
} | ||
|
||
public String generate(Map<String, Object> claims) { | ||
try { | ||
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder(); | ||
claims.forEach(builder::claim); | ||
|
||
if (config.hasPath("life-in-millis")) { | ||
builder.expirationTime(new Date(System.currentTimeMillis() + config.getLong("life-in-millis"))); | ||
} | ||
|
||
return internalGenerate(builder.build()); | ||
} catch (ParseException | JOSEException e) { | ||
throw new JwtGenerationException("Exception while parsing jwk"); | ||
} | ||
} | ||
|
||
private String internalGenerate(JWTClaimsSet claimsSet) throws ParseException, JOSEException { | ||
SignatureConfiguration signatureConfiguration = null; | ||
EncryptionConfiguration encryptionConfiguration = null; | ||
if (config.hasPath("private-key")) { | ||
Config privateKeyConf = config.getConfig("private-key"); | ||
signatureConfiguration = parseSignature(privateKeyConf); | ||
} | ||
if (config.hasPath("encryption")) { | ||
Config encryptionConf = config.getConfig("encryption"); | ||
encryptionConfiguration = parseEncryption(encryptionConf); | ||
} | ||
JWT jwt; | ||
if (signatureConfiguration == null) { | ||
jwt = new PlainJWT(claimsSet); | ||
} else { | ||
jwt = signatureConfiguration.sign(claimsSet); | ||
} | ||
return encryptionConfiguration != null ? encryptionConfiguration.encrypt(jwt) : jwt.serialize(); | ||
} | ||
|
||
/** | ||
* Генерация заголовка с хедером авторизации. | ||
* @param jwt jwt токен | ||
* @return {@link Function} | ||
*/ | ||
public static Function<RequestHeader, RequestHeader> buildJwtHeader(String jwt) { | ||
return header -> header.withHeader(AUTHORIZATION_HEADER, join(BEARER_HEADER_PREFIX, jwt)); | ||
} | ||
|
||
/** | ||
* Генерация заголовка с хедером авторизации. | ||
* @param claims параметры для заполнения токена | ||
* @return {@link Function} | ||
*/ | ||
public Function<RequestHeader, RequestHeader> buildJwtHeader(Map<String, Object> claims) { | ||
return header -> header.withHeader(AUTHORIZATION_HEADER, join(BEARER_HEADER_PREFIX, generate(claims))); | ||
} | ||
} |
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