Register and Enroll User with Test

Success to register and Enroll User using default Admin
This commit is contained in:
GME 2018-10-22 18:29:26 +02:00
parent c0d9b24d6b
commit 9422182dec
13 changed files with 119 additions and 3 deletions

Binary file not shown.

BIN
msp/Org1MSP/admin.context Normal file

Binary file not shown.

View file

@ -9,6 +9,7 @@ import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.hyperledger.fabric_ca.sdk.HFCAClient;
import org.hyperledger.fabric_ca.sdk.RegistrationRequest;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
@ -36,6 +37,7 @@ public class CAClientWrapper {
}
private void init() throws MalformedURLException, IllegalAccessException, InstantiationException, ClassNotFoundException, CryptoException, InvalidArgumentException, NoSuchMethodException, InvocationTargetException {
BasicConfigurator.configure();
CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();
hfcaClient = HFCAClient.createNewInstance(caUrl, properties);
hfcaClient.setCryptoSuite(cryptoSuite);
@ -65,5 +67,39 @@ public class CAClientWrapper {
return adminContext;
}
//Method to register an User
public String registerUser(String username, String organization) throws Exception {
UserContext userContext = Util.readUserContext(organization, username);
if (userContext!=null){
logger.warn("User already registered");
return null;
}
RegistrationRequest rr = new RegistrationRequest(username,organization);
logger.info("registrar is : "+adminContext.getName());
String enrollementSecret = hfcaClient.register(rr,adminContext);
logger.info("User registered");
return enrollementSecret;
}
//Method to enroll an User
public UserContext enrollUser(UserContext userContext, String secret) throws Exception{
UserContext uContext = Util.readUserContext(userContext.getAffiliation(),userContext.getName());
if(uContext!=null){
logger.warn("User already enrolled");
return uContext;
}
Enrollment enrollment = hfcaClient.enroll(userContext.getName(),secret);
userContext.setEnrollment(enrollment);
Util.writeUserContext(userContext);
logger.info("User enrolled");
return userContext;
}
//Method to revoke an User
public void revokeUser(UserContext admin, Enrollment userEnrollement, String reason) throws Exception{
hfcaClient.revoke(admin,userEnrollement,reason);
}
}

View file

@ -1,13 +1,17 @@
package blockchain.client;
import blockchain.configuration.Config;
import blockchain.user.UserContext;
import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.HFClient;
import org.hyperledger.fabric.sdk.Peer;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;
public class FabricClientWrapper {
private HFClient client;
@ -28,4 +32,11 @@ public class FabricClientWrapper {
ChannelClientWrapper channelClientWrapper = new ChannelClientWrapper(name, channel, this);
return channelClientWrapper;
}
public Set<String> queryForChannels() throws InvalidArgumentException, IllegalArgumentException, ProposalException {
client.newChannel(Config.CHANNEL_NAME);
client.newOrderer(Config.ORDERER_NAME,Config.ORDERER_URL);
Peer peer = client.newPeer(Config.ORG1_PEER_0,Config.ORG1_PEER_0_URL);
return client.queryChannels(peer);
}
}

View file

@ -4,7 +4,7 @@ public class Config {
public static final String ORG1_MSP = "Org1MSP";
public static final String ORG1 = "Org1";
public static final String ORG1 = "org1";
public static final String ADMIN = "admin";
@ -12,7 +12,7 @@ public class Config {
public static final String CA_ORG1_URL = "http://vps577432.ovh.net:7054";
public static final String ORDERER_URL = "http://vps577432.ovh.net:7050";
public static final String ORDERER_URL = "grpc://vps577432.ovh.net:7050";
public static final String ORDERER_NAME = "orderer.example.com";
@ -22,7 +22,9 @@ public class Config {
public static final String ORG1_PEER_0 = "peer0.org1.example.com";
public static final String ORG1_PEER_0_URL = "http://vps577432.ovh.net:7051";
public static final String ORG1_PEER_0_URL = "grpc://vps577432.ovh.net:7051";
public static final String USER_TEST_SECRET = "frwcKfCYPxWA";
}

View file

@ -0,0 +1,36 @@
package blockchain.client;
import blockchain.configuration.Config;
import blockchain.user.UserContext;
import blockchain.utility.Util;
import org.apache.log4j.Logger;
import org.junit.Test;
public class TestRegisterEnrollUser {
private static Logger logger = Logger.getLogger(TestRegisterEnrollUser.class);
//Success
@Test
public void RegisterEnrollUser(){
String caUrl = Config.CA_ORG1_URL;
try{
CAClientWrapper caClientWrapper = new CAClientWrapper(caUrl,null);
UserContext admin = Util.readUserContext(Config.ORG1,Config.ADMIN);
caClientWrapper.setAdminContext(admin);
UserContext userContext = new UserContext();
userContext.setName("UserJavaTest");
userContext.setAffiliation(Config.ORG1);
userContext.setMspId(Config.ORG1_MSP);
String userSecret = caClientWrapper.registerUser(userContext.getName(),userContext.getAffiliation());
logger.info("uSecret : "+userSecret);
userContext = caClientWrapper.enrollUser(userContext,userSecret);
logger.info("userContext enrollement : "+userContext.getEnrollment());
}catch (Exception e){
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,31 @@
package blockchain.query;
import blockchain.client.FabricClientWrapper;
import blockchain.configuration.Config;
import blockchain.user.UserContext;
import blockchain.utility.Util;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hyperledger.fabric.sdk.Peer;
import org.junit.Test;
import java.util.Set;
public class QueryTest {
private static Logger logger = Logger.getLogger(QueryTest.class);
//DON'T WORK
@Test
public void TestAQueryChannels() {
BasicConfigurator.configure();
UserContext admin = Util.readUserContext(Config.ORG1,Config.ADMIN);
try{
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(admin);
Set<String> channels = fabricClientWrapper.queryForChannels();
logger.info("Channels : "+channels);
}catch (Exception e){
e.printStackTrace();
}
}
}

Binary file not shown.