-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove unneccessary guava dependency
- Loading branch information
1 parent
01b557f
commit 438aadf
Showing
8 changed files
with
204 additions
and
93 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
42 changes: 42 additions & 0 deletions
42
src/main/java/com/mercateo/spring/security/jwt/token/verifier/AlgorithmFactory.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,42 @@ | ||
/* | ||
* Copyright © 2017 Mercateo AG (http://www.mercateo.com) | ||
* | ||
* 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 com.mercateo.spring.security.jwt.token.verifier; | ||
|
||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.auth0.jwt.exceptions.AlgorithmMismatchException; | ||
import com.auth0.jwt.interfaces.RSAKeyProvider; | ||
|
||
class AlgorithmFactory { | ||
private final RSAKeyProvider rsaKeyProvider; | ||
|
||
public AlgorithmFactory(RSAKeyProvider rsaKeyProvider) { | ||
this.rsaKeyProvider = rsaKeyProvider; | ||
} | ||
|
||
public Algorithm createByName(String algorithmName) throws AlgorithmMismatchException { | ||
switch (algorithmName.toLowerCase()) { | ||
case "rs256": | ||
return Algorithm.RSA256(rsaKeyProvider); | ||
case "rs384": | ||
return Algorithm.RSA384(rsaKeyProvider); | ||
case "rs512": | ||
return Algorithm.RSA512(rsaKeyProvider); | ||
default: | ||
throw new AlgorithmMismatchException( | ||
"The provided Algorithm has to be RSA."); | ||
} | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
src/main/java/com/mercateo/spring/security/jwt/token/verifier/RSAKeyProviderFactory.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,72 @@ | ||
/* | ||
* Copyright © 2020 Mercateo AG (http://www.mercateo.com) | ||
* | ||
* 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 com.mercateo.spring.security.jwt.token.verifier; | ||
|
||
import java.security.Key; | ||
import java.security.KeyFactory; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.interfaces.RSAPrivateKey; | ||
import java.security.interfaces.RSAPublicKey; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.X509EncodedKeySpec; | ||
|
||
import com.auth0.jwk.Jwk; | ||
import com.auth0.jwt.interfaces.RSAKeyProvider; | ||
import com.mercateo.spring.security.jwt.token.keyset.JWTKeyset; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@AllArgsConstructor | ||
class RSAKeyProviderFactory { | ||
|
||
private final JWTKeyset jwtKeyset; | ||
|
||
private static IllegalStateException map(Throwable cause) { | ||
return new IllegalStateException(cause); | ||
} | ||
|
||
private static RSAPublicKey createKey(byte[] bytes) throws NoSuchAlgorithmException, InvalidKeySpecException { | ||
return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic( | ||
new X509EncodedKeySpec(bytes)); | ||
} | ||
|
||
public RSAKeyProvider create() { | ||
return new RSAKeyProvider() { | ||
@Override | ||
public RSAPublicKey getPublicKeyById(String keyId) { | ||
return jwtKeyset | ||
.getKeysetForId(keyId) | ||
.mapTry(Jwk::getPublicKey) | ||
.map(Key::getEncoded) | ||
.mapTry(RSAKeyProviderFactory::createKey) | ||
.onFailure(e -> log.error("Error getting public key for id " + keyId, e)) | ||
.getOrElseThrow(RSAKeyProviderFactory::map); | ||
} | ||
|
||
@Override | ||
public RSAPrivateKey getPrivateKey() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getPrivateKeyId() { | ||
return null; | ||
} | ||
}; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/test/java/com/mercateo/spring/security/jwt/token/verifier/AlgorithmFactoryTest.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,73 @@ | ||
/* | ||
* Copyright © 2017 Mercateo AG (http://www.mercateo.com) | ||
* | ||
* 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 com.mercateo.spring.security.jwt.token.verifier; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.auth0.jwk.Jwk; | ||
import com.auth0.jwt.algorithms.Algorithm; | ||
import com.auth0.jwt.exceptions.AlgorithmMismatchException; | ||
import com.mercateo.spring.security.jwt.JWKProvider; | ||
import com.mercateo.spring.security.jwt.token.keyset.JWTKeyset; | ||
|
||
import io.vavr.control.Try; | ||
|
||
public class AlgorithmFactoryTest { | ||
|
||
private AlgorithmFactory uut; | ||
|
||
@Before | ||
public void setUp() { | ||
final JWKProvider jwkProvider = new JWKProvider(); | ||
String keyId = "4711"; | ||
final Jwk jwk = jwkProvider.create(keyId); | ||
JWTKeyset jwks = mock(JWTKeyset.class); | ||
when(jwks.getKeysetForId(keyId)).thenReturn(Try.success(jwk)); | ||
assertThat(jwks.getKeysetForId(keyId)).isNotNull(); | ||
RSAKeyProviderFactory keyProviderFactory = new RSAKeyProviderFactory(jwks); | ||
uut = new AlgorithmFactory(keyProviderFactory.create()); | ||
} | ||
|
||
@Test | ||
public void createsRSA256Algorithm() { | ||
Algorithm algo1 = uut.createByName("RS256"); | ||
assertEquals("RS256", algo1.getName()); | ||
} | ||
|
||
@Test | ||
public void createRSA384Algorithm() { | ||
Algorithm algo2 = uut.createByName("RS384"); | ||
assertEquals("RS384", algo2.getName()); | ||
} | ||
|
||
@Test | ||
public void createRSA512Algorithm() { | ||
Algorithm algo3 = uut.createByName("RS512"); | ||
assertEquals("RS512", algo3.getName()); | ||
} | ||
|
||
@Test | ||
public void failsCreatingUnknownAlgorithm() { | ||
assertThatThrownBy(() -> uut.createByName("foo")).isInstanceOf(AlgorithmMismatchException.class); | ||
} | ||
} |
Oops, something went wrong.