blob: d662a70203a2f1476f0b68b16fa16ea0fbaf9fc4 [file] [log] [blame]
Yingdi Yu5ff62102013-10-13 17:24:50 -07001/* -*- 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 Yu68aced92013-10-17 21:13:03 -070012#include "logging.h"
Yingdi Yu5ff62102013-10-13 17:24:50 -070013
14using namespace std;
15using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080016using namespace ndn::ptr_lib;
Yingdi Yu5ff62102013-10-13 17:24:50 -070017
Yingdi Yu68aced92013-10-17 21:13:03 -070018INIT_LOGGER("Profile");
Yingdi Yue9ea5c92013-11-06 18:42:34 -080019
20static string nameOid("2.5.4.41");
21static string orgOid("2.5.4.11");
22static string groupOid("2.5.4.1");
23static string homepageOid("2.5.4.3");
24static string advisor("2.5.4.80");
25static string emailOid("1.2.840.113549.1.9.1");
26
Yingdi Yu64206112013-12-24 11:16:32 +080027Profile::Profile(const IdentityCertificate& oldIdentityCertificate)
Yingdi Yue9ea5c92013-11-06 18:42:34 -080028{
Yingdi Yu64206112013-12-24 11:16:32 +080029 IdentityCertificate identityCertificate(oldIdentityCertificate);
Yingdi Yue9ea5c92013-11-06 18:42:34 -080030
31 Name keyName = identityCertificate.getPublicKeyName();
Yingdi Yu64206112013-12-24 11:16:32 +080032 m_identityName = keyName.getPrefix(-1);
Yingdi Yue9ea5c92013-11-06 18:42:34 -080033
34 const string& identityString = m_identityName.toUri();
Yingdi Yu64206112013-12-24 11:16:32 +080035 m_entries[string("IDENTITY")] = identityString;
Yingdi Yue9ea5c92013-11-06 18:42:34 -080036
Yingdi Yu64206112013-12-24 11:16:32 +080037 const vector<CertificateSubjectDescription>& subList = identityCertificate.getSubjectDescriptionList();
38 vector<CertificateSubjectDescription>::const_iterator it = subList.begin();
Yingdi Yue9ea5c92013-11-06 18:42:34 -080039 for(; it != subList.end(); it++)
40 {
Yingdi Yu64206112013-12-24 11:16:32 +080041 const string oidStr = it->getOidString();
42 string valueStr = it->getValue();
Yingdi Yue9ea5c92013-11-06 18:42:34 -080043 if(oidStr == nameOid)
Yingdi Yu64206112013-12-24 11:16:32 +080044 m_entries[string("name")] = valueStr;
Yingdi Yue9ea5c92013-11-06 18:42:34 -080045 else if(oidStr == orgOid)
Yingdi Yu64206112013-12-24 11:16:32 +080046 m_entries[string("institution")] = valueStr;
Yingdi Yue9ea5c92013-11-06 18:42:34 -080047 else if(oidStr == groupOid)
Yingdi Yu64206112013-12-24 11:16:32 +080048 m_entries[string("group")] = valueStr;
Yingdi Yue9ea5c92013-11-06 18:42:34 -080049 else if(oidStr == homepageOid)
Yingdi Yu64206112013-12-24 11:16:32 +080050 m_entries[string("homepage")] = valueStr;
Yingdi Yue9ea5c92013-11-06 18:42:34 -080051 else if(oidStr == advisor)
Yingdi Yu64206112013-12-24 11:16:32 +080052 m_entries[string("advisor")] = valueStr;
Yingdi Yue9ea5c92013-11-06 18:42:34 -080053 else if(oidStr == emailOid)
Yingdi Yu64206112013-12-24 11:16:32 +080054 m_entries[string("email")] = valueStr;
Yingdi Yue9ea5c92013-11-06 18:42:34 -080055 else
Yingdi Yu64206112013-12-24 11:16:32 +080056 m_entries[oidStr] = valueStr;
Yingdi Yue9ea5c92013-11-06 18:42:34 -080057 }
58}
59
Yingdi Yu5ff62102013-10-13 17:24:50 -070060Profile::Profile(const Name& identityName)
61 : m_identityName(identityName)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070062{
Yingdi Yu64206112013-12-24 11:16:32 +080063 const string& identityString = identityName.toUri();
64 m_entries[string("IDENTITY")] = identityString;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070065}
Yingdi Yu5ff62102013-10-13 17:24:50 -070066
67Profile::Profile(const Name& identityName,
68 const string& name,
69 const string& institution)
70 : m_identityName(identityName)
71{
Yingdi Yu64206112013-12-24 11:16:32 +080072 const string& identityString = identityName.toUri();
73 m_entries[string("IDENTITY")] = identityString;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070074
Yingdi Yu64206112013-12-24 11:16:32 +080075 m_entries[string("name")] = name;
76 m_entries[string("institution")] = institution;
Yingdi Yu5ff62102013-10-13 17:24:50 -070077}
78
79Profile::Profile(const Profile& profile)
80 : m_identityName(profile.m_identityName)
81 , m_entries(profile.m_entries)
82{}
83
84void
85Profile::setProfileEntry(const string& profileType,
Yingdi Yu64206112013-12-24 11:16:32 +080086 const string& profileValue)
Yingdi Yu5ff62102013-10-13 17:24:50 -070087{ m_entries[profileType] = profileValue; }
88
Yingdi Yu64206112013-12-24 11:16:32 +080089string
Yingdi Yu53b8a9c2013-10-14 09:36:31 -070090Profile::getProfileEntry(const string& profileType) const
Yingdi Yu5ff62102013-10-13 17:24:50 -070091{
92 if(m_entries.find(profileType) != m_entries.end())
Yingdi Yu64206112013-12-24 11:16:32 +080093 return m_entries.at(profileType);
Yingdi Yu5ff62102013-10-13 17:24:50 -070094
Yingdi Yueaa84e22014-01-16 10:30:26 -080095 return string();
Yingdi Yu5ff62102013-10-13 17:24:50 -070096}
97
Yingdi Yu64206112013-12-24 11:16:32 +080098void
99Profile::encode(string* output) const
Yingdi Yu5ff62102013-10-13 17:24:50 -0700100{
Yingdi Yu5ff62102013-10-13 17:24:50 -0700101
Yingdi Yu64206112013-12-24 11:16:32 +0800102 Chronos::ProfileMsg profileMsg;
103
104 profileMsg.set_identityname(m_identityName.toUri());
105
106 map<string, string>::const_iterator it = m_entries.begin();
Yingdi Yu5ff62102013-10-13 17:24:50 -0700107 for(; it != m_entries.end(); it++)
108 {
Yingdi Yu64206112013-12-24 11:16:32 +0800109 Chronos::ProfileMsg::ProfileEntry* profileEntry = profileMsg.add_entry();
110 profileEntry->set_oid(it->first);
111 profileEntry->set_data(it->second);
Yingdi Yu5ff62102013-10-13 17:24:50 -0700112 }
Yingdi Yu5ff62102013-10-13 17:24:50 -0700113
Yingdi Yu64206112013-12-24 11:16:32 +0800114 profileMsg.SerializeToString(output);
Yingdi Yu5ff62102013-10-13 17:24:50 -0700115}
116
Yingdi Yu64206112013-12-24 11:16:32 +0800117shared_ptr<Profile>
118Profile::decode(const string& input)
Yingdi Yu5ff62102013-10-13 17:24:50 -0700119{
Yingdi Yu64206112013-12-24 11:16:32 +0800120 Chronos::ProfileMsg profileMsg;
121
122 profileMsg.ParseFromString(input);
Yingdi Yu5ff62102013-10-13 17:24:50 -0700123
Yingdi Yu64206112013-12-24 11:16:32 +0800124 shared_ptr<Profile> profile = make_shared<Profile>(profileMsg.identityname());
Yingdi Yu5ff62102013-10-13 17:24:50 -0700125
Yingdi Yu64206112013-12-24 11:16:32 +0800126 for(int i = 0; i < profileMsg.entry_size(); i++)
Yingdi Yu5ff62102013-10-13 17:24:50 -0700127 {
Yingdi Yu64206112013-12-24 11:16:32 +0800128 const Chronos::ProfileMsg::ProfileEntry& profileEntry = profileMsg.entry(i);
129 profile->setProfileEntry(profileEntry.oid(), profileEntry.data());
Yingdi Yu5ff62102013-10-13 17:24:50 -0700130 }
131
132 return profile;
133}