Yingdi Yu | 5ff6210 | 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 | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 12 | #include "logging.h" |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 13 | |
| 14 | using namespace std; |
| 15 | using namespace ndn; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 16 | using namespace ndn::ptr_lib; |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 17 | |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 18 | INIT_LOGGER("Profile"); |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 19 | |
| 20 | static string nameOid("2.5.4.41"); |
| 21 | static string orgOid("2.5.4.11"); |
| 22 | static string groupOid("2.5.4.1"); |
| 23 | static string homepageOid("2.5.4.3"); |
| 24 | static string advisor("2.5.4.80"); |
| 25 | static string emailOid("1.2.840.113549.1.9.1"); |
| 26 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 27 | Profile::Profile(const IdentityCertificate& oldIdentityCertificate) |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 28 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 29 | IdentityCertificate identityCertificate(oldIdentityCertificate); |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 30 | |
| 31 | Name keyName = identityCertificate.getPublicKeyName(); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 32 | m_identityName = keyName.getPrefix(-1); |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 33 | |
| 34 | const string& identityString = m_identityName.toUri(); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 35 | m_entries[string("IDENTITY")] = identityString; |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 36 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 37 | const vector<CertificateSubjectDescription>& subList = identityCertificate.getSubjectDescriptionList(); |
| 38 | vector<CertificateSubjectDescription>::const_iterator it = subList.begin(); |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 39 | for(; it != subList.end(); it++) |
| 40 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 41 | const string oidStr = it->getOidString(); |
| 42 | string valueStr = it->getValue(); |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 43 | if(oidStr == nameOid) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 44 | m_entries[string("name")] = valueStr; |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 45 | else if(oidStr == orgOid) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 46 | m_entries[string("institution")] = valueStr; |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 47 | else if(oidStr == groupOid) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 48 | m_entries[string("group")] = valueStr; |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 49 | else if(oidStr == homepageOid) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 50 | m_entries[string("homepage")] = valueStr; |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 51 | else if(oidStr == advisor) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 52 | m_entries[string("advisor")] = valueStr; |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 53 | else if(oidStr == emailOid) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 54 | m_entries[string("email")] = valueStr; |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 55 | else |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 56 | m_entries[oidStr] = valueStr; |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 60 | Profile::Profile(const Name& identityName) |
| 61 | : m_identityName(identityName) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 62 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 63 | const string& identityString = identityName.toUri(); |
| 64 | m_entries[string("IDENTITY")] = identityString; |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 65 | } |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 66 | |
| 67 | Profile::Profile(const Name& identityName, |
| 68 | const string& name, |
| 69 | const string& institution) |
| 70 | : m_identityName(identityName) |
| 71 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 72 | const string& identityString = identityName.toUri(); |
| 73 | m_entries[string("IDENTITY")] = identityString; |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 74 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 75 | m_entries[string("name")] = name; |
| 76 | m_entries[string("institution")] = institution; |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | Profile::Profile(const Profile& profile) |
| 80 | : m_identityName(profile.m_identityName) |
| 81 | , m_entries(profile.m_entries) |
| 82 | {} |
| 83 | |
| 84 | void |
| 85 | Profile::setProfileEntry(const string& profileType, |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 86 | const string& profileValue) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 87 | { m_entries[profileType] = profileValue; } |
| 88 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 89 | string |
Yingdi Yu | 53b8a9c | 2013-10-14 09:36:31 -0700 | [diff] [blame] | 90 | Profile::getProfileEntry(const string& profileType) const |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 91 | { |
| 92 | if(m_entries.find(profileType) != m_entries.end()) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 93 | return m_entries.at(profileType); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 94 | |
Yingdi Yu | eaa84e2 | 2014-01-16 10:30:26 -0800 | [diff] [blame] | 95 | return string(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 98 | void |
| 99 | Profile::encode(string* output) const |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 100 | { |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 101 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 102 | Chronos::ProfileMsg profileMsg; |
| 103 | |
| 104 | profileMsg.set_identityname(m_identityName.toUri()); |
| 105 | |
| 106 | map<string, string>::const_iterator it = m_entries.begin(); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 107 | for(; it != m_entries.end(); it++) |
| 108 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 109 | Chronos::ProfileMsg::ProfileEntry* profileEntry = profileMsg.add_entry(); |
| 110 | profileEntry->set_oid(it->first); |
| 111 | profileEntry->set_data(it->second); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 112 | } |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 113 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 114 | profileMsg.SerializeToString(output); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 117 | shared_ptr<Profile> |
| 118 | Profile::decode(const string& input) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 119 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 120 | Chronos::ProfileMsg profileMsg; |
| 121 | |
| 122 | profileMsg.ParseFromString(input); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 123 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 124 | shared_ptr<Profile> profile = make_shared<Profile>(profileMsg.identityname()); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 125 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 126 | for(int i = 0; i < profileMsg.entry_size(); i++) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 127 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 128 | const Chronos::ProfileMsg::ProfileEntry& profileEntry = profileMsg.entry(i); |
| 129 | profile->setProfileEntry(profileEntry.oid(), profileEntry.data()); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | return profile; |
| 133 | } |