Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 11 | #include "profile.hpp" |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 12 | #include "logging.h" |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 13 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 14 | namespace chronos{ |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 15 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 16 | using std::vector; |
| 17 | using std::string; |
| 18 | using std::map; |
| 19 | |
| 20 | using ndn::IdentityCertificate; |
| 21 | using ndn::CertificateSubjectDescription; |
| 22 | |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 23 | const std::string Profile::OID_NAME("2.5.4.41"); |
| 24 | const std::string Profile::OID_ORG("2.5.4.11"); |
| 25 | const std::string Profile::OID_GROUP("2.5.4.1"); |
| 26 | const std::string Profile::OID_HOMEPAGE("2.5.4.3"); |
| 27 | const std::string Profile::OID_ADVISOR("2.5.4.80"); |
| 28 | const std::string Profile::OID_EMAIL("1.2.840.113549.1.9.1"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 29 | |
| 30 | Profile::Profile(const IdentityCertificate& identityCertificate) |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 31 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 32 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(identityCertificate.getName()); |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 33 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 34 | m_entries[string("IDENTITY")] = keyName.getPrefix(-1).toUri(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 35 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 36 | const vector<CertificateSubjectDescription>& subList = |
| 37 | identityCertificate.getSubjectDescriptionList(); |
| 38 | |
| 39 | for (vector<CertificateSubjectDescription>::const_iterator it = subList.begin(); |
| 40 | it != subList.end(); it++) { |
| 41 | const string oidStr = it->getOidString(); |
| 42 | string valueStr = it->getValue(); |
| 43 | if (oidStr == OID_NAME) |
| 44 | m_entries["name"] = valueStr; |
| 45 | else if (oidStr == OID_ORG) |
| 46 | m_entries["institution"] = valueStr; |
| 47 | else if (oidStr == OID_GROUP) |
| 48 | m_entries["group"] = valueStr; |
| 49 | else if (oidStr == OID_HOMEPAGE) |
| 50 | m_entries["homepage"] = valueStr; |
| 51 | else if (oidStr == OID_ADVISOR) |
| 52 | m_entries["advisor"] = valueStr; |
| 53 | else if (oidStr == OID_EMAIL) |
| 54 | m_entries["email"] = valueStr; |
| 55 | else |
| 56 | m_entries[oidStr] = valueStr; |
| 57 | } |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 60 | Profile::Profile(const Name& identityName) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 61 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 62 | m_entries["IDENTITY"] = identityName.toUri(); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 63 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 64 | |
| 65 | Profile::Profile(const Name& identityName, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 66 | const string& name, |
| 67 | const string& institution) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 68 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 69 | m_entries["IDENTITY"] = identityName.toUri(); |
| 70 | m_entries["name"] = name; |
| 71 | m_entries["institution"] = institution; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | Profile::Profile(const Profile& profile) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 75 | : m_entries(profile.m_entries) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 76 | { |
| 77 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 78 | |
| 79 | void |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 80 | Profile::encode(std::ostream& os) const |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 81 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 82 | Chronos::ProfileMsg profileMsg; |
| 83 | profileMsg << (*this); |
| 84 | profileMsg.SerializeToOstream(&os); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 87 | void |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 88 | Profile::decode(std::istream& is) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 89 | { |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 90 | Chronos::ProfileMsg profileMsg; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 91 | profileMsg.ParseFromIstream(&is); |
| 92 | profileMsg >> (*this); |
| 93 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 94 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 95 | bool |
| 96 | Profile::operator==(const Profile& profile) const |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 97 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 98 | if (m_entries.size() != profile.m_entries.size()) |
| 99 | return false; |
| 100 | |
| 101 | for(map<string, string>::const_iterator it = m_entries.begin(); it != m_entries.end(); it++) { |
| 102 | map<string, string>::const_iterator found = profile.m_entries.find(it->first); |
| 103 | if (found == profile.m_entries.end()) |
| 104 | return false; |
| 105 | if (found->second != it->second) |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | bool |
| 113 | Profile::operator!=(const Profile& profile) const |
| 114 | { |
| 115 | return !(*this == profile); |
| 116 | } |
| 117 | |
| 118 | Chronos::ProfileMsg& |
| 119 | operator<<(Chronos::ProfileMsg& profileMsg, const Profile& profile) |
| 120 | { |
| 121 | ; |
| 122 | for (map<string, string>::const_iterator it = profile.begin(); it != profile.end(); it++) { |
| 123 | Chronos::ProfileMsg::ProfileEntry* profileEntry = profileMsg.add_entry(); |
| 124 | profileEntry->set_oid(it->first); |
| 125 | profileEntry->set_data(it->second); |
| 126 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 127 | return profileMsg; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 130 | Chronos::ProfileMsg& |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 131 | operator>>(Chronos::ProfileMsg& profileMsg, Profile& profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 132 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 133 | for (int i = 0; i < profileMsg.entry_size(); i++) { |
| 134 | const Chronos::ProfileMsg::ProfileEntry& profileEntry = profileMsg.entry(i); |
| 135 | profile[profileEntry.oid()] = profileEntry.data(); |
| 136 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 137 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 138 | return profileMsg; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 139 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 140 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame^] | 141 | } // namespace chronos |