1919
2020package org .eclipse .tractusx .edc .tests ;
2121
22- import com . fasterxml . jackson . core . type . TypeReference ;
22+ import org . eclipse . edc . iam . decentralizedclaims . spi . validation . TokenValidationAction ;
2323import org .eclipse .edc .iam .verifiablecredentials .spi .model .CredentialSubject ;
2424import org .eclipse .edc .iam .verifiablecredentials .spi .model .Issuer ;
2525import org .eclipse .edc .iam .verifiablecredentials .spi .model .VerifiableCredential ;
3333import org .eclipse .edc .spi .types .TypeManager ;
3434
3535import java .time .Instant ;
36+ import java .util .Base64 ;
3637import java .util .List ;
3738import java .util .Map ;
3839
39- import static java .lang .String .format ;
40+ import static org .eclipse .edc .iam .decentralizedclaims .spi .SelfIssuedTokenConstants .PRESENTATION_TOKEN_CLAIM ;
41+ import static org .eclipse .edc .jwt .spi .JwtRegisteredClaimNames .AUDIENCE ;
42+ import static org .eclipse .edc .jwt .spi .JwtRegisteredClaimNames .EXPIRATION_TIME ;
43+ import static org .eclipse .edc .jwt .spi .JwtRegisteredClaimNames .ISSUED_AT ;
44+ import static org .eclipse .edc .jwt .spi .JwtRegisteredClaimNames .ISSUER ;
45+ import static org .eclipse .edc .jwt .spi .JwtRegisteredClaimNames .SUBJECT ;
4046
4147/**
4248 * An {@link IdentityService} that will inject the BPN claim in every token.
@@ -49,59 +55,94 @@ public class MockVcIdentityService implements IdentityService {
4955 private final String businessPartnerNumber ;
5056 private final String did ;
5157 private final TypeManager typeManager = new JacksonTypeManager ();
52-
53- public MockVcIdentityService (String businessPartnerNumber , String did ) {
58+ private final TokenValidationAction tokenValidationAction ;
59+
60+ public MockVcIdentityService (String businessPartnerNumber , String did , TokenValidationAction tokenValidationAction ) {
5461 this .businessPartnerNumber = businessPartnerNumber ;
5562 this .did = did ;
63+ this .tokenValidationAction = tokenValidationAction ;
5664 }
57-
65+
5866 @ Override
5967 public Result <TokenRepresentation > obtainClientCredentials (String participantContextId , TokenParameters parameters ) {
60- var credentials = List .of (membershipCredential (), dataExchangeGovernanceCredential ());
61- var token = Map .of (VC_CLAIM , credentials );
62-
6368 var tokenRepresentation = TokenRepresentation .Builder .newInstance ()
64- .token (typeManager . writeValueAsString ( token ))
69+ .token (getTestToken ( parameters . getStringClaim ( "aud" ) ))
6570 .build ();
71+
6672 return Result .success (tokenRepresentation );
6773 }
68-
74+
6975 @ Override
7076 public Result <ClaimToken > verifyJwtToken (String participantContextId , TokenRepresentation tokenRepresentation , VerificationContext verificationContext ) {
7177 var token = tokenRepresentation .getToken ().replace ("Bearer " , "" );
72- var tokenParsed = typeManager .readValue (token , Map .class );
73-
74- if (tokenParsed .containsKey (VC_CLAIM )) {
75- var credentials = typeManager .getMapper ().convertValue (tokenParsed .get (VC_CLAIM ), new TypeReference <List <VerifiableCredential >>(){});
76- var claimToken = ClaimToken .Builder .newInstance ()
77- .claim (VC_CLAIM , credentials )
78- .build ();
79- return Result .success (claimToken );
78+ tokenRepresentation = tokenRepresentation .toBuilder ().token (token ).build ();
79+ var claimTokenResult = tokenValidationAction .validate (participantContextId , tokenRepresentation );
80+
81+ if (claimTokenResult .failed ()) {
82+ return claimTokenResult ;
8083 }
81- return Result .failure (format ("Expected %s claim, but token did not contain them" , VC_CLAIM ));
84+
85+ var claimToken = claimTokenResult .getContent ();
86+ var bpnlConsumer = claimToken .getStringClaim (BUSINESS_PARTNER_NUMBER_CLAIM );
87+ var didConsumer = claimToken .getStringClaim (ISSUER );
88+ var credentials = List .of (membershipCredential (bpnlConsumer , didConsumer ), dataExchangeGovernanceCredential (bpnlConsumer , didConsumer ));
89+
90+ var claimTokenWithVc = ClaimToken .Builder .newInstance ()
91+ .claim (VC_CLAIM , credentials )
92+ .build ();
93+
94+ return Result .success (claimTokenWithVc );
8295 }
83-
84- private VerifiableCredential membershipCredential () {
96+
97+ private String getTestToken (String aud ) {
98+ var header = Map .of (
99+ "alg" , "ES256K" ,
100+ "typ" , "JWT" ,
101+ "kid" , did + "#key-1"
102+ );
103+
104+ var now = Instant .now ();
105+ var payload = new java .util .HashMap <String , Object >();
106+ payload .put (ISSUER , did );
107+ payload .put (SUBJECT , did );
108+ payload .put (AUDIENCE , aud );
109+ payload .put (EXPIRATION_TIME , now .plusSeconds (3600 ).getEpochSecond ());
110+ payload .put (ISSUED_AT , now .getEpochSecond ());
111+ payload .put (PRESENTATION_TOKEN_CLAIM , "token" );
112+ payload .put (BUSINESS_PARTNER_NUMBER_CLAIM , businessPartnerNumber );
113+
114+ var signature = "signature" ;
115+
116+ String headerJson = typeManager .writeValueAsString (header );
117+ String payloadJson = typeManager .writeValueAsString (payload );
118+ String encodedHeader = Base64 .getUrlEncoder ().withoutPadding ().encodeToString (headerJson .getBytes ());
119+ String encodedToken = Base64 .getUrlEncoder ().withoutPadding ().encodeToString (payloadJson .getBytes ());
120+ String encodedSignature = Base64 .getUrlEncoder ().withoutPadding ().encodeToString (signature .getBytes ());
121+
122+ return (encodedHeader + "." + encodedToken + "." + encodedSignature );
123+ }
124+
125+ private VerifiableCredential dataExchangeGovernanceCredential (String bpnlConsumer , String didConsumer ) {
85126 return VerifiableCredential .Builder .newInstance ()
86127 .type ("VerifiableCredential" )
87- .type ("MembershipCredential " )
128+ .type ("DataExchangeGovernanceCredential " )
88129 .credentialSubject (CredentialSubject .Builder .newInstance ()
89- .id (did )
90- .claim ("holderIdentifier" , businessPartnerNumber )
130+ .id (didConsumer )
131+ .claim ("holderIdentifier" , bpnlConsumer )
132+ .claim ("contractVersion" , "1.0" )
91133 .build ())
92134 .issuer (new Issuer ("issuer" , Map .of ()))
93135 .issuanceDate (Instant .now ())
94136 .build ();
95137 }
96138
97- private VerifiableCredential dataExchangeGovernanceCredential ( ) {
139+ private VerifiableCredential membershipCredential ( String bpnlConsumer , String didConsumer ) {
98140 return VerifiableCredential .Builder .newInstance ()
99141 .type ("VerifiableCredential" )
100- .type ("DataExchangeGovernanceCredential " )
142+ .type ("MembershipCredential " )
101143 .credentialSubject (CredentialSubject .Builder .newInstance ()
102- .id (did )
103- .claim ("holderIdentifier" , businessPartnerNumber )
104- .claim ("contractVersion" , "1.0" )
144+ .id (didConsumer )
145+ .claim ("holderIdentifier" , bpnlConsumer )
105146 .build ())
106147 .issuer (new Issuer ("issuer" , Map .of ()))
107148 .issuanceDate (Instant .now ())
0 commit comments