24
24
import org .eclipse .dataspaceconnector .iam .did .spi .document .DidDocument ;
25
25
import org .eclipse .dataspaceconnector .iam .did .spi .document .EllipticCurvePublicKey ;
26
26
import org .eclipse .dataspaceconnector .iam .did .spi .document .VerificationMethod ;
27
- import org .eclipse .dataspaceconnector .iam .did .spi .resolution .DidResolver ;
28
27
import org .eclipse .dataspaceconnector .iam .did .spi .resolution .DidResolverRegistry ;
29
- import org .eclipse .dataspaceconnector .spi .iam .ClaimToken ;
30
28
import org .eclipse .dataspaceconnector .spi .iam .TokenParameters ;
31
29
import org .eclipse .dataspaceconnector .spi .monitor .ConsoleMonitor ;
32
30
import org .eclipse .dataspaceconnector .spi .result .Result ;
36
34
37
35
import java .time .Clock ;
38
36
import java .util .Map ;
37
+ import java .util .UUID ;
39
38
39
+ import static org .assertj .core .api .Assertions .assertThat ;
40
40
import static org .eclipse .dataspaceconnector .junit .testfixtures .TestUtils .getResourceFileContentAsString ;
41
41
import static org .junit .jupiter .api .Assertions .assertEquals ;
42
42
import static org .junit .jupiter .api .Assertions .assertTrue ;
43
+ import static org .mockito .ArgumentMatchers .any ;
44
+ import static org .mockito .ArgumentMatchers .anyString ;
45
+ import static org .mockito .Mockito .mock ;
46
+ import static org .mockito .Mockito .when ;
43
47
44
48
/**
45
49
* Test the {@link DecentralizedIdentityService} with a key algorithm. See {@link WithP256Test} for concrete impl.
48
52
abstract class DecentralizedIdentityServiceTest {
49
53
private static final String DID_DOCUMENT = getResourceFileContentAsString ("dids.json" );
50
54
55
+ private JWK keyPair ;
56
+ private CredentialsVerifier credentialsVerifierMock ;
57
+ private DidResolverRegistry didResolverRegistryMock ;
51
58
private DecentralizedIdentityService identityService ;
52
59
53
60
@ Test
54
61
void generateAndVerifyJwtToken_valid () {
55
- var result = identityService . obtainClientCredentials ( TokenParameters . Builder . newInstance ()
56
- . scope ( "Foo" )
57
- . audience ( "Bar" )
58
- . build ());
62
+ when ( credentialsVerifierMock . getVerifiedCredentials ( any ())). thenReturn ( Result . success ( Map . of ( "region" , "eu" )));
63
+ when ( didResolverRegistryMock . resolve ( anyString ())). thenReturn ( Result . success ( createDidDocument (( ECKey ) keyPair . toPublicJWK ())));
64
+
65
+ var result = identityService . obtainClientCredentials ( defaultTokenParameters ());
59
66
assertTrue (result .succeeded ());
60
67
61
- Result < ClaimToken > verificationResult = identityService .verifyJwtToken (result .getContent (), "Bar" );
68
+ var verificationResult = identityService .verifyJwtToken (result .getContent (), "Bar" );
62
69
assertTrue (verificationResult .succeeded ());
63
70
assertEquals ("eu" , verificationResult .getContent ().getClaims ().get ("region" ));
64
71
}
65
72
73
+ @ Test
74
+ void generateAndVerifyJwtToken_wrongPublicKey () {
75
+ var otherKeyPair = getKeyPair ();
76
+ when (credentialsVerifierMock .getVerifiedCredentials (any ())).thenReturn (Result .success (Map .of ("region" , "eu" )));
77
+ when (didResolverRegistryMock .resolve (anyString ())).thenReturn (Result .success (createDidDocument ((ECKey ) otherKeyPair .toPublicJWK ())));
78
+
79
+ var result = identityService .obtainClientCredentials (defaultTokenParameters ());
80
+ assertTrue (result .succeeded ());
81
+
82
+ var verificationResult = identityService .verifyJwtToken (result .getContent (), "Bar" );
83
+ assertTrue (verificationResult .failed ());
84
+ assertThat (verificationResult .getFailureMessages ()).contains ("Token could not be verified!" );
85
+ }
86
+
66
87
@ Test
67
88
void generateAndVerifyJwtToken_wrongAudience () {
68
- var result = identityService .obtainClientCredentials (TokenParameters .Builder .newInstance ()
89
+ when (didResolverRegistryMock .resolve (anyString ())).thenReturn (Result .success (createDidDocument ((ECKey ) keyPair .toPublicJWK ())));
90
+
91
+ var result = identityService .obtainClientCredentials (defaultTokenParameters ());
92
+
93
+ var verificationResult = identityService .verifyJwtToken (result .getContent (), "Bar2" );
94
+ assertTrue (verificationResult .failed ());
95
+ }
96
+
97
+ @ Test
98
+ void generateAndVerifyJwtToken_getVerifiedCredentialsFailed () {
99
+ var errorMsg = UUID .randomUUID ().toString ();
100
+ when (credentialsVerifierMock .getVerifiedCredentials (any ())).thenReturn (Result .failure (errorMsg ));
101
+ when (didResolverRegistryMock .resolve (anyString ())).thenReturn (Result .success (createDidDocument ((ECKey ) keyPair .toPublicJWK ())));
102
+
103
+ var result = identityService .obtainClientCredentials (defaultTokenParameters ());
104
+ assertTrue (result .succeeded ());
105
+
106
+ var verificationResult = identityService .verifyJwtToken (result .getContent (), "Bar" );
107
+ assertTrue (verificationResult .failed ());
108
+ assertThat (verificationResult .getFailureDetail ()).contains (errorMsg );
109
+ }
110
+
111
+ private static TokenParameters defaultTokenParameters () {
112
+ return TokenParameters .Builder .newInstance ()
69
113
.scope ("Foo" )
70
114
.audience ("Bar" )
71
- .build ());
115
+ .build ();
116
+ }
72
117
73
- Result <ClaimToken > verificationResult = identityService .verifyJwtToken (result .getContent (), "Bar2" );
74
- assertTrue (verificationResult .failed ());
118
+ private static DidDocument createDidDocument (ECKey publicKey ) {
119
+ try {
120
+ var did = new ObjectMapper ().readValue (DID_DOCUMENT , DidDocument .class );
121
+ did .getVerificationMethod ().add (VerificationMethod .Builder .create ()
122
+ .type ("JsonWebKey2020" )
123
+ .id ("test-key" )
124
+ .publicKeyJwk (new EllipticCurvePublicKey (publicKey .getCurve ().getName (), publicKey .getKeyType ().toString (), publicKey .getX ().toString (), publicKey .getY ().toString ()))
125
+ .build ());
126
+ return did ;
127
+ } catch (JsonProcessingException e ) {
128
+ throw new AssertionError (e );
129
+ }
75
130
}
76
131
77
132
@ BeforeEach
78
133
void setUp () {
79
- var keyPair = getKeyPair ();
134
+ keyPair = getKeyPair ();
80
135
var privateKey = new EcPrivateKeyWrapper (keyPair .toECKey ());
81
-
82
- var didResolver = new TestResolverRegistry (DID_DOCUMENT , keyPair );
83
- CredentialsVerifier verifier = document -> Result .success (Map .of ("region" , "eu" ));
84
- String didUrl = "random.did.url" ;
85
- identityService = new DecentralizedIdentityService (didResolver , verifier , new ConsoleMonitor (), privateKey , didUrl , Clock .systemUTC ());
136
+ didResolverRegistryMock = mock (DidResolverRegistry .class );
137
+ credentialsVerifierMock = mock (CredentialsVerifier .class );
138
+ var didUrl = "random.did.url" ;
139
+ identityService = new DecentralizedIdentityService (didResolverRegistryMock , credentialsVerifierMock , new ConsoleMonitor (), privateKey , didUrl , Clock .systemUTC ());
86
140
}
87
141
88
142
@ NotNull
@@ -95,36 +149,4 @@ public static class WithP256Test extends DecentralizedIdentityServiceTest {
95
149
}
96
150
97
151
}
98
-
99
- private static class TestResolverRegistry implements DidResolverRegistry {
100
- private final String hubUrlDid ;
101
- private final JWK keyPair ;
102
-
103
- TestResolverRegistry (String hubUrlDid , JWK keyPair ) {
104
- this .hubUrlDid = hubUrlDid ;
105
- this .keyPair = keyPair ;
106
- }
107
-
108
- @ Override
109
- public void register (DidResolver resolver ) {
110
-
111
- }
112
-
113
- @ Override
114
- public Result <DidDocument > resolve (String didKey ) {
115
- try {
116
- var did = new ObjectMapper ().readValue (hubUrlDid , DidDocument .class );
117
- ECKey key = (ECKey ) keyPair .toPublicJWK ();
118
- did .getVerificationMethod ().add (VerificationMethod .Builder .create ()
119
- .type ("JsonWebKey2020" )
120
- .id ("test-key" )
121
- .publicKeyJwk (new EllipticCurvePublicKey (key .getCurve ().getName (), key .getKeyType ().toString (), key .getX ().toString (), key .getY ().toString ()))
122
- .build ());
123
- return Result .success (did );
124
- } catch (JsonProcessingException e ) {
125
- throw new AssertionError (e );
126
- }
127
- }
128
- }
129
-
130
152
}
0 commit comments