1 /* ****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "OCAccountManager.h"
22 #include "OCUtilities.h"
26 #define VERIFY_NON_EMPTY(arg, log_message, ret) \
29 oclog() << log_message << std::flush; \
30 return result_guard(ret); \
35 using OC::result_guard;
36 using OC::checked_guard;
38 OCAccountManager::OCAccountManager(std::weak_ptr<IClientWrapper> clientWrapper,
39 const std::string& host,
40 OCConnectivityType connectivityType)
41 : m_clientWrapper(clientWrapper), m_host(host), m_userUuid(""),
42 m_invitationObserveHandle(nullptr), m_groupObserveHandle(nullptr), m_connType(connectivityType)
44 if (m_host.empty() || m_clientWrapper.expired())
46 throw OCException(OC::Exception::INVALID_PARAM);
49 const char* di = OCGetServerInstanceIDString();
52 oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
53 throw OCException(OC::Exception::INVALID_PARAM);
56 m_deviceID.append(di);
57 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, m_defaultQos);
60 OCAccountManager::~OCAccountManager()
64 std::string OCAccountManager::host() const
69 OCConnectivityType OCAccountManager::connectivityType() const
74 OCStackResult OCAccountManager::signUp(const std::string& authProvider,
75 const std::string& authCode,
76 PostCallback cloudConnectHandler)
78 return result_guard(signUp(authProvider, authCode, QueryParamsMap(), cloudConnectHandler));
81 OCStackResult OCAccountManager::signUp(const std::string& authProvider,
82 const std::string& authCode,
83 const QueryParamsMap& options,
84 PostCallback cloudConnectHandler)
86 VERIFY_NON_EMPTY(authProvider, "authProvider cannot be empty.", OC_STACK_INVALID_PARAM);
87 VERIFY_NON_EMPTY(authCode, "authCode cannot be empty.", OC_STACK_INVALID_PARAM);
89 std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
92 rep.setValue<std::string>(OC_RSRVD_DEVICE_ID, m_deviceID);
93 rep.setValue<std::string>(OC_RSRVD_AUTHPROVIDER, authProvider);
94 rep.setValue<std::string>(OC_RSRVD_AUTHCODE, authCode);
98 for (auto iter : options)
100 rep.setValue(iter.first, iter.second);
104 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
105 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
106 m_connType, cloudConnectHandler, m_defaultQos);
109 OCStackResult OCAccountManager::signIn(const std::string& userUuid,
110 const std::string& accessToken,
111 PostCallback cloudConnectHandler)
113 VERIFY_NON_EMPTY(userUuid, "userUuid cannot be empty.", OC_STACK_INVALID_PARAM);
114 VERIFY_NON_EMPTY(accessToken, "accessToken cannot be empty.", OC_STACK_INVALID_PARAM);
116 OCStackResult result = result_guard(signInOut(userUuid, accessToken, true,
117 cloudConnectHandler));
118 if (OC_STACK_OK == result)
120 m_userUuid = userUuid;
126 OCStackResult OCAccountManager::signOut(const std::string& accessToken,
127 PostCallback cloudConnectHandler)
129 VERIFY_NON_EMPTY(m_userUuid, "Need to sign-in first.", OC_STACK_ERROR);
130 VERIFY_NON_EMPTY(accessToken, "accessToken cannot be empty.", OC_STACK_INVALID_PARAM);
132 OCStackResult result = result_guard(signInOut(m_userUuid, accessToken, false,
133 cloudConnectHandler));
134 if (OC_STACK_OK == result)
142 OCStackResult OCAccountManager::signInOut(const std::string& userUuid,
143 const std::string& accessToken,
145 PostCallback cloudConnectHandler)
147 std::string uri = m_host + OC_RSRVD_ACCOUNT_SESSION_URI;
149 OCRepresentation rep;
153 rep.setValue<std::string>(OC_RSRVD_USER_UUID, userUuid);
155 rep.setValue<std::string>(OC_RSRVD_DEVICE_ID, m_deviceID);
156 rep.setValue<std::string>(OC_RSRVD_ACCESS_TOKEN, accessToken);
157 rep.setValue<bool>(OC_RSRVD_LOGIN, isSignIn);
159 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
160 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
161 m_connType, cloudConnectHandler, m_defaultQos);
164 OCStackResult OCAccountManager::refreshAccessToken(const std::string& userUuid,
165 const std::string& refreshToken,
166 PostCallback cloudConnectHandler)
168 VERIFY_NON_EMPTY(userUuid, "userUuid cannot be empty.", OC_STACK_INVALID_PARAM);
169 VERIFY_NON_EMPTY(refreshToken, "refreshToken cannot be empty.", OC_STACK_INVALID_PARAM);
171 std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
173 OCRepresentation rep;
174 rep.setValue<std::string>(OC_RSRVD_USER_UUID, userUuid);
175 rep.setValue<std::string>(OC_RSRVD_DEVICE_ID, m_deviceID);
176 rep.setValue<std::string>(OC_RSRVD_GRANT_TYPE, std::string(OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN));
177 rep.setValue<std::string>(OC_RSRVD_REFRESH_TOKEN, refreshToken);
179 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
180 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
181 m_connType, cloudConnectHandler, m_defaultQos);
184 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
185 GetCallback cloudConnectHandler)
187 VERIFY_NON_EMPTY(userUuid, "userUuid cannot be empty.", OC_STACK_INVALID_PARAM);
189 return result_guard(searchUser(userUuid, QueryParamsMap(), cloudConnectHandler));
192 OCStackResult OCAccountManager::searchUser(const QueryParamsMap& queryParams,
193 GetCallback cloudConnectHandler)
195 VERIFY_NON_EMPTY(queryParams, "queryParams cannot be empty.", OC_STACK_INVALID_PARAM);
197 return result_guard(searchUser("", queryParams, cloudConnectHandler));
200 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
201 const QueryParamsMap& queryParams,
202 GetCallback cloudConnectHandler)
204 std::string uri = m_host + OC_RSRVD_ACCOUNT_USER_URI;
206 QueryParamsMap fullQuery = {};
208 if (!userUuid.empty())
210 fullQuery.insert(std::make_pair(OC_RSRVD_USER_UUID, userUuid));
213 if (!queryParams.empty())
215 std::string searchQuery;
216 for (auto iter : queryParams)
218 searchQuery.append(iter.first + ":" + iter.second + ",");
220 searchQuery.resize(searchQuery.size() - 1);
221 fullQuery.insert(std::make_pair(OC_RSRVD_SEARCH, searchQuery));
224 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
225 OCDevAddr(), uri, fullQuery, HeaderOptions(),
226 m_connType, cloudConnectHandler, m_defaultQos);
229 OCStackResult OCAccountManager::deleteDevice(const std::string& accessToken,
230 const std::string& deviceId,
231 DeleteCallback cloudConnectHandler)
233 VERIFY_NON_EMPTY(accessToken, "accessToken cannot be empty.", OC_STACK_INVALID_PARAM);
234 VERIFY_NON_EMPTY(deviceId, "deviceId cannot be empty.", OC_STACK_INVALID_PARAM);
236 std::string uri = m_host + OC_RSRVD_ACCOUNT_URI
237 + "?" + OC_RSRVD_ACCESS_TOKEN + "=" + accessToken
238 + ";" + OC_RSRVD_DEVICE_ID + "=" + deviceId;
240 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
241 OCDevAddr(), uri, HeaderOptions(),
242 m_connType, cloudConnectHandler, m_defaultQos);
245 OCStackResult OCAccountManager::createGroup(PostCallback cloudConnectHandler)
247 return result_guard(createGroup(QueryParamsMap(), cloudConnectHandler));
250 OCStackResult OCAccountManager::createGroup(const QueryParamsMap& queryParams,
251 PostCallback cloudConnectHandler)
253 VERIFY_NON_EMPTY(m_userUuid, "Need to sign-in first.", OC_STACK_ERROR);
255 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
257 std::vector<std::string> members{m_userUuid};
259 OCRepresentation rep;
260 rep.setValue<std::string>(OC_RSRVD_OWNER, m_userUuid);
261 rep.setValue<std::vector<std::string>>(OC_RSRVD_MEMBERS, members);
263 if (!queryParams.empty())
265 for (auto iter : queryParams)
267 rep.setValue(iter.first, iter.second);
271 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
272 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
273 m_connType, cloudConnectHandler, m_defaultQos);
276 OCStackResult OCAccountManager::deleteGroup(const std::string& groupId,
277 DeleteCallback cloudConnectHandler)
279 VERIFY_NON_EMPTY(m_userUuid, "Need to sign-in first.", OC_STACK_ERROR);
280 VERIFY_NON_EMPTY(groupId, "groupId cannot be empty.", OC_STACK_INVALID_PARAM);
282 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId
283 + "?" + OC_RSRVD_OWNER + "=" + m_userUuid;
285 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
286 OCDevAddr(), uri, HeaderOptions(),
287 m_connType, cloudConnectHandler, m_defaultQos);
290 OCStackResult OCAccountManager::getGroupInfoAll(GetCallback cloudConnectHandler)
292 VERIFY_NON_EMPTY(m_userUuid, "Need to sign-in first.", OC_STACK_ERROR);
294 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
296 QueryParamsMap query = {};
297 query.insert(std::make_pair(OC_RSRVD_MEMBERS, m_userUuid));
299 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
300 OCDevAddr(), uri, query, HeaderOptions(), m_connType,
301 cloudConnectHandler, m_defaultQos);
304 OCStackResult OCAccountManager::getGroupInfo(const std::string& groupId,
305 GetCallback cloudConnectHandler)
307 VERIFY_NON_EMPTY(m_userUuid, "Need to sign-in first.", OC_STACK_ERROR);
308 VERIFY_NON_EMPTY(groupId, "groupId cannot be empty.", OC_STACK_INVALID_PARAM);
310 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
312 QueryParamsMap query = {};
313 query.insert(std::make_pair(OC_RSRVD_MEMBERS, m_userUuid));
315 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
316 OCDevAddr(), uri, query, HeaderOptions(),
317 m_connType, cloudConnectHandler, m_defaultQos);
320 OCStackResult OCAccountManager::addPropertyValueToGroup(const std::string& groupId,
321 const OCRepresentation propertyValue,
322 PostCallback cloudConnectHandler)
324 VERIFY_NON_EMPTY(groupId, "groupId cannot be empty.", OC_STACK_INVALID_PARAM);
326 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
328 QueryParamsMap query = {};
329 query.insert(std::make_pair(OC_RSRVD_OPERATION, OC_RSRVD_ADD));
331 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
332 OCDevAddr(), uri, propertyValue, query, HeaderOptions(),
333 m_connType, cloudConnectHandler, m_defaultQos);
336 OCStackResult OCAccountManager::deletePropertyValueFromGroup(const std::string& groupId,
337 const OCRepresentation propertyValue,
338 PostCallback cloudConnectHandler)
340 VERIFY_NON_EMPTY(groupId, "groupId cannot be empty.", OC_STACK_INVALID_PARAM);
342 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
344 QueryParamsMap query = {};
345 query.insert(std::make_pair(OC_RSRVD_OPERATION, OC_RSRVD_DELETE));
347 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
348 OCDevAddr(), uri, propertyValue, query, HeaderOptions(),
349 m_connType, cloudConnectHandler, m_defaultQos);
352 OCStackResult OCAccountManager::updatePropertyValueOnGroup(const std::string& groupId,
353 const OCRepresentation propertyValue,
354 PostCallback cloudConnectHandler)
356 VERIFY_NON_EMPTY(groupId, "groupId cannot be empty.", OC_STACK_INVALID_PARAM);
358 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
360 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
361 OCDevAddr(), uri, propertyValue, QueryParamsMap(), HeaderOptions(),
362 m_connType, cloudConnectHandler, m_defaultQos);
365 OCStackResult OCAccountManager::observeGroup(ObserveCallback cloudConnectHandler)
367 VERIFY_NON_EMPTY(m_userUuid, "Need to sign-in first.", OC_STACK_ERROR);
369 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
371 QueryParamsMap query = {};
372 query.insert(std::make_pair(OC_RSRVD_MEMBERS, m_userUuid));
374 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
375 ObserveType::Observe, &m_groupObserveHandle, OCDevAddr(), uri,
376 query, HeaderOptions(), cloudConnectHandler, m_defaultQos);
379 OCStackResult OCAccountManager::cancelObserveGroup()
381 VERIFY_NON_EMPTY(m_userUuid, "Need to sign-in first.", OC_STACK_ERROR);
383 if (nullptr == m_groupObserveHandle)
385 oclog() << "observeGroup() has not been done." << std::flush;
386 return result_guard(OC_STACK_INVALID_PARAM);
389 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI
390 + "?" + OC_RSRVD_MEMBERS + "=" + m_userUuid;
392 OCStackResult result = checked_guard(m_clientWrapper.lock(),
393 &IClientWrapper::CancelObserveResource,
394 m_groupObserveHandle, (const char*)"", uri,
395 HeaderOptions(), m_defaultQos);
396 if (OC_STACK_OK == result)
398 m_groupObserveHandle = nullptr;
404 OCStackResult OCAccountManager::observeInvitation(ObserveCallback cloudConnectHandler)
406 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
408 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
409 ObserveType::Observe, &m_invitationObserveHandle, OCDevAddr(), uri,
410 QueryParamsMap(), HeaderOptions(), cloudConnectHandler, m_defaultQos);
413 OCStackResult OCAccountManager::cancelObserveInvitation()
415 if (nullptr == m_invitationObserveHandle)
417 oclog() << "observeInvitation() has not been done." << std::flush;
418 return result_guard(OC_STACK_INVALID_PARAM);
421 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
423 OCStackResult result = checked_guard(m_clientWrapper.lock(),
424 &IClientWrapper::CancelObserveResource,
425 m_invitationObserveHandle,
426 (const char*)"", uri, HeaderOptions(), m_defaultQos);
427 if (OC_STACK_OK == result)
429 m_invitationObserveHandle = nullptr;
435 OCStackResult OCAccountManager::sendInvitation(const std::string& groupId,
436 const std::string& userUuid,
437 PostCallback cloudConnectHandler)
439 VERIFY_NON_EMPTY(m_userUuid, "Need to sign-in first.", OC_STACK_ERROR);
440 VERIFY_NON_EMPTY(groupId, "groupId cannot be empty.", OC_STACK_INVALID_PARAM);
441 VERIFY_NON_EMPTY(userUuid, "userUuid cannot be empty.", OC_STACK_INVALID_PARAM);
443 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
445 OCRepresentation invitation;
446 invitation.setValue<std::string>(OC_RSRVD_GROUP_ID, groupId);
447 invitation.setValue<std::string>(OC_RSRVD_MEMBER_ID, userUuid);
449 std::vector<OCRepresentation> invite{invitation};
451 OCRepresentation rep;
452 rep.setValue<std::string>(OC_RSRVD_USER_UUID, m_userUuid);
453 rep.setValue<std::vector<OCRepresentation>>(OC_RSRVD_INVITE, invite);
455 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
456 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
457 m_connType, cloudConnectHandler, m_defaultQos);
460 OCStackResult OCAccountManager::cancelInvitation(const std::string& groupId,
461 const std::string& userUuid,
462 DeleteCallback cloudConnectHandler)
464 VERIFY_NON_EMPTY(groupId, "groupId cannot be empty.", OC_STACK_INVALID_PARAM);
465 VERIFY_NON_EMPTY(userUuid, "userUuid cannot be empty.", OC_STACK_INVALID_PARAM);
467 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI + "?" + OC_RSRVD_GROUP_ID + "=" + groupId
468 + ";" + OC_RSRVD_MEMBER_ID + "=" + userUuid;
470 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
471 OCDevAddr(), uri, HeaderOptions(),
472 m_connType, cloudConnectHandler, m_defaultQos);
475 OCStackResult OCAccountManager::replyToInvitation(const std::string& groupId,
477 DeleteCallback cloudConnectHandler)
479 VERIFY_NON_EMPTY(groupId, "groupId cannot be empty.", OC_STACK_INVALID_PARAM);
481 std::string isAccept = accept ? "1" : "0";
483 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI + "?" + OC_RSRVD_GROUP_ID + "=" + groupId
484 + ";" + OC_RSRVD_ACCEPT + "=" + isAccept;
486 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
487 OCDevAddr(), uri, HeaderOptions(),
488 m_connType, cloudConnectHandler, m_defaultQos);