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 | |
| 11 | #include "profile.h" |
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 | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 14 | using namespace ndn; |
| 15 | |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 16 | INIT_LOGGER("Profile"); |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 17 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 18 | namespace chronos{ |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 19 | |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 20 | const std::string Profile::OID_NAME("2.5.4.41"); |
| 21 | const std::string Profile::OID_ORG("2.5.4.11"); |
| 22 | const std::string Profile::OID_GROUP("2.5.4.1"); |
| 23 | const std::string Profile::OID_HOMEPAGE("2.5.4.3"); |
| 24 | const std::string Profile::OID_ADVISOR("2.5.4.80"); |
| 25 | 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] | 26 | |
| 27 | Profile::Profile(const IdentityCertificate& identityCertificate) |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 28 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 29 | Name keyName = IdentityCertificate::certificateNameToPublicKeyName(identityCertificate.getName()); |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 30 | |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 31 | m_entries[std::string("IDENTITY")] = keyName.getPrefix(-1).toUri(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 32 | |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 33 | const std::vector<CertificateSubjectDescription>& subList = identityCertificate.getSubjectDescriptionList(); |
| 34 | std::vector<CertificateSubjectDescription>::const_iterator it = subList.begin(); |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 35 | for(; it != subList.end(); it++) |
| 36 | { |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 37 | const std::string oidStr = it->getOidString(); |
| 38 | std::string valueStr = it->getValue(); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 39 | if(oidStr == OID_NAME) |
| 40 | m_entries["name"] = valueStr; |
| 41 | else if(oidStr == OID_ORG) |
| 42 | m_entries["institution"] = valueStr; |
| 43 | else if(oidStr == OID_GROUP) |
| 44 | m_entries["group"] = valueStr; |
| 45 | else if(oidStr == OID_HOMEPAGE) |
| 46 | m_entries["homepage"] = valueStr; |
| 47 | else if(oidStr == OID_ADVISOR) |
| 48 | m_entries["advisor"] = valueStr; |
| 49 | else if(oidStr == OID_EMAIL) |
| 50 | m_entries["email"] = valueStr; |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 51 | else |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 52 | m_entries[oidStr] = valueStr; |
Yingdi Yu | 2e3199c | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 56 | Profile::Profile(const Name& identityName) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 57 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 58 | m_entries["IDENTITY"] = identityName.toUri(); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 59 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 60 | |
| 61 | Profile::Profile(const Name& identityName, |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 62 | const std::string& name, |
| 63 | const std::string& institution) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 64 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 65 | m_entries["IDENTITY"] = identityName.toUri(); |
| 66 | m_entries["name"] = name; |
| 67 | m_entries["institution"] = institution; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | Profile::Profile(const Profile& profile) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 71 | : m_entries(profile.m_entries) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 72 | {} |
| 73 | |
| 74 | void |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 75 | Profile::encode(std::ostream& os) const |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 76 | { |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 77 | Chronos::ProfileMsg profileMsg; |
| 78 | profileMsg << (*this); |
| 79 | profileMsg.SerializeToOstream(&os); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 82 | void |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 83 | Profile::decode(std::istream& is) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 84 | { |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 85 | Chronos::ProfileMsg profileMsg; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 86 | profileMsg.ParseFromIstream(&is); |
| 87 | profileMsg >> (*this); |
| 88 | } |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 89 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 90 | Chronos::ProfileMsg& |
| 91 | operator << (Chronos::ProfileMsg& profileMsg, const Profile& profile) |
| 92 | { |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 93 | std::map<std::string, std::string>::const_iterator it = profile.begin(); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 94 | for(; it != profile.end(); it++) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 95 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 96 | Chronos::ProfileMsg::ProfileEntry* profileEntry = profileMsg.add_entry(); |
| 97 | profileEntry->set_oid(it->first); |
| 98 | profileEntry->set_data(it->second); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 99 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 100 | return profileMsg; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 103 | Chronos::ProfileMsg& |
| 104 | operator >> (Chronos::ProfileMsg& profileMsg, Profile& profile) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 105 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 106 | for(int i = 0; i < profileMsg.entry_size(); i++) |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 107 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 108 | const Chronos::ProfileMsg::ProfileEntry& profileEntry = profileMsg.entry(i); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 109 | profile[profileEntry.oid()] = profileEntry.data(); |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 110 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 111 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 112 | return profileMsg; |
Yingdi Yu | b4be64a | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 113 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 114 | |
| 115 | }//chronos |