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