blob: 40a7aee7b1e982360e27050bcd884af2af9ef26c [file] [log] [blame]
Yingdi Yub4be64a2013-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 Yu92e8e482013-10-17 21:13:03 -070012#include "logging.h"
Yingdi Yub4be64a2013-10-13 17:24:50 -070013
14using namespace std;
15using namespace ndn;
16
Yingdi Yu92e8e482013-10-17 21:13:03 -070017INIT_LOGGER("Profile");
Yingdi Yu2e3199c2013-11-06 18:42:34 -080018
Yingdi Yufa4ce792014-02-06 18:09:22 -080019namespace chronos{
Yingdi Yu2e3199c2013-11-06 18:42:34 -080020
Yingdi Yufa4ce792014-02-06 18:09:22 -080021const string Profile::OID_NAME("2.5.4.41");
22const string Profile::OID_ORG("2.5.4.11");
23const string Profile::OID_GROUP("2.5.4.1");
24const string Profile::OID_HOMEPAGE("2.5.4.3");
25const string Profile::OID_ADVISOR("2.5.4.80");
26const string Profile::OID_EMAIL("1.2.840.113549.1.9.1");
27
28Profile::Profile(const IdentityCertificate& identityCertificate)
Yingdi Yu2e3199c2013-11-06 18:42:34 -080029{
Yingdi Yufa4ce792014-02-06 18:09:22 -080030 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(identityCertificate.getName());
Yingdi Yu2e3199c2013-11-06 18:42:34 -080031
Yingdi Yufa4ce792014-02-06 18:09:22 -080032 m_entries[string("IDENTITY")] = keyName.getPrefix(-1).toUri();
Yingdi Yu2e3199c2013-11-06 18:42:34 -080033
Yingdi Yu76dd8002013-12-24 11:16:32 +080034 const vector<CertificateSubjectDescription>& subList = identityCertificate.getSubjectDescriptionList();
35 vector<CertificateSubjectDescription>::const_iterator it = subList.begin();
Yingdi Yu2e3199c2013-11-06 18:42:34 -080036 for(; it != subList.end(); it++)
37 {
Yingdi Yu76dd8002013-12-24 11:16:32 +080038 const string oidStr = it->getOidString();
39 string valueStr = it->getValue();
Yingdi Yufa4ce792014-02-06 18:09:22 -080040 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 Yu2e3199c2013-11-06 18:42:34 -080052 else
Yingdi Yu76dd8002013-12-24 11:16:32 +080053 m_entries[oidStr] = valueStr;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080054 }
55}
56
Yingdi Yub4be64a2013-10-13 17:24:50 -070057Profile::Profile(const Name& identityName)
Yingdi Yu3b318c12013-10-15 17:54:00 -070058{
Yingdi Yufa4ce792014-02-06 18:09:22 -080059 m_entries["IDENTITY"] = identityName.toUri();
Yingdi Yu3b318c12013-10-15 17:54:00 -070060}
Yingdi Yub4be64a2013-10-13 17:24:50 -070061
62Profile::Profile(const Name& identityName,
63 const string& name,
64 const string& institution)
Yingdi Yub4be64a2013-10-13 17:24:50 -070065{
Yingdi Yufa4ce792014-02-06 18:09:22 -080066 m_entries["IDENTITY"] = identityName.toUri();
67 m_entries["name"] = name;
68 m_entries["institution"] = institution;
Yingdi Yub4be64a2013-10-13 17:24:50 -070069}
70
71Profile::Profile(const Profile& profile)
Yingdi Yufa4ce792014-02-06 18:09:22 -080072 : m_entries(profile.m_entries)
Yingdi Yub4be64a2013-10-13 17:24:50 -070073{}
74
75void
Yingdi Yufa4ce792014-02-06 18:09:22 -080076Profile::encode(ostream& os) const
Yingdi Yub4be64a2013-10-13 17:24:50 -070077{
Yingdi Yufa4ce792014-02-06 18:09:22 -080078 Chronos::ProfileMsg profileMsg;
79 profileMsg << (*this);
80 profileMsg.SerializeToOstream(&os);
Yingdi Yub4be64a2013-10-13 17:24:50 -070081}
82
Yingdi Yu76dd8002013-12-24 11:16:32 +080083void
Yingdi Yufa4ce792014-02-06 18:09:22 -080084Profile::decode(istream& is)
Yingdi Yub4be64a2013-10-13 17:24:50 -070085{
Yingdi Yufa4ce792014-02-06 18:09:22 -080086 Chronos::ProfileMsg profileMsg;
87 profileMsg.ParseFromIstream(&is);
88 profileMsg >> (*this);
89}
Yingdi Yub4be64a2013-10-13 17:24:50 -070090
Yingdi Yufa4ce792014-02-06 18:09:22 -080091Chronos::ProfileMsg&
92operator << (Chronos::ProfileMsg& profileMsg, const Profile& profile)
93{
94 map<string, string>::const_iterator it = profile.begin();
95 for(; it != profile.end(); it++)
Yingdi Yub4be64a2013-10-13 17:24:50 -070096 {
Yingdi Yu76dd8002013-12-24 11:16:32 +080097 Chronos::ProfileMsg::ProfileEntry* profileEntry = profileMsg.add_entry();
98 profileEntry->set_oid(it->first);
99 profileEntry->set_data(it->second);
Yingdi Yub4be64a2013-10-13 17:24:50 -0700100 }
Yingdi Yufa4ce792014-02-06 18:09:22 -0800101 return profileMsg;
Yingdi Yub4be64a2013-10-13 17:24:50 -0700102}
103
Yingdi Yufa4ce792014-02-06 18:09:22 -0800104Chronos::ProfileMsg&
105operator >> (Chronos::ProfileMsg& profileMsg, Profile& profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700106{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800107 for(int i = 0; i < profileMsg.entry_size(); i++)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700108 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800109 const Chronos::ProfileMsg::ProfileEntry& profileEntry = profileMsg.entry(i);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800110 profile[profileEntry.oid()] = profileEntry.data();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700111 }
Yingdi Yufa4ce792014-02-06 18:09:22 -0800112
113 return profileMsg;
Yingdi Yub4be64a2013-10-13 17:24:50 -0700114}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800115
116}//chronos