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 std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
83 rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
84 rep.setValue(OC_RSRVD_AUTHPROVIDER, authProvider);
85 rep.setValue(OC_RSRVD_AUTHCODE, authCode);
89 OCRepresentation optionsRep;
90 for (auto iter : options)
92 optionsRep[iter.first] = iter.second;
94 rep.setValue(OC_RSRVD_OPTIONS, optionsRep);
97 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
98 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
99 m_connType, cloudConnectHandler, m_defaultQos);
102 OCStackResult OCAccountManager::signIn(const std::string& userUuid,
103 const std::string& accessToken,
104 PostCallback cloudConnectHandler)
106 return result_guard(signInOut(userUuid, accessToken, true, cloudConnectHandler));
109 OCStackResult OCAccountManager::signOut(PostCallback cloudConnectHandler)
111 return result_guard(signInOut("", "", false, cloudConnectHandler));
114 OCStackResult OCAccountManager::signInOut(const std::string& userUuid,
115 const std::string& accessToken,
117 PostCallback cloudConnectHandler)
119 std::string uri = m_host + OC_RSRVD_ACCOUNT_SESSION_URI;
121 OCRepresentation rep;
124 rep.setValue(OC_RSRVD_USER_UUID, userUuid);
125 rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
126 rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
128 rep.setValue(OC_RSRVD_LOGIN, isSignIn);
130 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
131 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
132 m_connType, cloudConnectHandler, m_defaultQos);
135 OCStackResult OCAccountManager::refreshAccessToken(const std::string& userUuid,
136 const std::string& refreshToken,
137 PostCallback cloudConnectHandler)
139 std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
141 OCRepresentation rep;
142 rep.setValue(OC_RSRVD_USER_UUID, userUuid);
143 rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
144 rep.setValue(OC_RSRVD_GRANT_TYPE, std::string(OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN));
145 rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);
147 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
148 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
149 m_connType, cloudConnectHandler, m_defaultQos);
152 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
153 GetCallback cloudConnectHandler)
155 return result_guard(searchUser(userUuid, QueryParamsMap(), cloudConnectHandler));
158 OCStackResult OCAccountManager::searchUser(const QueryParamsMap& queryParams,
159 GetCallback cloudConnectHandler)
161 return result_guard(searchUser("", queryParams, cloudConnectHandler));
164 OCStackResult OCAccountManager::searchUser(const std::string& userUuid,
165 const QueryParamsMap& queryParams,
166 GetCallback cloudConnectHandler)
168 std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
170 QueryParamsMap fullQuery = {};
172 if (!userUuid.empty())
174 fullQuery.insert(std::make_pair(OC_RSRVD_USER_UUID, userUuid));
177 if (!queryParams.empty())
179 std::string searchQuery;
180 for (auto iter : queryParams)
182 searchQuery.append(iter.first + ":" + iter.second + ",");
184 searchQuery.resize(searchQuery.size() - 1);
185 fullQuery.insert(std::make_pair(OC_RSRVD_SEARCH, searchQuery));
188 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
189 OCDevAddr(), uri, fullQuery, HeaderOptions(),
190 m_connType, cloudConnectHandler, m_defaultQos);
193 OCStackResult OCAccountManager::deleteDevice(const std::string& deviceId,
194 DeleteCallback cloudConnectHandler)
196 std::string uri = m_host + OC_RSRVD_ACCOUNT_URI
197 + "?" + OC_RSRVD_DEVICE_ID + "=" + deviceId;
199 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
200 OCDevAddr(), uri, HeaderOptions(),
201 m_connType, cloudConnectHandler, m_defaultQos);
204 OCStackResult OCAccountManager::createGroup(AclGroupType groupType,
205 PostCallback cloudConnectHandler)
207 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
212 case AclGroupType::PUBLIC:
213 gtype = OC_RSRVD_PUBLIC;
215 case AclGroupType::PRIVATE:
216 gtype = OC_RSRVD_PRIVATE;
219 return result_guard(OC_STACK_INVALID_PARAM);
221 OCRepresentation rep;
222 rep.setValue(OC_RSRVD_GROUP_TYPE, gtype);
224 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
225 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
226 m_connType, cloudConnectHandler, m_defaultQos);
229 OCStackResult OCAccountManager::getGroupList(GetCallback cloudConnectHandler)
231 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI;
233 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
234 OCDevAddr(), uri, QueryParamsMap(), HeaderOptions(),
235 m_connType, cloudConnectHandler, m_defaultQos);
238 OCStackResult OCAccountManager::deleteGroup(const std::string& groupId,
239 DeleteCallback cloudConnectHandler)
241 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI
242 + "?" + OC_RSRVD_GROUP_ID + "=" + groupId;
244 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
245 OCDevAddr(), uri, HeaderOptions(),
246 m_connType, cloudConnectHandler, m_defaultQos);
249 OCStackResult OCAccountManager::joinGroup(const std::string& groupId,
250 PostCallback cloudConnectHandler)
252 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
254 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
255 OCDevAddr(), uri, OCRepresentation(), QueryParamsMap(), HeaderOptions(),
256 m_connType, cloudConnectHandler, m_defaultQos);
259 OCStackResult OCAccountManager::addDeviceToGroup(const std::string& groupId,
260 const std::vector<std::string>& deviceId,
261 PostCallback cloudConnectHandler)
263 if (deviceId.empty())
265 return result_guard(OC_STACK_INVALID_PARAM);
268 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
270 OCRepresentation rep;
271 rep.setValue<std::vector<std::string>>(std::string(OC_RSRVD_DEVICE_ID_LIST), deviceId);
273 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
274 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
275 m_connType, cloudConnectHandler, m_defaultQos);
278 OCStackResult OCAccountManager::getGroupInfo(const std::string& groupId,
279 GetCallback cloudConnectHandler)
281 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
283 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetResourceRepresentation,
284 OCDevAddr(), uri, QueryParamsMap(), HeaderOptions(),
285 m_connType, cloudConnectHandler, m_defaultQos);
288 OCStackResult OCAccountManager::leaveGroup(const std::string& groupId,
289 DeleteCallback cloudConnectHandler)
291 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
293 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
294 OCDevAddr(), uri, HeaderOptions(),
295 m_connType, cloudConnectHandler, m_defaultQos);
298 OCStackResult OCAccountManager::deleteDeviceFromGroup(const std::string& groupId,
299 const std::vector<std::string>& deviceId,
300 DeleteCallback cloudConnectHandler)
302 if (deviceId.empty())
304 return result_guard(OC_STACK_INVALID_PARAM);
307 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
311 for (auto iter : deviceId)
313 uri.append((std::string)OC_RSRVD_DEVICE_ID_LIST + "=" + iter + ";");
315 uri.resize(uri.size() - 1);
317 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
318 OCDevAddr(), uri, HeaderOptions(),
319 m_connType, cloudConnectHandler, m_defaultQos);
322 OCStackResult OCAccountManager::observeGroup(const std::string& groupId,
323 ObserveCallback cloudConnectHandler)
325 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
327 OCDoHandle handle = nullptr;
329 OCStackResult result = checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
330 ObserveType::Observe, &handle, OCDevAddr(), uri,
331 QueryParamsMap(), HeaderOptions(), cloudConnectHandler,
334 if (OC_STACK_OK == result)
336 m_groupObserveHandles.insert(std::pair<std::string, OCDoHandle>(groupId, handle));
343 OCStackResult OCAccountManager::cancelObserveGroup(const std::string& groupId)
345 auto found = m_groupObserveHandles.find(groupId);
346 if (m_groupObserveHandles.end() == found)
348 return result_guard(OC_STACK_INVALID_PARAM);
351 OCDoHandle handle = found->second;
353 std::string uri = m_host + OC_RSRVD_ACL_GROUP_URI + "/" + groupId;
355 OCStackResult result = checked_guard(m_clientWrapper.lock(),
356 &IClientWrapper::CancelObserveResource, handle,
357 (const char*)"", uri, HeaderOptions(), m_defaultQos);
359 if (OC_STACK_OK == result)
361 m_groupObserveHandles.erase(groupId);
368 OCStackResult OCAccountManager::observeInvitation(ObserveCallback cloudConnectHandler)
370 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
372 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
373 ObserveType::Observe, &m_invitationObserveHandle, OCDevAddr(), uri,
374 QueryParamsMap(), HeaderOptions(), cloudConnectHandler, m_defaultQos);
377 OCStackResult OCAccountManager::cancelObserveInvitation()
379 if (nullptr == m_invitationObserveHandle)
381 return result_guard(OC_STACK_INVALID_PARAM);
384 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
386 OCStackResult result = checked_guard(m_clientWrapper.lock(),
387 &IClientWrapper::CancelObserveResource,
388 m_invitationObserveHandle,
389 (const char*)"", uri, HeaderOptions(), m_defaultQos);
391 if (OC_STACK_OK == result)
393 m_invitationObserveHandle = nullptr;
399 OCStackResult OCAccountManager::sendInvitation(const std::string& groupId,
400 const std::string& userUuid,
401 PostCallback cloudConnectHandler)
403 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI;
405 OCRepresentation invitation;
406 invitation.setValue(OC_RSRVD_GROUP_ID, groupId);
407 invitation.setValue(OC_RSRVD_MEMBER_ID, userUuid);
409 std::vector<OCRepresentation> invite{invitation};
411 OCRepresentation rep;
412 rep.setValue(OC_RSRVD_INVITE, invite);
414 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
415 OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
416 m_connType, cloudConnectHandler, m_defaultQos);
419 OCStackResult OCAccountManager::cancelInvitation(const std::string& groupId,
420 const std::string& userUuid,
421 DeleteCallback cloudConnectHandler)
423 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI + "?" + OC_RSRVD_GROUP_ID + "=" + groupId
424 + ";" + OC_RSRVD_MEMBER_ID + "=" + userUuid;
426 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
427 OCDevAddr(), uri, HeaderOptions(),
428 m_connType, cloudConnectHandler, m_defaultQos);
431 OCStackResult OCAccountManager::deleteInvitation(const std::string& groupId,
432 DeleteCallback cloudConnectHandler)
434 std::string uri = m_host + OC_RSRVD_ACL_INVITE_URI + "?" + OC_RSRVD_GROUP_ID + "=" + groupId;
436 return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
437 OCDevAddr(), uri, HeaderOptions(),
438 m_connType, cloudConnectHandler, m_defaultQos);