blob: 8583d167f69ba0272da9d68d5b6d46871072c156 [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -08001/* -*- 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
Yingdi Yueb692ac2015-02-10 18:46:18 -080011#ifndef CHRONOCHAT_CONTACT_HPP
12#define CHRONOCHAT_CONTACT_HPP
Yingdi Yu348f5ea2014-03-01 14:47:25 -080013
Yingdi Yu0b0a7362014-08-05 16:31:30 -070014#include "common.hpp"
Yingdi Yufa0b6a02014-04-30 14:26:42 -070015#include <ndn-cxx/security/identity-certificate.hpp>
16#include <ndn-cxx/util/regex.hpp>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070017#include "endorse-certificate.hpp"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080018
Yingdi Yueb692ac2015-02-10 18:46:18 -080019namespace chronochat {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080020
21class Contact
22{
23public:
Yingdi Yu0b0a7362014-08-05 16:31:30 -070024 typedef std::map<Name, shared_ptr<ndn::Regex> >::const_iterator const_iterator;
25 typedef std::map<Name, shared_ptr<ndn::Regex> >::iterator iterator;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080026
27 Contact(const ndn::IdentityCertificate& identityCertificate,
28 bool isIntroducer = false,
29 const std::string& alias = "")
30 : m_notBefore(identityCertificate.getNotBefore())
31 , m_notAfter(identityCertificate.getNotAfter())
32 , m_isIntroducer(isIntroducer)
33 , m_profile(identityCertificate)
34 {
35 m_name = m_profile.get("name");
36 m_alias = alias.empty() ? m_name : alias;
37 m_institution = m_profile.get("institution");
Yingdi Yufa0b6a02014-04-30 14:26:42 -070038
Yingdi Yu348f5ea2014-03-01 14:47:25 -080039 m_keyName = identityCertificate.getPublicKeyName();
40 m_namespace = m_keyName.getPrefix(-1);
41 m_publicKey = identityCertificate.getPublicKeyInfo();
42 }
43
44 Contact(const EndorseCertificate& endorseCertificate,
45 bool isIntroducer = false,
46 const std::string& alias = "")
47 : m_notBefore(endorseCertificate.getNotBefore())
48 , m_notAfter(endorseCertificate.getNotAfter())
49 , m_isIntroducer(isIntroducer)
Yingdi Yufa0b6a02014-04-30 14:26:42 -070050 {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080051 m_profile = endorseCertificate.getProfile();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070052
Yingdi Yu348f5ea2014-03-01 14:47:25 -080053 m_name = m_profile.get("name");
54 m_alias = alias.empty() ? m_name : alias;
55 m_institution = m_profile.get("institution");
Yingdi Yufa0b6a02014-04-30 14:26:42 -070056
Yingdi Yu348f5ea2014-03-01 14:47:25 -080057 m_keyName = endorseCertificate.getPublicKeyName();;
58 m_namespace = m_keyName.getPrefix(-1);
59 m_publicKey = endorseCertificate.getPublicKeyInfo();
60 }
61
Yingdi Yu0b0a7362014-08-05 16:31:30 -070062 Contact(const Name& identity,
Yingdi Yu348f5ea2014-03-01 14:47:25 -080063 const std::string& alias,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070064 const Name& keyName,
65 const time::system_clock::TimePoint& notBefore,
66 const time::system_clock::TimePoint& notAfter,
Yingdi Yu348f5ea2014-03-01 14:47:25 -080067 const ndn::PublicKey& key,
68 bool isIntroducer)
69 : m_namespace(identity)
70 , m_alias(alias)
71 , m_keyName(keyName)
72 , m_publicKey(key)
73 , m_notBefore(notBefore)
74 , m_notAfter(notAfter)
75 , m_isIntroducer(isIntroducer)
76 {
77 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -070078
Yingdi Yu348f5ea2014-03-01 14:47:25 -080079 Contact(const Contact& contact)
80 : m_namespace(contact.m_namespace)
81 , m_alias(contact.m_alias)
82 , m_name(contact.m_name)
83 , m_institution(contact.m_institution)
84 , m_keyName(contact.m_keyName)
85 , m_publicKey(contact.m_publicKey)
86 , m_notBefore(contact.m_notBefore)
87 , m_notAfter(contact.m_notAfter)
88 , m_isIntroducer(contact.m_isIntroducer)
89 , m_profile(contact.m_profile)
90 , m_trustScope(contact.m_trustScope)
Yingdi Yu0b0a7362014-08-05 16:31:30 -070091 {
92 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -070093
Yingdi Yu348f5ea2014-03-01 14:47:25 -080094 virtual
95 ~Contact()
Yingdi Yu0b0a7362014-08-05 16:31:30 -070096 {
97 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -080098
Yingdi Yu0b0a7362014-08-05 16:31:30 -070099 const Name&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800100 getNameSpace() const
101 {
102 return m_namespace;
103 }
104
105 const std::string&
106 getAlias() const
107 {
108 return m_alias;
109 }
110
111 const std::string&
112 getName() const
113 {
114 return m_name;
115 }
116
117 const std::string&
118 getInstitution() const
119 {
120 return m_institution;
121 }
122
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700123 const Name&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800124 getPublicKeyName() const
125 {
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700126 return m_keyName;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800127 }
128
129 const ndn::PublicKey&
130 getPublicKey() const
131 {
132 return m_publicKey;
133 }
134
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700135 const time::system_clock::TimePoint&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800136 getNotBefore() const
137 {
138 return m_notBefore;
139 }
140
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700141 const time::system_clock::TimePoint&
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800142 getNotAfter() const
143 {
144 return m_notAfter;
145 }
146
147 const Profile&
148 getProfile() const
149 {
150 return m_profile;
151 }
152
153 void
154 setProfile(const Profile& profile)
155 {
156 m_name = profile.get("name");
157 m_institution = profile.get("institution");
158 m_profile = profile;
159 }
160
161 bool
162 isIntroducer() const
163 {
164 return m_isIntroducer;
165 }
166
167 void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700168 setIsIntroducer(bool isIntroducer)
169 {
170 m_isIntroducer = isIntroducer;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800171 }
172
173 void
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700174 addTrustScope(const Name& nameSpace)
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700175 {
176 m_trustScope[nameSpace] = ndn::Regex::fromName(nameSpace);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800177 }
178
179 void
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700180 deleteTrustScope(const Name& nameSpace)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800181 {
182 m_trustScope.erase(nameSpace);
183 }
184
185 bool
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700186 canBeTrustedFor(const Name& name)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800187 {
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700188 for (std::map<Name, shared_ptr<ndn::Regex> >::iterator it = m_trustScope.begin();
189 it != m_trustScope.end(); it++)
190 if (it->second->match(name))
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800191 return true;
192 return false;
193 }
194
195 const_iterator
196 trustScopeBegin() const
197 {
198 return m_trustScope.begin();
199 }
200
201 const_iterator
202 trustScopeEnd() const
203 {
204 return m_trustScope.end();
205 }
206
207 iterator
208 trustScopeBegin()
209 {
210 return m_trustScope.begin();
211 }
212
213 iterator
214 trustScopeEnd()
215 {
216 return m_trustScope.end();
217 }
218
219protected:
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700220 typedef std::map<Name, shared_ptr<ndn::Regex> > TrustScopes;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800221
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700222 Name m_namespace;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800223 std::string m_alias;
224 std::string m_name;
225 std::string m_institution;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700226 Name m_keyName;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800227 ndn::PublicKey m_publicKey;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700228 time::system_clock::TimePoint m_notBefore;
229 time::system_clock::TimePoint m_notAfter;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800230
231 bool m_isIntroducer;
232 Profile m_profile;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700233
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800234 TrustScopes m_trustScope;
235};
236
Yingdi Yueb692ac2015-02-10 18:46:18 -0800237} // namespace chronochat
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800238
Yingdi Yueb692ac2015-02-10 18:46:18 -0800239#endif // CHRONOCHAT_CONTACT_HPP