Skip to content

Commit

Permalink
chore: dedicated class for default JwsSignerProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Aug 9, 2024
1 parent 7f51a3d commit 0be4abb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
3 changes: 2 additions & 1 deletion core/common/lib/crypto-common-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ plugins {
dependencies {
api(project(":spi:common:identity-did-spi"))
api(project(":spi:common:identity-trust-spi"))
api(libs.nimbus.jwt) // nimbus classes are exposed on the API surface of CryptoConverter and DefaultJwsSignerProvider
implementation(project(":core:common:lib:util-lib"))
implementation(project(":spi:common:core-spi"))
implementation(project(":spi:common:jwt-signer-spi"))

implementation(libs.nimbus.jwt)
// used for the Ed25519 Verifier in conjunction with OctetKeyPairs (OKP)
runtimeOnly(libs.tink)
// Java does not natively implement elliptic curve multiplication, so we need to get bouncy
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.security.token.jwt;

import com.nimbusds.jose.JWSSigner;
import org.eclipse.edc.jwt.signer.spi.JwsSignerProvider;
import org.eclipse.edc.keys.spi.PrivateKeyResolver;
import org.eclipse.edc.spi.result.Result;

/**
* Provides a {@link JWSSigner} that is created based on a private key's algorithm.
* Note that the private key will be held in memory for the duration of the instantiation of the {@link JWSSigner}.
*/
public class DefaultJwsSignerProvider implements JwsSignerProvider {

private final PrivateKeyResolver privateKeyResolver;

public DefaultJwsSignerProvider(PrivateKeyResolver privateKeyResolver) {
this.privateKeyResolver = privateKeyResolver;
}

@Override
public Result<JWSSigner> createJwsSigner(String privateKeyId) {
return privateKeyResolver.resolvePrivateKey(privateKeyId)
.compose(pk -> Result.ofThrowable(() -> CryptoConverter.createSignerFor(pk)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.security.token.jwt.CryptoConverter;
import org.eclipse.edc.spi.result.Result;
import org.eclipse.edc.security.token.jwt.DefaultJwsSignerProvider;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.token.spi.TokenDecoratorRegistry;
import org.eclipse.edc.token.spi.TokenValidationRulesRegistry;
Expand Down Expand Up @@ -57,7 +56,6 @@ public TokenDecoratorRegistry tokenDecoratorRegistry() {
@Provider(isDefault = true)
public JwsSignerProvider defaultSignerProvider() {
// default implementation: resolve the private key (from vault of config) and create a JWSSigner based on its algorithm
return privateKeyId -> privateKeyResolver.resolvePrivateKey(privateKeyId)
.compose(pk -> Result.ofThrowable(() -> CryptoConverter.createSignerFor(pk)));
return new DefaultJwsSignerProvider(privateKeyResolver);
}
}

0 comments on commit 0be4abb

Please sign in to comment.