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"
28 using OC::result_guard;
29 using OC::checked_guard;
31 OCAccountManager::OCAccountManager(std::weak_ptr<IClientWrapper> clientWrapper,
32 const std::string& host,
33 OCConnectivityType connectivityType)
34 : m_clientWrapper(clientWrapper), m_host(host), m_connType(connectivityType),
35 m_invitationObserveHandle(nullptr)
37 if (m_host.empty() || m_clientWrapper.expired())
39 throw OCException(OC::Exception::INVALID_PARAM);
42 const char* di = OCGetServerInstanceIDString();
45 oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
46 throw OCException(OC::Exception::INVALID_PARAM);
49 m_deviceID.append(di);
50 m_groupObserveHandles = {};
51 checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, m_defaultQos);
54 OCAccountManager::~OCAccountManager()
58 std::string OCAccountManager::host() const
63 OCConnectivityType OCAccountManager::connectivityType() const
68 OCStackResult OCAccountManager::signUp(const std::string& authProvider,
69 const std::string& authCode,
70 PostCallback cloudConnectHandler)
72 return result_guard(signUp(authProvider, authCode, QueryParamsMap(), cloudConnectHandler));
75 OCStackResult OCAccountManager::signUp(const std::string& authProvider,
76 const std::string& authCode,
77 const QueryParamsMap& options,
78 PostCallback cloudConnectHandler)
80 if (authProvider.empty() || authCode.empty())
82 return result_guard(OC_STACK_INVALID_PARAM);
85 std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
88 rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
89 rep.setValue(OC_RSRVD_AUTHPROVIDER, authProvider);
90 rep.setValue(OC_RSRVD_AUTHCODE, authCode);
94 for (auto iter : options)
96 rep.setValue(iter.first, iter.second);
100 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
101 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
102 m_connType, cloudConnectHandler, m_defaultQos);
105 OCStackResult OCAccountManager::signIn(const std::string& userUuid,
106 const std::string& accessToken,
107 PostCallback cloudConnectHandler)
109 if (userUuid.empty() || accessToken.empty())
111 return result_guard(OC_STACK_INVALID_PARAM);
114 return result_guard(signInOut(userUuid, accessToken, true, cloudConnectHandler));
117 OCStackResult OCAccountManager::signOut(PostCallback cloudConnectHandler)
119 return result_guard(signInOut("", "", false, cloudConnectHandler));
122 OCStackResult OCAccountManager::signInOut(const std::string& userUuid,
123 const std::string& accessToken,
125 PostCallback cloudConnectHandler)
127 std::string uri = m_host + OC_RSRVD_ACCOUNT_SESSION_URI;
129 OCRepresentation rep;
132 rep.setValue(OC_RSRVD_USER_UUID, userUuid);
133 rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
134 rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
136 rep.setValue(OC_RSRVD_LOGIN, isSignIn);
138 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
139 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
140 m_connType, cloudConnectHandler, m_defaultQos);
143 OCStackResult OCAccountManager::refreshAccessToken(const std::string& userUuid,
144 const std::string& refreshToken,
145 PostCallback cloudConnectHandler)
147 if (userUuid.empty() || refreshToken.empty())
149 return result_guard(OC_STACK_INVALID_PARAM);
152 std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
154 OCRepresentation rep;
155 rep.setValue(OC_RSRVD_USER_UUID, userUuid);
156 rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
157 rep.setValue(OC_RSRVD_GRANT_TYPE, std::string(OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN));
158 rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);
160 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
161 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
162 m_connType, cloudConnectHandler, m_defaultQos);
165 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
166 GetCallback cloudConnectHandler)
168 if (userUuid.empty())
170 return result_guard(OC_STACK_INVALID_PARAM);
173 return result_guard(searchUser(userUuid, QueryParamsMap(), cloudConnectHandler));
176 OCStackResult OCAccountManager::searchUser(const QueryParamsMap& queryParams,
177 GetCallback cloudConnectHandler)
179 if (queryParams.empty())
181 return result_guard(OC_STACK_INVALID_PARAM);
184 return result_guard(searchUser("", queryParams, cloudConnectHandler));
187 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
188 const QueryParamsMap& queryParams,
189 GetCallback cloudConnectHandler)
191 std::string uri = m_host + OC_RSRVD_ACCOUNT_USER_URI;
193 QueryParamsMap fullQuery = {};
195 if (!userUuid.empty())
197 fullQuery.insert(std::make_pair(OC_RSRVD_USER_UUID, userUuid));
200 if (!queryParams.empty())
202 std::string searchQuery;
203 for (auto iter : queryParams)
205 searchQuery.append(iter.first + ":" + iter.second + ",");
207 searchQuery.resize(searchQuery.size() - 1);
208 fullQuery.insert(std::make_pair(OC_RSRVD_SEARCH, searchQuery));
211 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
212 OCDevAddr(), uri, fullQuery, HeaderOptions(),
213 m_connType, cloudConnectHandler, m_defaultQos);
216 OCStackResult OCAccountManager::deleteDevice(const std::string& deviceId,
217 DeleteCallback cloudConnectHandler)
219 if (deviceId.empty())
221 return result_guard(OC_STACK_INVALID_PARAM);
224 std::string uri = m_host + OC_RSRVD_ACCOUNT_URI
225 + "?" + OC_RSRVD_DEVICE_ID + "=" + deviceId;
227 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
228 OCDevAddr(), uri, HeaderOptions(),
229 m_connType, cloudConnectHandler, m_defaultQos);
232 OCStackResult OCAccountManager::createGroup(AclGroupType groupType,
233 PostCallback cloudConnectHandler)
235 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
240 case AclGroupType::PUBLIC:
241 gtype = OC_RSRVD_PUBLIC;
243 case AclGroupType::PRIVATE:
244 gtype = OC_RSRVD_PRIVATE;
247 return result_guard(OC_STACK_INVALID_PARAM);
249 OCRepresentation rep;
250 rep.setValue(OC_RSRVD_GROUP_TYPE, gtype);
252 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
253 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
254 m_connType, cloudConnectHandler, m_defaultQos);
257 OCStackResult OCAccountManager::getGroupList(GetCallback cloudConnectHandler)
259 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
261 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
262 OCDevAddr(), uri, QueryParamsMap(), HeaderOptions(),
263 m_connType, cloudConnectHandler, m_defaultQos);
266 OCStackResult OCAccountManager::deleteGroup(const std::string& groupId,
267 DeleteCallback cloudConnectHandler)
271 return result_guard(OC_STACK_INVALID_PARAM);
274 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI
275 + "?" + OC_RSRVD_GROUP_ID + "=" + groupId;
277 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
278 OCDevAddr(), uri, HeaderOptions(),
279 m_connType, cloudConnectHandler, m_defaultQos);
282 OCStackResult OCAccountManager::joinGroup(const std::string& groupId,
283 PostCallback cloudConnectHandler)
287 return result_guard(OC_STACK_INVALID_PARAM);
290 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
292 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
293 OCDevAddr(), uri, OCRepresentation(), QueryParamsMap(), HeaderOptions(),
294 m_connType, cloudConnectHandler, m_defaultQos);
297 OCStackResult OCAccountManager::addDeviceToGroup(const std::string& groupId,
298 const std::vector<std::string>& deviceId,
299 PostCallback cloudConnectHandler)
301 if (groupId.empty() || deviceId.empty())
303 return result_guard(OC_STACK_INVALID_PARAM);
306 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
308 OCRepresentation rep;
309 rep.setValue<std::vector<std::string>>(std::string(OC_RSRVD_DEVICE_ID_LIST), deviceId);
311 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
312 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
313 m_connType, cloudConnectHandler, m_defaultQos);
316 OCStackResult OCAccountManager::getGroupInfo(const std::string& groupId,
317 GetCallback cloudConnectHandler)
321 return result_guard(OC_STACK_INVALID_PARAM);
324 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
326 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
327 OCDevAddr(), uri, QueryParamsMap(), HeaderOptions(),
328 m_connType, cloudConnectHandler, m_defaultQos);
331 OCStackResult OCAccountManager::leaveGroup(const std::string& groupId,
332 DeleteCallback cloudConnectHandler)
336 return result_guard(OC_STACK_INVALID_PARAM);
339 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
341 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
342 OCDevAddr(), uri, HeaderOptions(),
343 m_connType, cloudConnectHandler, m_defaultQos);
346 OCStackResult OCAccountManager::deleteDeviceFromGroup(const std::string& groupId,
347 const std::vector<std::string>& deviceId,
348 DeleteCallback cloudConnectHandler)
350 if (groupId.empty() || deviceId.empty())
352 return result_guard(OC_STACK_INVALID_PARAM);
355 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
359 for (auto iter : deviceId)
361 uri.append((std::string)OC_RSRVD_DEVICE_ID_LIST + "=" + iter + ";");
363 uri.resize(uri.size() - 1);
365 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
366 OCDevAddr(), uri, HeaderOptions(),
367 m_connType, cloudConnectHandler, m_defaultQos);
370 OCStackResult OCAccountManager::observeGroup(const std::string& groupId,
371 ObserveCallback cloudConnectHandler)
375 return result_guard(OC_STACK_INVALID_PARAM);
378 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
380 OCDoHandle handle = nullptr;
382 OCStackResult result = checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
383 ObserveType::Observe, &handle, OCDevAddr(), uri,
384 QueryParamsMap(), HeaderOptions(), cloudConnectHandler,
387 if (OC_STACK_OK == result)
389 m_groupObserveHandles.insert(std::pair<std::string, OCDoHandle>(groupId, handle));
396 OCStackResult OCAccountManager::cancelObserveGroup(const std::string& groupId)
400 return result_guard(OC_STACK_INVALID_PARAM);
403 auto found = m_groupObserveHandles.find(groupId);
404 if (m_groupObserveHandles.end() == found)
406 return result_guard(OC_STACK_INVALID_PARAM);
409 OCDoHandle handle = found->second;
411 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
413 OCStackResult result = checked_guard(m_clientWrapper.lock(),
414 &IClientWrapper::CancelObserveResource, handle,
415 (const char*)"", uri, HeaderOptions(), m_defaultQos);
417 if (OC_STACK_OK == result)
419 m_groupObserveHandles.erase(groupId);
426 OCStackResult OCAccountManager::observeInvitation(ObserveCallback cloudConnectHandler)
428 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
430 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
431 ObserveType::Observe, &m_invitationObserveHandle, OCDevAddr(), uri,
432 QueryParamsMap(), HeaderOptions(), cloudConnectHandler, m_defaultQos);
435 OCStackResult OCAccountManager::cancelObserveInvitation()
437 if (nullptr == m_invitationObserveHandle)
439 return result_guard(OC_STACK_INVALID_PARAM);
442 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
444 OCStackResult result = checked_guard(m_clientWrapper.lock(),
445 &IClientWrapper::CancelObserveResource,
446 m_invitationObserveHandle,
447 (const char*)"", uri, HeaderOptions(), m_defaultQos);
449 if (OC_STACK_OK == result)
451 m_invitationObserveHandle = nullptr;
457 OCStackResult OCAccountManager::sendInvitation(const std::string& groupId,
458 const std::string& userUuid,
459 PostCallback cloudConnectHandler)
461 if (groupId.empty() || userUuid.empty())
463 return result_guard(OC_STACK_INVALID_PARAM);
466 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
468 OCRepresentation invitation;
469 invitation.setValue(OC_RSRVD_GROUP_ID, groupId);
470 invitation.setValue(OC_RSRVD_MEMBER_ID, userUuid);
472 std::vector<OCRepresentation> invite{invitation};
474 OCRepresentation rep;
475 rep.setValue(OC_RSRVD_INVITE, invite);
477 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
478 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
479 m_connType, cloudConnectHandler, m_defaultQos);
482 OCStackResult OCAccountManager::cancelInvitation(const std::string& groupId,
483 const std::string& userUuid,
484 DeleteCallback cloudConnectHandler)
486 if (groupId.empty() || userUuid.empty())
488 return result_guard(OC_STACK_INVALID_PARAM);
491 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI + "?" + OC_RSRVD_GROUP_ID + "=" + groupId
492 + ";" + OC_RSRVD_MEMBER_ID + "=" + userUuid;
494 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
495 OCDevAddr(), uri, HeaderOptions(),
496 m_connType, cloudConnectHandler, m_defaultQos);
499 OCStackResult OCAccountManager::deleteInvitation(const std::string& groupId,
500 DeleteCallback cloudConnectHandler)
504 return result_guard(OC_STACK_INVALID_PARAM);
507 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI + "?" + OC_RSRVD_GROUP_ID + "=" + groupId;
509 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
510 OCDevAddr(), uri, HeaderOptions(),
511 m_connType, cloudConnectHandler, m_defaultQos);