Skip to content

Commit b07d977

Browse files
chore: dedicated class for default JwsSignerProvider (#4403)
1 parent f623a1e commit b07d977

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

core/common/lib/crypto-common-lib/build.gradle.kts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ plugins {
1818
dependencies {
1919
api(project(":spi:common:identity-did-spi"))
2020
api(project(":spi:common:identity-trust-spi"))
21+
api(libs.nimbus.jwt) // nimbus classes are exposed on the API surface of CryptoConverter and DefaultJwsSignerProvider
2122
implementation(project(":core:common:lib:util-lib"))
2223
implementation(project(":spi:common:core-spi"))
24+
implementation(project(":spi:common:jwt-signer-spi"))
2325

24-
implementation(libs.nimbus.jwt)
2526
// used for the Ed25519 Verifier in conjunction with OctetKeyPairs (OKP)
2627
runtimeOnly(libs.tink)
2728
// Java does not natively implement elliptic curve multiplication, so we need to get bouncy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.security.token.jwt;
16+
17+
import com.nimbusds.jose.JWSSigner;
18+
import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider;
19+
import org.eclipse.edc.keys.spi.PrivateKeyResolver;
20+
import org.eclipse.edc.spi.result.Result;
21+
22+
/**
23+
* Provides a {@link JWSSigner} that is created based on a private key's algorithm.
24+
* Note that the private key will be held in memory for the duration of the instantiation of the {@link JWSSigner}.
25+
*/
26+
public class DefaultJwsSignerProvider implements JwsSignerProvider {
27+
28+
private final PrivateKeyResolver privateKeyResolver;
29+
30+
public DefaultJwsSignerProvider(PrivateKeyResolver privateKeyResolver) {
31+
this.privateKeyResolver = privateKeyResolver;
32+
}
33+
34+
@Override
35+
public Result<JWSSigner> createJwsSigner(String privateKeyId) {
36+
return privateKeyResolver.resolvePrivateKey(privateKeyId)
37+
.compose(pk -> Result.ofThrowable(() -> CryptoConverter.createSignerFor(pk)));
38+
}
39+
}

core/common/token-core/src/main/java/org/eclipse/edc/token/TokenServicesExtension.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
2020
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
2121
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;
22+
import org.eclipse.edc.security.token.jwt.DefaultJwsSignerProvider;
2423
import org.eclipse.edc.spi.system.ServiceExtension;
2524
import org.eclipse.edc.token.spi.TokenDecoratorRegistry;
2625
import org.eclipse.edc.token.spi.TokenValidationRulesRegistry;
@@ -56,8 +55,6 @@ public TokenDecoratorRegistry tokenDecoratorRegistry() {
5655

5756
@Provider(isDefault = true)
5857
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)));
58+
return new DefaultJwsSignerProvider(privateKeyResolver);
6259
}
6360
}

extensions/common/iam/identity-trust/identity-trust-sts/identity-trust-sts-core/src/test/java/org/eclipse/edc/iam/identitytrust/sts/defaults/StsClientTokenIssuanceIntegrationTest.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
import org.eclipse.edc.keys.keyparsers.PemParser;
3030
import org.eclipse.edc.keys.spi.KeyParserRegistry;
3131
import org.eclipse.edc.keys.spi.PrivateKeyResolver;
32-
import org.eclipse.edc.security.token.jwt.CryptoConverter;
33-
import org.eclipse.edc.spi.result.Result;
32+
import org.eclipse.edc.security.token.jwt.DefaultJwsSignerProvider;
3433
import org.eclipse.edc.spi.security.Vault;
3534
import org.eclipse.edc.token.JwtGenerationService;
3635
import org.eclipse.edc.transaction.spi.NoopTransactionContext;
@@ -73,8 +72,7 @@ void setup() {
7372
privateKeyResolver = new VaultPrivateKeyResolver(keyParserRegistry, vault, mock(), mock());
7473

7574
tokenGeneratorService = new StsClientTokenGeneratorServiceImpl(
76-
client -> new JwtGenerationService(keyId -> privateKeyResolver.resolvePrivateKey(keyId)
77-
.compose(pk -> Result.ofThrowable(() -> CryptoConverter.createSignerFor(pk)))),
75+
client -> new JwtGenerationService(new DefaultJwsSignerProvider(privateKeyResolver)),
7876
StsClient::getPrivateKeyAlias,
7977
Clock.systemUTC(), 60 * 5);
8078

0 commit comments

Comments
 (0)