Search in sources :

Example 6 with AccessKeyPair

use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.

the class AccessKeyAuthenticatorTest method testSignForRequest.

@Test
void testSignForRequest() {
    URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(CommonConstants.APPLICATION_KEY, "test").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk");
    Invocation invocation = new RpcInvocation();
    AccessKeyAuthenticator helper = mock(AccessKeyAuthenticator.class);
    doCallRealMethod().when(helper).sign(invocation, url);
    when(helper.getSignature(eq(url), eq(invocation), eq("sk"), anyString())).thenReturn("dubbo");
    AccessKeyPair accessKeyPair = mock(AccessKeyPair.class);
    when(accessKeyPair.getSecretKey()).thenReturn("sk");
    when(helper.getAccessKeyPair(invocation, url)).thenReturn(accessKeyPair);
    helper.sign(invocation, url);
    assertEquals(String.valueOf(invocation.getAttachment(CommonConstants.CONSUMER)), url.getParameter(CommonConstants.APPLICATION_KEY));
    assertNotNull(invocation.getAttachments().get(Constants.REQUEST_SIGNATURE_KEY));
    assertEquals(invocation.getAttachments().get(Constants.REQUEST_SIGNATURE_KEY), "dubbo");
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) AccessKeyPair(org.apache.dubbo.auth.model.AccessKeyPair) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 7 with AccessKeyPair

use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.

the class AccessKeyAuthenticatorTest method testAuthenticateRequest.

@Test
void testAuthenticateRequest() throws RpcAuthenticationException {
    URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(CommonConstants.APPLICATION_KEY, "test").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk");
    Invocation invocation = new RpcInvocation();
    invocation.setAttachment(Constants.ACCESS_KEY_ID_KEY, "ak");
    invocation.setAttachment(Constants.REQUEST_SIGNATURE_KEY, "dubbo");
    invocation.setAttachment(Constants.REQUEST_TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
    invocation.setAttachment(CommonConstants.CONSUMER, "test");
    AccessKeyAuthenticator helper = mock(AccessKeyAuthenticator.class);
    doCallRealMethod().when(helper).authenticate(invocation, url);
    when(helper.getSignature(eq(url), eq(invocation), eq("sk"), anyString())).thenReturn("dubbo");
    AccessKeyPair accessKeyPair = mock(AccessKeyPair.class);
    when(accessKeyPair.getSecretKey()).thenReturn("sk");
    when(helper.getAccessKeyPair(invocation, url)).thenReturn(accessKeyPair);
    assertDoesNotThrow(() -> helper.authenticate(invocation, url));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) AccessKeyPair(org.apache.dubbo.auth.model.AccessKeyPair) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

AccessKeyPair (org.apache.dubbo.auth.model.AccessKeyPair)7 URL (org.apache.dubbo.common.URL)3 Invocation (org.apache.dubbo.rpc.Invocation)3 Test (org.junit.jupiter.api.Test)3 AccessKeyNotFoundException (org.apache.dubbo.auth.exception.AccessKeyNotFoundException)2 RpcAuthenticationException (org.apache.dubbo.auth.exception.RpcAuthenticationException)2 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)2 AccessKeyStorage (org.apache.dubbo.auth.spi.AccessKeyStorage)1