use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.
the class AccessKeyAuthenticator method authenticate.
@Override
public void authenticate(Invocation invocation, URL url) throws RpcAuthenticationException {
String accessKeyId = String.valueOf(invocation.getAttachment(Constants.AK_KEY));
String requestTimestamp = String.valueOf(invocation.getAttachment(Constants.REQUEST_TIMESTAMP_KEY));
String originSignature = String.valueOf(invocation.getAttachment(Constants.REQUEST_SIGNATURE_KEY));
String consumer = String.valueOf(invocation.getAttachment(CommonConstants.CONSUMER));
if (StringUtils.isEmpty(accessKeyId) || StringUtils.isEmpty(consumer) || StringUtils.isEmpty(requestTimestamp) || StringUtils.isEmpty(originSignature)) {
throw new RpcAuthenticationException("Failed to authenticate, maybe consumer not enable the auth");
}
AccessKeyPair accessKeyPair = null;
try {
accessKeyPair = getAccessKeyPair(invocation, url);
} catch (Exception e) {
throw new RpcAuthenticationException("Failed to authenticate , can't load the accessKeyPair", e);
}
String computeSignature = getSignature(url, invocation, accessKeyPair.getSecretKey(), requestTimestamp);
boolean success = computeSignature.equals(originSignature);
if (!success) {
throw new RpcAuthenticationException("Failed to authenticate, signature is not correct");
}
}
use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.
the class AccessKeyAuthenticator method getAccessKeyPair.
AccessKeyPair getAccessKeyPair(Invocation invocation, URL url) {
AccessKeyStorage accessKeyStorage = ExtensionLoader.getExtensionLoader(AccessKeyStorage.class).getExtension(url.getParameter(Constants.ACCESS_KEY_STORAGE_KEY, Constants.DEFAULT_ACCESS_KEY_STORAGE));
AccessKeyPair accessKeyPair = null;
try {
accessKeyPair = accessKeyStorage.getAccessKey(url, invocation);
if (accessKeyPair == null || StringUtils.isEmpty(accessKeyPair.getAccessKey()) || StringUtils.isEmpty(accessKeyPair.getSecretKey())) {
throw new AccessKeyNotFoundException("AccessKeyId or secretAccessKey not found");
}
} catch (Exception e) {
throw new RuntimeException("Can't load the AccessKeyPair from accessKeyStorage", e);
}
return accessKeyPair;
}
use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.
the class AccessKeyAuthenticator method sign.
@Override
public void sign(Invocation invocation, URL url) {
String currentTime = String.valueOf(System.currentTimeMillis());
String consumer = url.getParameter(CommonConstants.APPLICATION_KEY);
AccessKeyPair accessKeyPair = getAccessKeyPair(invocation, url);
invocation.setAttachment(Constants.REQUEST_SIGNATURE_KEY, getSignature(url, invocation, accessKeyPair.getSecretKey(), currentTime));
invocation.setAttachment(Constants.REQUEST_TIMESTAMP_KEY, currentTime);
invocation.setAttachment(Constants.AK_KEY, accessKeyPair.getAccessKey());
invocation.setAttachment(CommonConstants.CONSUMER, consumer);
}
use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.
the class DefaultAccessKeyStorage method getAccessKey.
@Override
public AccessKeyPair getAccessKey(URL url, Invocation invocation) {
AccessKeyPair accessKeyPair = new AccessKeyPair();
String accessKeyId = url.getParameter(Constants.ACCESS_KEY_ID_KEY, Constants.DEFAULT_ACCESS_KEY_ID);
String secretAccessKey = url.getParameter(Constants.SECRET_ACCESS_KEY_KEY);
accessKeyPair.setAccessKey(accessKeyId);
accessKeyPair.setSecretKey(secretAccessKey);
return accessKeyPair;
}
use of org.apache.dubbo.auth.model.AccessKeyPair in project dubbo by alibaba.
the class DefaultAccessKeyStorageTest method testGetAccessKey.
@Test
void testGetAccessKey() {
URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk");
DefaultAccessKeyStorage defaultAccessKeyStorage = new DefaultAccessKeyStorage();
AccessKeyPair accessKey = defaultAccessKeyStorage.getAccessKey(url, mock(Invocation.class));
assertNotNull(accessKey);
assertEquals(accessKey.getAccessKey(), "ak");
assertEquals(accessKey.getSecretKey(), "sk");
}
Aggregations