Skip to content

Commit

Permalink
fix: CGD-288 and code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-vavdiya committed Jul 3, 2023
1 parent d6b2264 commit f037c16
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.OAuthFlow;
import io.swagger.v3.oas.models.security.OAuthFlows;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
*/

package org.eclipse.tractusx.managedidentitywallets.config.security;

import org.springframework.core.convert.converter.Converter;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
Expand All @@ -49,18 +52,14 @@ public CustomAuthenticationConverter(String resourceId) {
this.resourceId = resourceId;
grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
}

@Override
public AbstractAuthenticationToken convert(Jwt source) {
Collection<GrantedAuthority> convert = grantedAuthoritiesConverter.convert(source);
if (!CollectionUtils.isEmpty(convert)) {
Collection<GrantedAuthority> authorities = new HashSet<>(convert);
authorities.addAll(extractResourceRoles(source, resourceId));
extractResourceRoles(source, resourceId);
return new JwtAuthenticationToken(source, authorities);
} else {
return new JwtAuthenticationToken(source, Collections.emptyList());
}
Collection<GrantedAuthority> authorities = (grantedAuthoritiesConverter.convert(source))
.stream()
.collect(Collectors.toSet());
authorities.addAll(extractResourceRoles(source, resourceId));
extractResourceRoles(source, resourceId);
return new JwtAuthenticationToken(source, authorities);
}

private Collection<? extends GrantedAuthority> extractResourceRoles(Jwt jwt, String resourceId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public PageImpl<VerifiableCredential> getCredentials(String credentialId, String

//Holder must be caller of API
Wallet holderWallet = commonService.getWalletByIdentifier(callerBPN);
filterRequest.appendCriteria(StringPool.HOLDER_DID, Operator.EQUALS, holderWallet.getDid().toString());
filterRequest.appendCriteria(StringPool.HOLDER_DID, Operator.EQUALS, holderWallet.getDid());

if (StringUtils.hasText(issuerIdentifier)) {
Wallet issuerWallet = commonService.getWalletByIdentifier(issuerIdentifier);
filterRequest.appendCriteria(StringPool.ISSUER_DID, Operator.EQUALS, issuerWallet.getDid().toString());
filterRequest.appendCriteria(StringPool.ISSUER_DID, Operator.EQUALS, issuerWallet.getDid());
}

if (StringUtils.hasText(credentialId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ private Wallet createWallet(CreateWalletRequest request, boolean authority) {
//Save key
walletKeyService.getRepository().save(WalletKey.builder()
.walletId(wallet.getId())
.referenceKey("dummy ref key") //TODO removed once vault setup is ready
.vaultAccessToken("dummy vault access token") ////TODO removed once vault setup is ready
.referenceKey("dummy ref key, removed once vault setup is ready")
.vaultAccessToken("dummy vault access token, removed once vault setup is ready")
.privateKey(encryptionUtils.encrypt(getPrivateKeyString(keyPair.getPrivateKey().asByte())))
.publicKey(encryptionUtils.encrypt(getPublicKeyString(keyPair.getPublicKey().asByte())))
.build());
Expand Down Expand Up @@ -277,24 +277,6 @@ private void validateCreateWallet(CreateWalletRequest request) {
}

}
/*
@SneakyThrows
private Ed25519KeySet createKeyPair() {
KeyPairGeneratorSpi.Ed25519 ed25519 = new KeyPairGeneratorSpi.Ed25519();
ed25519.initialize(256, new SecureRandom());
KeyPair keyPair = ed25519.generateKeyPair();
PublicKey PubKey = keyPair.getPublic();
PrivateKey PivKey = keyPair.getPrivate();
Ed25519PrivateKeyParameters ed25519PrivateKeyParameters =
(Ed25519PrivateKeyParameters) PrivateKeyFactory.createKey(PivKey.getEncoded());
Ed25519PublicKeyParameters publicKeyParameters =
(Ed25519PublicKeyParameters) PublicKeyFactory.createKey(PubKey.getEncoded());
byte[] privateKeyBytes = ed25519PrivateKeyParameters.getEncoded();
byte[] publicKeyBytes = publicKeyParameters.getEncoded();
return new Ed25519KeySet(privateKeyBytes, publicKeyBytes);
}*/

@SneakyThrows
private String getPrivateKeyString(byte[] privateKeyBytes) {
StringWriter stringWriter = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
import lombok.SneakyThrows;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
* The type String to did document converter.
*/
Expand Down

0 comments on commit f037c16

Please sign in to comment.