class AccessKeyAuthenticator implements Authenticator {

    @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");
        }
    }

}