Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. package org.awjh;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.regex.Pattern;
  7. import java.util.regex.Matcher;
  8. import java.security.cert.CertificateException;
  9.  
  10. import java.io.ByteArrayInputStream;
  11. import java.io.UnsupportedEncodingException;
  12. import java.security.cert.CertificateFactory;
  13. import java.security.cert.X509Certificate;
  14.  
  15. import com.google.protobuf.InvalidProtocolBufferException;
  16.  
  17. import org.hyperledger.fabric.protos.msp.Identities.SerializedIdentity;
  18. import org.hyperledger.fabric.shim.ChaincodeStub;
  19. import org.json.JSONObject;
  20.  
  21. public class ClientIdentity {
  22. protected String mspId;
  23. protected X509Certificate cert;
  24. protected Map<String, String> attrs;
  25. protected String id;
  26.  
  27. public ClientIdentity(ChaincodeStub stub) throws InvalidProtocolBufferException, CertificateException, UnsupportedEncodingException {
  28. final byte[] signingId = stub.getCreator();
  29.  
  30. SerializedIdentity si = SerializedIdentity.parseFrom(signingId);
  31. this.mspId = si.getMspid();
  32.  
  33. final byte[] idBytes = si.getIdBytes().toByteArray();
  34.  
  35. final X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X509").generateCertificate(new ByteArrayInputStream(idBytes));
  36. this.cert = cert;
  37.  
  38. final byte[] extVal = cert.getExtensionValue("1.2.3.4.5.6.7.8.1");
  39.  
  40. this.attrs = new HashMap<String, String>();
  41.  
  42. if (extVal != null) {
  43. final String extStr = new String(extVal, "UTF-8");
  44. final Pattern pattern = Pattern.compile("\\{(.*)");
  45. final Matcher matcher = pattern.matcher(extStr);
  46.  
  47. if (matcher.find()) {
  48. final String attrJSONString = "{" + matcher.group(1);
  49. final JSONObject attrJSON = new JSONObject(attrJSONString);
  50.  
  51. final JSONObject attrs = attrJSON.getJSONObject("attrs");
  52.  
  53. Iterator<String> keys = attrs.keys();
  54.  
  55. Map<String, String> attrMap = new HashMap<String, String>();
  56.  
  57. while(keys.hasNext()) {
  58. String key = keys.next();
  59.  
  60. attrMap.put(key, attrs.getString(key));
  61. }
  62.  
  63. this.attrs = attrMap;
  64. }
  65. }
  66.  
  67. this.id = "x509::" + cert.getSubjectDN().getName() + "::" + cert.getIssuerDN().getName();
  68. }
  69.  
  70. public String getId() {
  71. return this.id;
  72. }
  73.  
  74. public String getMSPID() {
  75. return this.mspId;
  76. }
  77.  
  78. public String getAttributeValue(String attrName) {
  79. if (this.attrs.containsKey(attrName)) {
  80. return this.attrs.get(attrName);
  81. }
  82. return null;
  83. }
  84.  
  85. public boolean assertAttributeValue(String attrName, String attrValue) {
  86. if (!this.attrs.containsKey(attrName)) {
  87. return false;
  88. } else {
  89. return attrValue.equals(this.attrs.get(attrName));
  90. }
  91. }
  92.  
  93. public X509Certificate getX509Certificate() {
  94. return this.cert;
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement