use of com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential in project jdchain-core by blockchain-jd-com.
the class BftsmartClientAuthencationService method authencateIncoming.
@Override
public BftsmartClientIncomingSettings authencateIncoming(ClientCredential clientCredential) {
if (!verify(clientCredential)) {
return null;
}
BftsmartTopology topology = nodeServer.getTopology();
if (topology == null) {
throw new IllegalStateException("Topology of node[" + nodeServer.getId() + "] still not created !!!");
}
BftsmartClientIncomingConfig clientIncomingSettings = new BftsmartClientIncomingConfig();
clientIncomingSettings.setTopology(BinarySerializeUtils.serialize(topology));
clientIncomingSettings.setTomConfig(BinarySerializeUtils.serialize(nodeServer.getTomConfig()));
clientIncomingSettings.setViewSettings(nodeServer.getConsensusSetting());
clientIncomingSettings.setPubKey(clientCredential.getPubKey());
BftsmartSessionCredential sessionCredential = (BftsmartSessionCredential) clientCredential.getSessionCredential();
// 如果历史会话凭证的客户端ID是小于全局的最小客户端ID,则是无效的客户端ID,对其重新分配;
// 注:忽略历史会话凭证的客户端ID不属于当前节点的分配空间的情形,此种情形是由于该客户端是从其它共识节点重定向过来的,
// 应该继续维持该客户端的 ID 复用;
int clientId = sessionCredential.getClientId();
int clientIdRange = sessionCredential.getClientIdRange();
if (clientIdRange < 1 || clientIdRange > POOL_SIZE_PEER_CLIENT) {
clientIdRange = POOL_SIZE_PEER_CLIENT;
}
if (clientId < GLOBAL_MIN_CLIENT_ID) {
// 重新分配
clientId = allocateClientId(clientIdRange);
}
sessionCredential = new BftsmartSessionCredentialConfig(clientId, clientIdRange, System.currentTimeMillis());
clientIncomingSettings.setSessionCredential(sessionCredential);
return clientIncomingSettings;
}
use of com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential in project jdchain-core by blockchain-jd-com.
the class BftsmartConsensusClientFactory method buildCredential.
@Override
public BftsmartClientAuthCredit buildCredential(SessionCredential sessionCredential, AsymmetricKeypair clientKeyPair, X509Certificate gatewayCertificate) {
if (sessionCredential == null) {
sessionCredential = BftsmartSessionCredentialConfig.createEmptyCredential();
} else if (!(sessionCredential instanceof BftsmartSessionCredential)) {
throw new IllegalArgumentException("Illegal credential info type! Requrie [" + BftsmartSessionCredential.class.getName() + "] but it is [" + sessionCredential.getClass().getName() + "]!");
}
PubKey pubKey = clientKeyPair.getPubKey();
PrivKey privKey = clientKeyPair.getPrivKey();
SignatureFunction signatureFunction = Crypto.getSignatureFunction(pubKey.getAlgorithm());
byte[] credentialBytes = BinaryProtocol.encode(sessionCredential, BftsmartSessionCredential.class);
SignatureDigest signatureDigest = signatureFunction.sign(privKey, credentialBytes);
BftsmartClientAuthCredit bftsmartClientAuthCredential = new BftsmartClientAuthCredit();
bftsmartClientAuthCredential.setSessionCredential((BftsmartSessionCredential) sessionCredential);
bftsmartClientAuthCredential.setPubKey(pubKey);
bftsmartClientAuthCredential.setSignatureDigest(signatureDigest);
bftsmartClientAuthCredential.setCertificate(null != gatewayCertificate ? CertificateUtils.toPEMString(gatewayCertificate) : null);
return bftsmartClientAuthCredential;
}
use of com.jd.blockchain.consensus.bftsmart.BftsmartSessionCredential in project jdchain-core by blockchain-jd-com.
the class GatewayConsensusClientManager method isCredentialUpated.
private boolean isCredentialUpated(ConsensusClient client, SessionCredential sessionCredential) {
if (client instanceof BftsmartConsensusClient && sessionCredential instanceof BftsmartSessionCredential) {
BftsmartConsensusClient bftsmartClient = (BftsmartConsensusClient) client;
BftsmartSessionCredential newCredential = (BftsmartSessionCredential) sessionCredential;
BftsmartSessionCredential oldCredential = bftsmartClient.getSettings().getSessionCredential();
// clientId 和 clientIdRange 任何一个有差异,都表示凭证已更新;
return oldCredential.getClientId() != newCredential.getClientId() || oldCredential.getClientIdRange() != newCredential.getClientIdRange();
} else if (client instanceof RaftConsensusClient) {
// return !((RaftConsensusClient)client).isInit();
return !((RaftConsensusClient) client).isConnected();
}
return true;
}
Aggregations