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