diff --git a/openid-connect-common/src/main/java/org/mitre/jwt/encryption/service/JwtEncryptionAndDecryptionService.java b/openid-connect-common/src/main/java/org/mitre/jwt/encryption/service/JwtEncryptionAndDecryptionService.java index fa0db1660..c96e25dcb 100644 --- a/openid-connect-common/src/main/java/org/mitre/jwt/encryption/service/JwtEncryptionAndDecryptionService.java +++ b/openid-connect-common/src/main/java/org/mitre/jwt/encryption/service/JwtEncryptionAndDecryptionService.java @@ -51,4 +51,9 @@ public interface JwtEncryptionAndDecryptionService { * @return */ public Collection getAllEncryptionAlgsSupported(); + + /** + * TODO add functionality for encrypting and decrypting using a specified key id. + * Example: public void encryptJwt(EncryptedJWT jwt, String kid); + */ } diff --git a/openid-connect-common/src/main/java/org/mitre/jwt/encryption/service/impl/DefaultJwtEncryptionAndDecryptionService.java b/openid-connect-common/src/main/java/org/mitre/jwt/encryption/service/impl/DefaultJwtEncryptionAndDecryptionService.java index f7d032287..c335c2155 100644 --- a/openid-connect-common/src/main/java/org/mitre/jwt/encryption/service/impl/DefaultJwtEncryptionAndDecryptionService.java +++ b/openid-connect-common/src/main/java/org/mitre/jwt/encryption/service/impl/DefaultJwtEncryptionAndDecryptionService.java @@ -103,13 +103,12 @@ public class DefaultJwtEncryptionAndDecryptionService implements JwtEncryptionAn @PostConstruct - public void afterPropertiesSet() throws NoSuchAlgorithmException, InvalidKeySpecException{ + public void afterPropertiesSet() throws NoSuchAlgorithmException, InvalidKeySpecException, JOSEException{ if (keys == null) { throw new IllegalArgumentException("Encryption and decryption service must have at least one key configured."); } - - // TODO call build..() again? see default signer service. + buildEncryptersAndDecrypters(); } public String getDefaultEncryptionKeyId() { @@ -141,7 +140,18 @@ public class DefaultJwtEncryptionAndDecryptionService implements JwtEncryptionAn */ @Override public void encryptJwt(EncryptedJWT jwt) { - // TODO Auto-generated method stub + if (getDefaultEncryptionKeyId() == null) { + throw new IllegalStateException("Tried to call default encryption with no default encrypter ID set"); + } + + JWEEncrypter encrypter = encrypters.get(getDefaultEncryptionKeyId()); + + try { + jwt.encrypt(encrypter); + } catch (JOSEException e) { + + logger.error("Failed to encrypt JWT, error was: ", e); + } } @@ -150,7 +160,18 @@ public class DefaultJwtEncryptionAndDecryptionService implements JwtEncryptionAn */ @Override public void decryptJwt(EncryptedJWT jwt) { - // TODO Auto-generated method stub + if (getDefaultDecryptionKeyId() == null) { + throw new IllegalStateException("Tried to call default decryption with no default decrypter ID set"); + } + + JWEDecrypter decrypter = decrypters.get(getDefaultDecryptionKeyId()); + + try { + jwt.decrypt(decrypter); + } catch (JOSEException e) { + + logger.error("Failed to decrypt JWT, error was: ", e); + } } diff --git a/openid-connect-common/src/test/java/org/mitre/jwt/encryption/service/impl/TestDefaultJwtEncryptionAndDecryptionService.java b/openid-connect-common/src/test/java/org/mitre/jwt/encryption/service/impl/TestDefaultJwtEncryptionAndDecryptionService.java new file mode 100644 index 000000000..03e7573dc --- /dev/null +++ b/openid-connect-common/src/test/java/org/mitre/jwt/encryption/service/impl/TestDefaultJwtEncryptionAndDecryptionService.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright 2013 The MITRE Corporation + * and the MIT Kerberos and Internet Trust Consortium + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.mitre.jwt.encryption.service.impl; + +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * @author wkim + * + */ +public class TestDefaultJwtEncryptionAndDecryptionService { + + @Test + public void test() { + fail("Not yet implemented"); + } + +}