Changesets · Branches · Tags · Compare
| Changeset | 84ffd9a22d37f1417a89f4a7ebc5aaa903f009effd18fcb966be23d81588cd00 |
|---|---|
| Author | Paul Latzelsperger <43503240+paullatzelsperger@users.noreply.github.com> · 2024-08-08 09:29 +02:00 |
| Committer | GitHub <noreply@github.com> · 2024-08-08 09:29 +02:00 |
| Parents | 49f52aca5a72 |
29 files changed
modified core/common/token-core/build.gradle.kts +1 −0
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | api(project(":spi:common:keys-spi")) |
| 22 | 22 | api(project(":spi:common:token-spi")) |
| 23 | 23 | api(project(":spi:common:jwt-spi")) |
| 24 | + api(project(":spi:common:jwt-signer-spi")) | |
| 24 | 25 | |
| 25 | 26 | implementation(project(":core:common:lib:crypto-common-lib")) // for the CryptoConverter |
| 26 | 27 | implementation(libs.nimbus.jwt) |
modified core/common/token-core/src/main/java/org/eclipse/edc/token/JwtGenerationService.java +13 −8
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import com.nimbusds.jose.JWSHeader; |
| 21 | 21 | import com.nimbusds.jwt.JWTClaimsSet; |
| 22 | 22 | import com.nimbusds.jwt.SignedJWT; |
| 23 | +import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider; | |
| 23 | 24 | import org.eclipse.edc.security.token.jwt.CryptoConverter; |
| 24 | 25 | import org.eclipse.edc.spi.EdcException; |
| 25 | 26 | import org.eclipse.edc.spi.iam.TokenParameters; |
| @@ -29,25 +30,29 @@ | ||
| 29 | 30 | import org.eclipse.edc.token.spi.TokenGenerationService; |
| 30 | 31 | import org.jetbrains.annotations.NotNull; |
| 31 | 32 | |
| 32 | -import java.security.PrivateKey; | |
| 33 | 33 | import java.text.ParseException; |
| 34 | 34 | import java.util.ArrayList; |
| 35 | 35 | import java.util.Arrays; |
| 36 | 36 | import java.util.Map; |
| 37 | -import java.util.function.Supplier; | |
| 38 | 37 | |
| 39 | 38 | public class JwtGenerationService implements TokenGenerationService { |
| 40 | 39 | |
| 40 | + private final JwsSignerProvider jwsGeneratorFunction; | |
| 41 | + | |
| 42 | + public JwtGenerationService(JwsSignerProvider jwsSignerProvider) { | |
| 43 | + | |
| 44 | + this.jwsGeneratorFunction = jwsSignerProvider; | |
| 45 | + } | |
| 41 | 46 | |
| 42 | 47 | @Override |
| 43 | - public Result<TokenRepresentation> generate(Supplier<PrivateKey> privateKeySupplier, @NotNull TokenDecorator... decorators) { | |
| 44 | - | |
| 45 | - var privateKey = privateKeySupplier.get(); | |
| 46 | - if (privateKey == null) { | |
| 47 | - return Result.failure("PrivateKey cannot be resolved."); | |
| 48 | + public Result<TokenRepresentation> generate(String privateKeyId, @NotNull TokenDecorator... decorators) { | |
| 49 | + | |
| 50 | + var tokenSignerResult = jwsGeneratorFunction.createJwsSigner(privateKeyId); | |
| 51 | + if (tokenSignerResult.failed()) { | |
| 52 | + return Result.failure("JWSSigner cannot be generated for private key '%s': %s".formatted(privateKeyId, tokenSignerResult.getFailureDetail())); | |
| 48 | 53 | } |
| 49 | 54 | |
| 50 | - var tokenSigner = CryptoConverter.createSignerFor(privateKey); | |
| 55 | + var tokenSigner = tokenSignerResult.getContent(); | |
| 51 | 56 | var jwsAlgorithm = CryptoConverter.getRecommendedAlgorithm(tokenSigner); |
| 52 | 57 | |
| 53 | 58 | var bldr = TokenParameters.Builder.newInstance(); |
modified core/common/token-core/src/main/java/org/eclipse/edc/token/TokenServicesExtension.java +15 −0
| @@ -14,8 +14,13 @@ | ||
| 14 | 14 | |
| 15 | 15 | package org.eclipse.edc.token; |
| 16 | 16 | |
| 17 | +import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider; | |
| 18 | +import org.eclipse.edc.keys.spi.PrivateKeyResolver; | |
| 17 | 19 | import org.eclipse.edc.runtime.metamodel.annotation.Extension; |
| 20 | +import org.eclipse.edc.runtime.metamodel.annotation.Inject; | |
| 18 | 21 | import org.eclipse.edc.runtime.metamodel.annotation.Provider; |
| 22 | +import org.eclipse.edc.security.token.jwt.CryptoConverter; | |
| 23 | +import org.eclipse.edc.spi.result.Result; | |
| 19 | 24 | import org.eclipse.edc.spi.system.ServiceExtension; |
| 20 | 25 | import org.eclipse.edc.token.spi.TokenDecoratorRegistry; |
| 21 | 26 | import org.eclipse.edc.token.spi.TokenValidationRulesRegistry; |
| @@ -31,6 +36,9 @@ | ||
| 31 | 36 | public class TokenServicesExtension implements ServiceExtension { |
| 32 | 37 | public static final String NAME = "Token Services Extension"; |
| 33 | 38 | |
| 39 | + @Inject | |
| 40 | + private PrivateKeyResolver privateKeyResolver; | |
| 41 | + | |
| 34 | 42 | @Provider |
| 35 | 43 | public TokenValidationRulesRegistry tokenValidationRulesRegistry() { |
| 36 | 44 | return new TokenValidationRulesRegistryImpl(); |
| @@ -45,4 +53,11 @@ | ||
| 45 | 53 | public TokenDecoratorRegistry tokenDecoratorRegistry() { |
| 46 | 54 | return new TokenDecoratorRegistryImpl(); |
| 47 | 55 | } |
| 56 | + | |
| 57 | + @Provider(isDefault = true) | |
| 58 | + public JwsSignerProvider defaultSignerProvider() { | |
| 59 | + // default implementation: resolve the private key (from vault of config) and create a JWSSigner based on its algorithm | |
| 60 | + return privateKeyId -> privateKeyResolver.resolvePrivateKey(privateKeyId) | |
| 61 | + .compose(pk -> Result.ofThrowable(() -> CryptoConverter.createSignerFor(pk))); | |
| 62 | + } | |
| 48 | 63 | } |
modified core/common/token-core/src/test/java/org/eclipse/edc/token/JwtGenerationServiceTest.java +16 −9
| @@ -23,6 +23,8 @@ | ||
| 23 | 23 | import com.nimbusds.jose.jwk.gen.RSAKeyGenerator; |
| 24 | 24 | import com.nimbusds.jose.util.Base64URL; |
| 25 | 25 | import com.nimbusds.jwt.SignedJWT; |
| 26 | +import org.eclipse.edc.security.token.jwt.CryptoConverter; | |
| 27 | +import org.eclipse.edc.spi.result.Result; | |
| 26 | 28 | import org.eclipse.edc.token.spi.TokenDecorator; |
| 27 | 29 | import org.junit.jupiter.api.BeforeEach; |
| 28 | 30 | import org.junit.jupiter.api.Test; |
| @@ -39,26 +41,31 @@ | ||
| 39 | 41 | |
| 40 | 42 | class JwtGenerationServiceTest { |
| 41 | 43 | |
| 44 | + public static final String TEST_KEY_ID = "test-key-id"; | |
| 42 | 45 | private RSAKey keys; |
| 43 | 46 | private JwtGenerationService tokenGenerationService; |
| 44 | 47 | |
| 45 | 48 | @BeforeEach |
| 46 | 49 | void setUp() throws JOSEException { |
| 47 | 50 | keys = testKey(); |
| 48 | - tokenGenerationService = new JwtGenerationService(); | |
| 51 | + tokenGenerationService = new JwtGenerationService(keyId -> { | |
| 52 | + if (TEST_KEY_ID.equals(keyId)) { | |
| 53 | + try { | |
| 54 | + var pk = keys.toPrivateKey(); | |
| 55 | + return Result.success(CryptoConverter.createSignerFor(pk)); | |
| 56 | + } catch (JOSEException e) { | |
| 57 | + return Result.failure(e.getMessage()); | |
| 58 | + } | |
| 59 | + } | |
| 60 | + return Result.failure("key not found"); | |
| 61 | + }); | |
| 49 | 62 | } |
| 50 | 63 | |
| 51 | 64 | @Test |
| 52 | 65 | void verifyTokenGeneration() throws ParseException, JOSEException { |
| 53 | 66 | var decorator = testDecorator(); |
| 54 | 67 | |
| 55 | - var result = tokenGenerationService.generate(() -> { | |
| 56 | - try { | |
| 57 | - return keys.toPrivateKey(); | |
| 58 | - } catch (JOSEException e) { | |
| 59 | - throw new RuntimeException(e); | |
| 60 | - } | |
| 61 | - }, decorator); | |
| 68 | + var result = tokenGenerationService.generate(TEST_KEY_ID, decorator); | |
| 62 | 69 | |
| 63 | 70 | assertThat(result.succeeded()).isTrue(); |
| 64 | 71 | var token = result.getContent().getToken(); |
| @@ -84,7 +91,7 @@ | ||
| 84 | 91 | void shouldFail_whenPrivateKeyCannotBeResolved() { |
| 85 | 92 | var decorator = testDecorator(); |
| 86 | 93 | |
| 87 | - var result = tokenGenerationService.generate(() -> null, decorator); | |
| 94 | + var result = tokenGenerationService.generate("not-exist-key", decorator); | |
| 88 | 95 | |
| 89 | 96 | assertThat(result.failed()).isTrue(); |
| 90 | 97 | } |
modified core/data-plane/data-plane-core/src/main/java/org/eclipse/edc/connector/dataplane/framework/DataPlaneDefaultIamServicesExtension.java +5 −13
| @@ -18,21 +18,19 @@ | ||
| 18 | 18 | import org.eclipse.edc.connector.dataplane.spi.iam.DataPlaneAccessControlService; |
| 19 | 19 | import org.eclipse.edc.connector.dataplane.spi.iam.DataPlaneAccessTokenService; |
| 20 | 20 | import org.eclipse.edc.connector.dataplane.spi.store.AccessTokenDataStore; |
| 21 | +import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider; | |
| 21 | 22 | import org.eclipse.edc.keys.spi.LocalPublicKeyService; |
| 22 | 23 | import org.eclipse.edc.keys.spi.PrivateKeyResolver; |
| 23 | 24 | import org.eclipse.edc.runtime.metamodel.annotation.Extension; |
| 24 | 25 | import org.eclipse.edc.runtime.metamodel.annotation.Inject; |
| 25 | 26 | import org.eclipse.edc.runtime.metamodel.annotation.Provider; |
| 26 | 27 | import org.eclipse.edc.runtime.metamodel.annotation.Setting; |
| 27 | -import org.eclipse.edc.spi.monitor.Monitor; | |
| 28 | 28 | import org.eclipse.edc.spi.result.Result; |
| 29 | 29 | import org.eclipse.edc.spi.system.ServiceExtension; |
| 30 | 30 | import org.eclipse.edc.spi.system.ServiceExtensionContext; |
| 31 | 31 | import org.eclipse.edc.token.JwtGenerationService; |
| 32 | 32 | import org.eclipse.edc.token.spi.TokenValidationService; |
| 33 | -import org.jetbrains.annotations.NotNull; | |
| 34 | 33 | |
| 35 | -import java.security.PrivateKey; | |
| 36 | 34 | import java.util.function.Supplier; |
| 37 | 35 | |
| 38 | 36 | |
| @@ -54,6 +52,8 @@ | ||
| 54 | 52 | private PrivateKeyResolver privateKeyResolver; |
| 55 | 53 | @Inject |
| 56 | 54 | private LocalPublicKeyService localPublicKeyService; |
| 55 | + @Inject | |
| 56 | + private JwsSignerProvider jwsSignerProvider; | |
| 57 | 57 | |
| 58 | 58 | @Override |
| 59 | 59 | public String name() { |
| @@ -71,8 +71,8 @@ | ||
| 71 | 71 | var tokenVerifierPublicKeyAlias = context.getConfig().getString(TOKEN_VERIFIER_PUBLIC_KEY_ALIAS); |
| 72 | 72 | var tokenSignerPrivateKeyAlias = context.getConfig().getString(TOKEN_SIGNER_PRIVATE_KEY_ALIAS); |
| 73 | 73 | var monitor = context.getMonitor().withPrefix("DataPlane IAM"); |
| 74 | - return new DefaultDataPlaneAccessTokenServiceImpl(new JwtGenerationService(), | |
| 75 | - accessTokenDataStore, monitor, getPrivateKeySupplier(tokenSignerPrivateKeyAlias, monitor), | |
| 74 | + return new DefaultDataPlaneAccessTokenServiceImpl(new JwtGenerationService(jwsSignerProvider), | |
| 75 | + accessTokenDataStore, monitor, () -> tokenSignerPrivateKeyAlias, | |
| 76 | 76 | publicKeyIdSupplier(tokenVerifierPublicKeyAlias), tokenValidationService, localPublicKeyService); |
| 77 | 77 | } |
| 78 | 78 | |
| @@ -80,12 +80,4 @@ | ||
| 80 | 80 | return () -> tokenVerifierPublicKeyAlias; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - @NotNull | |
| 84 | - private Supplier<PrivateKey> getPrivateKeySupplier(String tokenSignerPrivateKeyAlias, Monitor monitor) { | |
| 85 | - return () -> privateKeyResolver.resolvePrivateKey(tokenSignerPrivateKeyAlias) | |
| 86 | - .orElse(f -> { | |
| 87 | - monitor.warning("Cannot resolve private key: " + f.getFailureDetail()); | |
| 88 | - return null; | |
| 89 | - }); | |
| 90 | - } | |
| 91 | 83 | } |
modified core/data-plane/data-plane-core/src/main/java/org/eclipse/edc/connector/dataplane/framework/iam/DefaultDataPlaneAccessTokenServiceImpl.java +5 −6
| @@ -32,7 +32,6 @@ | ||
| 32 | 32 | import org.eclipse.edc.token.spi.TokenValidationRule; |
| 33 | 33 | import org.eclipse.edc.token.spi.TokenValidationService; |
| 34 | 34 | |
| 35 | -import java.security.PrivateKey; | |
| 36 | 35 | import java.util.ArrayList; |
| 37 | 36 | import java.util.List; |
| 38 | 37 | import java.util.Map; |
| @@ -56,7 +55,7 @@ | ||
| 56 | 55 | private final TokenGenerationService tokenGenerationService; |
| 57 | 56 | private final AccessTokenDataStore accessTokenDataStore; |
| 58 | 57 | private final Monitor monitor; |
| 59 | - private final Supplier<PrivateKey> privateKeySupplier; | |
| 58 | + private final Supplier<String> privateKeyIdSupplier; | |
| 60 | 59 | private final Supplier<String> publicKeyIdSupplier; |
| 61 | 60 | private final TokenValidationService tokenValidationService; |
| 62 | 61 | private final PublicKeyResolver publicKeyResolver; |
| @@ -64,14 +63,14 @@ | ||
| 64 | 63 | public DefaultDataPlaneAccessTokenServiceImpl(TokenGenerationService tokenGenerationService, |
| 65 | 64 | AccessTokenDataStore accessTokenDataStore, |
| 66 | 65 | Monitor monitor, |
| 67 | - Supplier<PrivateKey> privateKeySupplier, | |
| 66 | + Supplier<String> privateKeyIdSupplier, | |
| 68 | 67 | Supplier<String> publicKeyIdSupplier, |
| 69 | 68 | TokenValidationService tokenValidationService, |
| 70 | 69 | PublicKeyResolver publicKeyResolver) { |
| 71 | 70 | this.tokenGenerationService = tokenGenerationService; |
| 72 | 71 | this.accessTokenDataStore = accessTokenDataStore; |
| 73 | 72 | this.monitor = monitor; |
| 74 | - this.privateKeySupplier = privateKeySupplier; | |
| 73 | + this.privateKeyIdSupplier = privateKeyIdSupplier; | |
| 75 | 74 | this.publicKeyIdSupplier = publicKeyIdSupplier; |
| 76 | 75 | this.tokenValidationService = tokenValidationService; |
| 77 | 76 | this.publicKeyResolver = publicKeyResolver; |
| @@ -106,9 +105,9 @@ | ||
| 106 | 105 | allDecorators.add(tokenIdDecorator); |
| 107 | 106 | } |
| 108 | 107 | |
| 109 | - var tokenResult = tokenGenerationService.generate(privateKeySupplier, allDecorators.toArray(new TokenDecorator[0])); | |
| 108 | + var tokenResult = tokenGenerationService.generate(privateKeyIdSupplier.get(), allDecorators.toArray(new TokenDecorator[0])); | |
| 110 | 109 | if (tokenResult.failed()) { |
| 111 | - return tokenResult.mapTo(); | |
| 110 | + return tokenResult.mapEmpty(); | |
| 112 | 111 | } |
| 113 | 112 | |
| 114 | 113 | // store a record of the token for future reference. We'll need that when we resolve the AccessTokenData later. |
modified extensions/common/iam/identity-trust/identity-trust-core/src/main/java/org/eclipse/edc/iam/identitytrust/core/DcpDefaultServicesExtension.java +4 −8
| @@ -25,12 +25,11 @@ | ||
| 25 | 25 | import org.eclipse.edc.iam.identitytrust.spi.verification.SignatureSuiteRegistry; |
| 26 | 26 | import org.eclipse.edc.iam.identitytrust.sts.embedded.EmbeddedSecureTokenService; |
| 27 | 27 | import org.eclipse.edc.iam.verifiablecredentials.spi.validation.TrustedIssuerRegistry; |
| 28 | -import org.eclipse.edc.keys.spi.PrivateKeyResolver; | |
| 28 | +import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider; | |
| 29 | 29 | import org.eclipse.edc.runtime.metamodel.annotation.Extension; |
| 30 | 30 | import org.eclipse.edc.runtime.metamodel.annotation.Inject; |
| 31 | 31 | import org.eclipse.edc.runtime.metamodel.annotation.Provider; |
| 32 | 32 | import org.eclipse.edc.runtime.metamodel.annotation.Setting; |
| 33 | -import org.eclipse.edc.spi.EdcException; | |
| 34 | 33 | import org.eclipse.edc.spi.iam.AudienceResolver; |
| 35 | 34 | import org.eclipse.edc.spi.iam.ClaimToken; |
| 36 | 35 | import org.eclipse.edc.spi.result.Result; |
| @@ -38,11 +37,9 @@ | ||
| 38 | 37 | import org.eclipse.edc.spi.system.ServiceExtensionContext; |
| 39 | 38 | import org.eclipse.edc.token.JwtGenerationService; |
| 40 | 39 | |
| 41 | -import java.security.PrivateKey; | |
| 42 | 40 | import java.time.Clock; |
| 43 | 41 | import java.util.Map; |
| 44 | 42 | import java.util.concurrent.TimeUnit; |
| 45 | -import java.util.function.Supplier; | |
| 46 | 43 | |
| 47 | 44 | import static org.eclipse.edc.spi.result.Result.failure; |
| 48 | 45 | import static org.eclipse.edc.spi.result.Result.success; |
| @@ -63,7 +60,7 @@ | ||
| 63 | 60 | @Inject |
| 64 | 61 | private Clock clock; |
| 65 | 62 | @Inject |
| 66 | - private PrivateKeyResolver privateKeyResolver; | |
| 63 | + private JwsSignerProvider externalSigner; | |
| 67 | 64 | |
| 68 | 65 | @Provider(isDefault = true) |
| 69 | 66 | public SecureTokenService createDefaultTokenService(ServiceExtensionContext context) { |
| @@ -78,10 +75,9 @@ | ||
| 78 | 75 | |
| 79 | 76 | |
| 80 | 77 | var publicKeyId = context.getSetting(STS_PUBLIC_KEY_ID, null); |
| 81 | - var privKeyAlias = context.getSetting(STS_PRIVATE_KEY_ALIAS, null); | |
| 78 | + var privateKeyAlias = context.getSetting(STS_PRIVATE_KEY_ALIAS, null); | |
| 82 | 79 | |
| 83 | - Supplier<PrivateKey> supplier = () -> privateKeyResolver.resolvePrivateKey(privKeyAlias).orElseThrow(f -> new EdcException("This EDC instance is not operational due to the following error: %s".formatted(f.getFailureDetail()))); | |
| 84 | - return new EmbeddedSecureTokenService(new JwtGenerationService(), supplier, () -> publicKeyId, clock, TimeUnit.MINUTES.toSeconds(tokenExpiration)); | |
| 80 | + return new EmbeddedSecureTokenService(new JwtGenerationService(externalSigner), () -> privateKeyAlias, () -> publicKeyId, clock, TimeUnit.MINUTES.toSeconds(tokenExpiration)); | |
| 85 | 81 | } |
| 86 | 82 | |
| 87 | 83 | @Provider(isDefault = true) |
modified extensions/common/iam/identity-trust/identity-trust-sts/identity-trust-sts-core/build.gradle.kts +1 −0
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | |
| 15 | 15 | testImplementation(testFixtures(project(":spi:common:identity-trust-sts-spi"))) |
| 16 | 16 | testImplementation(project(":core:common:lib:boot-lib")) |
| 17 | + testImplementation(project(":core:common:lib:crypto-common-lib")) | |
| 17 | 18 | testImplementation(project(":core:common:lib:keys-lib")) |
| 18 | 19 | testImplementation(project(":core:common:junit")) |
| 19 | 20 | testImplementation(libs.nimbus.jwt) |
modified extensions/common/iam/identity-trust/identity-trust-sts/identity-trust-sts-core/src/main/java/org/eclipse/edc/iam/identitytrust/sts/defaults/StsDefaultServicesExtension.java +5 −4
| @@ -16,10 +16,11 @@ | ||
| 16 | 16 | |
| 17 | 17 | import org.eclipse.edc.iam.identitytrust.sts.defaults.service.StsClientServiceImpl; |
| 18 | 18 | import org.eclipse.edc.iam.identitytrust.sts.defaults.service.StsClientTokenGeneratorServiceImpl; |
| 19 | +import org.eclipse.edc.iam.identitytrust.sts.spi.model.StsClient; | |
| 19 | 20 | import org.eclipse.edc.iam.identitytrust.sts.spi.service.StsClientService; |
| 20 | 21 | import org.eclipse.edc.iam.identitytrust.sts.spi.service.StsClientTokenGeneratorService; |
| 21 | 22 | import org.eclipse.edc.iam.identitytrust.sts.spi.store.StsClientStore; |
| 22 | -import org.eclipse.edc.keys.spi.PrivateKeyResolver; | |
| 23 | +import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider; | |
| 23 | 24 | import org.eclipse.edc.runtime.metamodel.annotation.Extension; |
| 24 | 25 | import org.eclipse.edc.runtime.metamodel.annotation.Inject; |
| 25 | 26 | import org.eclipse.edc.runtime.metamodel.annotation.Provider; |
| @@ -53,7 +54,7 @@ | ||
| 53 | 54 | private Vault vault; |
| 54 | 55 | |
| 55 | 56 | @Inject |
| 56 | - private PrivateKeyResolver privateKeyResolver; | |
| 57 | + private JwsSignerProvider jwsSignerProvider; | |
| 57 | 58 | |
| 58 | 59 | @Inject |
| 59 | 60 | private Clock clock; |
| @@ -67,8 +68,8 @@ | ||
| 67 | 68 | public StsClientTokenGeneratorService clientTokenService(ServiceExtensionContext context) { |
| 68 | 69 | var tokenExpiration = context.getSetting(STS_TOKEN_EXPIRATION, DEFAULT_STS_TOKEN_EXPIRATION_MIN); |
| 69 | 70 | return new StsClientTokenGeneratorServiceImpl( |
| 70 | - (client) -> new JwtGenerationService(), | |
| 71 | - (client) -> privateKeyResolver.resolvePrivateKey(client.getPrivateKeyAlias()).orElse(null), | |
| 71 | + (client) -> new JwtGenerationService(jwsSignerProvider), | |
| 72 | + StsClient::getPrivateKeyAlias, | |
| 72 | 73 | clock, |
| 73 | 74 | TimeUnit.MINUTES.toSeconds(tokenExpiration)); |
| 74 | 75 | } |
modified extensions/common/iam/identity-trust/identity-trust-sts/identity-trust-sts-core/src/main/java/org/eclipse/edc/iam/identitytrust/sts/defaults/service/StsClientTokenGeneratorServiceImpl.java +2 −3
| @@ -22,7 +22,6 @@ | ||
| 22 | 22 | import org.eclipse.edc.spi.iam.TokenRepresentation; |
| 23 | 23 | import org.eclipse.edc.spi.result.ServiceResult; |
| 24 | 24 | |
| 25 | -import java.security.PrivateKey; | |
| 26 | 25 | import java.time.Clock; |
| 27 | 26 | import java.util.Collections; |
| 28 | 27 | import java.util.HashMap; |
| @@ -42,10 +41,10 @@ | ||
| 42 | 41 | |
| 43 | 42 | private final long tokenExpiration; |
| 44 | 43 | private final StsTokenGenerationProvider tokenGenerationProvider; |
| 45 | - private final Function<StsClient, PrivateKey> keyFunction; | |
| 44 | + private final Function<StsClient, String> keyFunction; | |
| 46 | 45 | private final Clock clock; |
| 47 | 46 | |
| 48 | - public StsClientTokenGeneratorServiceImpl(StsTokenGenerationProvider tokenGenerationProvider, Function<StsClient, PrivateKey> keyFunction, Clock clock, long tokenExpiration) { | |
| 47 | + public StsClientTokenGeneratorServiceImpl(StsTokenGenerationProvider tokenGenerationProvider, Function<StsClient, String> keyFunction, Clock clock, long tokenExpiration) { | |
| 49 | 48 | this.tokenGenerationProvider = tokenGenerationProvider; |
| 50 | 49 | this.keyFunction = keyFunction; |
| 51 | 50 | this.clock = clock; |
modified extensions/common/iam/identity-trust/identity-trust-sts/identity-trust-sts-core/src/test/java/org/eclipse/edc/iam/identitytrust/sts/defaults/StsClientTokenIssuanceIntegrationTest.java +6 −2
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import org.eclipse.edc.iam.identitytrust.sts.defaults.service.StsClientServiceImpl; |
| 21 | 21 | import org.eclipse.edc.iam.identitytrust.sts.defaults.service.StsClientTokenGeneratorServiceImpl; |
| 22 | 22 | import org.eclipse.edc.iam.identitytrust.sts.defaults.store.InMemoryStsClientStore; |
| 23 | +import org.eclipse.edc.iam.identitytrust.sts.spi.model.StsClient; | |
| 23 | 24 | import org.eclipse.edc.iam.identitytrust.sts.spi.model.StsClientTokenAdditionalParams; |
| 24 | 25 | import org.eclipse.edc.junit.annotations.ComponentTest; |
| 25 | 26 | import org.eclipse.edc.keys.KeyParserRegistryImpl; |
| @@ -28,6 +29,8 @@ | ||
| 28 | 29 | import org.eclipse.edc.keys.keyparsers.PemParser; |
| 29 | 30 | import org.eclipse.edc.keys.spi.KeyParserRegistry; |
| 30 | 31 | import org.eclipse.edc.keys.spi.PrivateKeyResolver; |
| 32 | +import org.eclipse.edc.security.token.jwt.CryptoConverter; | |
| 33 | +import org.eclipse.edc.spi.result.Result; | |
| 31 | 34 | import org.eclipse.edc.spi.security.Vault; |
| 32 | 35 | import org.eclipse.edc.token.JwtGenerationService; |
| 33 | 36 | import org.eclipse.edc.transaction.spi.NoopTransactionContext; |
| @@ -70,8 +73,9 @@ | ||
| 70 | 73 | privateKeyResolver = new VaultPrivateKeyResolver(keyParserRegistry, vault, mock(), mock()); |
| 71 | 74 | |
| 72 | 75 | tokenGeneratorService = new StsClientTokenGeneratorServiceImpl( |
| 73 | - client -> new JwtGenerationService(), | |
| 74 | - stsClient -> privateKeyResolver.resolvePrivateKey(stsClient.getPrivateKeyAlias()).orElse(null), | |
| 76 | + client -> new JwtGenerationService(keyId -> privateKeyResolver.resolvePrivateKey(keyId) | |
| 77 | + .compose(pk -> Result.ofThrowable(() -> CryptoConverter.createSignerFor(pk)))), | |
| 78 | + StsClient::getPrivateKeyAlias, | |
| 75 | 79 | Clock.systemUTC(), 60 * 5); |
| 76 | 80 | |
| 77 | 81 | } |
modified extensions/common/iam/identity-trust/identity-trust-sts/identity-trust-sts-core/src/test/java/org/eclipse/edc/iam/identitytrust/sts/defaults/service/StsClientTokenGeneratorServiceImplTest.java +1 −1
| @@ -43,7 +43,7 @@ | ||
| 43 | 43 | |
| 44 | 44 | @BeforeEach |
| 45 | 45 | void setup() { |
| 46 | - clientTokenService = new StsClientTokenGeneratorServiceImpl(tokenGenerationProvider, (client) -> mock(), Clock.systemUTC(), TOKEN_EXPIRATION); | |
| 46 | + clientTokenService = new StsClientTokenGeneratorServiceImpl(tokenGenerationProvider, (client) -> "test-key-id", Clock.systemUTC(), TOKEN_EXPIRATION); | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | @Test |
modified extensions/common/iam/identity-trust/identity-trust-sts/identity-trust-sts-embedded/src/main/java/org/eclipse/edc/iam/identitytrust/sts/embedded/EmbeddedSecureTokenService.java +9 −10
| @@ -21,7 +21,6 @@ | ||
| 21 | 21 | import org.eclipse.edc.token.spi.TokenGenerationService; |
| 22 | 22 | import org.jetbrains.annotations.Nullable; |
| 23 | 23 | |
| 24 | -import java.security.PrivateKey; | |
| 25 | 24 | import java.time.Clock; |
| 26 | 25 | import java.util.HashMap; |
| 27 | 26 | import java.util.List; |
| @@ -50,15 +49,15 @@ | ||
| 50 | 49 | |
| 51 | 50 | private static final List<String> ACCESS_TOKEN_INHERITED_CLAIMS = List.of(ISSUER); |
| 52 | 51 | private final TokenGenerationService tokenGenerationService; |
| 53 | - private final Supplier<PrivateKey> privateKeySupplier; | |
| 54 | - private final Supplier<String> publicKeyId; | |
| 52 | + private final Supplier<String> privateKeyIdSupplier; | |
| 53 | + private final Supplier<String> publicKeyIdSupplier; | |
| 55 | 54 | private final Clock clock; |
| 56 | 55 | private final long validity; |
| 57 | 56 | |
| 58 | - public EmbeddedSecureTokenService(TokenGenerationService tokenGenerationService, Supplier<PrivateKey> privateKeySupplier, Supplier<String> publicKeyId, Clock clock, long validity) { | |
| 57 | + public EmbeddedSecureTokenService(TokenGenerationService tokenGenerationService, Supplier<String> privateKeyIdSupplier, Supplier<String> publicKeyIdSupplier, Clock clock, long validity) { | |
| 59 | 58 | this.tokenGenerationService = tokenGenerationService; |
| 60 | - this.privateKeySupplier = privateKeySupplier; | |
| 61 | - this.publicKeyId = publicKeyId; | |
| 59 | + this.privateKeyIdSupplier = privateKeyIdSupplier; | |
| 60 | + this.publicKeyIdSupplier = publicKeyIdSupplier; | |
| 62 | 61 | this.clock = clock; |
| 63 | 62 | this.validity = validity; |
| 64 | 63 | } |
| @@ -70,8 +69,8 @@ | ||
| 70 | 69 | .map(scope -> createAndAcceptAccessToken(claims, scope, selfIssuedClaims::put)) |
| 71 | 70 | .orElse(success()) |
| 72 | 71 | .compose(v -> { |
| 73 | - var keyIdDecorator = new KeyIdDecorator(publicKeyId.get()); | |
| 74 | - return tokenGenerationService.generate(privateKeySupplier, keyIdDecorator, new SelfIssuedTokenDecorator(selfIssuedClaims, clock, validity)); | |
| 72 | + var keyIdDecorator = new KeyIdDecorator(publicKeyIdSupplier.get()); | |
| 73 | + return tokenGenerationService.generate(privateKeyIdSupplier.get(), keyIdDecorator, new SelfIssuedTokenDecorator(selfIssuedClaims, clock, validity)); | |
| 75 | 74 | }); |
| 76 | 75 | } |
| 77 | 76 | |
| @@ -88,8 +87,8 @@ | ||
| 88 | 87 | return addClaim(claims, ISSUER, withClaim(AUDIENCE, accessTokenClaims::put)) |
| 89 | 88 | .compose(v -> addClaim(claims, AUDIENCE, withClaim(SUBJECT, accessTokenClaims::put))) |
| 90 | 89 | .compose(v -> { |
| 91 | - var keyIdDecorator = new KeyIdDecorator(publicKeyId.get()); | |
| 92 | - return tokenGenerationService.generate(privateKeySupplier, keyIdDecorator, new SelfIssuedTokenDecorator(accessTokenClaims, clock, validity)); | |
| 90 | + var keyIdDecorator = new KeyIdDecorator(publicKeyIdSupplier.get()); | |
| 91 | + return tokenGenerationService.generate(privateKeyIdSupplier.get(), keyIdDecorator, new SelfIssuedTokenDecorator(accessTokenClaims, clock, validity)); | |
| 93 | 92 | }); |
| 94 | 93 | |
| 95 | 94 | } |
modified extensions/common/iam/identity-trust/identity-trust-sts/identity-trust-sts-embedded/src/test/java/org/eclipse/edc/iam/identitytrust/sts/embedded/EmbeddedSecureTokenServiceIntegrationTest.java +4 −2
| @@ -17,8 +17,10 @@ | ||
| 17 | 17 | import com.nimbusds.jose.JOSEException; |
| 18 | 18 | import com.nimbusds.jose.JWSHeader; |
| 19 | 19 | import com.nimbusds.jose.JWSVerifier; |
| 20 | +import com.nimbusds.jose.crypto.RSASSASigner; | |
| 20 | 21 | import com.nimbusds.jose.crypto.factories.DefaultJWSVerifierFactory; |
| 21 | 22 | import com.nimbusds.jwt.SignedJWT; |
| 23 | +import org.eclipse.edc.spi.result.Result; | |
| 22 | 24 | import org.eclipse.edc.token.JwtGenerationService; |
| 23 | 25 | import org.junit.jupiter.api.BeforeEach; |
| 24 | 26 | import org.junit.jupiter.api.Test; |
| @@ -63,8 +65,8 @@ | ||
| 63 | 65 | @BeforeEach |
| 64 | 66 | void setup() throws NoSuchAlgorithmException { |
| 65 | 67 | keyPair = generateKeyPair(); |
| 66 | - var tokenGenerationService = new JwtGenerationService(); | |
| 67 | - secureTokenService = new EmbeddedSecureTokenService(tokenGenerationService, () -> keyPair.getPrivate(), () -> "test-keyid", Clock.systemUTC(), 10 * 60); | |
| 68 | + var tokenGenerationService = new JwtGenerationService(s -> Result.success(new RSASSASigner(keyPair.getPrivate()))); | |
| 69 | + secureTokenService = new EmbeddedSecureTokenService(tokenGenerationService, () -> "test-private-keyid", () -> "test-keyid", Clock.systemUTC(), 10 * 60); | |
| 68 | 70 | } |
| 69 | 71 | |
| 70 | 72 | @Test |
modified extensions/common/iam/identity-trust/identity-trust-sts/identity-trust-sts-embedded/src/test/java/org/eclipse/edc/iam/identitytrust/sts/embedded/EmbeddedSecureTokenServiceTest.java +7 −8
| @@ -21,9 +21,7 @@ | ||
| 21 | 21 | import org.eclipse.edc.token.spi.TokenGenerationService; |
| 22 | 22 | import org.junit.jupiter.api.Test; |
| 23 | 23 | import org.mockito.ArgumentCaptor; |
| 24 | -import org.mockito.Mockito; | |
| 25 | 24 | |
| 26 | -import java.security.PrivateKey; | |
| 27 | 25 | import java.time.Clock; |
| 28 | 26 | import java.util.Map; |
| 29 | 27 | import java.util.function.Supplier; |
| @@ -40,15 +38,16 @@ | ||
| 40 | 38 | |
| 41 | 39 | public class EmbeddedSecureTokenServiceTest { |
| 42 | 40 | |
| 41 | + public static final String TEST_PRIVATEKEY_ID = "test-privatekey-id"; | |
| 43 | 42 | private final TokenGenerationService tokenGenerationService = mock(); |
| 44 | - private final Supplier<PrivateKey> keySupplier = Mockito::mock; | |
| 43 | + private final Supplier<String> keySupplier = () -> TEST_PRIVATEKEY_ID; | |
| 45 | 44 | |
| 46 | 45 | @Test |
| 47 | 46 | void createToken_withoutBearerAccessScope() { |
| 48 | 47 | var sts = new EmbeddedSecureTokenService(tokenGenerationService, keySupplier, () -> "test-key", Clock.systemUTC(), 10 * 60); |
| 49 | 48 | var token = TokenRepresentation.Builder.newInstance().token("test").build(); |
| 50 | 49 | |
| 51 | - when(tokenGenerationService.generate(eq(keySupplier), any(TokenDecorator[].class))).thenReturn(Result.success(token)); | |
| 50 | + when(tokenGenerationService.generate(eq(TEST_PRIVATEKEY_ID), any(TokenDecorator[].class))).thenReturn(Result.success(token)); | |
| 52 | 51 | var result = sts.createToken(Map.of(), null); |
| 53 | 52 | |
| 54 | 53 | assertThat(result.succeeded()).isTrue(); |
| @@ -71,7 +70,7 @@ | ||
| 71 | 70 | var sts = new EmbeddedSecureTokenService(tokenGenerationService, keySupplier, () -> "test-key", Clock.systemUTC(), 10 * 60); |
| 72 | 71 | var token = TokenRepresentation.Builder.newInstance().token("test").build(); |
| 73 | 72 | |
| 74 | - when(tokenGenerationService.generate(eq(keySupplier), any(TokenDecorator[].class))) | |
| 73 | + when(tokenGenerationService.generate(eq(TEST_PRIVATEKEY_ID), any(TokenDecorator[].class))) | |
| 75 | 74 | .thenReturn(Result.success(token)) |
| 76 | 75 | .thenReturn(Result.success(token)); |
| 77 | 76 | |
| @@ -104,7 +103,7 @@ | ||
| 104 | 103 | var sts = new EmbeddedSecureTokenService(tokenGenerationService, keySupplier, () -> "test-key", Clock.systemUTC(), 10 * 60); |
| 105 | 104 | var token = TokenRepresentation.Builder.newInstance().token("test").build(); |
| 106 | 105 | |
| 107 | - when(tokenGenerationService.generate(eq(keySupplier), any(TokenDecorator[].class))) | |
| 106 | + when(tokenGenerationService.generate(eq(TEST_PRIVATEKEY_ID), any(TokenDecorator[].class))) | |
| 108 | 107 | .thenReturn(Result.failure("Failed to create access token")) |
| 109 | 108 | .thenReturn(Result.success(token)); |
| 110 | 109 | |
| @@ -128,7 +127,7 @@ | ||
| 128 | 127 | var sts = new EmbeddedSecureTokenService(tokenGenerationService, keySupplier, () -> "test-key", Clock.systemUTC(), 10 * 60); |
| 129 | 128 | var token = TokenRepresentation.Builder.newInstance().token("test").build(); |
| 130 | 129 | |
| 131 | - when(tokenGenerationService.generate(eq(keySupplier), any(TokenDecorator[].class))) | |
| 130 | + when(tokenGenerationService.generate(eq(TEST_PRIVATEKEY_ID), any(TokenDecorator[].class))) | |
| 132 | 131 | .thenReturn(Result.success(token)) |
| 133 | 132 | .thenReturn(Result.failure("Failed to create access token")); |
| 134 | 133 | |
| @@ -137,7 +136,7 @@ | ||
| 137 | 136 | |
| 138 | 137 | assertThat(result.failed()).isTrue(); |
| 139 | 138 | |
| 140 | - verify(tokenGenerationService, times(2)).generate(eq(keySupplier), any(TokenDecorator[].class)); | |
| 139 | + verify(tokenGenerationService, times(2)).generate(eq(TEST_PRIVATEKEY_ID), any(TokenDecorator[].class)); | |
| 141 | 140 | |
| 142 | 141 | } |
| 143 | 142 |
modified extensions/common/iam/oauth2/oauth2-core/src/main/java/org/eclipse/edc/iam/oauth2/Oauth2ServiceExtension.java +5 −4
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | import org.eclipse.edc.iam.oauth2.jwt.X509CertificateDecorator; |
| 23 | 23 | import org.eclipse.edc.iam.oauth2.spi.Oauth2AssertionDecorator; |
| 24 | 24 | import org.eclipse.edc.iam.oauth2.spi.client.Oauth2Client; |
| 25 | +import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider; | |
| 25 | 26 | import org.eclipse.edc.keys.spi.CertificateResolver; |
| 26 | 27 | import org.eclipse.edc.keys.spi.PrivateKeyResolver; |
| 27 | 28 | import org.eclipse.edc.runtime.metamodel.annotation.Extension; |
| @@ -42,7 +43,6 @@ | ||
| 42 | 43 | import org.eclipse.edc.token.spi.TokenValidationService; |
| 43 | 44 | import org.jetbrains.annotations.NotNull; |
| 44 | 45 | |
| 45 | -import java.security.PrivateKey; | |
| 46 | 46 | import java.time.Clock; |
| 47 | 47 | import java.util.Optional; |
| 48 | 48 | import java.util.concurrent.TimeUnit; |
| @@ -110,6 +110,8 @@ | ||
| 110 | 110 | private TokenValidationService tokenValidationService; |
| 111 | 111 | @Inject |
| 112 | 112 | private TokenDecoratorRegistry jwtDecoratorRegistry; |
| 113 | + @Inject | |
| 114 | + private JwsSignerProvider jwsSignerProvider; | |
| 113 | 115 | |
| 114 | 116 | @Override |
| 115 | 117 | public String name() { |
| @@ -157,12 +159,11 @@ | ||
| 157 | 159 | private Oauth2ServiceImpl createOauth2Service(Oauth2ServiceConfiguration configuration, |
| 158 | 160 | TokenDecoratorRegistry jwtDecoratorRegistry, |
| 159 | 161 | IdentityProviderKeyResolver providerKeyResolver) { |
| 160 | - Supplier<PrivateKey> privateKeySupplier = () -> privateKeyResolver.resolvePrivateKey(configuration.getPrivateKeyAlias()) | |
| 161 | - .orElseThrow(f -> new EdcException(f.getFailureDetail())); | |
| 162 | + Supplier<String> privateKeySupplier = configuration::getPrivateKeyAlias; | |
| 162 | 163 | |
| 163 | 164 | return new Oauth2ServiceImpl( |
| 164 | 165 | configuration, |
| 165 | - new JwtGenerationService(), | |
| 166 | + new JwtGenerationService(jwsSignerProvider), | |
| 166 | 167 | privateKeySupplier, |
| 167 | 168 | oauth2Client, |
| 168 | 169 | jwtDecoratorRegistry, |
modified extensions/common/iam/oauth2/oauth2-core/src/main/java/org/eclipse/edc/iam/oauth2/identity/Oauth2ServiceImpl.java +4 −5
| @@ -36,7 +36,6 @@ | ||
| 36 | 36 | import org.eclipse.edc.token.spi.TokenValidationService; |
| 37 | 37 | import org.jetbrains.annotations.NotNull; |
| 38 | 38 | |
| 39 | -import java.security.PrivateKey; | |
| 40 | 39 | import java.util.function.Supplier; |
| 41 | 40 | |
| 42 | 41 | import static org.eclipse.edc.iam.oauth2.Oauth2ServiceExtension.OAUTH2_TOKEN_CONTEXT; |
| @@ -49,7 +48,7 @@ | ||
| 49 | 48 | private static final String GRANT_TYPE = "client_credentials"; |
| 50 | 49 | |
| 51 | 50 | private final Oauth2ServiceConfiguration configuration; |
| 52 | - private final Supplier<PrivateKey> privateKeySupplier; | |
| 51 | + private final Supplier<String> privateKeySupplier; | |
| 53 | 52 | private final Oauth2Client client; |
| 54 | 53 | private final TokenDecoratorRegistry jwtDecoratorRegistry; |
| 55 | 54 | private final TokenGenerationService tokenGenerationService; |
| @@ -66,11 +65,11 @@ | ||
| 66 | 65 | * @param jwtDecoratorRegistry Registry containing the decorator for build the JWT |
| 67 | 66 | * @param tokenValidationService Service used for token validation |
| 68 | 67 | */ |
| 69 | - public Oauth2ServiceImpl(Oauth2ServiceConfiguration configuration, TokenGenerationService tokenGenerationService, Supplier<PrivateKey> privateKeySupplier, | |
| 68 | + public Oauth2ServiceImpl(Oauth2ServiceConfiguration configuration, TokenGenerationService tokenGenerationService, Supplier<String> privateKeyIdSupplier, | |
| 70 | 69 | Oauth2Client client, TokenDecoratorRegistry jwtDecoratorRegistry, TokenValidationRulesRegistry tokenValidationRuleRegistry, TokenValidationService tokenValidationService, |
| 71 | 70 | PublicKeyResolver publicKeyResolver) { |
| 72 | 71 | this.configuration = configuration; |
| 73 | - this.privateKeySupplier = privateKeySupplier; | |
| 72 | + this.privateKeySupplier = privateKeyIdSupplier; | |
| 74 | 73 | this.client = client; |
| 75 | 74 | this.jwtDecoratorRegistry = jwtDecoratorRegistry; |
| 76 | 75 | this.tokenValidationRuleRegistry = tokenValidationRuleRegistry; |
| @@ -94,7 +93,7 @@ | ||
| 94 | 93 | @NotNull |
| 95 | 94 | private Result<String> generateClientAssertion() { |
| 96 | 95 | var decorators = jwtDecoratorRegistry.getDecoratorsFor(OAUTH2_TOKEN_CONTEXT).toArray(TokenDecorator[]::new); |
| 97 | - return tokenGenerationService.generate(privateKeySupplier, decorators) | |
| 96 | + return tokenGenerationService.generate(privateKeySupplier.get(), decorators) | |
| 98 | 97 | .map(TokenRepresentation::getToken); |
| 99 | 98 | } |
| 100 | 99 |
modified extensions/common/iam/oauth2/oauth2-core/src/test/java/org/eclipse/edc/iam/oauth2/identity/Oauth2ServiceImplTest.java +2 −2
| @@ -75,6 +75,7 @@ | ||
| 75 | 75 | private static final String PROVIDER_AUDIENCE = "audience-test"; |
| 76 | 76 | private static final String ENDPOINT_AUDIENCE = "endpoint-audience-test"; |
| 77 | 77 | |
| 78 | + private static final String TEST_PRIVATE_KEY_ID = "test-private-key-id"; | |
| 78 | 79 | private static final VerificationContext VERIFICATION_CONTEXT = VerificationContext.Builder.newInstance() |
| 79 | 80 | .policy(Policy.Builder.newInstance().build()) |
| 80 | 81 | .build(); |
| @@ -90,7 +91,6 @@ | ||
| 90 | 91 | @BeforeEach |
| 91 | 92 | void setUp() throws JOSEException { |
| 92 | 93 | var testKey = testKey(); |
| 93 | - var privateKey = testKey.toPrivateKey(); | |
| 94 | 94 | |
| 95 | 95 | jwsSigner = new RSASSASigner(testKey.toPrivateKey()); |
| 96 | 96 | var publicKeyResolverMock = mock(PublicKeyResolver.class); |
| @@ -114,7 +114,7 @@ | ||
| 114 | 114 | registry.addRule(OAUTH2_TOKEN_CONTEXT, new NotBeforeValidationRule(Clock.systemUTC(), configuration.getNotBeforeValidationLeeway())); |
| 115 | 115 | registry.addRule(OAUTH2_TOKEN_CONTEXT, new ExpirationIssuedAtValidationRule(Clock.systemUTC(), configuration.getIssuedAtLeeway())); |
| 116 | 116 | |
| 117 | - authService = new Oauth2ServiceImpl(configuration, tokenGenerationService, () -> privateKey, client, jwtDecoratorRegistry, registry, | |
| 117 | + authService = new Oauth2ServiceImpl(configuration, tokenGenerationService, () -> TEST_PRIVATE_KEY_ID, client, jwtDecoratorRegistry, registry, | |
| 118 | 118 | tokenValidationService, publicKeyResolverMock); |
| 119 | 119 | |
| 120 | 120 | } |
modified extensions/control-plane/transfer/transfer-data-plane/src/main/java/org/eclipse/edc/connector/controlplane/transfer/dataplane/TransferDataPlaneCoreExtension.java +4 −13
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | import org.eclipse.edc.connector.controlplane.transfer.spi.flow.DataFlowManager; |
| 26 | 26 | import org.eclipse.edc.connector.dataplane.selector.spi.DataPlaneSelectorService; |
| 27 | 27 | import org.eclipse.edc.connector.dataplane.selector.spi.client.DataPlaneClientFactory; |
| 28 | +import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider; | |
| 28 | 29 | import org.eclipse.edc.keys.spi.LocalPublicKeyService; |
| 29 | 30 | import org.eclipse.edc.keys.spi.PrivateKeyResolver; |
| 30 | 31 | import org.eclipse.edc.runtime.metamodel.annotation.Extension; |
| @@ -41,11 +42,8 @@ | ||
| 41 | 42 | import org.eclipse.edc.web.spi.WebService; |
| 42 | 43 | import org.eclipse.edc.web.spi.configuration.ApiContext; |
| 43 | 44 | import org.eclipse.edc.web.spi.configuration.context.ControlApiUrl; |
| 44 | -import org.jetbrains.annotations.NotNull; | |
| 45 | 45 | |
| 46 | -import java.security.PrivateKey; | |
| 47 | 46 | import java.time.Clock; |
| 48 | -import java.util.function.Supplier; | |
| 49 | 47 | |
| 50 | 48 | @Extension(value = TransferDataPlaneCoreExtension.NAME) |
| 51 | 49 | public class TransferDataPlaneCoreExtension implements ServiceExtension { |
| @@ -100,6 +98,8 @@ | ||
| 100 | 98 | |
| 101 | 99 | @Inject |
| 102 | 100 | private TokenValidationService tokenValidationService; |
| 101 | + @Inject | |
| 102 | + private JwsSignerProvider jwsSignerProvider; | |
| 103 | 103 | |
| 104 | 104 | @Override |
| 105 | 105 | public String name() { |
| @@ -117,7 +117,7 @@ | ||
| 117 | 117 | var controller = new ConsumerPullTransferTokenValidationApiController(tokenValidationService, dataEncrypter, typeManager, (i) -> publicKeyService.resolveKey(publicKeyAlias)); |
| 118 | 118 | webService.registerResource(ApiContext.CONTROL, controller); |
| 119 | 119 | |
| 120 | - var resolver = new ConsumerPullDataPlaneProxyResolver(dataEncrypter, typeManager, new JwtGenerationService(), getPrivateKeySupplier(context, privateKeyAlias), () -> publicKeyAlias, tokenExpirationDateFunction); | |
| 120 | + var resolver = new ConsumerPullDataPlaneProxyResolver(dataEncrypter, typeManager, new JwtGenerationService(jwsSignerProvider), () -> privateKeyAlias, () -> publicKeyAlias, tokenExpirationDateFunction); | |
| 121 | 121 | dataFlowManager.register(new ConsumerPullTransferDataFlowController(selectorService, resolver)); |
| 122 | 122 | } else { |
| 123 | 123 | monitor.info("One of these settings is not configured, so the connector won't be able to provide 'consumer-pull' transfers: [%s, %s]" |
| @@ -130,13 +130,4 @@ | ||
| 130 | 130 | dataAddressValidatorRegistry.registerDestinationValidator("HttpProxy", dataAddress -> ValidationResult.success()); |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - @NotNull | |
| 134 | - private Supplier<PrivateKey> getPrivateKeySupplier(ServiceExtensionContext context, String privateKeyAlias) { | |
| 135 | - return () -> privateKeyResolver.resolvePrivateKey(privateKeyAlias) | |
| 136 | - .orElse(f -> { | |
| 137 | - context.getMonitor().warning("Cannot resolve private key: " + f.getFailureDetail()); | |
| 138 | - return null; | |
| 139 | - }); | |
| 140 | - } | |
| 141 | - | |
| 142 | 133 | } |
modified extensions/control-plane/transfer/transfer-data-plane/src/main/java/org/eclipse/edc/connector/controlplane/transfer/dataplane/proxy/ConsumerPullDataPlaneProxyResolver.java +4 −5
| @@ -27,7 +27,6 @@ | ||
| 27 | 27 | import org.eclipse.edc.token.spi.KeyIdDecorator; |
| 28 | 28 | import org.eclipse.edc.token.spi.TokenGenerationService; |
| 29 | 29 | |
| 30 | -import java.security.PrivateKey; | |
| 31 | 30 | import java.util.Optional; |
| 32 | 31 | import java.util.function.Supplier; |
| 33 | 32 | |
| @@ -42,18 +41,18 @@ | ||
| 42 | 41 | private final DataEncrypter dataEncrypter; |
| 43 | 42 | private final TypeManager typeManager; |
| 44 | 43 | private final TokenGenerationService tokenGenerationService; |
| 45 | - private final Supplier<PrivateKey> keySupplier; | |
| 44 | + private final Supplier<String> privateKeyIdSupplier; | |
| 46 | 45 | private final Supplier<String> publicKeyIdSupplier; |
| 47 | 46 | private final ConsumerPullTokenExpirationDateFunction tokenExpirationDateFunction; |
| 48 | 47 | |
| 49 | 48 | public ConsumerPullDataPlaneProxyResolver(DataEncrypter dataEncrypter, TypeManager typeManager, TokenGenerationService tokenGenerationService, |
| 50 | - Supplier<PrivateKey> keySupplier, Supplier<String> publicKeyIdSupplier, | |
| 49 | + Supplier<String> privateKeyIdSupplier, Supplier<String> publicKeyIdSupplier, | |
| 51 | 50 | ConsumerPullTokenExpirationDateFunction tokenExpirationDateFunction) { |
| 52 | 51 | this.dataEncrypter = dataEncrypter; |
| 53 | 52 | this.typeManager = typeManager; |
| 54 | 53 | this.tokenExpirationDateFunction = tokenExpirationDateFunction; |
| 55 | 54 | this.tokenGenerationService = tokenGenerationService; |
| 56 | - this.keySupplier = keySupplier; | |
| 55 | + this.privateKeyIdSupplier = privateKeyIdSupplier; | |
| 57 | 56 | this.publicKeyIdSupplier = publicKeyIdSupplier; |
| 58 | 57 | } |
| 59 | 58 | |
| @@ -87,7 +86,7 @@ | ||
| 87 | 86 | .compose(expiration -> { |
| 88 | 87 | var keyIdDecorator = new KeyIdDecorator(publicKeyIdSupplier.get()); |
| 89 | 88 | var dataAddressDecorator = new ConsumerPullDataPlaneProxyTokenDecorator(expiration, encryptedDataAddress); |
| 90 | - return tokenGenerationService.generate(keySupplier, keyIdDecorator, dataAddressDecorator); | |
| 89 | + return tokenGenerationService.generate(privateKeyIdSupplier.get(), keyIdDecorator, dataAddressDecorator); | |
| 91 | 90 | }) |
| 92 | 91 | .map(TokenRepresentation::getToken); |
| 93 | 92 | } |
modified extensions/control-plane/transfer/transfer-data-plane/src/test/java/org/eclipse/edc/connector/controlplane/transfer/dataplane/proxy/ConsumerPullDataPlaneProxyResolverTest.java +4 −4
| @@ -30,7 +30,6 @@ | ||
| 30 | 30 | import org.eclipse.edc.token.spi.TokenGenerationService; |
| 31 | 31 | import org.junit.jupiter.api.Test; |
| 32 | 32 | import org.mockito.ArgumentCaptor; |
| 33 | -import org.mockito.Mockito; | |
| 34 | 33 | |
| 35 | 34 | import java.sql.Date; |
| 36 | 35 | import java.time.Instant; |
| @@ -41,6 +40,7 @@ | ||
| 41 | 40 | import static org.eclipse.edc.connector.controlplane.transfer.dataplane.spi.TransferDataPlaneConstants.DATA_ADDRESS; |
| 42 | 41 | import static org.eclipse.edc.connector.controlplane.transfer.dataplane.spi.TransferDataPlaneConstants.HTTP_PROXY; |
| 43 | 42 | import static org.mockito.ArgumentMatchers.any; |
| 43 | +import static org.mockito.ArgumentMatchers.anyString; | |
| 44 | 44 | import static org.mockito.Mockito.mock; |
| 45 | 45 | import static org.mockito.Mockito.when; |
| 46 | 46 | |
| @@ -52,7 +52,7 @@ | ||
| 52 | 52 | private final TokenGenerationService tokenGenerationService = mock(); |
| 53 | 53 | private final ConsumerPullTokenExpirationDateFunction tokenExpirationDateFunction = mock(); |
| 54 | 54 | |
| 55 | - private final ConsumerPullDataPlaneProxyResolver resolver = new ConsumerPullDataPlaneProxyResolver(dataEncrypter, TYPE_MANAGER, tokenGenerationService, Mockito::mock, () -> "test-public-key", tokenExpirationDateFunction); | |
| 55 | + private final ConsumerPullDataPlaneProxyResolver resolver = new ConsumerPullDataPlaneProxyResolver(dataEncrypter, TYPE_MANAGER, tokenGenerationService, () -> "test-private-key", () -> "test-public-key", tokenExpirationDateFunction); | |
| 56 | 56 | |
| 57 | 57 | private static DataAddress dataAddress() { |
| 58 | 58 | return DataAddress.Builder.newInstance().type(UUID.randomUUID().toString()).build(); |
| @@ -75,7 +75,7 @@ | ||
| 75 | 75 | var captor = ArgumentCaptor.forClass(TokenDecorator[].class); |
| 76 | 76 | when(dataEncrypter.encrypt(TYPE_MANAGER.writeValueAsString(address))).thenReturn(encryptedAddress); |
| 77 | 77 | when(tokenExpirationDateFunction.expiresAt(address, transferProcess.getContractId())).thenReturn(Result.success(expiration)); |
| 78 | - when(tokenGenerationService.generate(any(), captor.capture())) | |
| 78 | + when(tokenGenerationService.generate(anyString(), captor.capture())) | |
| 79 | 79 | .thenReturn(Result.success(TokenRepresentation.Builder.newInstance().token(token).build())); |
| 80 | 80 | |
| 81 | 81 | var result = resolver.toDataAddress(transferProcess, address, instance); |
| @@ -157,7 +157,7 @@ | ||
| 157 | 157 | |
| 158 | 158 | when(dataEncrypter.encrypt(TYPE_MANAGER.writeValueAsString(address))).thenReturn("encryptedAddress"); |
| 159 | 159 | when(tokenExpirationDateFunction.expiresAt(address, transferProcess.getContractId())).thenReturn(Result.success(expiration)); |
| 160 | - when(tokenGenerationService.generate(any(), any(TokenDecorator[].class))).thenReturn(Result.failure(errorMsg)); | |
| 160 | + when(tokenGenerationService.generate(anyString(), any(TokenDecorator[].class))).thenReturn(Result.failure(errorMsg)); | |
| 161 | 161 | |
| 162 | 162 | var result = resolver.toDataAddress(transferProcess, address, instance); |
| 163 | 163 |
modified extensions/data-plane/data-plane-http-oauth2-core/src/main/java/org/eclipse/edc/connector/dataplane/http/oauth2/DataPlaneHttpOauth2Extension.java +3 −3
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import org.eclipse.edc.connector.dataplane.http.spi.HttpRequestParamsProvider; |
| 19 | 19 | import org.eclipse.edc.iam.oauth2.spi.client.Oauth2Client; |
| 20 | -import org.eclipse.edc.keys.spi.PrivateKeyResolver; | |
| 20 | +import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider; | |
| 21 | 21 | import org.eclipse.edc.runtime.metamodel.annotation.Extension; |
| 22 | 22 | import org.eclipse.edc.runtime.metamodel.annotation.Inject; |
| 23 | 23 | import org.eclipse.edc.spi.security.Vault; |
| @@ -43,7 +43,7 @@ | ||
| 43 | 43 | private Vault vault; |
| 44 | 44 | |
| 45 | 45 | @Inject |
| 46 | - private PrivateKeyResolver privateKeyResolver; | |
| 46 | + private JwsSignerProvider jwsSignerProvider; | |
| 47 | 47 | |
| 48 | 48 | @Inject |
| 49 | 49 | private Oauth2Client oauth2Client; |
| @@ -55,7 +55,7 @@ | ||
| 55 | 55 | |
| 56 | 56 | @Override |
| 57 | 57 | public void initialize(ServiceExtensionContext context) { |
| 58 | - var requestFactory = new Oauth2CredentialsRequestFactory(privateKeyResolver, clock, vault, context.getMonitor()); | |
| 58 | + var requestFactory = new Oauth2CredentialsRequestFactory(jwsSignerProvider, clock, vault); | |
| 59 | 59 | var oauth2ParamsDecorator = new Oauth2HttpRequestParamsDecorator(requestFactory, oauth2Client); |
| 60 | 60 | |
| 61 | 61 | paramsProvider.registerSourceDecorator(oauth2ParamsDecorator); |
modified extensions/data-plane/data-plane-http-oauth2-core/src/main/java/org/eclipse/edc/connector/dataplane/http/oauth2/Oauth2CredentialsRequestFactory.java +6 −14
| @@ -18,9 +18,8 @@ | ||
| 18 | 18 | import org.eclipse.edc.iam.oauth2.spi.client.Oauth2CredentialsRequest; |
| 19 | 19 | import org.eclipse.edc.iam.oauth2.spi.client.PrivateKeyOauth2CredentialsRequest; |
| 20 | 20 | import org.eclipse.edc.iam.oauth2.spi.client.SharedSecretOauth2CredentialsRequest; |
| 21 | -import org.eclipse.edc.keys.spi.PrivateKeyResolver; | |
| 21 | +import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider; | |
| 22 | 22 | import org.eclipse.edc.spi.iam.TokenRepresentation; |
| 23 | -import org.eclipse.edc.spi.monitor.Monitor; | |
| 24 | 23 | import org.eclipse.edc.spi.result.Result; |
| 25 | 24 | import org.eclipse.edc.spi.security.Vault; |
| 26 | 25 | import org.eclipse.edc.spi.types.domain.DataAddress; |
| @@ -46,16 +45,14 @@ | ||
| 46 | 45 | |
| 47 | 46 | private static final long DEFAULT_TOKEN_VALIDITY = TimeUnit.MINUTES.toSeconds(5); |
| 48 | 47 | private static final String GRANT_CLIENT_CREDENTIALS = "client_credentials"; |
| 49 | - private final PrivateKeyResolver privateKeyResolver; | |
| 48 | + private final JwsSignerProvider jwsSignerProvider; | |
| 50 | 49 | private final Clock clock; |
| 51 | 50 | private final Vault vault; |
| 52 | - private final Monitor monitor; | |
| 53 | 51 | |
| 54 | - public Oauth2CredentialsRequestFactory(PrivateKeyResolver privateKeyResolver, Clock clock, Vault vault, Monitor monitor) { | |
| 55 | - this.privateKeyResolver = privateKeyResolver; | |
| 52 | + public Oauth2CredentialsRequestFactory(JwsSignerProvider jwsSignerProvider, Clock clock, Vault vault) { | |
| 53 | + this.jwsSignerProvider = jwsSignerProvider; | |
| 56 | 54 | this.clock = clock; |
| 57 | 55 | this.vault = vault; |
| 58 | - this.monitor = monitor; | |
| 59 | 56 | } |
| 60 | 57 | |
| 61 | 58 | /** |
| @@ -104,18 +101,13 @@ | ||
| 104 | 101 | |
| 105 | 102 | @NotNull |
| 106 | 103 | private Result<TokenRepresentation> createAssertion(String pkSecret, DataAddress dataAddress) { |
| 107 | - var privateKey = privateKeyResolver.resolvePrivateKey(pkSecret); | |
| 108 | - if (privateKey.failed()) { | |
| 109 | - return Result.failure("Failed to resolve private key with alias: " + pkSecret); | |
| 110 | - } | |
| 111 | - | |
| 112 | 104 | var validity = Optional.ofNullable(dataAddress.getStringProperty(VALIDITY)) |
| 113 | 105 | .map(this::parseLong) |
| 114 | 106 | .orElse(DEFAULT_TOKEN_VALIDITY); |
| 115 | 107 | var decorator = new Oauth2AssertionDecorator(dataAddress.getStringProperty(TOKEN_URL), dataAddress.getStringProperty(CLIENT_ID), clock, validity); |
| 116 | - var service = new JwtGenerationService(); | |
| 108 | + var service = new JwtGenerationService(jwsSignerProvider); | |
| 117 | 109 | |
| 118 | - return service.generate(privateKey::getContent, decorator); | |
| 110 | + return service.generate(pkSecret, decorator); | |
| 119 | 111 | } |
| 120 | 112 | |
| 121 | 113 | @Nullable |
modified extensions/data-plane/data-plane-http-oauth2-core/src/test/java/org/eclipse/edc/connector/dataplane/http/oauth2/Oauth2CredentialsRequestFactoryTest.java +14 −13
| @@ -15,16 +15,17 @@ | ||
| 15 | 15 | package org.eclipse.edc.connector.dataplane.http.oauth2; |
| 16 | 16 | |
| 17 | 17 | import com.nimbusds.jose.JOSEException; |
| 18 | +import com.nimbusds.jose.crypto.RSASSASigner; | |
| 18 | 19 | import com.nimbusds.jose.crypto.RSASSAVerifier; |
| 19 | 20 | import com.nimbusds.jose.jwk.KeyUse; |
| 20 | 21 | import com.nimbusds.jose.jwk.RSAKey; |
| 21 | 22 | import com.nimbusds.jose.jwk.gen.RSAKeyGenerator; |
| 22 | 23 | import com.nimbusds.jwt.SignedJWT; |
| 24 | +import org.assertj.core.api.AssertionsForClassTypes; | |
| 23 | 25 | import org.eclipse.edc.connector.dataplane.http.spi.HttpDataAddress; |
| 24 | 26 | import org.eclipse.edc.iam.oauth2.spi.client.PrivateKeyOauth2CredentialsRequest; |
| 25 | 27 | import org.eclipse.edc.iam.oauth2.spi.client.SharedSecretOauth2CredentialsRequest; |
| 26 | -import org.eclipse.edc.keys.spi.PrivateKeyResolver; | |
| 27 | -import org.eclipse.edc.spi.monitor.Monitor; | |
| 28 | +import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider; | |
| 28 | 29 | import org.eclipse.edc.spi.result.Result; |
| 29 | 30 | import org.eclipse.edc.spi.security.Vault; |
| 30 | 31 | import org.junit.jupiter.api.Test; |
| @@ -45,6 +46,7 @@ | ||
| 45 | 46 | import static org.eclipse.edc.iam.oauth2.spi.Oauth2DataAddressSchema.SCOPE; |
| 46 | 47 | import static org.eclipse.edc.iam.oauth2.spi.Oauth2DataAddressSchema.TOKEN_URL; |
| 47 | 48 | import static org.eclipse.edc.iam.oauth2.spi.Oauth2DataAddressSchema.VALIDITY; |
| 49 | +import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; | |
| 48 | 50 | import static org.mockito.Mockito.mock; |
| 49 | 51 | import static org.mockito.Mockito.verifyNoInteractions; |
| 50 | 52 | import static org.mockito.Mockito.when; |
| @@ -53,10 +55,9 @@ | ||
| 53 | 55 | |
| 54 | 56 | private final Instant now = Instant.now(); |
| 55 | 57 | private final Clock clock = Clock.fixed(now, UTC); |
| 56 | - private final PrivateKeyResolver privateKeyResolver = mock(PrivateKeyResolver.class); | |
| 57 | - private final Vault vault = mock(Vault.class); | |
| 58 | - private final Monitor monitor = mock(Monitor.class); | |
| 59 | - private final Oauth2CredentialsRequestFactory factory = new Oauth2CredentialsRequestFactory(privateKeyResolver, clock, vault, monitor); | |
| 58 | + private final JwsSignerProvider privateKeyResolver = mock(); | |
| 59 | + private final Vault vault = mock(); | |
| 60 | + private final Oauth2CredentialsRequestFactory factory = new Oauth2CredentialsRequestFactory(privateKeyResolver, clock, vault); | |
| 60 | 61 | |
| 61 | 62 | @Test |
| 62 | 63 | void shouldCreateSharedSecretRequestWithKey_whenPrivateKeyNameIsAbsent() { |
| @@ -68,7 +69,7 @@ | ||
| 68 | 69 | |
| 69 | 70 | var result = factory.create(address); |
| 70 | 71 | |
| 71 | - assertThat(result).matches(Result::succeeded).extracting(Result::getContent) | |
| 72 | + AssertionsForClassTypes.assertThat(result).matches(Result::succeeded).extracting(Result::getContent) | |
| 72 | 73 | .asInstanceOf(type(SharedSecretOauth2CredentialsRequest.class)) |
| 73 | 74 | .satisfies(request -> { |
| 74 | 75 | assertThat(request.getGrantType()).isEqualTo("client_credentials"); |
| @@ -90,13 +91,13 @@ | ||
| 90 | 91 | |
| 91 | 92 | var result = factory.create(address); |
| 92 | 93 | |
| 93 | - assertThat(result).matches(Result::failed); | |
| 94 | + AssertionsForClassTypes.assertThat(result).matches(Result::failed); | |
| 94 | 95 | } |
| 95 | 96 | |
| 96 | 97 | @Test |
| 97 | 98 | void shouldCreatePrivateKeyRequest_whenPrivateKeyNameIsPresent() throws JOSEException { |
| 98 | 99 | var keyPair = generateKeyPair(); |
| 99 | - when(privateKeyResolver.resolvePrivateKey("pk-test")).thenReturn(Result.success(keyPair.toPrivateKey())); | |
| 100 | + when(privateKeyResolver.createJwsSigner("pk-test")).thenReturn(Result.success(new RSASSASigner(keyPair.toPrivateKey()))); | |
| 100 | 101 | |
| 101 | 102 | var address = defaultAddress() |
| 102 | 103 | .property(PRIVATE_KEY_NAME, "pk-test") |
| @@ -105,7 +106,7 @@ | ||
| 105 | 106 | |
| 106 | 107 | var result = factory.create(address); |
| 107 | 108 | |
| 108 | - assertThat(result).matches(Result::succeeded).extracting(Result::getContent) | |
| 109 | + AssertionsForClassTypes.assertThat(result).matches(Result::succeeded).extracting(Result::getContent) | |
| 109 | 110 | .asInstanceOf(type(PrivateKeyOauth2CredentialsRequest.class)) |
| 110 | 111 | .satisfies(request -> { |
| 111 | 112 | assertThat(request.getGrantType()).isEqualTo("client_credentials"); |
| @@ -130,7 +131,7 @@ | ||
| 130 | 131 | |
| 131 | 132 | @Test |
| 132 | 133 | void shouldFailIfPrivateKeySecretNotFound() { |
| 133 | - when(privateKeyResolver.resolvePrivateKey("pk-test")).thenReturn(Result.failure("not found")); | |
| 134 | + when(privateKeyResolver.createJwsSigner("pk-test")).thenReturn(Result.failure("foo bar")); | |
| 134 | 135 | |
| 135 | 136 | var address = defaultAddress() |
| 136 | 137 | .property(PRIVATE_KEY_NAME, "pk-test") |
| @@ -139,8 +140,8 @@ | ||
| 139 | 140 | |
| 140 | 141 | var result = factory.create(address); |
| 141 | 142 | |
| 142 | - assertThat(result).matches(Result::failed) | |
| 143 | - .extracting(Result::getFailureDetail).asString().contains("pk-test"); | |
| 143 | + assertThat(result).isFailed() | |
| 144 | + .detail().contains("foo bar"); | |
| 144 | 145 | } |
| 145 | 146 | |
| 146 | 147 | private HttpDataAddress.Builder defaultAddress() { |
modified settings.gradle.kts +1 −0
| @@ -234,6 +234,7 @@ | ||
| 234 | 234 | include(":spi:common:identity-did-spi") |
| 235 | 235 | include(":spi:common:json-ld-spi") |
| 236 | 236 | include(":spi:common:jwt-spi") |
| 237 | +include(":spi:common:jwt-signer-spi") | |
| 237 | 238 | include(":spi:common:token-spi") |
| 238 | 239 | include(":spi:common:oauth2-spi") |
| 239 | 240 | include(":spi:common:policy-engine-spi") |
added spi/common/jwt-signer-spi/build.gradle.kts +26 −0
| @@ -0,0 +1,26 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2022 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +plugins { | |
| 16 | + `java-library` | |
| 17 | + `maven-publish` | |
| 18 | +} | |
| 19 | + | |
| 20 | +dependencies { | |
| 21 | + api(libs.nimbus.jwt) | |
| 22 | + api(project(":spi:common:boot-spi")) //Result | |
| 23 | + implementation(libs.edc.runtime.metamodel) | |
| 24 | +} | |
| 25 | + | |
| 26 | + |
added spi/common/jwt-signer-spi/src/main/java/org/eclipse/edc/jwt/signer/spi/JwsSignerProvider.java +28 −0
| @@ -0,0 +1,28 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | + | |
| 15 | +package org.eclipse.edc.jwt.signer.spi; | |
| 16 | + | |
| 17 | +import com.nimbusds.jose.JWSSigner; | |
| 18 | +import org.eclipse.edc.runtime.metamodel.annotation.ExtensionPoint; | |
| 19 | +import org.eclipse.edc.spi.result.Result; | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * A JwsSignerProvider provides a {@link JWSSigner} for a given private key ID. | |
| 23 | + */ | |
| 24 | +@ExtensionPoint | |
| 25 | +@FunctionalInterface | |
| 26 | +public interface JwsSignerProvider { | |
| 27 | + Result<JWSSigner> createJwsSigner(String privateKeyId); | |
| 28 | +} |
added spi/common/jwt-signer-spi/src/main/java/org/eclipse/edc/jwt/signer/spi/package-info.java +18 −0
| @@ -0,0 +1,18 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | |
| 3 | + * | |
| 4 | + * This program and the accompanying materials are made available under the | |
| 5 | + * terms of the Apache License, Version 2.0 which is available at | |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 | |
| 7 | + * | |
| 8 | + * SPDX-License-Identifier: Apache-2.0 | |
| 9 | + * | |
| 10 | + * Contributors: | |
| 11 | + * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | |
| 12 | + * | |
| 13 | + */ | |
| 14 | +@Spi(value = "Implementation SPI that is used to contribute custom JWSSigners to the JwtGenerationService") | |
| 15 | + | |
| 16 | +package org.eclipse.edc.jwt.signer.spi; | |
| 17 | + | |
| 18 | +import org.eclipse.edc.runtime.metamodel.annotation.Spi; | |
| No newline at the end of the file |
modified spi/common/token-spi/src/main/java/org/eclipse/edc/token/spi/TokenGenerationService.java +3 −7
| @@ -17,9 +17,6 @@ | ||
| 17 | 17 | import org.eclipse.edc.spi.iam.TokenRepresentation; |
| 18 | 18 | import org.eclipse.edc.spi.result.Result; |
| 19 | 19 | |
| 20 | -import java.security.PrivateKey; | |
| 21 | -import java.util.function.Supplier; | |
| 22 | - | |
| 23 | 20 | /** |
| 24 | 21 | * Interface for generating token based on a set of claims and header. |
| 25 | 22 | */ |
| @@ -28,9 +25,8 @@ | ||
| 28 | 25 | /** |
| 29 | 26 | * Generate a signed token based on the request. |
| 30 | 27 | * |
| 31 | - * @param privateKeySupplier A {@link Supplier} that provides the private on-demand. | |
| 32 | - * Be advised that holding the private key in memory poses a considerable security risk and should be avoided. | |
| 33 | - * @param decorators an optional list of {@code JwtDecorator} objects to determine the shape of the token, i.e. headers and claims | |
| 28 | + * @param privateKeyId A String that identifies the private key, e.g. a secret alias in a {@link org.eclipse.edc.spi.security.Vault} | |
| 29 | + * @param decorators an optional list of {@code JwtDecorator} objects to determine the shape of the token, i.e. headers and claims | |
| 34 | 30 | */ |
| 35 | - Result<TokenRepresentation> generate(Supplier<PrivateKey> privateKeySupplier, TokenDecorator... decorators); | |
| 31 | + Result<TokenRepresentation> generate(String privateKeyId, TokenDecorator... decorators); | |
| 36 | 32 | } |